BZOJ3229 Stone Merge

Source: Internet
Author: User

Description a row of n piles of stones in a playground. It is now necessary to merge the stones into a pile in order.  It is stipulated that each of the adjacent 2 stones can be merged into a new pile each time, and a new pile of stones is counted as the score of the merger. Try to design an algorithm that calculates the minimum score to combine n heap stones into a pile.  The first line of Input is a number n. The following n rows are a number a per line, representing the number of stones. Output

A total of one number, that is, n heap of stones combined into a pile of the smallest score.

Sample Input4
1
1
1
1

Sample Output8
HINT

For 100% of data, 1≤n≤40000

For 100% of data, 1≤a≤200

Positive solution: Garsiawachs Algorithm Problem solving report: The Magic algorithm, incredibly can be the stone merge to do O (Nlogn), incredible, feel afraid.  In the morning when the topic mentioned the use of quadrilateral inequalities can have monotonic n^2 optimization dp, but the face of 40000 of the data is still a little bit virtual. Shift Learning Garsiawachs algorithm: http://www.cnblogs.com/lwq12138/p/5425465.html or http://blog.csdn.net/Regina8023/article/ details/45244733 not to be told in detail.
1 //It's made by jump~2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> - #ifdef WIN32 - #defineOT "%i64d" the #else - #defineOT "%lld" - #endif - using namespacestd; +typedefLong LongLL; - Const intINF =5201314; + Const intMAXN =40011; A intA[MAXN]; at intN; - LL ans; -  -InlineintGetint () - { -        intw=0, q=0; in        CharC=GetChar (); -         while((c<'0'|| C>'9') && c!='-') c=GetChar (); to        if(c=='-') q=1, c=GetChar (); +         while(c>='0'&& c<='9') w=w*Ten+c-'0', c=GetChar (); -        returnQ? -w:w; the } *  $InlinevoidWork () {Panax Notoginsengn=getint (); -      for(intI=1; i<=n;i++) a[i]=getint (); the     intm=N; +a[0]=a[n+1]=inf; A      for(intI=1; i<n;i++) {//not for the last time. the     intK; +      for(intj=1; j<=m;j++) {//from left to right, find the first k satisfying a[k−1]≤a[k+1] each time and merge A[k−1],a[k] -         if(a[j-1]<a[j+1]) {k=j; Break; } $     } $a[k-1]+=A[k]; -      for(intj=k;j<m;j++) a[j]=a[j+1];//Move left -ans+=a[k-1]; k--;//point to current processing node the      while(k>0&& a[k-1]<=a[k]) {//from the current position to the left to find the first a[j]>a[k−1]+a[k] J, insert the combined value behind J -Swap (a[k-1],a[k]);Wuyik--; the     } -a[m]=inf; Wum--; -     } About printf (Ot,ans); $ } -  - intMain () - { A Work (); +   return 0; the}

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.