HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心)
题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442
Description
WANGPENG is a freshman. He is requested to have a physical examination when entering the university.
Now WANGPENG arrives at the hospital. Er….. There are so many students, and the number is increasing!
There are many examination subjects to do, and there is a queue for every subject. The queues are getting longer as time goes by. Choosing the queue to stand is always a problem. Please help WANGPENG to determine an exam sequence, so that he can finish all the physical examination subjects as early as possible.
Input
There are several test cases. Each test case starts with a positive integer n in a line, meaning the number of subjects(queues).
Then n lines follow. The i-th line has a pair of integers (ai, bi) to describe the i-th queue:
- If WANGPENG follows this queue at time 0, WANGPENG has to wait for ai seconds to finish this subject.
- As the queue is getting longer, the waiting time will increase bi seconds every second while WANGPENG is not in the queue.
The input ends with n = 0.For all test cases, 0<n≤100000, 0≤ai,bi<2^31.
Output
For each test case, output one line with an integer: the earliest time (counted by seconds) that WANGPENG can finish all exam subjects. Since WANGPENG is always confused by years, just print the seconds mod 365×24×60×60.
Sample Input
5
1 2
2 3
3 4
4 5
5 6
0
Sample Output
1419
题意:
给你n个队伍每个队伍都要排一次,每个队伍要排a秒,如果不排,则每秒过后排这个队伍要增加b秒问最少多少时间排完所有的队伍。
题解:
我们现在假设两个队伍分别是a1,b1,a2,b2。现在如果先排第一队,花费a1+a2+a1b1。如果先排第二队则花费a1+a2+a2b1这样我们明显可以得到应该选择a1b2与a2b1中小的那个。这样按照这个排序,得出最小的即可。
代码:
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 100100;
const int mod = 365*24*60*60;
struct node{
long long a,b;
bool operator < (const node &R)const{
return a*R.b < b*R.a;
}
}s[maxn];
int main()
{
while (scanf("%d",&n)&&(n!=0)){
for (int i = 0; i < n; i++)
scanf("%lld %lld",&s[i].a,&s[i].b);
sort(s,s+n);
long long ans = 0;
for (int i = 0; i < n; i++){
ans = (ans + s[i].a + s[i].b*ans%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}
HDU 4442 Physical Examination(贪心)的更多相关文章
- hdu 4442 Physical Examination 贪心排序
Physical Examination Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4442 Physical Examination
Physical Examination Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 4442 Physical Examination(关于贪心排序)
这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1, ...
- hdu 4442 Physical Examination (2012年金华赛区现场赛A题)
昨天模拟赛的时候坑了好久,刚开始感觉是dp,仔细一看数据范围太大. 题目大意:一个人要参加考试,一共有n个科目,每个科目都有一个相应的队列,完成这门科目的总时间为a+b*(前面已完成科目所花的总时间) ...
- hdu4442 Physical Examination(贪心)
这种样式的最优解问题一看就是贪心.如果一下不好看,那么可以按照由特殊到一般的思维方式,先看n==2时怎么选顺序(这种由特殊到一般的思维方式是思考很多问题的入口): 有两个队时,若先选第一个,则ans= ...
- HDU 5835 Danganronpa (贪心)
Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- hdu 4004 (二分加贪心) 青蛙过河
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...
- Saving HDU(hdu2111,贪心)
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- [置顶] JAVA从零单排4-----继承、封装和多态详解
继承 Java三大特征之一:继承.Java的继承具有单继承的特点,每个子类只能有一个直接父类. 继承的特点 Java的继承用extends关键字来实现,被继承的类成为父类,实现继承的类被称为子类.子类 ...
- aix 禁止root远程登录
Aix禁止root远程登录 aix用户扩展信息大都在/etc/security/user这个文本文件里.你可以修改: login=false 用户不能登录系统 rlogin=false 用户不能从远程 ...
- linux apt-get 源配置
linux中apt-get不能使用可能因为源不对,需要修改/etc/apt下的sources.list文件 apt-get源网上有很多,但是试了很多都不能用,以下提供一个我自己这边使用成功的源: de ...
- [ios2]iOS 使用subversion管理iOS源代码 【转】
使用subversion管理iOS源代码 1.安装和配置subversion服务器 在windows 服务器上安装VisualSVN-Server,下载地址http://www.visualsvn.c ...
- [留念贴] C#开发技术期末大作业——星月之痕
明天就要去上海大学参加 2015赛季 ACM/ICPC最后一场比赛 —— EC-Final,在这之前,顺利地把期末大作业赶出来了. 在这种期末大作业10个人里面有9个是从网上下载的国内计算机水平五六流 ...
- hdu1040
#include<stdio.h>#include<stdlib.h>int a[100];int cmp(const void *a,const void *b){ retu ...
- C# 线程同步之排它锁/Monitor监视器类
一.Monitor类说明,提供同步访问对象的机制. 1.位于System.Threading命名空间下,mscorlib.dll程序集中. 2.Monitor通过获取和释放排它锁的方式实现多线程的同步 ...
- [UWP小白日记-7]转换MVA学院的XML字幕为SRT (二)
瞎扯淡 上个版本,非常蠢用来N多的循环导致非常卡性能烂得不行,这次使用XmlDocument类来读取XML字幕 其实根本不用各种扒XML字幕,好吧我这是学习使用XmlDocument类,嗯就是这个样子 ...
- 网页视频下载牛逼工具,支持各种格式转换,比如腾讯视频格式qlv转mp4
这种思路真是创新,原文地址:http://jingyan.baidu.com/article/5225f26b03f047e6fb090860.html 软件工具名字:维棠下载. 上图: 1:搜索视频 ...
- 多表查询 INNER JOIN ON WHERE
SELECT *FROM STUDENT_INFO siINNER JOIN CLASS_INFO ci on si.CLASS_INFO_ID = ci.ID INNER JOIN TEACHER_ ...
题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442
Description
WANGPENG is a freshman. He is requested to have a physical examination when entering the university.
Now WANGPENG arrives at the hospital. Er….. There are so many students, and the number is increasing!
There are many examination subjects to do, and there is a queue for every subject. The queues are getting longer as time goes by. Choosing the queue to stand is always a problem. Please help WANGPENG to determine an exam sequence, so that he can finish all the physical examination subjects as early as possible.
Input
There are several test cases. Each test case starts with a positive integer n in a line, meaning the number of subjects(queues).
Then n lines follow. The i-th line has a pair of integers (ai, bi) to describe the i-th queue:
- If WANGPENG follows this queue at time 0, WANGPENG has to wait for ai seconds to finish this subject.
- As the queue is getting longer, the waiting time will increase bi seconds every second while WANGPENG is not in the queue.
The input ends with n = 0.For all test cases, 0<n≤100000, 0≤ai,bi<2^31.
Output
For each test case, output one line with an integer: the earliest time (counted by seconds) that WANGPENG can finish all exam subjects. Since WANGPENG is always confused by years, just print the seconds mod 365×24×60×60.
Sample Input
5
1 2
2 3
3 4
4 5
5 6
0
Sample Output
1419
题意:
给你n个队伍每个队伍都要排一次,每个队伍要排a秒,如果不排,则每秒过后排这个队伍要增加b秒问最少多少时间排完所有的队伍。
题解:
我们现在假设两个队伍分别是a1,b1,a2,b2。现在如果先排第一队,花费a1+a2+a1b1。如果先排第二队则花费a1+a2+a2b1这样我们明显可以得到应该选择a1b2与a2b1中小的那个。这样按照这个排序,得出最小的即可。
代码:
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 100100;
const int mod = 365*24*60*60;
struct node{
long long a,b;
bool operator < (const node &R)const{
return a*R.b < b*R.a;
}
}s[maxn];
int main()
{
while (scanf("%d",&n)&&(n!=0)){
for (int i = 0; i < n; i++)
scanf("%lld %lld",&s[i].a,&s[i].b);
sort(s,s+n);
long long ans = 0;
for (int i = 0; i < n; i++){
ans = (ans + s[i].a + s[i].b*ans%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}
Physical Examination Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
Physical Examination Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1, ...
昨天模拟赛的时候坑了好久,刚开始感觉是dp,仔细一看数据范围太大. 题目大意:一个人要参加考试,一共有n个科目,每个科目都有一个相应的队列,完成这门科目的总时间为a+b*(前面已完成科目所花的总时间) ...
这种样式的最优解问题一看就是贪心.如果一下不好看,那么可以按照由特殊到一般的思维方式,先看n==2时怎么选顺序(这种由特殊到一般的思维方式是思考很多问题的入口): 有两个队时,若先选第一个,则ans= ...
Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
继承 Java三大特征之一:继承.Java的继承具有单继承的特点,每个子类只能有一个直接父类. 继承的特点 Java的继承用extends关键字来实现,被继承的类成为父类,实现继承的类被称为子类.子类 ...
Aix禁止root远程登录 aix用户扩展信息大都在/etc/security/user这个文本文件里.你可以修改: login=false 用户不能登录系统 rlogin=false 用户不能从远程 ...
linux中apt-get不能使用可能因为源不对,需要修改/etc/apt下的sources.list文件 apt-get源网上有很多,但是试了很多都不能用,以下提供一个我自己这边使用成功的源: de ...
使用subversion管理iOS源代码 1.安装和配置subversion服务器 在windows 服务器上安装VisualSVN-Server,下载地址http://www.visualsvn.c ...
明天就要去上海大学参加 2015赛季 ACM/ICPC最后一场比赛 —— EC-Final,在这之前,顺利地把期末大作业赶出来了. 在这种期末大作业10个人里面有9个是从网上下载的国内计算机水平五六流 ...
#include<stdio.h>#include<stdlib.h>int a[100];int cmp(const void *a,const void *b){ retu ...
一.Monitor类说明,提供同步访问对象的机制. 1.位于System.Threading命名空间下,mscorlib.dll程序集中. 2.Monitor通过获取和释放排它锁的方式实现多线程的同步 ...
瞎扯淡 上个版本,非常蠢用来N多的循环导致非常卡性能烂得不行,这次使用XmlDocument类来读取XML字幕 其实根本不用各种扒XML字幕,好吧我这是学习使用XmlDocument类,嗯就是这个样子 ...
这种思路真是创新,原文地址:http://jingyan.baidu.com/article/5225f26b03f047e6fb090860.html 软件工具名字:维棠下载. 上图: 1:搜索视频 ...
SELECT *FROM STUDENT_INFO siINNER JOIN CLASS_INFO ci on si.CLASS_INFO_ID = ci.ID INNER JOIN TEACHER_ ...