您的位置:首页 > 大数据 > 人工智能

文章标题 HDU 1789 : Doing Homework again (贪心)

2017-01-22 17:40 453 查看

Doing Homework again

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

题意:有n个作业需要做,没做一个作业需要一天,然后每个作业有作业的期限和没完成作业时所扣的分。在完成尽可能多的作业的同时,需要尽可能的减少扣的分数。

分析:很简单的可以想到,要想减少的分数尽可能的少,所以得将分数高的先完成,所以我们应该先将这些作业按照分数的高低排序,分数高的在前面。然后用一个数组flag来标记这一天是否被用来做作业了,然后每次的作业出来,就从期限的这一天往前找一个还没有用来做作业的时间来做作业,如果没有符合的时间则说明这个作业没办法完成,所以这个作业的分数被扣了。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int n;
struct node {
int date;
int score;
bool operator <(const node & t)const{//优先级,分数高的在前面
return score>t.score||score==t.score&&date<t.date;
}
};
node a[1005];
int flag[1005];//flag[i]表示第i天是否被用来做作业
int main ()
{
int t;
cin>>t;
while (t--){
cin>>n;
memset (flag,0,sizeof (flag));
for (int i=0;i<n;i++){
cin>>a[i].date;//输入时间
}
for (int i=0;i<n;i++){
cin>>a[i].score;//输入分数
}
sort(a,a+n);//排序
int ans=0;
for (int i=0;i<n;i++){
int judge=0;
for (int j=a[i].date;j>=1;j--){//往前找
if (flag[j]==0){//如果找到有一天还没用
judge=1;//标记这个作业做好了
flag[j]=1;//这一天已经用来做作业了
break;
}
}
if (!judge) ans+=a[i].score;//如果这个作业没办法做,需要扣分数
}
cout<<ans<<endl;//输出答案
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: