zoukankan      html  css  js  c++  java
  • codeforces 859B

     Lazy Security Guard

     Your security guard friend recently got a new job at a new security company. The company requires him to patrol an area of the city encompassing exactly N city blocks, but they let him choose which blocks. That is, your friend must walk the perimeter of a region whose area is exactly N blocks. Your friend is quite lazy and would like your help to find the shortest possible route that meets the requirements. The city is laid out in a square grid pattern, and is large enough that for the sake of the problem it can be considered infinite.

    Input
    Input will consist of a single integer N (1 ≤ N ≤ 106), the number of city blocks that must be enclosed by the route.


    Output
    Print the minimum perimeter that can be achieved.


    Example
    Input
    4
    Output
    8
    Input
    11
    Output
    14
    Input
    22
    Output
    20

    Note
    Here are some possible shapes for the examples:




    题意:给出正方形的个数,拼到一起能够拼出最少的边;
    思路:找规律,打表。
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<math.h>
    using namespace std;
    long long a[1100000];
    int main()
    {
        int n;
        a[0]=0;
        a[1]=4;
        a[2]=6;
        long long t=8;
        long long q=2;
        int i=3;
        while(1)
        {
            for(int j=0; j<q; j++)
            {
                a[i++]=t;
            }
            if(i>1000010)
                break;
            t+=2;
            for(int j=0; j<q; j++)
            {
                a[i++]=t;
            }
            q++;
            t+=2;
            if(i>1000010)
                break;
        }
    //    for(int i=1;i<=25;i++)
    //        cout<<a[i]<<endl;
        while(~scanf("%d",&n))
        {
    
            cout<<a[n]<<endl;
        }
    }
    










  • 相关阅读:
    分分钟制作微信朋友圈页面
    js模板引擎原理,附自己写的简洁模板引擎
    基于H5 pushState实现无跳转页面刷新
    随手学和记——PHP快速上手基础
    ES5 特性概览
    JavaScript错误和异常
    JavaScript闭包探究
    FastDFS总结
    C++11笔记
    leveldb源码笔记
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053326.html
Copyright © 2011-2022 走看看