2.0 s
256 MB
standard input
standard output
The Malbork castle is the largest brick castle in the world. It was built in 1274 in honor to Holy Mary, and is located near the river Nogat, in Poland. During World War II it was totally destroyed. In this times it was discovered that the castle was actually built in many phases, and it was surrounded by a fortress and a wall centuries after it had began building.
In those times wall building was a very elaborate task. War tactics showed that it was convenient to build niches in the walls, that is, a recess on them. In these niches a soldier could stand in relative safety, since he could see all the area determined by the line segments in the niche without needing to turn his head (it was considered they field of vision of 180 degrees). The more niches a wall had, the safer the fortress was considered.
Your task in this problem is, given N points, the corners of the fortress, determine how many of them are niches as described above, in which soldiers could stand to protect the castle.
The first line has an integer N, the number of corners of the fortress. Each of the next N lines has two integers Xi and Yi, the coordinates of a fortress corner. The corners are given in clockwise or counterclockwise order. There are no three consecutive colinear points and the wall does not intersect itself.
Limits
- 3 ≤ N ≤ 105
- |X|, |Y| ≤ 1.8·104
Print a single integer, the number of niches in the fortress.
13
0 0
700 100
400 500
1300 600
700 400
1400 200
2200 1200
1500 2000
1500 1700
400 1700
400 2200
0 1900
10 1000
5
3
-32 45
2 19
-100 -3
0
The image shows the first example.
思路:判断多边形是否为逆时针输入(计算多边形面积),找有向面积小于0的连续三角形数目;
否则相反;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<bitset>
#include<set>
#include<map>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; struct Point
{
double x, y ;
} p[N];
int n ;
double Area( Point p0, Point p1, Point p2 )// 求三角形面积公式//叉积
{
double area = ;
area = (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
return area / ;
}
int ans1,ans2;
double xjhz1()
{
double sum_area = ;
for ( int i = ; i < n ; i++ )
{
double area = Area(p[],p[i-],p[i]) ;
sum_area += area ;
}
return sum_area;
}
void gans()
{
for (int i = ; i-<= n ; i++ )
{
double temp = Area(p[i-],p[i-],p[i]) ;
if(temp<0.0)ans1++;
else ans2++;
}
}
int main ()
{
scanf ( "%d", &n );
for(int i=; i<n; i++)
scanf ( "%lf%lf", &p[i].x, &p[i].y ),p[i+n]=p[i];
double sum=xjhz1();
gans();
if(sum>)printf("%d\n",ans1);
else printf("%d\n",ans2);
return ;
}