B. Yet Another Array Partitioning Task ——cf
An array bb is called to be a subarray of aa if it forms a continuous subsequence of aa , that is, if it is equal to alal , al+1al+1 , …… , ar
Suppose mm is some known constant. For any array, having mm or more elements, let‘s define it‘s beauty as the sum of mm largest elements of that array. For example:
- For array x=[4,3,1,5,2]x=[4,3,1,5,2] and m=3m=3 , the 33 largest elements of xx are 55 , 44 and 33 , so the beauty of
- For array x=[10,10,10]x=[10,10,10] and m=2m=2 , the beauty of xx is 10+10=2010+10=20 .
You are given an array a1,a2,…,ana1,a2,…,an , the value of the said constant mm and an integer kk . Your need to split the array aa into exactly kk subarrays such that:
- Each element from aa belongs to exactly one subarray.
- Each subarray has at least mm elements.
- The sum of all beauties of kk subarrays is maximum possible.
The first line contains three integers nn , mm and kk (2≤n≤2?1052≤n≤2?105 , 1≤m1≤m , 2≤k2≤k , m?k≤nm?k≤n ) — the number of elements in aa , the constant mm in the definition of beauty and the number of subarrays to split to.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (?109≤ai≤109?109≤ai≤109 ).
OutputIn the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.
In the second line, print k?1k?1 integers p1,p2,…,pk?1p1,p2,…,pk?1 (1≤p1<p2<…<pk?1<n1≤p1<p2<…<pk?1<n ) representing the partition of the array, in which:
- All elements with indices from 11 to p1p1 belong to the first subarray.
- All elements with indices from p1+1p1+1 to p2p2 belong to the second subarray.
- …… .
- All elements with indices from pk?1+1pk?1+1 to nn belong to the last, kk -th subarray.
If there are several optimal partitions, print any of them.
Examples Input Copy9 2 3 5 2 5 2 4 1 1 3 2Output Copy
21 3 5Input Copy
6 1 4 4 1 3 2 2 3Output Copy
12 1 3 5Input Copy
2 1 2 -1000000000 1000000000Output Copy
0 1Note
In the first example, one of the optimal partitions is [5,2,5][5,2,5] , [2,4][2,4] , [1,1,3,2][1,1,3,2] .
- The beauty of the subarray [5,2,5][5,2,5] is 5+5=105+5=10 .
- The beauty of the subarray [2,4][2,4] is 2+4=62+4=6 .
- The beauty of the subarray [1,1,3,2][1,1,3,2] is 3+2=53+2=5 .
The sum of their beauties is 10+6+5=2110+6+5=21 .
In the second example, one optimal partition is [4][4] , [1,3][1,3] , [2,2][2,2] , [3][3] .
大意:
這個題目是給你n個數據,讓你分成k段,每一段至少m個數,並且把每一段的前m大的數求和
輸出求和的結果,和分段的位置、
思路:
是求這每一段的前m大的數,其實可以轉化成,求這一組數前m*k大的數之和。
至於求分段的位置,這個就有點麻煩了,可以把每一個位置初始標記為0,
然後在求sum的同時,將求過的數位置標記成1,之後求分段位置的時候,
就可以進行累加,一旦累加之和為m,說明這m個數組成了一段,也就是在這個位置將數組分段。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> using namespace std; typedef long long ll; const int maxn=2e5+10; struct node { int x,id; }a[maxn]; bool vis[maxn]; bool cmp(node a,node b) { return a.x>b.x; } int main() { int n,m,k; memset(vis,0,sizeof(vis)); scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) { scanf("%d",&a[i].x); a[i].id=i; } sort(a+1,a+1+n,cmp); ll sum=0; for(int i=1;i<=m*k;i++) { sum+=a[i].x; vis[a[i].id]=1; //printf("a.id=%d\n",a[i]) } printf("%I64d\n",sum); int s=0;; for(int i=1;i<=n;i++) { s=s+vis[i]; // printf("%d %d\n",i,s); if(s==m&&k!=1) { k--; s=0; printf("%d ",i); } } printf("\n"); return 0; }
B. Yet Another Array Partitioning Task ——cf