现在的位置: 首页 > 综合 > 正文

CF 392A Blocked Points

2014年04月05日 ⁄ 综合 ⁄ 共 1459字 ⁄ 字号 评论关闭
 Blocked Points
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Imagine you have an infinite 2D plane with Cartesian coordinate system. Some of the integral points are blocked, and others are not. Two integral points A and B on
the plane are 4-connected if and only if:

  • the Euclidean distance between A and B is one unit
    and neither A nor B is blocked;
  • or there is some integral point C, such that A is
    4-connected with C, and C is 4-connected with B.

Let's assume that the plane doesn't contain blocked points. Consider all the integral points of the plane whose Euclidean distance from the origin is no more than n,
we'll name these points special. Chubby Yang wants to get the following property: no special point is 4-connected to some non-special point. To get the property she can pick some integral points of the plane and make them blocked. What is the minimum number
of points she needs to pick?

Input

The first line contains an integer n (0 ≤ n ≤ 4·107).

Output

Print a single integer — the minimum number of points that should be blocked.

Sample test(s)
input
1
output
4
input
2
output
8
input
3
output
16
暴力搜索,从第一象限的i=0,j=n开始搜索到i=n,j=0,找出每一个符合条件的点,最后*4即为所求。
PS:队友给力!
#include<stdio.h>
long long n,m,sum;
bool dis(long long x,long long y)
{
    return x*x+y*y<=m;
}
int main()
{
    while(scanf("%lld",&n)!=EOF)
    {
        sum=0;
        m=n*n;
        bool flag;
        int pos=n;
        if(!n){printf("1\n");continue;}
        for(long long i=0;i<n;i++)
        {
            for(long long j=pos,flag=false;!(dis(i+1,j)&&dis(i,j+1))&&j>=0;j--)//j=pos,每次从pos这个点开始往下搜,防止搜索不必要的点。次循环的条件是此点的左面或者上面必须有一个点在圆外
            {
                if((i*i+j*j)<=m)
                {
                    flag=true;
                    sum++;
                    pos=j;
                }
            }
        }
        printf("%d\n",sum*4);
    }
    return 0;
}

抱歉!评论已关闭.