HDU 1789 Doing Homework again

来源:互联网 发布:地图龟淘宝哪家的好 编辑:程序博客网 时间:2024/06/16 17:49

Doing Homework again

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


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
 

题目大意:老师给了一个最后期限,每一个老师的作业有不同的分值,帮助Ignatius失去的分尽量的少

贪心水题。怎么贪呢?我们对所有的deadline从小到大排序,如果deadline相同,就让他们的分数从大到小排序

#include<iostream>#include<algorithm>using namespace std;struct Node{int deadline;int score;int flag;}homework[1010];bool cmp(Node a,Node b){if(a.deadline==b.deadline) return a.score>b.score;return a.deadline<b.deadline;}int main(){ios::sync_with_stdio(0);int n;cin>>n;while(n--){int num;cin>>num;for(int i=0;i<num;i++) cin>>homework[i].deadline;for(int i=0;i<num;i++){homework[i].flag=1;cin>>homework[i].score;}sort(homework,homework+num,cmp);int sum=0,day=1;for(int i=0;i<num;i++){if(homework[i].deadline>=day){day++;}else{int temp=homework[i].score,flag=i;for(int j=0;j<i;j++){if(homework[j].score<temp&&homework[j].flag){temp=homework[j].score;flag=j;}}sum+=temp;homework[flag].flag=0;}}cout<<sum<<endl;}}