Doing Homework again
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
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 <string.h>
#include <algorithm>
using namespace std;
struct node{
int t; //最后期限
int s; //分数
}num[1004];
int cmp(node a,node b) //排序
{
if(a.s>b.s || a.s==b.s && a.t>b.t)
return 1;
else
return 0;
}
int main()
{
int t;
int vis[1005]; //标记数组,若第i天有空闲,则vis[i]=0,反之v[i]=1;
scanf("%d",&t);
while(t--)
{
memset(vis,0,sizeof(vis));
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&num[i].t);
for(i=0;i<n;i++)
scanf("%d",&num[i].s);
sort(num,num+n,cmp);
int ans=0;
for(i=0;i<n;i++) //核心代码
{
for(j=num[i].t;j>0;j--)
{
if(!vis[j])
{
vis[j]=1;
break;
}
}
if(j==0)
ans+=num[i].s;
}
printf("%d\n",ans);
}
return 0;
}