Topic link: http://codeforces.com/problemset/problem/1006/B
meaning of the title
An array of length n is divided into k segments. Find the maximum sum of the sum of the maximum values in each segment and the number of data in each segment of the partitioned K segment.
Title Solution
In order to ensure that the maximum sum of the maximum values of each segment is the largest, all the data should be arranged, and the first k data will be the maximum value of each required segment. The next step is to find the interval length. The subscripts of the first k data can be saved for calculating the interval length. The first interval extends from its subscript to the left to the end and to the right to the next subscript (the case of no next coordinate should be considered). The remaining intervals are extended from the corresponding subscript to the right to the next coordinate (the last interval extends to n)
- AC Code
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; struct Data { int x; int id; }; bool cmp1(Data a, Data b) { if(a.x > b.x) return true; else if(a.x == b.x) { return a.id > b.id; } else return false; } bool cmp2(int x, int y) { return x < y; } Data st[2000+5]; int a[2000+5]; int b[2000+5]; int main() { int n, k; cin>> n >> k; for(int i=1; i<=n; i++) { cin>> st[i].x; st[i].id = i; } sort(st+1, st+1+n, cmp1); int sum = 0; for(int i=1; i<=k; i++) { sum = sum + st[i].x; a[i] = st[i].id; } sort(a+1, a+1+k, cmp2); for(int i=1; i<=k; i++) //Divide the interval according to the subscript (the key is the first interval and the last interval) { if(i==1) { b[i] = a[i]; if(a[2]!=0) b[i] = b[i] + a[2]-a[1]-1; //A key if(a[2]==0) b[i] = n; } else if(1<i && i<k) { b[i] = a[i+1] - a[i]; } else b[i] = n - a[i] + 1; } cout<< sum << endl; for(int i=1; i<=k; i++) { if(i==1) printf("%d",b[i]); else printf(" %d",b[i]); } printf("\n"); return 0; }