CodeForces - 748E Santa Claus and Tangerines(枚举)
2017-03-04 19:44
417 查看
题目地址:http://codeforces.com/problemset/problem/748/E
思路:求使得分得的最小值最大。由于最大值为1e7,所以可以直接从大到小枚举最小数i。当分得的个数总和大于人数时,代表此即为最小值(从大到小枚举,也即为最小值最大)。则a[i/2]=a[i/2]+a[i];a[(i+1)/2]=a[(i+1)/2]+a[i](a[i]代表可以由i分得的数的个数,初值为1)。tot记录当前值i时已分得的不小于i的数的个数,每次判断前需要减去i上一状态的个数(因为已将其分为两个数),则tot不小于人数时退出。注意i==1时i*2-1的判断。
思路:求使得分得的最小值最大。由于最大值为1e7,所以可以直接从大到小枚举最小数i。当分得的个数总和大于人数时,代表此即为最小值(从大到小枚举,也即为最小值最大)。则a[i/2]=a[i/2]+a[i];a[(i+1)/2]=a[(i+1)/2]+a[i](a[i]代表可以由i分得的数的个数,初值为1)。tot记录当前值i时已分得的不小于i的数的个数,每次判断前需要减去i上一状态的个数(因为已将其分为两个数),则tot不小于人数时退出。注意i==1时i*2-1的判断。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long LL; const int maxn=1e7+50; const int INF=0x3f3f3f3f; int n,num; LL a[maxn]; int main() { LL tot=0; int maxx=-INF; memset(a,0,sizeof(a)); scanf("%d%d",&n,&num); for(int i=1; i<=n; i++) { int x; scanf("%d",&x); a[x]++,tot+=x; maxx=max(maxx,x); } if(tot<num) printf("-1\n"); else { tot=0; for(int i=maxx; i>0; i--) { tot+=a[i]; if(i*2<=maxx) tot-=a[i*2]; if(i*2-1<=maxx&&(i!=1)) tot-=a[i*2-1]; if(tot>=num) { printf("%d\n",i); break; } a[i/2]+=a[i]; a[(i+1)/2]+=a[i]; } } return 0; }
相关文章推荐
- CodeForces 748E. Santa Claus and Tangerines 题解(递推or二分)
- codeforces 748E Santa Claus and Tangerines
- CodeForces 748E. Santa Claus and Tangerines(二分,递推)
- Codeforces 748E - Santa Claus and Tangerines
- Codeforces 748 E Santa Claus and Tangerines
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法二】
- 【codeforces 752E】Santa Claus and Tangerines
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法一】
- Codeforces 752E Santa Claus and Tangerines 二分+记忆化
- 【codeforces 752C】Santa Claus and Robot
- Codeforces 752C Santa Claus and Robot 思维
- 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
- E. Santa Claus and Tangerines
- 【codeforces 752C】Santa Claus and Robot
- Santa Claus and Tangerines
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines
- 【codeforces 752C】Santa Claus and Robot