2017中国大学生程序设计竞赛

题目链接: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
", tmp);
            }
            else if(n<=x+m-0.5){
                printf("%lld
", tmp+1);
            }
            else if(n<=x+2*m){
                printf("%lld
", tmp+2);
            }
            else if(n<=x+3*m+0.5){
                printf("%lld
", tmp+3);
            }
            else printf("%lld
", tmp+4);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/spfa/p/7397855.html