F. Halum and Candies
Score: 100
CPU: 1s
Memory: 1024MB
Halum has been elected as the captain of his school’s football team. To celebrate this, he is going to throw a party. Halum bought candies of N different flavors for the party. There are ai number of candies of the ith flavor. To satisfy a guest he has to give him/her some positive number of candies of at least K different flavors. Otherwise the guest is not satisfied. Given this information now you have to find the maximum number of guests Halum can satisfy with the available candies.
Input Specification First line of the input contains an integer T, denoting the number of test cases. T cases follow. First line of each case contains two integers N and K. N space separated integers a1, a2… aN follow in the next line. The ith of these integers ai denotes the number of candies of ith flavor.
Output Specification For each case print one line containing “Case X: Y” (without the quotes) where X is the case number starting from 1 and Y is an integer denoting the maximum number of guests who can be satisfied.
Constraints Subtask 1: 30 points
● 1 ≤ T ≤ 100
● 1 ≤ K ≤ N ≤ 20
● 0 ≤ ai ≤ 100
Subtask 2: 70 points
● 1 ≤ T ≤ 100
● 1 ≤ K ≤ N ≤ 1000
● 0 ≤ ai ≤ 10^9
Sample
Input
3
3 3
1 2 3
3 1
1 2 3
3 2
3 2 4
Output
Case 1: 1
Case 2: 6
Case 3: 4
思路:
二分
假设二分到x个人,只要看从1到n min(a[i],x)的和跟x∗k的大小关系即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int ,int > P;
#define INF 0x3f3f3f3f
const int Max=10000+10;
int t,k,n;
ll a[Max];
bool jiance(ll x) {
ll sum=0,tt=x*k;
for(int i=1; i<=n; i++)
sum+=min(a[i],x);
if(sum>=tt)
return 1;
return 0;
}
int main() {
scanf("%d",&t);
int num=0;
while(t--) {
scanf("%d%d",&n,&k);
for(int i=1; i<=n; i++)
scanf("%I64d",&a[i]);
ll ans,mid,l=0,r=ll(1e12);
while(l<=r) {
mid=(l+r)>>1;
if(jiance(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("Case %d: %I64d\n",++num,ans);
}
return 0;
}