Codeforces Round #404 (Div. 2) -- C. Anton and Fairy Tale(二分)
来源:互联网 发布:云数据库语言 编辑:程序博客网 时间:2024/06/01 19:06
题意:
谷仓里最多有n 个谷子, 每天早上都会运来m 个 ,多了的就不要了,第i 天呢会有i 只鸟来吃谷子, 每一个鸟都会吃,都只吃一个,问你哪一天谷仓为空?
思路:
分类讨论了:
当n <= m时候 ans = n
当n > m时候 我们发现 前m 天 一直都是满的 ,不可能为空, 第m+1天早上 满的,晚上 会少m-1个, 第m+2天早上又运来m 个,下午没了m-2个,
那么假设第x 天没了的话,
那么 m +1 - m + m + 2 -m + m +3 - m ....+ m + x >= n
即1 + 2 +3 +。。。 + x + m >= n
即 ((1+x)*x)/2 + m >= n 二分答案即可。
别忘了加上前面的m 天。
#include <bits/stdc++.h>#define mr make_pair#define fi first#define se second#define ps push_back#define Siz(x) (int)x.size()using namespace std;const int inf = 0x3f3f3f3f;const double eps = 1e-10;const double pi = acos(-1.0);typedef long long LL;typedef unsigned long long ULL;const int maxn = 200000 + 10;LL n,m;bool judge(LL x){ return (x*x+x)/2+m >= n;}int main(){ scanf("%lld %lld",&n, &m); if (n <= m) return 0 * printf("%lld\n",n); LL L = 1, R = (LL)2e9+1; LL ans; while(L <= R){ LL m = L+R>>1LL; if (judge(m)){ ans = m; R = m-1; } else L = m+1; } printf("%lld\n",ans + m); return 0;}
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.
- 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 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【二分】
- C. Anton and Fairy Tale-Codeforces Round #404 (Div. 2)-二分或者规律
- Codeforces Round #404 C. Anton and Fairy Tale
- codeforces 785C. 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 785 C. Anton and Fairy Tale (数学 二分搜索)
- Codeforces 785C Anton and Fairy Tale (规律+二分查找)
- 【codeforces 785C】Anton and Fairy Tale
- Codeforces 785C-Anton and Fairy Tale
- Codeforces Round #404 (Div. 2) -- E. Anton and Permutation(分块xjb 搞)
- Codeforces Round #404 (Div. 2) 题解
- Java核心技术:卷一——前言
- windows 下编译ffmpeg
- string库之模拟实现strchr、strrchr与memchr
- Codeforces Round #404 (Div. 2) -- C. Anton and Fairy Tale(二分)
- tar解压失败:gzip: stdin: not in gzip format
- 【codeforces 785A】Anton and Polyhedrons
- 【codeforces 785B】Anton and Classes
- 【codeforces 785C】Anton and Fairy Tale
- Coursera ML笔记 -----week3 Logistic Regression
- O(n^1/2) isPrime
- UC_COS移植到STM32
- 算法导论-归并排序