题目
题解思路
肯定是每次拿最大的K个数,优先队列T了。
我们枚举二分能操作的次数为P
具体证明看下面题解
参考题解
参考题解
AC代码
#include <bits/stdc++.h>
//#include <unordered_map>
//priority_queue
#define PII pair<int,int>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 200010 ;
long long a[N] ;
long long n , k ,ans = 0 ;
bool che(long long p)
{
long long sum = 0 ;
for (int i = 1 ; i <= n ; i++ )
{
sum += min(a[i],p);
}
return sum >= p*k ;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> k ;
for (int i = 1 ; i <= n ; i++ )
cin >> a[i] ;
long long t1 = 0 , t2 = 1e18/k ;
while ( t1 < t2 )
{
long long mid = t2 + t1 + 1 >> 1 ;
if (che(mid))
{
t1 = mid ;
}else
t2 = mid - 1 ;
}
cout << t1 << "\n" ;
return 0 ;
}