1673: 集训队组队计划
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 16 Solved: 9
[ Submit][ Status][ Web Board]
Description
经过1个月的训练,集训工作组决定让大家组队参加组队赛。为了让每一队内部水平尽量平均,集训工作组希望能令每个队能力最强的队员和能力最弱的队员的能力值差值的最大值尽量小。已知每个队由三个人组成,一个人不能加入多个队伍,现在有N个队员,能力分别为a1,a2...an,要从中选出3M个队员组成M支队。请问这M支队伍中,能力差值的最大值最小为多少?
Input
多组数据,第一行有一个整数T,表示有T组数据。(T<=10)
以下每组数据第一行有两个整数N,M。(3<=N<=100000,0<M*3<=N)
第二行N个整数a1,a2......an。(0<=ai<=1e^9)
Output
一个整数,表示最小的最大能力差。
Sample Input
2
6 2
1 3 2 8 7 9
8 2
10 80 90 20 50 60 30 40
Sample Output
2
20
HINT
对样例1:Dmin=max(3-1,9-7);
对样例2:Dmin=max(30-10,60-40)
Source
一个二分贪心的问题
二分一个差值,然后用贪心的思想来扫一遍多少个组合满足差值,然后看能不能满足组数
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int M=1e5+5;
int op[M];
int canu(int n,int m,int cha)
{
int co=0;
for(int i=0;i<n-2;) //目测之前这个写错了
{
if(op[i+2]-op[i]<=cha)
{
co++;
i+=3;
}
else
i++;
}
return co>=m;
}
int bs(int n,int m)
{
int lo=0,hi=1e9+5;
int mid,ans=1e9+5;
while(lo<=hi)
{
mid=lo+(hi-lo)/2;
if(canu(n,m,mid))
{
ans=min(ans,mid);
hi=mid-1;
}
else
lo=mid+1;
}
return ans;
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&op[i]);
sort(op,op+n);
printf("%d\n",bs(n,m));
}
return 0;
}