题目链接:HDU6154
题目大意:给你n个面积,输出围成每个面积需要的最少的边数,每次只能选择格点的一条边或者对角线。
找规律题目。
规律大概这样,如果还不是很清楚,建议手推一下。不懂也可以在下面评论,欢迎一起探讨。
AC代码:
/*
2017年8月20日10:27:52
HDU6154
WA:很可惜,比赛的时候MAXN开小了,没过,结束之后开了1E5就过了
AC
*/
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int maxn=1e5;
map<double,int> mp;
void init(){
mp[0.5]=3;
//mp[1.0]=4;
for(int i=4;i<maxn;i=i+2){
double b=i/2;
int l=floor(b/(2*1.0));//梯形下底
int h=ceil(b/(2*1.0));//梯形上底
double s1=l*h*2.0; //求偶数边的面积
mp[s1]=i;
double s2=s1+(h+h-1)*1.0/2;//求对应奇数边增加的梯形面积或者三角形面积
mp[s2]=i+1;
// printf("%d:%lf %lf\n",i,s1,s2);如果不是很懂可以把这样去掉观察一下输出
}
}
int main(){
init();
int t;
scanf("%d",&t);
while(t--){
int s;
scanf("%d",&s);
if(s<1) printf("0\n");
else{
map<double,int>::iterator it;
it=mp.lower_bound(s);
printf("%d\n",(it->second));
}
}
return 0;
}