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
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
Sample Output
0 3 5
题目大意:做作业,每个作业有最晚的完成时间,还有相应的处罚值,如果在这天还没能完成作业则会受到处罚。问怎么样使处罚值最小。。。。。
解题思路:按照处罚值从大到小排序,然后把从处罚最大值的开始遍历,如果在规定的日期前的天数都被占用只能加上这个处罚值。因为是按处罚值拍的序列,所以加起来处罚值是最小的。
代码如下:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct note
{
int a,b;
}e[1010];
bool cmp(note x,note y)
{
if(x.b==y.b)
return x.a<y.a;
else
return x.b>y.b;
}
int book[1010];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{ memset(book,0,sizeof(book));
int m,i,j;
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&e[i].a); //输入最晚完成作业的时间
for(i=0;i<m;i++)
scanf("%d",&e[i].b); //输入没完成的处罚
值 sort(e,e+m,cmp); //将结构体按处罚值从大到小排序
int sum=0;
for(i=0;i<m;i++)
{
int k=e[i].a; //k为最晚完成的时间
for( ;k>0;k--)
{
if(!book[k]) //这天有没有被占用,有的话,推到前一天
{
book[k]=1;
break;
}
}
if(k==0) //如果之前的天都被占用了,只能加上这个处罚值。
sum=sum+e[i].b; //此时的罚值为最小。
}
printf("%d\n",sum);
}
return 0;
}