Codeforces 748E - Santa Claus and Tangerines
2017-11-09 18:07
369 查看
题目大意:
有n个数分给m个人
每个数可以分成两个新的数 i/ 2 、(i+1)/2 分后的数可以继续分,但不允许分1=1+0
最后分给每个人,求分得的数最小的人分得的数最大能多少。
有两个方法。
第一个方法是二分答案,然后从1e7到二分的值按照i的奇偶性把i的值传给i/2或i/2和
i/2+1,然后计算当前二分的值是否能分给m个人。时间复杂度是n*logn*logn,勉强能过.
第二个方法是倒着直接枚举答案,枚举到i时,把当前值传给i/2和(i+1)/2两个儿子,统计
当前值能分给多少人时需要减去父亲的贡献,i的父亲有i*2-1, i*2, i*2+1, 但是为防止重复
删去父亲贡献,每次只需减去i*2-1, i*2这两个父亲(如只有一个7片的橘子,分成3 4,
枚举到4时删除7 8,到3时删除5 6,不用再删除7)。复杂度是n,快了很多。
方法一:
方法二:
有n个数分给m个人
每个数可以分成两个新的数 i/ 2 、(i+1)/2 分后的数可以继续分,但不允许分1=1+0
最后分给每个人,求分得的数最小的人分得的数最大能多少。
有两个方法。
第一个方法是二分答案,然后从1e7到二分的值按照i的奇偶性把i的值传给i/2或i/2和
i/2+1,然后计算当前二分的值是否能分给m个人。时间复杂度是n*logn*logn,勉强能过.
第二个方法是倒着直接枚举答案,枚举到i时,把当前值传给i/2和(i+1)/2两个儿子,统计
当前值能分给多少人时需要减去父亲的贡献,i的父亲有i*2-1, i*2, i*2+1, 但是为防止重复
删去父亲贡献,每次只需减去i*2-1, i*2这两个父亲(如只有一个7片的橘子,分成3 4,
枚举到4时删除7 8,到3时删除5 6,不用再删除7)。复杂度是n,快了很多。
方法一:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e6+5; const int maxm = 1e7+5; ll a[maxn], num[maxm], n, k; bool judge(ll x) { ll cnt = 0; memset(num, 0, sizeof(num)); for(int i = 1; i <= n; i++) num[a[i]]++; for(int i = maxm-1; i >= x; i--) { if(i/2 >= x) { if(i&1) { num[i/2] += num[i]; num[i/2+1] += num[i]; } else num[i/2] += num[i]*2; } else cnt += num[i]; } return cnt >= k; } int main(void) { while(cin >> n >> k) { ll l = 0, r = 1e7; for(int i = 1; i <= n; i++) scanf("%I64d", &a[i]); int ans = -1; while(l <= r) { ll mid = (l+r)/2; if(judge(mid)) l = mid+1, ans = mid; else r = mid-1; } printf("%d\n", ans); } return 0; }
方法二:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <set> #include <map> #include <stack> #include <vector> #include <queue> #define ri(n) scanf("%d",&n) #define oi(n) printf("%d\n",n) #define rl(n) scanf("%lld",&n) #define ol(n) printf("%lld\n",n) #define rep(i,l,r) for(i=l;i<=r;i++) #define rep1(i,l,r) for(i=l;i<r;i++) using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int epg=10-8; ll a[1000000+10]; ll sum[10000000+10]; int main() { int n,m; while(scanf("%d%d",&n,&m)==2) { ll mx=0; ll cnt=0; memset(sum,0,sizeof(sum)); for(int i=1; i<=n; i++) { scanf("%lld",&a[i]); sum[a[i]]++; cnt+=a[i]; mx=max(mx,a[i]); } if(cnt<m) printf("-1\n"); else { cnt=0; for(int i=mx; i; i--) { cnt+=sum[i]; if(i*2<=mx) cnt-=sum[i*2]; if(i*2-1<=mx&&i!=1) cnt-=sum[i*2-1]; if(cnt>=m) { printf("%d\n",i); break; } sum[i/2]+=sum[i]; sum[(i+1)/2]+=sum[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 752E】Santa Claus and Tangerines
- Codeforces 752E Santa Claus and Tangerines 二分+记忆化
- Codeforces 748 E Santa Claus and Tangerines
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法一】
- 【Codeforces Round #389】Codeforces 752E Santa Claus and Tangerines【解法二】
- CodeForces 748C Santa Claus and Robot (思维)
- 【codeforces 752C】Santa Claus and Robot
- Codeforces Round #389 Technocup 2017 E. Santa Claus and Tangerines(二分+DP)
- Codeforces Round #389 Div.2 E. Santa Claus and Tangerines
- 【codeforces 752C】Santa Claus and Robot
- Codeforces Round #389 (Div. 2):Santa Claus and Tangerines(二分+DP)
- 【codeforces 752C】Santa Claus and Robot
- 模拟· CodeForces - 748B · Santa Claus and Keyboard Check
- 【codeforces 752C】Santa Claus and Robot
- 【codeforces 752B】Santa Claus and Keyboard Check
- Codeforces 748B-Santa Claus and Keyboard Check