题面:
Yet Another Array Partitioning Task
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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, ……, arar for some l,rl,r.
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 xx is 5+4+3=125+4+3=12.
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.
Input
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).
Output
In 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.
input
9 2 3
5 2 5 2 4 1 1 3 2
output
21
3 5
input
6 1 4
4 1 3 2 2 3
output
12
1 3 5
input
2 1 2
-1000000000 1000000000
output
0
1
题面描述:
给出n个数,分成k组,求每组中m个最大的数全部相加起来的值最大是多少,第一行输出这个值,第二行输出分组的情况,例如第1个到第p1个是第一组,第p1+1到p2是第二组…一共k-1个数。
题目分析:
因为要分成k组并且每组中m个最大相加的和,所以要找出k×m个最大的数相加就是所求最大值,如果直接找复杂度是O(km)会爆,所以后面考虑用sort排序,将无顺序的数字用pair数组存值和下标,找出前k×m个最大在在这k×m个数中从小到大找下标,每m个输出一次。
代码:
#include<algorithm>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
int n,m,k,i,j;
long long num,sum=0;
pair<int,int> a[200005];
bool cmp1(pair<int,int> a,pair<int,int> b){
return a.first>b.first;
}
bool cmp2(pair<int,int> a,pair<int,int> b){
return a.second<b.second;
}
int main(){
cin>>n>>m>>k;
num=m*k;
for(i=0;i<n;i++){
cin>>a[i].first;
a[i].second=i;
}
sort(a,a+n,cmp1);//从大到小排first
for(i=0;i<num;i++){
sum+=a[i].first;
// a[i].first=0;
}
cout<<sum<<endl;
sort(a,a+num,cmp2);//从小到大排second
for(i=m-1;i<num-m;i+=m) cout<<a[i].second+1<<' ';
}