HDU 1789 Doing Homework again (贪心)

Doing Homework again

http://acm.hdu.edu.cn/showproblem.php?pid=1789

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
 

 解题思路:每次取扣分最多的先安排,安排到那天没有任务的那天,所以将数据按扣分多少,由高到低排序,扣分相同的按最后期限,由少到多排序!

解题代码 :

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 using namespace std;
 6 
 7 const int max_n = 1005;
 8 
 9 struct SBU
10 {
11     int a, b;
12     bool operator < (const SBU sbu) const
13     {
14         if (b == sbu.b)
15             return a < sbu.a;
16         return b > sbu.b;
17     }
18 };
19 
20 int dp[max_n], tag[max_n];
21 
22 int main ()
23 {
24     int T, n;
25     scanf ("%d", &T);
26     SBU home[max_n];
27     while (T--)
28     {
29         int max_sum = 0;
30         scanf ("%d", &n);
31         memset (tag, 0, sizeof (tag));
32         for (int i = 0; i < n; i ++)
33         {
34             scanf ("%d", &home[i].a);
35         }
36         for (int i = 0; i < n; i ++)
37         {
38             scanf ("%d", &home[i].b);
39             max_sum += home[i].b;
40         }
41         sort(home, home+n);
42         int sum = 0;
43         for (int i = 0; i < n; i ++)
44         {
45             int d = home[i].a;
46             for (;d >= 1; d --)//寻找能放置任务的那天 
47             {
48                 if (tag[d] == 0)//找到退出 
49                     break;
50             }
51             if (tag[d] == 0 && d > 0)//判断是否可以安置 
52             {
53                 tag[d] = 1;//可以的话,将这天标记已有任务 
54                 sum += home[i].b;
55             }
56         }
57         cout << (max_sum - sum) <<endl;
58     }
59     return 0; 
60 }
View Code
原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3191479.html