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

hdu1789 Doing Homework again

2017-01-20 17:55 246 查看
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.
InputThe 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.
OutputFor 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


思路:和处理前几篇贪心博文差不多,比如所需最少的椅子,就是着手这段区域,这道题也是一样,在限制完成作业时间的区域中选择最优解;先按照扣分严重的排序,然后在规定日期之前做完,v[ j ] 中的 j 是记录某一天已经被用了,按照这个思想处理到数据结束,切记局部最优;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int v[100000];
struct node
{
int a;
int b;
}s[1010];
int cmp(node x,node y)
{
if(x.b!=y.b)
return x.b>y.b;
else return x.a<y.a;
}
int main()
{
int n,m,i,j,t;
scanf("%d",&t);
while(t--)
{
int ans=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&s[i].a);
for(i=0;i<n;i++)
scanf("%d",&s[i].b);
sort(s,s+n,cmp);
memset(v,0,sizeof(v));
for(i=0;i<n;i++)
{
for(j=s[i].a;j>0;j--)
{
if(!v[j])
{
v[j]=1;
break;
}
}
if(j==0)
ans+=s[i].b;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: