HDU 4442 Physical Examination (贪心+排序)

Physical Examination

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5985    Accepted Submission(s): 1682

Problem 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:
1. If WANGPENG follows this queue at time 0, WANGPENG has to wait for ai seconds to finish this subject.
2. 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<231.

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

Hint

In the Sample Input, WANGPENG just follow the given order. He spends 1 second in the first queue, 5 seconds in the 2th queue, 27 seconds in the 3th queue,
169 seconds in the 4th queue, and 1217 seconds in the 5th queue. So the total time is 1419s. WANGPENG has computed all possible orders in his
120-core-parallel head, and decided that this is the optimal choice.

Source

2012 Asia JinHua Regional Contest

Recommend

zhuyuanchen520

讨论2个的情况:

a1 b1

a2 b2

如果这是最优的顺序,则满足:

a1 + a1*b2 + a2 <= a2 + a2*b1 + a1

等价于

a1/b1 < a2/b2

a1*b2 < a2*b1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int mod=365*24*60*60;
const int MAXN=100000+5;
struct node
{
    __int64 ai,bi;
    bool operator<(const node &B)const
    {
        return ai*B.bi<B.ai*bi;
    }
}a[MAXN];
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        __int64 ans=0;
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
            scanf("%I64d %I64d",&a[i].ai,&a[i].bi);
        sort(a,a+n);
        for(int i=0;i<n;i++)
        {
            if(i==0) ans+=a[i].ai;
            else ans+=(a[i].ai+ans*a[i].bi);
            ans=ans%mod;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

时间: 2024-11-02 23:40:49

HDU 4442 Physical Examination (贪心+排序)的相关文章

hdu 4442 Physical Examination (排序)

Physical Examination Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5813    Accepted Submission(s): 1625 Problem Description WANGPENG is a freshman. He is requested to have a physical examinat

HDU 4442 Physical Examination(关于贪心排序)

这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1,总的耗费就是a2 + a2 * b1 + a1.这时比较两个数的大小,发现两边都有a1+a2,所以只是比较a1*b2和a2 * b1的大小. #include <cstdio> #include <cstring> #include <algorithm> using na

HDU 4442 Physical Examination

水题...但是研究了半天 最后发现是这样排序的,两个项目进行排序,哪一个代价小,哪一个放前面 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include &l

hdu4442 Physical Examination(贪心)

这种样式的最优解问题一看就是贪心.如果一下不好看,那么可以按照由特殊到一般的思维方式,先看n==2时怎么选顺序(这种由特殊到一般的思维方式是思考很多问题的入口): 有两个队时,若先选第一个,则ans=a1+a2+b2*a1;若先选第二个,则ans=a2+a1+b1*a2;所以选择顺序就比b2*a1和b1*a2就好了. 那么当有n>2个队时,能不能也这么搞?当然可以,每次剩下那几个队没选,我就两两比,两两之间比较时,之前用的时间都要加上是和顺序无关的,顺序影响的只是b2*a1和b1*a2而已(如果

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem 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 nu

HDU 1338 Game Prediction 贪心

Problem Description Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M. And there are no two cards with the same pip. Du

hdu 1009 FatMouse&#39; Trade(贪心)

题目来源:hdu 1009 FatMouse' Trade FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 54581 Accepted Submission(s): 18299 Problem Description FatMouse prepared M pounds of cat food, ready

HDU 4857 (反向拓扑排序 + 优先队列)

题意:有N个人,M个优先级a,b表示a优先于b,并且每个人有个编号的优先级,输出顺序. 思路来自:与PKU3687一样 在基本的拓扑排序的基础上又增加了一个要求:编号最小的节点要尽量排在前面:在满足上一个条件的基础上,编号第二小的节点要尽量排在前面:在满足前两个条件的基础上,编号第三小的节点要尽量排在前面--依此类推.(注意,这和字典序是两回事,不可以混淆.) 如图 1 所示,满足要求的拓扑序应该是:6 4 1 3 9 2 5 7 8 0. 图 1 一个拓扑排序的例子 一般来说,在一个有向无环图

HDU Crixalis&#39;s Equipment(贪心)

贪心策略: 数据(v1, v2),按 v2 - v1 的差值从大到小进行排序 分析: 例(1): v = 10 2件物品:a(2, 5) b(6, 8) -----如果先进a,再进b: 进a:v = 10 --> v >= 5 (成立) --> v -= 2 --> v = 8 进b:v = 8 --> v >= 8 (成立) --> v -= 6 --> v = 2 物品全部进入 输出Yes -----如果先进b,再进a: 进b:v = 10 -->