Codeforce 1175 D. Array Splitting

Warming up fresh questions codeforces 1175 D .

The meaning of problems: a given length n-$ $ $ A $ sequence, you need to divide it into $ k $ segments, each of which elements need to immediately paragraph. After the good points, to calculate $ \ sum_ {i = 1} ^ {n} (a_i \ centerdot f (i)) $, so that the maximum value. Where $ f (i) $ $ I $ represents the elements was in the first paragraphs. Simply put, each element was in the first few paragraphs on the need to take a few, and the largest sequence.

Suppose $ SUM $ $ A $ prefix and, $ k $ dividing points are $ l_j $, the sequence into $ [1, l_1], [l_1 + 1, l_2] ...... [l_ {k-1} + 1, l_k] $, then the contribution of $ $ J segment is a sum of $ (sum_ {l_j} - sum_ {l_ {j-1}}) * j $.

Thus, the sum should be $ sum_ {l_1} + (sum_ {l_2} - sum_ {l_1}) * 2 + (sum_ {l_3} - sum_ {l_2}) * 3 + ...... + (sum_ {l_k} - sum_ {l_ {k-1}}) * k $, it is clear that there can be canceled between the adjacent two portions, while $ $ L_K necessarily $ n $. Simplification of which: $ sum_n \ centerdot k - sum_ {l_1} - sum_ {l_2} - ...... -sum_ {l_ {k-1}} $, we only need to forward $ n-1 $ prefix item and find the minimum $ k-1 $ a will be the sum of the maximum.

 

int n, k;
int a;
ll sum[maxn];
int main() {
    scanf("%d %d", &n, &k);
    priority_queue<ll, vector<ll>, greater<> > q;
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &a);
        sum[i] = sum[i - 1] + a;
        if(i != n)
            q.push(sum[i]);
    }
    ll ans = sum[n] * k;
    for( Int I = . 1 ; I <= K - . 1 ; I ++) { // find the smallest prefix and the k-1 
        ANS - = q.top (); 
        q.pop (); 
    } 
    the printf ( " % LLD \ n- " , ANS); 
}

 

Guess you like

Origin www.cnblogs.com/sun-of-Ice/p/10982844.html