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;
}