hdu6154 CaoHaha's staff CCPC网赛1005 找规律+构造

来源:互联网 发布:淘宝卖家如何退出村淘 编辑:程序博客网 时间:2024/05/16 07:28

http://acm.split.hdu.edu.cn/showproblem.php?pid=6154

题意:在坐标系中,你可以连长度为sqrt(2)的对角线或者就是单位长度的边,要求最少的边构成面积为n的图形。求这个最小边。

题解:可以发现如果边数是4的倍数,构造菱形是面积最大的。那在这个基础上加一条边,两条边,三条边,四条边呢?

下面四幅图len为边长,另一个数字是增加的面积(画的有些残。。。):




代码:

#include<bits/stdc++.h>#define debug cout<<"aaa"<<endl#define d(a) cout<<a<<endl#define mem(a,b) memset(a,b,sizeof(a))#define LL long long#define lson l,mid,root<<1#define rson mid+1,r,root<<1|1#define MIN_INT (-2147483647-1)#define MAX_INT 2147483647#define MAX_LL 9223372036854775807i64#define MIN_LL (-9223372036854775807i64-1)using namespace std;const int N = 100000 + 5;const int mod = 1000000000 + 7;const double eps = 1e-8;int main(){int t,len,area,n;scanf("%d",&t);while(t--){scanf("%d",&n);if(n==1||n==2){puts("4");}else if(n==3||n==4){puts("6");}else if(n==5){puts("7");}else if(n==6||n==7||n==8){puts("8");}else{//向下取整,len为构成菱形,在菱形的一条边要画的数量 len=floor(sqrt(n/2));area=len*len*2;if(n==area){printf("%d\n",len*4);} else if(n<=area+len-0.5){//加一条边 printf("%d\n",len*4+1); }else if(n<=area+len*2){//加两条边 printf("%d\n",len*4+2);}else if(n<=area+len*3+0.5){//加三条边 printf("%d\n",len*4+3);}else{//加四条边 printf("%d\n",len*4+4);} }}return 0;}


原创粉丝点击