Hrbust 1818 Stone Merging problem--linear version

Source: Internet
Author: User

The problem of stone merging--linear version time Limit:1000msmemory limit:32768kbthis problem'll be is judged onHrbust. Original id:1818
64-bit integer IO format: %lld Java class name: Main a line of stones with a total of n piles. Now we are going to combine the stones in an orderly manner. It is stipulated that each of the two adjacent piles can be merged into a new pile at a time, and a new pile of stones is counted as the score of that merger. Edit the minimum score to combine the N heap into a pile and the maximum score to combine the n heap of stones into a pile. Input

Input has multiple sets of test data.

The first behavior of each group N (n<=100), indicating that there are n heap of stones,.

Two acts n a space-separated integer, which in turn represents the number of stones in this n heap of stone ai (0<ai<=100)

Outputone row for each set of test data outputs. The output merges the N heap of pebbles into a pile of minimum scores and merges the N heap into a pile of maximum scores. The middle is separated by a space. Sample Input

3

1 2 3

Sample Output
9 11

Problem solving: A typical interval type DP
1#include <iostream>2#include <cstdio>3 #defineINF 0x3f3f3f3f4 using namespacestd;5 Const intMAXN = -;6 intMAXS[MAXN][MAXN],MINS[MAXN][MAXN],SUM[MAXN];7 intMain () {8     intN;9      while(~SCANF ("%d",&N)) {Ten          for(inti =1; I <= N; ++i) { Onescanf"%d", sum+i); AMaxs[i][i] = Mins[i][i] =0; -Sum[i] + = sum[i-1]; -         } the          for(intj =2; J <= N; ++j) { -              for(inti =1; i+j-1<= N; ++i) { -                 intt = i+j-1, TMP = sum[t]-sum[i-1]; -MAXS[I][T] =-INF; +MINS[I][T] =INF; -                  for(intK = i; K < T; ++k) { +Mins[i][t] = min (mins[i][t],mins[i][k]+mins[k+1][t]+tmp); AMaxs[i][t] = max (maxs[i][t],maxs[i][k]+maxs[k+1][t]+tmp); at                 } -             } -         } -printf"%d%d\n", mins[1][n],maxs[1][n]); -     } -     return 0; in}
View Code

Hrbust 1818 Stone Merging problem--linear version

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.