hdu1789 Doing Homework again---(经典贪心)

题目链接:

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

题目大意:

给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案。

思路:

贪心, 

正确的策略是:

  1. 扣除分数大的先做
  2. 扣除分数相同,先截止的先做
  3. 做一件事的时候,从截止时间开始向第一天遍历,如果当天没有被作业占据则标记为占据。做这件事的日期越大越好。
  4. 如果不能满足3的条件,则为不能完成
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<set>
 6 #include<cmath>
 7 using namespace std;
 8 const int maxn = 1e4 + 10;
 9 typedef long long ll;
10 int T, n, m;
11 struct node
12 {
13     int time, score;
14     bool operator <(const node& a)const
15     {
16         return score > a.score || score == a.score && time < a.time;
17     }
18 };
19 node a[maxn];
20 bool vis[maxn];
21 int main()
22 {
23     cin >> T;
24     while(T--)
25     {
26         cin >> n;
27         memset(vis, 0, sizeof(vis));
28         for(int i = 0; i < n; i++)cin >> a[i].time;
29         for(int i = 0; i < n; i++)cin >> a[i].score;
30         sort(a, a + n);
31         int sum = 0;
32         for(int i = 0; i < n; i++)
33         {
34             int ok = 0;
35             for(int j = a[i].time; j >= 1; j--)
36             {
37                 if(!vis[j]){vis[j] = ok = 1;break;}
38             }
39             if(!ok)sum += a[i].score;
40         }
41         cout<<sum<<endl;
42     }
43     return 0;
44 }
原文地址:https://www.cnblogs.com/fzl194/p/8696535.html