题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4442
题目描述:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1555 Accepted Submission(s): 461
5 1 2 2 3 3 4 4 5 5 6 0
1419HintIn 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.
题意:说要参加一场考试,每个科目就是一个考试队列,每个科目队列有如下两个属性,a,b,那么该科目的消耗时间=a+b*(不在该队列排队的时间);问给出一系列科目队列,确定一个最短总耗时的队列次序。
题解:贪心,策略为比较 bi/ai 与 bj/aj 的大小,较大者就先考较大的这门科目,ai=0的 排在前面(bi较大者排在前面),bi=0的 排在最后,因为相当于此科目没有成长性,随便什么时候做都行,这样利用这个策略将科目队列排序,然后依照顺序做就行了,注意数据应 longlong 和 结果取模。
代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<iostream> #include<string> #include<algorithm> #define DM 31536000 using namespace std; __int64 Examin[100000+5][2]; int n=0; __int64 Sum=0; /*compare for the qsort*/ int qcmp(const void *a,const void *b) { __int64 *A=(__int64 *)a; __int64 *B=(__int64 *)b; if(A[0]==0&&B[0]==0) { if(A[1]>B[1]) { return(-1); } else if(A[1]<B[1]) { return(1); } else { return(0); } } else if(A[0]==0&&B[0]!=0) { return(-1); } else if(A[0]!=0&&B[0]==0) { return(1); } else//A[0]!=0 && B[0]!=0 { if(A[1]==0&&B[1]==0) { return(0); } else if(A[1]==0&&B[1]!=0) { return(1); } else if(A[1]!=0&&B[1]==0) { return(-1); } else { if((double)A[1]/A[0]>(double)B[1]/B[0]) { return(-1); } else if((double)A[1]/A[0]<(double)B[1]/B[0]) { return(1); } else { return(0); } } } return(0); } /*for test*/ int test() { return(0); } /*main process*/ int MainProc() { while(scanf("%d",&n)!=EOF&&n) { int i=0; Sum=0; for(i=0;i<=n-1;i++) { scanf("%I64d%I64d",&Examin[i][0],&Examin[i][1]); } qsort(Examin,n,sizeof(Examin[0]),qcmp); //for(i=0;i<=n-1;i++) //{ // printf("%d %d\n",Examin[i][0],Examin[i][1]); //} Sum+=Examin[0][0]; for(i=1;i<=n-1;i++) { Sum+=Examin[i][0]+(Examin[i][1]*Sum)%DM; Sum%=DM; //printf("%d\n",Sum); } printf("%I64d\n",Sum); } return(0); } int main() { MainProc(); return(0); }