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

hdu1789 Doing Homework again (贪心)

2017-04-22 20:42 405 查看
Doing Homework again

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 13864 Accepted Submission(s): 8042

Problem Description

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

唉 想不到

虽然知道是贪心 可是就想不到贪心策略

非常佩服第一个想到的人

对于这道题而言 贪心策略是:

按照惩罚从大到小排序 ,然后从截至这一天开始往前遍历,直到找到一个没有使用的天。如果未能找到没有使用的天,那么就无法工作。

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct node
{
int d;
int cost;
}work[1005];
bool used[1005];
bool cmp(node x,node y)
{
return x.cost>y.cost;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
memset(used,false,sizeof(used));
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&work[i].d);
for(int i=0;i<n;i++)
scanf("%d",&work[i].cost);
sort(work,work+n,cmp);
int res=0;
for(int i=0;i<n;i++)
{
int k=work[i].d;
while(k>0&&used[k]) k--;
if(k==0)
{
res+=work[i].cost;
}
else
{
used[k]=true;
}
}
printf("%d\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: