hdoj 1789 Doing Homework again

来源:互联网 发布:软件外部接口设计 编辑:程序博客网 时间:2024/05/21 14:08

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10034    Accepted Submission(s): 5869


Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
 

Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
 

Output
For each test case, you should output the smallest total reduced score, one line per test case.
 

Sample Input
333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4
 

Sample Output
035
 

Author
lcy
 

Source
2007省赛集训队练习赛(10)_以此感谢DOOMIII
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1257 1978 1421 2159 1159 
一道很有水平的贪心题,写了很长时间,不过很有收获
题意:每门学科都有一天的复习时间,每门学科距离考试时间已知,学分已知,如何使学分丢的最少
思路:利用了优先队列的思想,按当前的天数进入对列,当元素进不去时,,让学分最低的出对列
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;struct node{int day;int score; } t[110000]; int cmp(node a,node b) { if(a.day==b.day) return a.score > b.score; return a.day < b.day; } bool operator < (const node &a,const node &b) { return a.score>b.score;//对  优先 队列 元素 进行 重载,队首元素为储存 分数  }//最低的  那一天  int i,j,k,l,m,n,p; int main() { scanf("%d",&p); while(p--) { scanf("%d",&n); memset(t,0,sizeof(t)); for(i=0;i<n;i++) scanf("%d",&t[i].day); for(i=0;i<n;i++) scanf("%d",&t[i].score); sort(t,t+n,cmp); priority_queue<node> q;//创建  优先 队列  int help=1; int ans=0; for(i=0;i<n;i++) { if(t[i].day>=help)//如果当前这一天 没有 超出时间限制  { q.push(t[i]);//就让这个元素进队列  help++;//同时 标记日期多了一天 }else//如果当前这一天超出了时间限制 {node temp=q.top();//我就取队首(最差的那个元素) {if(t[i].score > temp.score )//如果不取最差的元素 {//把最差元素浪费的那一天用来取当前这个元素比较好 q.pop();//消去队首元素,表示不去取 q.push(t[i]);//把当前这个元素压入栈中表示取了当前这个 ans+=temp.score ;//同时,当前最差的元素表示这个分数没有+上  }elseans+=t[i].score ;//如果当前这个比较差,那就不取当前的; }} } printf("%d\n",ans); } }
还可以模拟对列,不过这个的时间复杂度比较高
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std; struct node { int day; int score; }t[110000]; int cmp(node a,node b) { if(a.day==b.day) return a.score>b.score; return a.day<b.day; } int i,j,k,l,m,n,p,a[110000]; int main() { scanf("%d",&p); while(p--) { memset(t,0,sizeof(t)); memset(a,0,sizeof(a)); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&t[i].day); for(i=0;i<n;i++)        scanf("%d",&t[i].score);        sort(t,t+n,cmp);        k=1;        int ans=0;        int flag=0;        for(i=0;i<n;i++)        {        if(t[i].day>=k)        {        a[flag++]=t[i].score;        k++;}else{sort(a,a+flag);                 if(t[i].score>a[0])                 {                 ans+=a[0];                 a[0]=t[i].score; } else ans+=t[i].score; }}printf("%d\n",ans++); } }






0 0
原创粉丝点击