您的位置:首页 > 其它

Codeforces 748 E Santa Claus and Tangerines

2017-06-07 10:20 393 查看
题目地址:http://codeforces.com/contest/748/problem/E

题意:告诉你有n个橘子,要分给m个人,然后告诉你n个橘子每个橘子有ai瓣,橘子每次可以对半分,可以把它分到全部都是1为止,求m个人中分的最少的最多是有多少瓣橘子。

思路:看这个翻译应该都会懵逼吧,我最初还以为这题这么简单呀,居然能分到最终都是1瓣,那不就是直接求和就好了,再除以m,然后就光荣的wa了,然后才发现其实每个人都是要整个的,就是说每个人手上不能有几瓣橘子,只能有一整瓣橘子,所以在标记数组里存下有这个瓣数的橘子有多少个。然后从最大的那个橘子开始出发向下遍历标记数组,如果i*2或i*2+1的结果大于mmax(要特判i==1的情况),就说明这个瓣数的橘子可能有部分是之前那个转来的,所以先加上这个瓣数的橘子个数减去之前的橘子个数,当橘子个数总数大于等于m个人的时候,当前的瓣数就是最少的人的瓣数。

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#define N 10000010
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
int cnt
;
int main() {
cin.s
4000
ync_with_stdio(false);
int n, ans;
LL m, sum, mmax;
LL num;
while (cin >> n >> m) {
sum = 0;
mmax = 0;
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; i++) {
cin >> num;
sum += num;
cnt[num]++;
mmax = max(mmax, num);
}
if (sum < m) {
cout << -1 << endl;
}
else {
ans = 0;
int flag=1;
for (int i = mmax; i > 0; i--) {
ans += cnt[i];
if (i * 2 <= mmax) {
ans -= cnt[i << 1];
}
if (i * 2 - 1 <= mmax&&i != 1) {
ans -= cnt[(i << 1) - 1];
}
if (ans >= m) {
cout << i << endl;
flag=0;
break;
}
cnt[i >> 1] += cnt[i];
cnt[(i + 1) >> 1] += cnt[i];
}
if(flag){
cout<<1<<endl;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: