HDU - 1789 Doing Homework again

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;

struct Node {
	int day, cost;
} node[maxn];
int n;
int f[maxn];

int cmp(Node a, Node b) {
	return a.cost < b.cost;
}

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



你可能感兴趣的:(HDU - 1789 Doing Homework again)