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

HDU 1789 Doing Homework again

2016-07-23 15:01 603 查看
Doing Homework again

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

Total Submission(s): 11349 Accepted Submission(s): 6670

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

题意:第一行输入t,第二行输入n(表示n个作业),第三行输入n个作业要完成的最低天数,第四行时n个作业的分数,求最少扣多少分

题解:贪心算法,以作业分数排序,分数相同时以完成时间排序,注意要记录在某天是否已经已经被占用

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
#define M 1100

struct pro
{
int a, b;
};
pro p[M];
bool cmp(pro x, pro y)
{
if(x.b == y.b)
{
x.a < y.a;//当作业量相同就先完成所需时间短的
}
return x.b > y.b;//先按照作业分数从大到小排序
}
bool ok[M];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
int max = 0;
for(int i=0; i<n; i++)
{
scanf("%d", &p[i].a);
}
for(int i=0; i<n; i++)
{
scanf("%d", &p[i].b);
}
sort(p, p+n, cmp);
int ans = 0;
memset(ok, false, sizeof(ok));
for(int i=0; i<n; i++)
{
int j;
for(j=p[i].a; j>0; j--)//依次判断是否每一天都有需要完成的作业
{
if(!ok[j])
{
ok[j] = true;
break;
}
}
if(j == 0)//表示每天都已经被占
ans += p[i].b;
}

printf("%d\n", ans);
}

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