ARC109B log 二分

传送门

题意:

给你n+1根木头,它们的长度分别是1,2,3......n+1,价格都是1元,现在要求你拿出长度分别为1,2,3......n的n根木头,木头可以切,求最少的钱数

题解:

很显然,最省钱的方法就是先买了最长的那个n+1的木头,然后切出1,2,3....能切多少切多少,最后再把剩下的买了

就是求在满足$sum_{i=1}^{k}i leq n+1$的条件下,最大的k

注意,不要用double,用二分去求

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define LL long long
LL upper_bound2 (LL l,LL r,LL key) {
    LL it;
    LL count, step;
    count = r-l+1;
    while (count>0) {
        step=count/2;
        it=l+step;
        if ( (it*(it+1)/2) <= key) {               
            l=it+1;
            count-=step+1;
        } else count=step;
    }
    return l;
}
int main(){ 
    LL k;
    scanf("%lld",&k);
    LL cutting=upper_bound2(1ll,min(k,2000000007ll)+1,k+1)-1;
    printf("%lld",k+1-cutting);
}
原文地址:https://www.cnblogs.com/isakovsky/p/14163143.html