rt,P6377 [PA2010]Termites 和 P3210 [HNOI2010]取石头游戏,两道题题面意思相同,甚至样例也相同,但是下面的程序可以A掉后面的题,前面的却WA三个点,求dalao解答
#include<bits/stdc++.h>
using namespace std;
long long n,top,b[10000005],l=1,r,times,sum,ans,temp[10000005],cnt;
int main()
{
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>b[++top];
if(b[top])
{
++times;
sum+=b[top];
}
while(top>=3&&b[top]&&b[top-1]&&b[top-2]&&b[top]<=b[top-1]&&b[top-1]>=b[top-2])
{
b[top-2]+=b[top]-b[top-1];
top-=2;
}
}
for(;b[l+1]<=b[l]&&b[l]&&b[l+1];l+=2)
ans+=times&1?b[l]-b[l+1]:b[l+1]-b[l];
for(r=top;b[r-1]<=b[r]&&b[r]&&b[r-1];r-=2)
ans+=times&1?b[r]-b[r-1]:b[r-1]-b[r];
for(int i=l;i<=r;++i)
if(b[i])
temp[++cnt]=b[i];
sort(temp+1,temp+1+cnt,greater<long long>());
for(int i=1;i<=cnt;++i)
ans+=i&1?temp[i]:-temp[i];
cout<<(sum+ans>>1)<<' '<<(sum-ans>>1)<<endl;
return 0;
}