An old Stone Game ~~GarsiaWachs算法

来源:互联网 发布:mac pro retina 编辑:程序博客网 时间:2024/05/17 23:11
/*******************author:YKYemail:191217500@qq.comlanguage:c++problem:GarsiaWachs*******************/ #include<iostream> #include<stdio.h>#define maxx 50000using namespace std;int stone[maxx],n,t;int ret;void combine(int k){int tmp=stone[k]+stone[k-1];ret+=tmp;for(int i=k;i<t-1;i++)stone[i]=stone[i+1];t--;int j;for(j=k-1;j>0 && stone[j-1]<tmp;j--)stone[j]=stone[j-1];stone[j]=tmp;while(j>=2 && stone[j]>=stone[j-2]){int d=t-j;combine(j-1);j=t-d;} }int main(){while(scanf("%d",&n),n){for(int i=0;i<n;i++)scanf("%d",&stone[i]);t=1,ret=0;for(int i=1;i<n;i++){stone[t++]=stone[i];while(t>=3 && stone[t-3]<=stone[t-1])combine(t-2);}while(t>1) combine(t-1);printf("%d\n",ret);}return 0; }