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 ...
随机推荐
- MSSQL 清空日志 删除日志文件
MSSQL 清空日志 删除日志文件 最近的项目主要做数据的归档,把数据从一个数据库拉到另一个数据库,照成新数据库的日志文件非常大:于是想把日志文件删除.最简单就是先分离数据库->删除日志文件-& ...
- linux命令——iotop
查看CPU使用情况用top,查看I/O使用情况就需要iotop.这个命令是在 kernel v2.6.20中添加,安装的时候要注意内核的版本号. iotop常用快捷键 1. 左右箭头 --> 改 ...
- ext2 源代码解析之 “从路径名到目标结点” (一)
两个主要函数,path_init和path_walk,他们结合在一起根据给定的文件路径名称在内存中找到或者建立代表着目标文件或目录的dentry和inode结构.注意,最终是信息是读取到内存中的.其中 ...
- JAXP的SAX解析
* 使用Sax解析XML文档 * * 使用Sax解析文档和DOM解析不一样. * 概述: * 是一种XML解析的替代方法.相比于DOM,SAX是一种速度更快,更有效的方法.它逐行扫描文档,一 ...
- C#中的集合类——ArrayList
1. ArrayList与数组 数组的长度不可变,元素的类型单一: ArrayList 实际上相当于一个可变长度的动态数组,由于集合中的元素都是object类型,元素的类型可以有多种了:与数组一样, ...
- HttpContext.Current.Request.ServerVariables
Request.ServerVariables("Url") 返回服务器地址Value 0: /WebSite1/Default.aspx Request.ServerVariab ...
- 浅谈CSS3 box-reflect 属性
今天说一下 CSS3的box-reflect属性,也就是倒影特效. 语法: box-reflect:包括3个值. 1. direction 定义方向,取值包括 above . below . left ...
- Windows/Linux 生成iOS证书及p12文件
操作步骤 生成csr文件(通过OpenSSL命令) 生成mobileprovision文件(通过Apple开发者后台) 生成cer文件(通过Apple开发者后台) 生成P12文件(通过OpenSSL命 ...
- getResourceAsStream和getResource的用法
用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大家最经常用的,就是用JAVA的File类,如要取得 D:/test.txt文件,就会这样用File file ...
- Java学习笔记-File
//文件操作 //2015年4月30日15:40:21 package com.alfredsun.first.main; import java.io.File; import java.io.IO ...
题目链接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 ...
MSSQL 清空日志 删除日志文件 最近的项目主要做数据的归档,把数据从一个数据库拉到另一个数据库,照成新数据库的日志文件非常大:于是想把日志文件删除.最简单就是先分离数据库->删除日志文件-& ...
查看CPU使用情况用top,查看I/O使用情况就需要iotop.这个命令是在 kernel v2.6.20中添加,安装的时候要注意内核的版本号. iotop常用快捷键 1. 左右箭头 --> 改 ...
两个主要函数,path_init和path_walk,他们结合在一起根据给定的文件路径名称在内存中找到或者建立代表着目标文件或目录的dentry和inode结构.注意,最终是信息是读取到内存中的.其中 ...
* 使用Sax解析XML文档 * * 使用Sax解析文档和DOM解析不一样. * 概述: * 是一种XML解析的替代方法.相比于DOM,SAX是一种速度更快,更有效的方法.它逐行扫描文档,一 ...
1. ArrayList与数组 数组的长度不可变,元素的类型单一: ArrayList 实际上相当于一个可变长度的动态数组,由于集合中的元素都是object类型,元素的类型可以有多种了:与数组一样, ...
Request.ServerVariables("Url") 返回服务器地址Value 0: /WebSite1/Default.aspx Request.ServerVariab ...
今天说一下 CSS3的box-reflect属性,也就是倒影特效. 语法: box-reflect:包括3个值. 1. direction 定义方向,取值包括 above . below . left ...
操作步骤 生成csr文件(通过OpenSSL命令) 生成mobileprovision文件(通过Apple开发者后台) 生成cer文件(通过Apple开发者后台) 生成P12文件(通过OpenSSL命 ...
用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大家最经常用的,就是用JAVA的File类,如要取得 D:/test.txt文件,就会这样用File file ...
//文件操作 //2015年4月30日15:40:21 package com.alfredsun.first.main; import java.io.File; import java.io.IO ...