bzoj3229 Stone Merge

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.