http://codeforces.com/contest/1006/problem/B
解题思路:
1.遍历 K 次找到前 K 大的值的下标,顺便计算前 K 大的和
2.下标排序
3.划分区间
-
第一个区间为以其下标往左延申到尽头,往右延申到下一个下标
-
剩余区间均为对应下标向右延申到下一个坐标(最后一个区间延申到 n )
import java.util.Collections;
import java.util.Scanner;
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(),k = sc.nextInt();
int[] num = new int[4010];
for(int i = 0;i < n;i++) {
num[i] = sc.nextInt();
}
Vector<Integer> ve = new Vector<>(); //存储前K大值的下标
boolean[] book = new boolean[4010]; //标记已存
int ans = 0;
for(int i = 1;i <= k;i++) { //遍历K次找到前K大的值的下标
int tag = 0,ma = 0;
for(int j = 0;j < n;j++) {
if(num[j] > ma && book[j] == false) {
ma = num[j];
tag = j;
}
}
book[tag] = true;
ve.add(tag);
ans += num[tag]; //计算前K大值的和
}
Collections.sort(ve); //前K大值的下标排序
ve.set(0,0); //第一个区间定义为第一个下标的左右两边
ve.add(n); //剩余区间都为到右边下一个下标
System.out.println(ans);
for(int i = 1;i < ve.size();i++) {
System.out.print((ve.get(i) - ve.get(i - 1)) + " ");
}
System.out.println();
}
}