hdu 1789 贪心

画一个图分析一下就清楚啦,应该优先考虑完成损失最大的作业,因为一天只能完成一门作业,而同时应该倒着从deadline往回找合适的完成时间,因为这样对于别的作业来说,它们有更多的机会被完成。另外,deadline的数据范围也是1000以内。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6
 7 const int N = 1007;
 8 bool visit[N];
 9
10 struct Node
11 {
12     int d, s;
13     bool operator < ( const Node & o ) const
14     {
15         return s > o.s;
16     }
17 } node[N];
18
19 int main ()
20 {
21     int t;
22     scanf("%d", &t);
23     while ( t-- )
24     {
25         int n, sum = 0;
26         scanf("%d", &n);
27         for ( int i = 0; i < n; i++ )
28         {
29             scanf("%d", &node[i].d);
30         }
31         for ( int i = 0; i < n; i++ )
32         {
33             scanf("%d", &node[i].s);
34             sum += node[i].s;
35         }
36         sort( node, node + n );
37         memset( visit, 0, sizeof(visit) );
38         for ( int i = 0; i < n; i++ )
39         {
40             for ( int j = node[i].d; j > 0; j-- )
41             {
42                 if ( !visit[j] )
43                 {
44                     visit[j] = 1;
45                     sum -= node[i].s;
46                     break;
47                 }
48             }
49         }
50         printf("%d\n", sum);
51     }
52     return 0;
53 }
时间: 2024-10-16 23:11:27

hdu 1789 贪心的相关文章

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu

hdu 1050(贪心算法)

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

HDU2111 Saving HDU 【贪心】

Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5245    Accepted Submission(s): 2397 Problem Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的. 一天,当他正在苦思冥想解困良策的时

hdu 1257 &amp;&amp; hdu 1789(简单DP或贪心)

第一题;http://acm.hdu.edu.cn/showproblem.php?pid=1257 贪心与dp傻傻分不清楚,把每一个系统的最小值存起来比较 1 #include<cstdio> 2 using namespace std; 3 int a[30001],b[30001]; 4 int main() 5 { 6 int n,i,j; 7 while (~scanf("%d",&n)) 8 { 9 int ans=0; 10 b[0]=-1; 11 f