noip1995 Stone Merge-DP

Source: Internet
Author: User
Tags min

title address: https://www.rqnoj.cn/problem/490

In a circular playground surrounded by n heap of Stones (n≤500), now the stones are to be combined into a pile in sequence. The rule is that only two adjacent stacks can be merged into a new pile at a time, and a new pile of stones will be counted as the score of that merger.

A program that reads the number of heaps N and the number of stones per heap;

⑴ chooses a scheme of combining pebbles, which makes n-1 times merge, the sum of the scores is the smallest;

⑵ Choose a method of combining stones, make n-1 times merge, the sum of the scores is the biggest.

input File:

1th Act n, representing n heap of stones;

The 2nd Act n number, which represents the number of stones per heap.

Output File:

The sum of the scores of the 1th Act is combined with the minimum value;

The 2nd behavior merges the maximum score sum.

Input Sample:

4

4 5 9 4 (clockwise number from top of pile)

Sample output:

43 (min)

54 (max)

"Analysis"

The first time you get the title, many players will merge with greedy methods that are as close to the target as possible. As a sample, starting from the top of the pile, in a clockwise direction into a ring, to calculate the minimum score, the first time to take the lowest adjacent score 4, 42 heap merge, the score is 8 points, after the merger of 3 piles, each pile of scores of 8,9,5; then select the 5,8 with the smallest adjacent score to merge, 13, and 9 , 22 points, a total score of 43, as with the output, as if this greedy strategy is right. In fact, this strategy has a counter-example. For example, 6 piles of gravel, from the top of a pile clockwise, the 1th heap stone number is 3, the 2nd heap stone number is 4, the 3rd heap stone number is 6, the 4th heap stone number is 5, the 5th heap stone number is 4, the 6th heap stone number is 2, uses the greedy strategy merger, the merging process as shown in Figure 2-1.

Obviously, the merge scenario in Figure 2-2 is better than the greedy merge scheme in Figure 2-1. The example created a greedy strategy to solve the problem of the illusion, resulting in a lot of competitors using greedy strategy, thus lost a lot of points. The second merge scenario is analyzed below. From the back forward, it can be seen from figure 2-2 that the minimum scoring scheme for the 6 heap of Stones Min{merge (①,②,③,④,⑤,⑥)} is derived from the merge (①,②,③) +merge (④,⑤,⑥), in which the merge represents a merger. Merge (①,②,③), it has two kinds of merging schemes, that is, merge (①,②) two piles, then merge the results of ①② merge with the ③ heap, or merge (②,③) First, and then merge the results of ②③ merge with the ① heap. The merge process for both scenarios is as follows:

The first merger scenario: (3+4) +6 combined score of 7+13=20;

The second merger scenario: The 4+6 combined score is 10+13=23.

In comparison with the two schemes, the first scheme obviously scored small. Merge (④,⑤,⑥), in the same way, there are two options, that is, the first Merge④,⑤ two heap, and then merge the results of the ④⑤ merge with the ⑥ heap, or Merge⑤,⑥, and then merge the results of ⑤⑥ with the ④ heap. The merge process for both scenarios is as follows:

The first merger scenario: (5+4) +2 combined score of 9+11=20;

The second merger scenario: 5+ (4+2) combined with a score of 6+11=17.

The second scenario is significantly smaller than the two scenarios. Judging from the results of the smallest combined score in Figure 2-2, the calculation of the minimum score Min{merge (①,②,③,④,⑤,⑥)} for the 6 heap of gravel merges is obviously taken from Min{merge (①,②,③)} and Min{merge (④,⑤,⑥)}. It can be seen from this that the properties of the optimal substructure are found when the stones are combined. See the process of merging 1 to 6 stacks in detail.

Combined 1 piles: ①,②,..., ⑥;

Combined 2 piles: ①②,②③,..., ⑥①;

Consolidation of 3 piles: ①②③,②③④,...,⑥①②;

Consolidation of 4 piles: ①②③④,②③④⑤,...,⑥①②③;

Consolidation of 5 piles: ①②③④⑤,②③④⑤⑥,...,⑥①②③④;

Merge 6 piles: ①②③④⑤⑥,②③④⑤⑥①,..., ⑥①②③④⑤.

It can be seen from the merging process that when the J Heap is merged from the first heap, its value can be heap +min{merge (i+1,i+2,..., i+j-1)}. If a 4 heap is merged from the ① heap, it can be ① heap +min{merge (②,③,④)}, Min{merge (①,②)}+min{merge (③,④)}, Min{merge (①,②,③)}+ Heap, a total of 3 sources, Related to the merging of intervals. And so on, when merging into the 6 heap, take the minimum value of merging 6 heaps from the first heap, that is, the total minimum value of the merge. Therefore, we can say with certainty that this problem has the properties of the optimal substructure, and no effect.

Using the combined number of piles as a stage, using f[i,j] as the state, indicating that the total score of the J Heap is combined clockwise from the number of heaps of I, it includes the minimum total score of the pre-j-1 heap plus the combined score, with SUM[I,J] to represent the combined score. The number of heaps at the time of consolidation can be expressed as the sequence {i heap, i+1 heap,..., (i+j-2) mod n+1 heap}. There are many ways to get the sequence, we use sub-sequence 1 and sub-sequence 2, such as sub-sequence 1 is {i heap}, then sub-sequence 2 is {i+1 heap, ..., (i+j-2) mod n+1 heap}. Subsequence 1 and sub-sequence 2 are adjacent, so if the subsequence 1 is a k heap, then the Subsequence 2 is the J-k heap. Thus, the motion regulation equation can be obtained:

F[I,J]=MIN{F[I,K]+F[I+K,J-K]+SUM[I,J],1≤K≤J-1}

Use stone[i] to indicate the initial number of stones per heap, the initial conditions of the dynamic regulation are:

The boundary of the f[i,1]=0 motion gauge is 1≤i Code

#include <cstdio> using namespace std;
const int maxn=99999999;
int num[510]; int sum[510][510];//This time the combined score int fmax[510][510];//starts with the first heap of gravel, and the maximum value int fmin[510][510];//from the aggregate J Stone is started from the first heap of gravel,
    The minimum value int main () {int n,i,j is obtained by merging J heap Stones;
    scanf ("%d", &n);
        for (i=1;i<=n;++i) {scanf ("%d", &num[i]);
        Sum[i][1]=num[i];
        fmax[i][1]=0;
    fmin[i][1]=0; } for (J=2;j<=n;++j) for (i=1;i<=n;++i) sum[i][j]=num[i]+sum[(i%n) +1][j-1];//turns around, which is equivalent to considering who started the merge

    The
            for (J=2;J&LT;=N;++J) {for (i=1;i<=n;++i) {fmax[i][j]=0;
            FMIN[I][J]=MAXN;
                for (int k=1;k<=j-1;++k) {int next= ((i+k-1)%n) +1;
                    if (Fmax[i][j]<sum[i][j]+fmax[next][j-k]+fmax[i][k])//sequence a sequence of two merges will also score and the score is sum[i][j] because the addition satisfies the binding law.
                FMAX[I][J]=SUM[I][J]+FMAX[NEXT][J-K]+FMAX[I][K];
             if (Fmin[i][j]>sum[i][j]+fmin[next][j-k]+fmin[i][k])       FMIN[I][J]=SUM[I][J]+FMIN[NEXT][J-K]+FMIN[I][K];
    }}}//which position when merging int MIN=MAXN for the first time, max=0;
        for (I=1;i<=n;++i) {if (Min>fmin[i][n]) min=fmin[i][n];
    if (Max<fmax[i][n]) max=fmax[i][n];
    } printf ("%d\n%d", Min,max);
return 0;
 }


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.