hdu 6154 CaoHaha's staff 二分

题意:

一笔可以画一条长为1的边或者一条根号二的对角线

问围成一个面积是x的图形最少需要几条边

思路:

用公式直接二分

 1 #include<bits/stdc++.h>
 2 #define cl(a,b) memset(a,b,sizeof(a))
 3 #define debug(a) cerr<<#a<<"=="<<a<<endl
 4 using namespace std;
 5 typedef long long ll;
 6 typedef pair<int,int> pii;
 7 
 8 const int maxn=1e6+10;
 9 const int mod=1e9+7;
10 
11 ll area(ll x) //计算x条边能围成的最大面积
12 {
13     ll tmp=x/4;
14     if(x%4==0) return 2*tmp*tmp;
15     else if(x%4==1) return 2*tmp*tmp+tmp-1;
16     else if(x%4==2) return 2*tmp*tmp+2*tmp;
17     else return 2*tmp*tmp+3*tmp;
18 }
19 
20 ll Search(ll n) //二分查找
21 {
22     ll l=1,r=1e9;
23     while(l<=r)
24     {
25         ll mid=(l+r)/2;
26         if(area(mid)<=n) l=mid+1;
27         else r=mid-1;
28 //        debug(l);
29     }
30     while(true) //二分写不准专用的尝试性左移
31     {
32         if(area(l-1)>=n) l--;
33         else break;
34 //        debug(l);
35     }
36     return l;
37 }
38 
39 int main()
40 {
41     int T;
42     scanf("%d",&T);
43     while(T--)
44     {
45         ll n;
46         scanf("%lld",&n);
47         ll ans=Search(n);
48 //        debug(ans);
49         printf("%lld
",ans);
50     }
51     return 0;
52 }/*
53 
54 5
55 1
56 2
57 3
58 4
59 5
60 
61 */
原文地址:https://www.cnblogs.com/general10/p/7398825.html