2017省组队训练 zoj 3385Hanami Party (贪心+栈优化)

来源:互联网 发布:linux wc命令C实现 编辑:程序博客网 时间:2024/06/18 17:21

题目链接:点击打开链接

Saigyouji Yuyuko and Konpaku Youmu are going to hold a hanami party (a Japanese traditional custom of enjoying the beauty of cherry blossoms). They decide to invite all their friends to come to the yard enjoying the feast under the flowering trees, and sometimes the party will go on until late at night.

th4770.jpg

To prepare for the grand party, Youmu need to make as much food as possible. But soon she noticed that the "hungry ghost" Yuyuko will eat some food she made every day. If there is no enough food, the hungry Yuyuko will become angry.

They have N days to prepare for the party. On the first day of their preparation, Youmu has an initial cooking level L, which means she can make L units of food in one day. Everyday, Youmu can choose to do only one of the actions below:

  • Improve her cooking level L by 1.
  • Make L units of food during that day.

Anyway, Youmu don't want to enrage Yuyuko, so she turned to you for help. You should maximize the amount of food Youmu can present on the party, without making Yuyuko angry.

Input

There are multiple test cases (no more than 20).

The first line contains two integers N and L (1 <= N <= 100000, 0 <= L <= 10^9). The second line contains N integers ai (0 <= ai <= 10^9), which means on the ith day of their preparation, Yuyuko will eat ai units of food that Youmu made.

Output

If Yuyuko won't get angry during the preparation, output the maximal amount of food Youmu can present on the party. Otherwise, output a line of "Myon".

Sample Input
5 21 1 1 4 2
Sample Output
2
题目大意:

要举行一个派对,为了这个派对准备了n天。在这n天了要做蛋糕。起初设定每天做L单位长度的蛋糕,每天被某人吃掉ai的长度。现在有一个方案,就是每天可以做L单位长度的蛋糕,如果这一天不做蛋糕,那么L可以自增1,(当然第i天还是要吃ai长度的),如果没有办法使得总长度满足第i天的ai,那么输出Myon。求一种方案使得最后剩下的蛋糕长度最大。

题解:

这个是很容易想到的,当初自己也想到怎么做了,但是感觉如果数据很大或者很变态的货是肯定会超时的就没有写。就是尽量的先让L加1,然后将加1的位置入栈,然后当走到不能满足ai的时候,让栈顶出栈,将栈顶的位置不是L加1操作了,而是改成做蛋糕,那么从当前位置到栈顶的那个位置就要全部-1,而且L要减1,知道总长度满足ai为止。如果栈空了也不能满足那就不能满足了。最后的时候要全部退栈试试有没有比当前方案更优的数。

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <queue>#include <stack>using namespace std;int a[211111];int main(){    int n;    long long L;    while(~scanf("%d%lld",&n,&L))    {        for(int i=0; i<n; i++)        {            scanf("%d",&a[i]);        }        stack<int>s;        while(!s.empty())s.pop();        long long sum=0;        int ok=1;        for(int i=0; i<n; i++)        {            if(sum>=a[i])            {                L++;                s.push(i);                sum-=a[i];            }            else            {                sum+=L;                while(sum<a[i])                {                    if(s.empty())                    {                        ok=0;                        break;                    }                    int x=s.top();                    s.pop();                    sum=sum-(i-x);                    L--;                    sum+=L;                }                if(ok==0)break;                sum=sum-a[i];            }        }        if(!ok)        {            printf("Myon\n");        }        else        {            long long num=sum;            int i=n-1;            while(!s.empty())            {                int x=s.top();                s.pop();                L--;                num=num-(i-x)+L;                sum=max(sum,num);            }            printf("%lld\n",sum);        }    }    return 0;}


0 0
原创粉丝点击