题目描述:
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.
输入描述:
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.
输出描述:
For each test case, you should output the smallest total reduced score, one line per test case.
输入:
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
输出:
0
3
5
题意:
有 n 个作业,每个作业都有自己的 DDL,如果没有在 DDL 前做完这个作业,那么老师会扣掉这个作业的全部平时分。
如何安排做作业的顺序,才能尽可能少扣一点分。
题解:
简单贪心,按照分数从大往小排序。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 5;
struct point{
int s,t;
};
int vis[maxn];
point a[maxn];
int cmp(point x,point y){
if(x.s != y.s) return x.s > y.s;
return x.t < y.t;
}
int main(){
int tt,n;
scanf("%d",&tt);
while(tt--){
memset(vis,0,sizeof(vis));
scanf("%d",&n);
for(int i = 0; i < n; i ++){
scanf("%d",&a[i].t);
}
for(int i = 0; i < n; i ++){
scanf("%d",&a[i].s);
}
sort(a,a + n,cmp);
int ans = 0;
for(int i = 0; i < n; i ++){
int time = a[i].t;
while(time){
if(vis[time] == 0){
vis[time] = 1;
break;
}
else time --;
if(time == 0){
ans += a[i].s;
}
}
}
printf("%d\n",ans);
}
return 0;
}