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个人的时候,当前的瓣数就是最少的人的瓣数。
题意:告诉你有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; }
相关文章推荐
- codeforces 748E Santa Claus and Tangerines
- 【codeforces 752E】Santa Claus and Tangerines
- 【Codeforces 748 C Santa Claus and Robot】+ 思维
- Codeforces 752E Santa Claus and Tangerines 二分+记忆化
- CodeForces 748E. Santa Claus and Tangerines 题解(递推or二分)
- 【Codeforces 748 B Santa Claus and Keyboard Check】
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法一】
- CodeForces 748E. Santa Claus and Tangerines(二分,递推)
- Codeforces 748E - Santa Claus and Tangerines
- CodeForces - 748E Santa Claus and Tangerines(枚举)
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法二】
- 【codeforces 752C】Santa Claus and Robot
- Santa Claus and Tangerines
- 748 C. Santa Claus and Robot codeforces
- Codeforces Round #389 Technocup 2017 E. Santa Claus and Tangerines(二分+DP)
- Codeforces Round #389 Div.2 E. Santa Claus and Tangerines
- Codeforces Round #389 (Div. 2):Santa Claus and Tangerines(二分+DP)
- Codeforces 748B-Santa Claus and Keyboard Check
- CodeForces - 748C Santa Claus and Robot(思维)
- Codeforces 752C Santa Claus and Robot 思维