码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 985 D - Sand Fortress

时间:2018-05-23 00:07:28      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:can   color   head   两种   技术分享   #define   second   long   scanf   

D - Sand Fortress

思路:

二分

有以下两种构造,

技术分享图片

分别二分取个最小。

代码:

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0) 
#define LL long long 
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int> 
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head
ULL n, h;
bool check(ULL t) {
    ULL res;
    if(t >= h) res = (1+t)*t/2 + t*(t-1)/2 - h*(h-1)/2;
    else res = (1+t)*t/2;
    if(res >= n) return true;
    else return false;
} 
bool Check(ULL t) {
    ULL res;
    if(t >= h) res = (1+t)*t - h*(h-1)/2;
    else res = (1+t)*t/2;
    if(res >= n) return true;
    else return false;
}
int main() {
    scanf("%llu%llu", &n, &h);
    ULL l = 0, r = 2e9, m = (l+r) >> 1;
    while(l < r) {
        if(check(m)) r = m;
        else l = m+1;
        m = (l+r) >> 1;
    }
    ULL ans;
    if(m >= h) ans = 2*m - h;
    else ans = m;
    l = 0, r = 2e9, m = (l+r) >> 1;
    while(l < r) {
        if(Check(m)) r = m;
        else l = m+1;
        m = (l+r) >> 1;
    }
    if(m >= h) ans = min(ans, 2*m - h + 1);
    else ans = min(ans, m);
    printf("%llu\n", ans); 
    return 0;
}

 

Codeforces 985 D - Sand Fortress

标签:can   color   head   两种   技术分享   #define   second   long   scanf   

原文地址:https://www.cnblogs.com/widsom/p/9074529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!