1. 程式人生 > >2017中國大學生程序設計競賽 - 網絡選拔賽 HDU 6154 CaoHaha's staff 思維

2017中國大學生程序設計競賽 - 網絡選拔賽 HDU 6154 CaoHaha's staff 思維

解法 什麽是 ica str can ++ eight || clas

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6154

題意:在笛卡爾坐標系下,畫一個面積至少為 n 的簡單多邊形,每次只能畫一條邊或者一個格子的對角線,問至少要畫幾條。

解法:如果一個斜著的矩形長寬分別是 a,b,那麽它的面積是 2ab。最優解肯定是離 sqrt(n/2)很近的位置。想想 n=5 時答案為什麽是7 然後在那個小範圍內枚舉一下就好了。我給一張做題時畫的圖

技術分享

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        LL n;
        scanf("%lld", &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<=8){
            puts("8");
        }
        else{
            LL m = floor(sqrt(n/2));
            LL tmp = m*4;
            LL x = m*m*2;
            if(n==x){
                printf("%lld\n", tmp);
            }
            else if(n<=x+m-0.5){
                printf("%lld\n", tmp+1);
            }
            else if(n<=x+2*m){
                printf("%lld\n", tmp+2);
            }
            else if(n<=x+3*m+0.5){
                printf("%lld\n", tmp+3);
            }
            else printf("%lld\n", tmp+4);
        }
    }
    return 0;
}

2017中國大學生程序設計競賽 - 網絡選拔賽 HDU 6154 CaoHaha's staff 思維