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.
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.
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.
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".
5 21 1 1 4 2
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;}
- 2017省组队训练 zoj 3385Hanami Party (贪心+栈优化)
- 2017组队训练 ZOJ
- ZOJ 3941 Kpop Music Party(省赛, 贪心)
- 2017组队训练赛11
- 2017省组队训练 zoj3780 Paint the Grid Again (模拟)
- 2017、5、13日省赛前组队训练赛。
- 【第四场省赛组队赛训练补题】ZOJ
- 5.24 省赛组队训练(1)
- 2016省赛第一次组队训练总结
- ZOJ 2343(贪心)
- ZOJ 3715 (贪心)
- zoj-3721(贪心)
- OUC_2014 Spring Training 省赛组队训练赛 #1总结
- SDUT 2017 春夏组队训练赛3
- Is Derek lying?-2017 暑期组队训练赛1-HDU-
- 2017年暑假组队训练赛-No.2 F
- 2017年暑假组队训练赛-No.7 K
- CF#808 C. Tea Party(贪心)
- java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
- Ghost blog的文件迁移
- h5调用支付宝接口
- 欢迎使用CSDN-markdown编辑器
- SQLITE3 使用总结
- 2017省组队训练 zoj 3385Hanami Party (贪心+栈优化)
- 蓝桥杯 高僧斗法(nim博弈)
- ryzom common.cfg
- Cocos code ide 启动报错:An internal error occurred during: "Initialize DLTK".
- shell脚本(转)
- config files problem
- 在linux环境下实现页面读取邮件
- Vue 2.0 全选,反选,全不选(二)
- ryzom-contribution /code/ryzom/server/shard_unifier_service_default.cfg