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.
题意就是 一个hdu的神牛从world final英勇归来了。然后他要补作业。。
一共n个作业 完成每个作业都需要一天。但是每个作业有截止日期 如果超过了截止日期 就会扣分
给出 n个作业的截止日期 和 如果超过截止日期要扣的分 现在要计算 怎么设计做作业的顺序扣分最少 计算出这个最少的扣分值
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.
SampleInput
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
SampleOutput
0 3 5
先按照扣分从大到小排序,分数相同则按照截止日期从小到大排序。。
然后按顺序,从截止日期开始往前找没有占用掉的时间。
如果找不到了,则加到罚分里面
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 6 const int MAXN=1010; 7 8 struct Node 9 { 10 int d,s; 11 }node[MAXN]; 12 13 bool used[10000]; 14 15 bool cmp(Node a,Node b) //先按扣分从大到小排,若相等按时间从小到大排 16 { 17 if(a.s==b.s) 18 { 19 return a.d<b.d; 20 } 21 return a.s>b.s; 22 } 23 24 int main() 25 { 26 int T; 27 int n; 28 int j; 29 scanf("%d",&T); 30 while(T--) 31 { 32 scanf("%d",&n); 33 for(int i=0;i<n;i++) scanf("%d",&node[i].d); 34 for(int i=0;i<n;i++) scanf("%d",&node[i].s); 35 sort(node,node+n,cmp); 36 memset(used,false,sizeof(used)); 37 int ans=0; 38 for(int i=0;i<n;i++) 39 { 40 for(j=node[i].d;j>0;j--) //j指向node[i]的截止日期,从j开始往前找,如果有空闲时间则标记为true 41 { 42 if(!used[j]) 43 { 44 used[j]=true; 45 break; 46 } 47 } 48 if(j==0) //如果找到第1天也没有则需要加到扣分ans中 49 ans+=node[i].s; 50 } 51 printf("%d ",ans); 52 } 53 return 0; 54 }