HDU-1789-Doing Homework again(贪心)
2018-01-18 21:28
330 查看
Doing Homework again
[align=left]Problem Description[/align]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.[align=left]Input[/align]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.[align=left]Output[/align]For each test case, you should output the smallest total reduced score, one line per test case.[align=left]Sample Input[/align]3
3
3 3 3
10 5 1
3
1 3 1
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4[align=left]Sample Output[/align]0
3
5
#include<cstdio> #include<cstring> #include<stdlib.h> #include<algorithm> using namespace std; struct cheng { int dline; int score; }a[1111]; int cmp (cheng a,cheng b) { if(a.score==b.score) { return a.dline < b.dline; } else { return a.score > b.score; } } int main() { int flag[1111]; int sum; int complete; int t,n; scanf("%d",&t); int i,j; while(t--) { scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i].dline); } for(i=0;i<n;i++) { scanf("%d",&a[i].score); } sort(a,a+n,cmp); memset(flag,0,sizeof(flag)); sum=0; for(i=0;i<n;i++) { for(j=a[i].dline-1;j>=0;j--) { if(flag[j]==0) { flag[j]=1; break; } } if(j==-1) { sum+=a[i].score; } } printf("%d\n",sum); }}
本题涉及1.结构体排序2.贪心算法每种作业按照扣分从大到小排序,分数相同时按照限期少的排在前面,但是,如果在自己限期内能完成,尽量往后拖,这样才能保证扣分最少!
还有第二种做法。。。
相关文章推荐
- HDU-1789 Doing Homework again 贪心问题 有时间限制的最小化惩罚问题
- hdu 1789 Doing Homework again(贪心)
- hdu 1789 Doing Homework again(贪心)
- HDU 1789 Doing Homework again (贪心)
- HDU1789 Doing Homework again(贪心+sort)
- hdu 1789 Doing Homework again(贪心)
- hdu 1789 Doing Homework again(贪心)
- hdu1789(Doing Homework again 贪心)
- HDU 1789 Doing Homework again 贪心 DP
- hdu 1789 doing homework again(贪心)
- hdu 1789 Doing Homework again(贪心)
- HDU 1789 Doing Homework again(贪心)
- HDU1789 Doing Homework again 贪心
- hdu1789 Doing Homework again (贪心+优先队列)
- HDU - 1789 Doing Homework again(贪心)
- HDU 1789 Doing Homework again【贪心】
- hdu 1789 Doing Homework again(贪心)
- HDU 1789 Doing Homework again(贪心)
- hdu 1789 Doing Homework again 贪心
- HDU:1789 Doing Homework again(经典贪心)