[ICPC2016上海D] Ice Cream Tower - 二分
作者:互联网
[ICPC2016上海D] Ice Cream Tower - 二分
Description
给你n个冰淇淋球,告诉你每个冰淇淋球的体积,你可以将若干个冰淇淋球堆成一个塔,需要满足的条件是当前冰淇淋球的体积至少是在它上面的那个球的体积的两倍,一个塔至少有k层,即一个塔至少由k个冰淇淋组成,问最多可以堆成多少个塔。
Solution
二分最终能堆成多少个塔,然后贪心检验即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
int caseid = 0;
bool check(int mid, int k, const vector<int> &a)
{
if (mid <= 0)
return true;
int cnt = 0;
int n = a.size();
vector<int> b(mid);
int p = 0;
for (int i = 0; i < n; i++)
{
int x = a[i];
if (x >= 2 * b[p])
{
b[p] = x;
p = (p + 1) % mid;
if (p == 0)
++cnt;
}
}
return cnt >= k;
}
signed main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
++caseid;
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a.begin(), a.end());
int l = 0, r = n / k + 1;
while (l < r)
{
int mid = (l + r) / 2;
if (check(mid, k, a))
l = mid + 1;
else
r = mid;
}
l++;
for (int i = 0; i < 3; i++)
if (check(l, k, a) == 0)
--l;
cout << "Case #" << caseid << ": ";
cout << l << endl;
}
}
标签:ICPC2016,cin,int,mid,Ice,冰淇淋,++,Tower,check 来源: https://www.cnblogs.com/mollnn/p/14667807.html