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的时间不多了

HDU 6154 CaoHaha's staff 思维 找规律

时间: 2024-10-20 17:21:37

HDU 6154 CaoHaha's staff 思维 找规律的相关文章

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

hdu 6154 CaoHaha&#39;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;

hdu 2147 kiki&#39;s game(找规律)

kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)Total Submission(s): 10656    Accepted Submission(s): 6455 Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his

HDU 4349 Xiao Ming&#39;s Hope 找规律

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1723    Accepted Submission(s): 1144 Problem Description Xiao Ming likes coun

HDU 1564 Play a game (博弈&amp;&amp;找规律)

Play a game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1430    Accepted Submission(s): 1168 Problem Description New Year is Coming! ailyanlu is very happy today! and he is playing a chessbo

HDU 4588 Count The Carries(找规律,模拟)

题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <stack> #include <vector> using namespace std; int main() { int

HDU 5351 MZL&#39;s Border(找规律+高精度)

MZL's Border Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 895    Accepted Submission(s): 287 Problem Description As is known to all, MZL is an extraordinarily lovely girl. One day, MZL was p

HDU 1517 A Multiplication Game (博弈&amp;&amp;找规律)

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3691    Accepted Submission(s): 2097 Problem Description Stan and Ollie play the game of multiplication by multiplying an in