hdu - 4442 - Physical Examination
来源:互联网 发布:杭州亚信软件 编辑:程序博客网 时间:2024/05/16 06:50
题意:WANGPENG排队体检,共n个项目,对于第i个项目,完成该项目要ai的时间,若不在队列中,每过1秒需要等bi秒,问他完成所有项目的最短时间。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4442
——>>看了题后,我想到了贪心,但是怎样贪法呢?看看题中的数据,想了下,是完成时间少的排在前面,完成时间相等时等待时间长的排在前面,于是写了个,submit——WA了!原来贪心是推出来的,刚才随便想的真错!:
假设有2个项目(a1, b1), (a2, b2),
先排第一个时,总时间为:a1 + a1*b2 + a2 —— a1 + a2 + a1*b2;
先排第二个时,总时间为:a2 + a2*b1 + a1 —— a1 + a2 + a2*b1;
对比可发现,排序的依据是:a1*b2与a2*b1的大小。
另外发现:题中说:0≤ai,bi<231但是开int来存ai与bi却会WA,需用64位整数来存;还有那个取模的,需要每步取模,若只是最后取一次模,结果还是WA!这题,真是……
#include <cstdio>#include <algorithm>using namespace std;const int MOD = 365*24*60*60;const int maxn = 100000 + 10;struct node{ long long a; long long b; bool operator < (const node &e) const { return a*e.b < e.a*b; }}item[maxn];int main(){ int n, i; while(~scanf("%d", &n)) { if(!n) return 0; for(i = 0; i < n; i++) scanf("%I64d%I64d", &item[i].a, &item[i].b); sort(item, item+n); long long sum = 0; for(i = 0; i < n; i++) sum = (sum + sum*item[i].b + item[i].a) % MOD; //每步取模,若只是最后取模WA! printf("%I64d\n", sum); } return 0;}
- hdu 4442 Physical Examination
- HDU 4442 Physical Examination
- hdu 4442 Physical Examination
- hdu 4442 Physical Examination
- hdu - 4442 - Physical Examination
- HDU 4442 Physical Examination
- hdu 4442 Physical Examination
- hdu 4442 Physical Examination
- HDU 4442 Physical Examination
- hdu 4442 Physical Examination
- hdu 4442 Physical Examination
- hdu 4442 Physical Examination
- hdu 4442 Physical Examination (排序)
- 贪心--HDU - 4442 Physical Examination
- HDU 4442 Physical Examination (贪心)
- HDU-#4442 Physical Examination(贪心)
- hdu 4442 Physical Examination(贪心)
- hdu 4442 Physical Examination(水题)
- Project Explorer (1) -- "Build Project" 和 "Clean Project"
- 【iOS开发】---- 二维码
- [APUE读书笔记] 有关exit 和_exit区别的比较
- android开发实战系列(05)-- 运行你的应用
- django系列1 - User Authentication(翻译+整理)
- hdu - 4442 - Physical Examination
- java 枚举
- LZW算法PHP实现方法
- JS实现抛物线运动
- javaWeb_07-用refresh控制浏览器定时刷新
- SQL优化---活动监视器
- 博通首款LTE移动芯片将量产 挑战高通统治地位
- android开发实战系列(06)-- 创建一个简单的用户界面
- linux 查看版本号