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 2 1 1 1 4 2
2
References
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3385
比赛的时候想了我一个多小时。。。题目的意思是说两个人要举行一个party,他们有n天准备,第一个人是个贪吃鬼,每天要吃掉a[i]的食物,第二个人负责做饭,他每天可以做L份食物,现在我们优先满足第一个人够吃,问你最后最多可以剩多少份饭,对于第二个人来说,他有两种选择,一种是在当天我们可以选择做L份饭,另一种是修仙,即能力加一,意思就是这天不做饭,但L=L+1。
这是一个贪心题,我一开始当做dp推了半天......我们首先考虑把所有可修仙的天数(第几天)记录下来,从前往后推,如果当天第一个人不够吃了,那我们回退一天,即把上一个可修仙的天数回退掉,让那一天不修仙了,做饭,最后我们把所有的可修仙的天数都遍历一遍做饭,取最大值,然后就可以了,注意时间复杂度,用栈优化一下。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#define LL long long
using namespace std;
stack<int >sta;
int a[200000];
int main(){
int n,L;
while(~scanf("%d%d",&n,&L)){
LL sum=0;
bool flag=true;
while(!sta.empty()){
sta.pop();
}
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
if(sum>=a[i]){
L++;
sta.push(i);
sum-=a[i];
}
else{
sum+=L;
while(sum<a[i]){
if(sta.empty()){
flag=false;
break;
}
int x=sta.top();
sta.pop();
L--;
sum+=L-(i-x);
}
if(!flag){
break;
}
sum-=a[i];
}
}
LL tem=sum;
if(!flag){
printf("Myon\n");
continue;
}
else{
while(!sta.empty()){
int x=sta.top();
sta.pop();
L--;
tem+=L-(n-x);
sum=max(sum,tem);
}
printf("%lld\n",sum);
}
}
return 0;
}