CF思维联系– CodeForces - 991C Candies(二分)
After passing a test, Vasya got himself a box of n candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.
This means the process of eating candies is the following: in the beginning Vasya chooses a single integer k, same for all days. After that, in the morning he eats k candies from the box (if there are less than k candies in the box, he eats them all), then in the evening Petya eats 10% of the candies remaining in the box. If there are still candies left in the box, the process repeats — next day Vasya eats k candies again, and Petya — 10% of the candies left in a box, and so on.
If the amount of candies in the box is not divisible by 10, Petya rounds the amount he takes from the box down. For example, if there were 97 candies in the box, Petya would eat only 9 of them. In particular, if there are less than 10 candies in a box, Petya won’t eat any at all.
Your task is to find out the minimal amount of k that can be chosen by Vasya so that he would eat at least half of the n candies he initially got. Note that the number k must be integer.
Input The first line contains a single integer n (1≤n≤1018) — the initial amount of candies in the box.
Output Output a single integer — the minimal amount of k that would allow Vasya to eat at least half of candies he got.
Example Input 68 Output 3 Note In the sample, the amount of candies, with k=3, would change in the following way (Vasya eats first):
68→65→59→56→51→48→44→41→37→34→31→28→26→23→21→18→17→14→13→10→9→6→6→3→3→0.
In total, Vasya would eat 39 candies, while Petya — 29. 因为是求最小的,想枚举,或者倍增,但是枚举会超时,倍增需要单调,所以不如直接二分,验证单调性。 之后可以使用二分,不过这个题看完数据就只能用log2n的复杂度过,除了优先队列,map,set,二分没有其他的算法合适,就它了。 然后没读清楚题意,题目说的是不小于,大于等于,在二分的时候没有注意check的等号。
#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
char ch = getchar();
x = 0;
t f = 1;
while (ch < '0' || ch > '9')
f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
x *= f;
}
#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n; ++i)
#define rrep(m, n, i) for (int i = m; i > n; --i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
#define N 10005
#define fil(a, n) rep(0, n, i) read(a[i])
//---------------https://lunatic.blog.csdn.net/-------------------//
bool check(long long k, long long sum)
{
ll a = 0, b = 0;
while (sum)
{
// cout<<a<<" "<<b<<endl;
if (sum >= k)
{
sum -= k;
a += k;
}
else
{
a += sum;
sum = 0;
}
if (sum >= 10)
{
long long tem = sum / 10;
b += tem;
sum -= tem;
}
}
return a >= b; //这没等号会错//
}
int main()
{
long long sum;
read(sum);
ll l = 1, r = sum;
while (l < r)
{
ll mid = (l + r) / 2;
if (check(mid, sum))
r = mid;
else l=mid+1;
}
cout<<l<<endl;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- springBoot快速入门
- springBoot配置文件
- springBoot按条件装配:Condition
- springBoot @Enable*注解的工作原理
- 使用dom4j修改XML格式的字符串
- springBoot @EnableAutoConfiguration深入分析
- SpringBoot事件监听
- SpringBoot Web(SpringMVC)
- SpringBoot使用servletAPI与异常处理
- Redis数据迁移至Codis集群方案
- Oracle分析函数
- springBoot定制内嵌的Tomcat
- SpringBoot JDBC/AOP
- SpringBoot日志
- DockerFile就这么简单