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
Author
lcy
Source
思路
这是一道典型的贪心题。显然先完成惩罚分数大的会让答案更小,如果惩罚分数相同的时候,我们就要尽量将截止日期先的排在前面。实现的方式是按照惩罚分数来枚举截止日期,就可以完成我们的目的。详见注释
#include<bits/stdc++.h>
using namespace std;
struct node
{
int d; //截止日期
int r; //惩罚分数
}a[1010];
bool vis[1010];
bool cmp(node x,node y)
{
if(x.r==y.r)
return x.d < y.d;
else
return x.r > y.r;
} //排序函数,先按照惩罚分数排,如果惩罚分数一样就按照截止日期从小到大排
int main()
{
int T;
int ans;
while(cin>>T)
{
while(T--)
{
int N;
cin >> N;
for(int i=1;i<=N;i++) cin >> a[i].d;
for(int i=1;i<=N;i++) cin >> a[i].r;
sort(a+1,a+N+1,cmp);
memset(vis,false,sizeof(vis)); //读入并排序和初始化
ans = 0;
for(int i=1;i<=N;i++)
{
if(!vis[a[i].d])
vis[a[i].d] = true; //每个任务都是一天做完的,如果当天还没用就使用
else
{
int j;
for(j=a[i].d;j>=1;j--)
if(!vis[j]) break; //枚举截止日期,如果先前有一天没有用到就退出
if(j>0) vis[j] = true; //说明a]i].d之前的截止日期没有充分利用
else ans += a[i].r; //a[i].d之前每天都有任务,所以要进行惩罚分数的累加
}
}
cout << ans << endl;
}
}
return 0;
}