Problem Description
你可以选择今天不制作,让你每天做的数量变为L+1,或者今天制作L个
如何N天后剩下的蛋糕最多
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 2
1 1 1 4 2
Sample Output
2
思路:
每天优先选择,不做提高一天可以做的个数,发现剩余蛋糕不满足了。在撤销不做。变成做,直到剩余蛋糕满足为止。
#include<bits/stdc++.h>
#define LL long long
using namespace std;
int a[100055];
int main()
{
int N, L, flag, i;
stack<int> q;//栈
while(~scanf("%d %d", &N, &L))//N天,一开始每天做L个
{
while(!q.empty())//初始化栈
{
q.pop();
}
for(i = 0; i < N; i++)//输入
scanf("%d", &a[i]);
LL sum = 0, ans; flag = 0;//sum 表示剩余蛋糕的数量
int u;
for(i = 0; i < N; i++)
{
q.push(i);//假设今天不做,L+1,记录今天
L++;
while(sum < a[i] && !q.empty())//如果剩余的数量不够
{
u = q.top();//要出栈的是第几天
q.pop();
L--;//技能降低
sum += L - (i - u);//更新剩余蛋糕数量 期间过程每天都会少做一个
}
if(sum < a[i] && q.empty())//如果不满足
{
flag = 1;//标记一下
break;
}
sum -= a[i];//给小伙子吃,更新剩余蛋糕
}
if(flag) printf("Myon\n");//不满足
else
{
ans = sum;//满足的话 , 求最大值
while(!q.empty())
{
u = q.top();
q.pop();
L--;
sum += L - (N - 1 - u);
ans = max(ans, sum);//不断更新最大值
}
printf("%lld\n", ans);
}
}
}