The main topic: Stone combined data oversized version.
Idea: Introduce a garsiawachs algorithm. Each time I find the position of the smallest one ai[i-1]<ai[i+1], merge the ai[i] and the ai[i-1], and then put it in the back of a stone that was closest to it before I was placed in it. (The concrete proof is not very clear, hoped that has the great God to be able to instruct.) )
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMaxnode 40005#defineLL Long Longusing namespacestd;intai[maxnode]={0},t; LL ans=0;voidWorkintx) { inttmp=ai[x]+ai[x-1];ans+= (LL) tmp;inti,j; for(i=x;i<t-1; ++i) ai[i]=ai[i+1]; --T; for(i=x-1; i&&ai[i-1]<tmp;--i) ai[i]=ai[i-1]; Ai[i]=tmp; while(i>=2&&ai[i-2]<=Ai[i]) {J=t-i;work (I-1); i=t-J; }}intMain () {intI,J,N;SCANF ("%d",&N); for(i=0; i<n;++i) scanf ("%d",&Ai[i]); T=1; for(i=1; i<n;++i) {ai[t++]=Ai[i]; while(t>=3&&ai[t-3]<=ai[t-1]) Work (t2); } while(t>1) Work (t1); printf ("%i64d\n", ans);}View Code
bzoj3229 Stone Merge