HDU 6154 CaoHaha'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 <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator> #include <cmath> #include <algorithm> #include <stack> #include <deque> #include <map> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) #define fi(n) for(i=0;i<n;i++) #define fj(m) for(j=0;j<m;j++) #define sca(x) scanf("%d",&x) #define scalld(x) scanf("%I64d",&x) #define print(x) printf("%d\n",x) #define printlld(x) printf("%I64d\n",x) #define d printf("=======\n") using namespace std; const int maxn = 1e5; const int INF = 0x3fffffff; int main() { double n; double s; int t; cin >> t; while( t-- ) { cin >> s; if( s == 1 || s == 2 ) { cout << "4" << endl; continue; } if( s == 3 || s == 4 ) { cout << "6" << endl; continue; } if( s == 5 ) { cout << "7" << endl; continue;} if( s == 6 || s == 7 || s == 8 ) { cout << "8" << endl; continue; } for( n = 1; n <= maxn; n++ ) { if( 2 * n * n <= s && s <= 2 * (n+1) * (n+1) ) break; } // cout << n << " " << n+1 << endl; n++; double area = 2*(n)*(n); double d1 = n+0.5; double d2 = 2*n; double d3 = 3*n+0.5; // cout << area << endl; // cout << area - d3 << endl; // cout << area - d2 << endl; // cout << area - d1 << endl; if( s <= area - d3 ) { cout << 4 * n - 3 << endl; continue; } if( s <= area - d2 ) { cout << 4 * n - 2 << endl; continue; } if( s <= area - d1 ) { cout << 4 * n - 1 << endl; continue; } cout << 4 * n << endl; } return 0; }
思考: 自己花了4个多点儿才想出来的这道题, 是真的菜, 一开始自己想歪了, 但是就算想歪了代码也应该写出来的啊, 可是自己并没有写出来, 自己是真的菜, 以后要继续加油, 可以打ACM的时间不多了
posted on 2017-08-19 23:23 FriskyPuppy 阅读(244) 评论(0) 编辑 收藏 举报