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.
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
题目大意:求四个面垂直看下去的都看不到的长度总和
这个题太水了,但是逼哥和我都读不懂英语…..然后我就信了逼哥的邪,什么求射线穿插多边形,交点大于四啊什么的,三个小时也写不出来……以后得多看几遍题目,不能轻易相信逼哥
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
struct Node{
int x,y;
}node[1111];
bool cmpx1(Node a, Node b){
return a.x < b.x ||(a.x == b.x && a.y < b.y);
}
bool cmpy(Node a, Node b){
return a.y < b.y ||(a.y == b.y && a.x < b.x);
}
int main(){
int n;
while(~scanf("%d", &n)){
for(int i=1;i<=n; i++){
scanf("%d%d", &node[i].x, &node[i].y);
}
sort(node+1, node+1+n, cmpx1);
int num=0;
int sum=0;
for(int i=1;i<n;i+=2){ //第一遍
sum += node[i+1].y-node[i].y;
}
int num1=node[1].x,num2 = node[n].x;
sort(node+1, node+1+n, cmpy);
for(int i=1;i<n;i+=2){ //第一遍
sum += node[i+1].x-node[i].x;
}
int num3=node[1].y, num4=node[n].y;
num= num2 - num1 + num4 - num3;
sum -= 2*num;
printf("%d\n",sum);
}
return 0;
}