[COJ6024] Merge fruit • Change (enhanced version)
Question Description
In an orchard, Toto has beaten down all the fruits and divided them into different heaps according to the different kinds of fruit. Make a lot of these fruit piles in a row, and then all the fruit into a pile.
Each time a merger, a lot can combine the adjacent two piles of fruit, the energy consumed equals the weight of the two piles of fruit. It can be seen that all the fruit after the N-1 merger, there is only a pile. The total amount of energy consumed in the merging of fruits equals the physical strength of each merger.
Because it's going to take a lot of effort to bring the fruit home, you need to save as much as you can when you combine the fruit. Assuming that each fruit has a weight of 1, and that the number of fruits and the number of each fruit is known, your task is to design a combination of sequential schemes that will consume the least amount of energy and output this minimum physical cost.
For example, there are 3 kinds of fruit, the number is 1,2,9. The 1 and 2 stacks can be merged first, and the new heap number is 3, which consumes 3 of the energy. Next, the new heap is merged with the original third heap, and a new heap is obtained, with a number of 12, which consumes 12 of the energy. So the total cost of energy =3+12=15. It can be shown that 15 is the minimum physical cost.
Input
Consists of two rows, the first line being an integer n, indicating the number of species of fruit. The second line contains n integers, separated by spaces, and the first integer AI (1<=ai<=20000) is the number of fruit of the first I.
Output
Includes one row, which contains only an integer, which is the minimum physical cost. The input data guarantees that this value is less than 2^63.
Input example
41 2 5 2
Output example
20
Data size and conventions
1<=n<=1000
Exercises
We can use the DP method of the previous question and then optimize it. We can make g[i][j] show that [I, j] in the optimal combination of the boundary point, even if the f[i][g[i][j]] [f[g[i][j]+1][j] The smallest, not difficult to find g[i][j-1]≤g[i][j]≤g[i+1][j], so f[i][j] = min{F[i][k] + f[k+1][j] + S (i,j) | g[i][j-1]≤k≤g[i+1][j]}, it is not difficult to find (I-j + 1) fixed, all G[i+1][j]-g[i][j-1] and the sum is n level , so the total time complexity becomes O (n2).
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin); Tail = (Head = buffer) + L; } return *head++;} int read () {int x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = GetChar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 110#define oo (1ll <<)-1#define LL long longint N; LL S[MAXN], F[maxn][maxn];int Main () {n = read (); for (int i = 1; I <= n; i++) s[i] = S[i-1] + read (); for (int len = 2; le n <= N; len++) for (int l = 1; l + len-1 <= N; l++) {int r = l + len-1;f[l][r] = oo;for (int k = l; k< R; k++) F[l][r] = min (F[l][r], f[l][k] + f[k+1][r]); F[l][r] + = S[r]-s[l-1];} printf ("%lld\n", F[1][n]); return 0;}
[COJ6024] Merge fruit • Change (enhanced version)