Description
zichen 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 zichen 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 zichen 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
zichen 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 zichen 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 zichen 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
题意:已知你有n个作业没做完,你每天可以做1个作业,告诉你这些作业最晚期限,还有分值。问你最少扣多少分。
思路:这道题可以用贪心做。将作业分值从大到小排,先做分值大的作业。相同分值先做天数最晚的。对做过作业的天数和做过的作业记录为1,最后剩下没做的作业分值相加即为被扣最小分值。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct st{
int end;
int s;
}a[1001];
bool cmp(st x,st y)
{
if(x.s!=y.s)
return x.s>y.s;
else
return x.end>y.end;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int b[1001];
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i].end);
}
for(int i=0;i<n;i++)
{
scanf("%d",&a[i].s);
}
sort(a,a+n,cmp);
int ans=0;
int k=a[0].end;
for(int i=0;i<n;i++)
{
if(b[k]==0)
{
b[k]=1;
k=a[i+1].end;
}
else
{
k--;
i--;
if(k<1)
{
i++;
ans+=a[i].s;
k=a[i+1].end;
}
}
}
printf("%d\n",ans);
}
return 0;
}