D. Yet Another Yet Another Tasktime limit per test1.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output
Alice and Bob are playing yet another card game. This time the rules are the following. There are nn cards lying in a row in front of them. The ii-th card has value aiai.First, Alice chooses a non-empty consecutive segment of cards [l;r][l;r] (l≤rl≤r). After that Bob removes a single card jj from that segment (l≤j≤r)(l≤j≤r). The score of the game is the total value of the remaining cards on the segment(al+al+1+⋯+aj−1+aj+1+⋯+ar−1+ar)(al+al+1+⋯+aj−1+aj+1+⋯+ar−1+ar). In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is 00.Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.What segment should Alice choose so that the score is maximum possible? Output the maximum score.InputThe first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of cards.The second line contains nn integers a1,a2,…,ana1,a2,…,an (−30≤ai≤30−30≤ai≤30) — the values on the cards.OutputPrint a single integer — the final score of the game.
Examples
input
5
5 -2 10 -1 4
output
6
input
8
5 2 5 3 -30 -30 6 9
output
10
input
3
-10 6 -15
output
0
NoteIn the first example Alice chooses a segment [1;5][1;5] — the entire row of cards. Bob removes card 33 with the value 1010 from the segment. Thus, the final score is 5+(−2)+(−1)+4=65+(−2)+(−1)+4=6.In the second example Alice chooses a segment [1;4][1;4], so that Bob removes either card 11 or 33 with the value 55, making the answer 5+2+3=105+2+3=10.In the third example Alice can choose any of the segments of length 11: [1;1][1;1], [2;2][2;2] or [3;3][3;3]. Bob removes the only card, so the score is 00. If Alice chooses some other segment then the answer will be less than 00.
#include <bits/stdc++.h>
using namespace std;
long long ll,tt,i,j,b[1212145],c[1111456],d;
long long x,m,n,r,s,t,k,l,y,z;
string p,q ;
long long a[1234567];
vector<int> h,hh;
int main()
{
cin >> n;
for (i = 0; i <n; i ++)
cin >> a[i];
s = 0;
for (z=1;z<=30; z++){
x=-100;
m=0;
for (i = 0; i <n; i ++){
if (a[i]>m+a[i]){
m=a[i];
x=a[i];
}
else {
m += a[i];
x=max(x,a[i]);
}
if (x > z){
m=0;
x=0;
}
s=max(s,m-x);
}
}
cout <<s<<endl;
return 0;
}
本文探讨了一种卡牌游戏中Alice与Bob之间的最优策略选择。游戏规则为:Alice选取一段连续的卡牌,Bob移除一张,最终得分由剩余卡牌价值决定。文章通过算法分析了如何使Alice获得最大可能的分数。
338

被折叠的 条评论
为什么被折叠?



