首页 > 其他 > 详细

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff 思维

时间:2017-08-19 22:11:25      阅读:326      评论:0      收藏:0      [点我收藏+]

题目链接: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 思维

原文:http://www.cnblogs.com/spfa/p/7397855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!