Problem I. Intelligence in Perpendicularia

Problem I. Intelligence in Perpendicularia
Input file: intel.in Time limit: 3 seconds
Output file: intel.out Memory limit: 512 megabytes
There are only two directions in Perpendicularia: vertical and horizontal. Perpendicularia government
are going to build a new secret service facility. They have some proposed facility plans and want to
calculate total secured perimeter for each of them.
The total secured perimeter is calculated as the total length of the facility walls invisible for the
perpendicularly-looking outside observer. The figure below shows one of the proposed plans and
corresponding secured perimeter.
0 1 2 3 4 5 6 x
y
1
2
3
4
Write a program that calculates the total secured perimeter for the given plan of the secret service facility.
Input
The plan of the secret service facility is specified as a polygon.
The first line of the input contains one integer n — the number of vertices of the polygon (4 ≤ n ≤ 1000).
Each of the following n lines contains two integers xi and yi – the coordinates of the i-th vertex
(−106 ≤ xi
, yi ≤ 106
). Vertices are listed in the consecutive order.
All polygon vertices are distinct and none of them lie at the polygon’s edge. All polygon edges are either
vertical (xi = xi+1) or horizontal (yi = yi+1) and none of them intersect each other.
Output
Output a single integer — the total secured perimeter of the secret service facility.
Example
intel.in intel.out
10
1 1
6 1
6 4
3 4
3 3
5 3
5 2
2 2
2 3
1 3
6

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=1000001;
struct node
{
    LL x;
    LL y;
};
struct node q[N];
LL a,b,c,d,e,f,g,h;
int main()
{
    freopen("intel.in","r",stdin);
    freopen("intel.out","w",stdout);
    scanf("%lld",&a);
    LL num=0;
    for(b=0; b<a; b++)
    {
        scanf("%lld%lld",&q[b].x,&q[b].y);
        if(b!=0)
        {
            c=q[b-1].x;
            d=q[b-1].y;
            e=q[b].x;
            f=q[b].y;
            if(c!=e)
                num=num+abs(c-e);
            else
                num=num+abs(d-f);
        }
    }
    b--;
    if(q[b].x!=q[0].x)
        num=num+abs(q[b].x-q[0].x);
    else
        num=num+abs(q[b].y-q[0].y);
    c=q[0].x;
    d=q[0].y;
    f=q[0].x;
    g=q[0].y;
    for(e=0; e<a; e++)
    {
        if(q[e].x>c)
            c=q[e].x;
        if(q[e].y>d)
            d=q[e].y;
    }
    for(e=0; e<a; e++)
    {
        if(q[e].x<f)
            f=q[e].x;
        if(q[e].y<g)
            g=q[e].y;
    }
    LL ans;
    ans=2*(c-f)+2*(d-g);
    printf("%lld\n",num-ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值