【codeforces 785C】Anton and Fairy Tale
【题目链接】:http://codeforces.com/contest/785/problem/C
【题意】
容量为n的谷仓,每一天都会有m个谷子入仓(满了就视为m);第i天
会有i只鸟叼走i个谷子;
问哪一天谷仓最早变成空的了;
【题解】
当n<=m的时候,答案就为n;
当n>m的时候;
从第m+1天起谷仓的入库量比不上鸟的食量了;
会开始减少;
其中第m+1天减少m+1;
此后第m+2…依次减少2,3,4…
即首项为2公差为1的等差数列;
先让n减去m+1
然后让这个数列的和大于等于剩下的n时计算出项数x
则m+1+x就是答案了;
这里我在写二分的时候,上界弄小了,导致最后对于n=1e18,m=1的情况无法通过。
主要在于没注意到那个除2
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;
LL n, m, ans;
void input_data()
{
cin >> n >> m;
}
void get_ans()
{
if (n <= m)
{
ans = n;
return;
}
n -= (m + 1);
if (n <= 0)
{
ans = m + 1;
return;
}
LL l = 0, r = 2e9 + 2, t = 0;//hack点,r=1e18会爆LL
ans = m;
//首项是2
//2,3,4..x
//
while (l <= r)
{
LL mid = (l + r) >> 1;
LL temp = (mid + 2)*(mid - 1) / 2;
if (temp >= n)
{
t = mid;
r = mid - 1;
}
else
l = mid + 1;
}
ans += t;
}
void o()
{
cout << ans << endl;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_data();
get_ans();
o();
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 785C】Anton and Fairy Tale的更多相关文章
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【81.37%】【codeforces 734B】Anton and Digits
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 508B】Anton and currency you all know
[题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
随机推荐
- 【hihocoder 1562】⼩Hi的钟表
[链接]点击打开链接 [题意] 在这里写题意 [题解] 时针每过1分钟转0.5°. (360/(12*60)) 分钟每过1分钟转6° (360/60); 根据这个就能算出时针和分针的角度之差了. [错 ...
- OC中对于属性的总结(@property)
在没有属性之前: 对成员变量进行改动都要用到设置器:setter来改动 Person *per =[[Person alloc] init]; 对象通过设置器对成员变量内容进行修该 [per setN ...
- Dcloud课程3 什么是HBuilder和MUI
Dcloud课程3 什么是HBuilder和MUI 一.总结 一句话总结:DCloud(数字天堂)推出一款支持HTML5的Web开发IDE.最大的特点是快.MUI是高性能App的框架,也是目前最接近 ...
- 常用的Windows命令
常用的Windows命令 explorer-------打开资源管理器 logoff---------注销命令 shutdown-------关机命令 lusrmgr.msc----本机用户和组 se ...
- CNTK 搞深度学习-1
CNTK 搞深度学习 Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包.本文介绍CNTK的基本内容,如何写CNTK的网络定义语言,以及跑通一个简 ...
- (转)yum & wget代理设置
转自 http://www.cnblogs.com/windows/archive/2012/12/14/2817533.html yum 配置代理服务器访问要设置所有 yum 操作都使用代理服 ...
- linux的几个内核镜像格式Image 和 u-boot启动内核和文件系统时的一些环境变量的设置
关于编译powerpc linux的几个Image参考原文 http://blog.sina.com.cn/s/blog_86a30b0c0100wfzt.html 转载▼ PowerPC架构 L ...
- C#判断操作系统类型
操作系统 PlatformID 主版本号 副版本号 Windows95 1 4 0 Windows98 1 4 10 WindowsMe 1 4 90 WindowsN ...
- Android入门——Bitmap和BitmapFactory
我们都知道一个App的成败,首先取决于是否具有优秀的UI,而除了交互功能之外还需要丰富的图片背景和动画去支撑.在开发中我们应用到的图片不仅仅包括.png..gif..9.png..jpg和各种Draw ...
- C语言之基本算法11—牛顿迭代法求平方根
//迭代法 /* ================================================================== 题目:牛顿迭代法求a的平方根!迭代公式:Xn+1 ...