Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 贪心+二分
来源:互联网 发布:linux下python mmap 编辑:程序博客网 时间:2024/06/06 16:34
Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:
"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."
More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:
- m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
- Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.
Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.
5 2
4
8 1
5
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:
- At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
- At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
- At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
- At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
- At the beginning of the third day two grains are brought. The barn becomes full again.
- At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
- At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
- At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.
So the answer is 4, because by the end of the fourth day the barn becomes empty.
Source
Codeforces Round #404 (Div. 2)
My Solution
题意:初始是n,每次放入m,然后拿走i, n' = max(n, n' + m),问i为多少的时候剩余的数为0.
贪心+二分
首先如果 m >= n 则ans = n;
否则贪心,前m天,必定是拿完之后就重新填满的,
这个时候剩余 n -= m;
然后对这个新的n进行二分,找到最大的cnt,使得 (cnt + 1)*cnt / 2 <= n;
然后如果最后得到的cnt,有 (cnt + 1)*cnt / 2 < n; 则还可以再拿一次,最后只有部分人可以拿到1个,cnt ++
最后答案就是 ans = m + cnt了
复杂度 O(nlogn)
#include <iostream>#include <cstdio>using namespace std;typedef long long LL;const int MAXN = 1e6 + 8;const LL INF = 2e9;int main(){ #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); int T = 4; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); LL n, m, ans = 0, cnt = 0; cin >> n >> m; if(m >= n){ cout << n << endl; return 0; } ans = m; n -= m; LL l = 0, r = INF, mid; while(l + 1 < r){ mid = (l + r) / 2; if((1ll + mid)*mid / 2 <= n){ cnt = max(cnt, mid); l = mid; } else r = mid; } if((1ll + cnt)*cnt / 2 < n) cnt++; //cout << cnt << endl; ans += cnt; cout << ans << endl; #ifdef LOCAL cout << endl; } #endif // LOCAL return 0;}
------from ProLights
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 贪心+二分
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale
- codeforces round#404(div.2) C. Anton and Fairy Tale
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale
- Codeforces #404 (Div. 2) C. Anton and Fairy Tale (二分
- Codeforces Round #404 (Div. 2) -- C. Anton and Fairy Tale(二分)
- Codeforces Round #404(Div. 2)C. Anton and Fairy Tale【二分】
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale(二分)
- C. Anton and Fairy Tale-Codeforces Round #404 (Div. 2)-二分或者规律
- Codeforces Round #404 C. Anton and Fairy Tale
- CodeForces #404.Div.2 C. Anton and Fairy Tale
- codeforces 785C. Anton and Fairy Tale(二分)
- Codeforces 785C Anton and Fairy Tale 找规律 二分
- CodeForces 785C Anton and Fairy Tale【二分答案+思维】
- codeforces 785 C. Anton and Fairy Tale (数学 二分搜索)
- Codeforces 785C Anton and Fairy Tale (规律+二分查找)
- 【codeforces 785C】Anton and Fairy Tale
- Codeforces 785C-Anton and Fairy Tale
- android studio 百度地图打包后key验证出错问题解决
- 在片头就开始播放 《双月之城》的片尾曲 memory 效果特别震撼!!!!!
- 在activity中获取Edittext焦点创建popupwindow的坑
- ViewDragHelper实现QQ5.0侧滑并处理与ViewPager的滑动冲突
- git stash简单介绍
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 贪心+二分
- 使用py2exe打包发布
- maven添加jar到项目中
- Web SQL Database的异步机制
- 垃圾收集器与内存分配策略
- POJ2001呵呵不好
- 覆盖索引
- 51nod 1478 括号序列的最长合法子段(栈-括号匹配寻找最长合法子串长度及其个数)
- 转载:移动前端开发之viewport的深入理解