1. 程式人生 > >HDU 6154 CaoHaha's staff 思維 找規律

HDU 6154 CaoHaha's staff 思維 找規律

ble fin namespace math break while set call closed

  題目鏈接: 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; }
View Code

  思考: 自己花了4個多點兒才想出來的這道題, 是真的菜, 一開始自己想歪了, 但是就算想歪了代碼也應該寫出來的啊, 可是自己並沒有寫出來, 自己是真的菜, 以後要繼續加油, 可以打ACM的時間不多了

HDU 6154 CaoHaha's staff 思維 找規律