HDU 2831 (贪心)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2831

题目大意:植物大战僵尸。给定种植植物时间间隔t,以及每个僵尸的到达时间v,生命d。问是否能赢。

解题思路

按照打完每只Zombie之后剩余时间v-d,从小到大排序。

理由如下:

设打完第i只Zombie的剩余时间为:$Remain(v,d)=V-i*t-D$

那么本题的目标函数为:$arg\max \limits_{i}\sum Remain(v,d)=V-i*t-D$

所以,应当尽可能把v-d较小的值放在i的前面,让Remain(v,d)尽可能大。

排序完之后,进行模拟,now=0,当前时间为0

从第1只Zombie算起,now+=t:

$if \quad Attack Time<D \quad then\quad GameOver$

注意,不可以取等,样例说明,正好到达位置被打死也会GameOver。

#include "cstdio"
#include "algorithm"
using namespace std;
struct Zombie
{
    int v,d,idx;
    bool operator < (const Zombie &a) const {return v-d<a.v-a.d;}
}z[105];
int main()
{
    //freopen("in.txt","r",stdin);
    int n,t;
    while(scanf("%d%d",&n,&t)!=EOF)
    {
        for(int i=0;i<n;i++) scanf("%d%d",&z[i].v,&z[i].d),z[i].idx=i+1;
        sort(z,z+n);
        int now=0;
        bool flag=true;
        for(int i=0;i<n;i++)
        {
            now+=t;
            int ret=z[i].v-now;
            if(ret<z[i].d) {flag=false;break;}
        }
        if(flag)
        {
            for(int i=0;i<n-1;i++) printf("%d ",z[i].idx);
            printf("%d\n",z[n-1].idx);
        }
        else printf("The zombies eat your brains!\n");
    }
}
时间: 2024-09-14 16:00:13

HDU 2831 (贪心)的相关文章

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 3183 贪心

A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3019    Accepted Submission(s): 1172 Problem Description Kiki likes traveling. One day she finds a magic lamp, unfortunately the geni