Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16478 Accepted Submission(s): 9602
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.
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
/*
第一步按照分数score从大到小排序,如果score相等,则按照截止时间deadline从前往后排。
然后开始选择,让当前的课排在其deadline上面,如果这一天已经被占用了,那么就往前循环,有位置了就安排,
没位置了就ans+=score。
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct homework
{
int time;
int score;
}hm[1011];
bool cmp(homework a,homework b)
{
if(a.score>b.score)
return true;
else if(a.time<b.time&&a.score==b.score)
return true;
return false;
}
int main()
{
int N,n,i,j;
int work[1005];//代表那天是否已经用来做作业
scanf("%d",&N);
while(N--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&hm[i].time);
for(i=1;i<=n;i++)
scanf("%d",&hm[i].score);
sort(hm+1,hm+n+1,cmp);//因为往结构体数组里读入数据是从1开始的,所以快排的时候记得n+1;
memset(work,0,sizeof(work));
int ans=0;
for(i=1;i<=n;i++)//排好序之后从第一个作业开始处理
{
for(j=hm[i].time;j>0;j--)//作业是可以当天做的
{
if(work[j]==0)//如果这一天没有任务。
{
work[j]=1;
break;
}
}
if(j<=0)//前面没有可用的时间
ans+=hm[i].score;
}
printf("%d\n",ans);
}
return 0;
}