HDU 4442 Physical Examination (greedy + sort)

Source: Internet
Author: User

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 Descriptionwangpeng is a freshman. He is requested to has a physical examination when entering the university.
Now Wangpeng arrives at the hospital. Er ..... There many students, and the number is increasing!
There was many examination subjects to do, and there was a queue for every subject. The queues is getting longer as time goes by. Choosing the queue to stand are always a problem. Wangpeng to determine an exam sequence, so that he can finish all the physical examination subjects as early a s possible.

Inputthere is several test cases. Each test case is starts with a positive an integer n in a line, meaning the number of subjects (queues).
then n lines follow. The i-th line have a pair of integers (AI, bi) to describe the i-th queue:
1. If Wangpeng follows this queue at time 0, Wangpeng have to wait for AI seconds to finish this subject.
2. As the queue is getting longer, the waiting time would increase bi seconds every second while Wangpeng are not in the Que Ue.
The input ends with n = 0.
For all test cases, 0<n≤100000, 0≤ai,bi<231.

Outputfor each test case, output one line with a integer:the earliest time (counted by seconds) that Wangpeng can finish All exam subjects. Since Wangpeng is all confused by years, just print the Seconds mod 365x24x60x60.

Sample Input51 22 33 44 55 60

Sample Output1419HintIn the Sample Input, Wangpeng just follow the given order. He spends 1 second in the first queue, 5 seconds in the 2th queue, seconds in the 3th queue, 169 seconds in the 4th que UE, and 1217 seconds in the 5th queue. The total time is 1419s. Wangpeng have computed all possible orders in his 120-core-parallel head, and decided that's the optimal choice.

Source2012 Asia Jinhua Regional Contest

Recommendzhuyuanchen520

2 cases were discussed:

A1 B1

A2 B2

If this is the optimal order, then the following is true:

A1 + A1*B2 + a2 <= A2 + a2*b1 + A1

Equivalent to

A1/B1 < A2/B2

A1*b2 < A2*B1

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<stdlib.h>#include<algorithm>using namespacestd;Const intMod=365* -* -* -;Const intmaxn=100000+5;structnode{__int64 Ai,bi; BOOL operator< (ConstNode &b)Const    {        returnai*b.bi<b.ai*i; }}A[MAXN];intMain () {intN;  while(SCANF ("%d", &n) &&N) {__int64 ans=0; Memset (A,0,sizeof(a));  for(intI=0; i<n;i++) scanf ("%i64d%i64d",&a[i].ai,&a[i].bi); Sort (A,a+N);  for(intI=0; i<n;i++)        {            if(i==0) ans+=A[i].ai; Elseans+= (a[i].ai+ans*a[i].bi); Ans=ans%MoD; } printf ("%i64d\n", ans); }    return 0;}
View Code

HDU 4442 Physical Examination (greedy + sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.