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