Http://codeforces.com/contest/313/problem/C
Greedy, constructor
I first thought it was a problem that could be solved by direct recursion. Later I discovered that it was necessary to construct a matrix. 4 ^ n numbers are given, and then you can use these numbers to construct a square matrix of 2 ^ n * 2 ^ n, And the beauty value of this square matrix is maximized.
The beauty value of a square matrix is defined in this way. If this square matrix has only one element, its beauty value is the element. Of course, the largest element in this square matrix is also it. If the square matrix contains more than one element, the square matrix can be divided into four small square arrays of equal size (because it must be 2 ^ n * 2 ^ n. The beauty value of a large array is equal to the beauty value of the four small squares, plus the largest element in the big rule. Your goal is to construct a square matrix to maximize its beauty value.
The bea value can be computed recursively. However, if you think about the bea value calculation process, you can obtain the constructed policy.
It will be found that each element in the square matrix will contribute to the final beauty value, but different elements make different contributions.
It seems that one square matrix is 2*2, and the three elements contribute once to the bea value of the square matrix, while the maximum element contributes twice. If the scale of the square matrix is larger, we will find that some elements have contributed four times, some elements have contributed three times, some two times, and some one time.
So in order to maximize the beauty value, we make the larger element contribute more times, so that the largest beauty can be larger (this is the greedy part of this question)
The following are examples:
1*1 matrix of scale
One element contributed once
2*2 scale matrix
One element contributes twice, and three elements contribute once.
4*4 matrix of scale
One element contributes three times, three elements contribute two times, and 12 elements contribute one time.
8*8 scale matrix
One element contributes 4 times, three elements contribute 3 times, 12 elements contribute 2 times, and 48 elements contribute 1 time.
The rule is more obvious. We only need to use some arrays to save the rule, so we can use it directly. For details, refer to the code.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; typedef long ll; const int N = 2000010; ll a [N], M [30], C [30], sum [30]; bool cmp (ll x, ll y) {return x> y ;} int main () {M [1] = 1; for (int I = 2; I <= 15; I ++) M [I] = M [I-1] * 4; C [1] = 1; C [2] = 3; for (int I = 3; I <= 15; I ++) C [I] = C [I-1] * 4; sum [0] = 0; sum [1] = 1; for (int I = 2; I <= 15; I ++) sum [I] = sum [I-1] + C [I]; int tot, n; cin> tot; for (int I = 1; I <= tot; I ++) cin> a [I]; sort (a + 1, a + 1 + tot, cmp); for (int I = 1; true; I ++) if (tot = M [I]) {n = I; break;} // n is its scale ll res = 0; int I = 0, j, k, m = n; for (k = 1; I <= tot; k ++) {for (I = sum [k-1] + 1; I <= sum [k] & I <= tot; I ++) res + = a [I] * m; m -- ;} cout <res <endl; return 0 ;}