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

HDU_1789_doing homework again_贪心

2016-05-13 15:04 429 查看

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10705 Accepted Submission(s): 6299


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, you should output the smallest total reduced score, one line per test case.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

0

3

5

之前做doing homework是用的dp,这道题一来也想用dp,没想到思路,想了好久,然后搜了题解,发现是用的贪心,然后又不知道怎么贪。。。
最后用了题解的思路,按罚分由大到小排序,deadline之前只要有空着的天,那么就在最晚的那一天完成这项任务,否则,无空闲天,则会被罚分。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

struct Node
{
int dead;
int sco;
} node[1005];
int dead[1005];
int sco[1005];

bool cmp(Node a,Node b)
{
if(a.sco>b.sco)
return 1;
return 0;
}

int vis[1005];

int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(vis,0,sizeof(vis));
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&dead[i]);
for(int i=0; i<n; i++)
scanf("%d",&sco[i]);
for(int i=0; i<n; i++)
{
node[i].dead=dead[i];
node[i].sco=sco[i];
}
sort(node,node+n,cmp);
int day=1,spent=0;
for(int i=0; i<n; i++)
{
int j;
for( j=node[i].dead; j>=1; j--)
{
if(vis[j]==0)
{
vis[j]=1;
break;
}
}
if(j==0)
spent+=node[i].sco;
}
printf("%d\n",spent);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: