题解:P13759 Basketball
P13759 Basketball 题解
题目链接( https://www.luogu.com.cn/problem/P13759 )
这道题是本蒟蒻在整场考试中唯一会写的题QAQ
题意分析
就是将
1.首先我们需要把这当然也可以逆序的)
2.然后为了确保每个小组的中位数最小,我们尽量使每组都有较大数和较小数,比如说(以题目为例子):
- 1 2 3 4 5 6 7 8 9最优的分法是将:1 2 9作为一组,3 4 8作为一组,5 6 7作为一组
- 这就使得较大的数不会成为某个小组的中位数(若以1 2 3为一组,4 5 6为一组,7 8 9为一组时,8就会成为第三小组的中位数)
有了以上分析,代码的实现就不难了
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,m,k;
cin>>n>>m;
vector<int>a(n+1);
for(int i=1;i<=n;i++)cin>>a[i];
sort(a.begin(),a.end());
long long ans=0;
k=n/m;
int tmp=(k+1)/2,tmp2=(k-1)/2;
for(int i=tmp,j=n;i<=j;i+=tmp,j-=tmp2)
ans+=a[i];
cout<<ans;
}