【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个括号为左括号,后 ...
随机推荐
- CView::OnPreparePrinting
http://technet.microsoft.com/zh-cn/subscriptions/a59dff1e(v=vs.71).aspx CView::OnPreparePrinting Cal ...
- 微信支付v2开发(5) 订单查询
本文介绍微信支付中订单查询功能的实现. 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一.订单查询 因为某一方 ...
- JS学习笔记 - 面向对象 - 原型
<script> var arr1 = new Array(12, 55, 34, 78, 676); var arr2 = new Array(12, 33, 1) Array.prot ...
- 为SSO 5.5恢复忘记的administrator@vsphere.local密码
转自:http://blog.itpub.net/27042095/viewspace-1179938/ 1. cd \Program Files\VMware\Infrastructure\VMwa ...
- Android 最火的高速开发框架AndroidAnnotations使用具体解释
Android 最火的高速开发框架androidannotations配置具体解释文章中有eclipse配置步骤.Android 最火高速开发框架AndroidAnnotations简介文章中的简介. ...
- Linux平台Makefile文件的编写基础篇
目的: 基本掌握了 make 的用法,能在Linux系统上编程. 环境: Linux系统,或者有一台Linux服务器,通过终端连接.一句话:有Linux编译环境. 准备: ...
- Cmake 实现debug和release lib依赖项处理
一.说明 最近用cmake开发东西,编译vs时候,发现debug和release版本的lib库的依赖项问题,故此小结一下.若有不对之处,还请看官多多指教. 使用的工程有自己编写的工程,也有借用第三方库 ...
- hdu 5375 Gray code dp
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; cons ...
- 【转载】zookeeper数据模型
[转载请注明作者和原文链接, 如有谬误, 欢迎在评论中指正. ] ZooKeeper的数据结构, 与普通的文件系统极为类似. 见下图: 图片引用自developerworks 图中的每个节点称为一个 ...
- linux 安装完mysql 密码重置
If you have forgot the MySQL root password, can’t remember or want to break in….. you can reset them ...