贪心初步 hdu1789 Doing Homework again

一道非常经典的题目

题目地址

题目大意

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

正确的策略是:

  1. 扣除分数大的先做
  2. 扣除分数相同,先截止的先做
  3. 做一件事的时候,从截止时间开始向第一天遍历,如果当天没有被作业占据则标记为占据。做这件事的日期越大越好。
  4. 如果不能满足3的条件,则为不能完成
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <string.h>
 5
 6 #define MAX 1010
 7
 8 using namespace std;
 9 int V[MAX];
10 int E[MAX];
11 bool t[MAX];
12 int order[MAX];
13
14 void init()
15 {
16     memset(t,0,MAX * sizeof(bool));
17     return ;
18 }
19
20 bool cmpA (int a ,int b)
21 {
22     if(V[a] > V[b])
23     {
24         return 1;
25     }
26     else if(V[a] == V[b] && E[a] < E[b])
27     {
28         return 0;
29     }
30     else
31         return 0;
32 }
33
34 int main()
35 {
36     int T;
37     while(~scanf("%d",&T))
38     {
39         for (int i = 0; i < T; ++i)
40         {
41             init();
42             int N;
43             int All = 0;
44             int ans = 0;
45             scanf("%d",&N);
46             for (int j = 0; j < N; ++j)
47             {
48                 scanf("%d",&E[j]);
49             }
50             for (int j = 0; j < N; ++j)
51             {
52                 scanf("%d",&V[j]);
53                 All = All + V[j];
54                 order[j] = j;
55             }
56             //////////////////////////
57             sort(order,order + N,cmpA);
58             /////////////////////////////
59             //////////////////////////////
60             for (int j = 0; j < N; ++j)
61             {
62                 int event = order[j];
63                 bool flag = 0;
64                 for (int k = E[event]; k >= 1; k--)
65                 {
66                     if(!t[k])
67                     {
68                         ans = ans + V[event];
69                         t[k] = 1;
70                         flag = 1;
71                     }
72                     if(flag) break;
73                 }
74             }
75             ///////////////////////////
76             cout << All - ans << endl;
77
78         }
79     }
80     return 0;
81 }
时间: 2024-10-15 02:52:29

贪心初步 hdu1789 Doing Homework again的相关文章

HDU1789 Doing Homework again 【贪心】

Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6878    Accepted Submission(s): 4096 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he h

HDU1789 Doing Homework again 做作业【贪心】

题目链接:https://vjudge.net/problem/HDU-1789 题目大意: 给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案. 解析: 与上一道销售商品类似,将分数从大到小排序,找到deadline,如果它的期限没有被占用,就在该天写完,然后vis置1,如果占用,则从它的前一天开始向前查找有没有空闲的日期,如果有则占用,vis置1.这样就可以得到最大分数.然后用总分数减去得到最大分数即为扣除的最小分数. #include <cstdio> #in

贪心练习 hdu1789

贪心算法 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关.(摘自百度百科) 贪心选择 贪心选择是指所求问题的整体最优解可以通过一系列局部最优的选择,即贪心选择来达到.这是贪心算法可行的第一个基本要素,也是贪心算法与动态规划算法的主要区别.贪心

HDU-1050-Moving Tables(C++ &amp;&amp; 贪心初步)

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23147    Accepted Submission(s): 7779 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a

解题报告 HDU1789 Doing Homework again

Doing Homework again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

贪心/hdu 1789 Doing Homework again

1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int n,ans; 6 struct node 7 { 8 int score; 9 int ddl; 10 }; 11 node a[1010]; 12 bool day[1010]; 13 bool cmp(node x,node y) 14 { 15 return x.score>y.s

HDU1789(Doing Homework again)题解

HDU1789(Doing Homework again)题解 以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定任务分数和其截止日期,每日可完成一任务,输出当罚分尽可能小时的最小罚分. [题目分析] 由于写的时候就知道是贪心了(专项练习= =||),所以要设计贪心策略,但是应该先处理数据以便使用.由于要求罚分尽可能小,那么我们就根据罚分来排序.根据罚分从大到小排序,如果罚分相同则根据日期从小到大排序.(现在想想觉得似乎日期排不排都行..)那么我们的贪心策略应该尽可能保

hdu1789Doing Homework again(贪心)

题目链接: 啊哈哈,点我点我 思路: 这道题是简单的贪心..先按分数从大到小排序,然后将这个分数的截止日期从后向前扫描,如果碰到没有被标记的则这一天可以做这个作业... 题目: Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6451    Accepted Submission(s): 383

HDU1789时间贪心

Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13883    Accepted Submission(s): 8053 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he h