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\n",ans);
50     }
51     return 0;
52 }/*
53
54 5
55 1
56 2
57 3
58 4
59 5
60
61 */

hdu 6154 CaoHaha's staff 二分

时间: 2024-08-05 23:41:19

hdu 6154 CaoHaha's staff 二分的相关文章

HDU 6154 CaoHaha&#39;s staff 思维 找规律

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6154 题目描述: 围成一个面积不小于S的多边形, 最少需要多少根儿线段, 线段可以为单元格边或者对角线 解题思路: 最大的面积肯定是由根号2为边长的正方形围成了, 那么我们把所有正方形都遍历一遍, 找出S介于N, N+1的那个上界N+1设为max, 因为MAX所围成的多边形面积和MAX-1, MAX-2, MAX-3围成的多边形面积, 找出满足条件的最小的一个即可 代码: #include <io

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff 思维

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为  n 的简单多边形,每次只能画一条边或者一个格子的对角线,问至少要画几条. 解法:如果一个斜着的矩形长宽分别是 a,b,那么它的面积是 2ab.最优解肯定是离 sqrt(n/2)很近的位置.想想 n=5 时答案为什么是7 然后在那个小范围内枚举一下就好了.我给一张做题时画的图 #include <bits/stdc++.h> using namesp

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff(几何找规律)

Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in CaoHaha's hand.As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.But now,hi

[HDOJ6154] CaoHaha&#39;s staff(规律, 打表, 二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 有些题一辈子只会做一次,比如这个题.. 题意:炒鸡难懂,学弟读明白的.懒得描述,反正这题以后不会再做. f(i)表示i个线段能围成的最大面积,画画图就会发现一个规律. 然后查询的时候二分最小的大于等于s的即可. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛

【推导】【找规律】【二分】hdu6154 CaoHaha&#39;s staff

题意:网格图.给你一个格点多边形的面积,问你最少用多少条边(可以是单位线段或单位对角线),围出这么大的图形. 如果我们得到了用n条边围出的图形的最大面积f(n),那么二分一下就是答案. n为偶数时,显然要尽量用斜边去拼矩形,于是f(i)=i*i/4-1 (i mod 4 == 2),f(i)=i*i/4-1(i mod 4 == 0). 当n为奇数时,尽量用i-1情况下的最长边向外扩张一个单位,于是f(i)=f(i-1)+[(i+1)/4]*2-1(i mod 2 == 1),方括号表示下取整.

HDU 3622 Bomb Game(二分+2-SAT)

Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5396    Accepted Submission(s): 1925 Problem Description Robbie is playing an interesting computer game. The game field is an unbounde

hdu 3264 圆的交+二分

Open-air shopping malls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1822    Accepted Submission(s): 651 Problem Description The city of M is a famous shopping city and its open-air shopping

HDU 3081Marriage Match II(二分+并查集+网络流之最大流)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3081 有一段时间没写最大流的题了,这题建图居然想了好长时间...刚开始是按着最终的最大流即是做多轮数去想建图,结果根本没思路,后来想了想,可以用二分答案的思想来找最终答案.然后很明显的并查集,但是并查集学的略渣,居然卡在并查集上了..= =. 但是也不是并查集的事..是我建图的思想太正了,稍微用点逆向思维并查集就可以很好利用了. 建图思路是:建立一个源点与汇点,将女孩与源点相连,男孩与汇点相连,权值

hdu 2899 Strange fuction (二分)

题目链接:http://acm.hdu.edu.cn/showproblem.pihp?pid=2899 题目大意:找出满足F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)的x值.注意精确度的问题. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 double y; 6 7 doub