codeforces #346 div 2 D. Bicycle Race (思维,计算几何,公式)
题目链接 题意:给出n+1个点,每次由i点到i+1点,每段线段之间保证不同向或者反向,第一个点和最后一个点保证重合。路径围城的封闭图形中间都是水,问有多少个危险点,使得如果在这个点忘记转弯就会掉进水里。
思路:搞了半天没搞出来qaq
From the track description follows that Maria moves the way that the water always located to the right from her, so she could fall into the water only while turning left. To check if the turn is to the left, let's give every Maria's moves directions a number: moving to the north — 0, moving to the west — 1, to the south — 2 and to the east — 3. Then the turn is to the left if and only if the number of direction after performing a turn _dir_ is equal to the number before performing a turn _oldDir_ plus one modulo 4 .This solution has complexity O(n).
/* ***********************************************
Author :111qqz
Created Time :2016年03月31日 星期四 01时51分56秒
File Name :code/cf/#346/D.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1005;
7int n;
8int dir[N];
1struct node
2{
3 int x,y;
4 int getdir( node b)
5 {
6 if (x==b.x)
7 {
8 if (y>b.y) return 0;
9 else return 2;
10 }
11 else
12 {
13 if (x>b.x) return 1;
14 else return 3;
15 }
16 }
17}p[N];
1int main()
2{
3 #ifndef ONLINE_JUDGE
4 freopen("code/in.txt","r",stdin);
5 #endif
6 cin>>n;
7 ms(dir,-1);
8 for ( int i = 1 ; i <= n+1 ; i++) cin>>p[i].x>>p[i].y;
9 for ( int i = 1 ; i <= n ; i++)
10 {
11 dir[i] = p[i].getdir(p[i+1]);
12 }
1 int ans = 0 ;
2 for ( int i = 1 ; i <= n-1 ; i++)
3 {
4 if (dir[i]==(dir[i+1]+1)%4) ans++;
5 }
6 cout<<ans<<endl;
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}
以及还有一个O(1)的做法。。太神啦。。。
One can solve this problem in alternative way. Let the answer be equal to _x_ (that means that the number of inner corners of 270 degrees equals _x_, but the number of inner corners of 90 degrees to _n_ - _x_). As soon as the sum of the inner corners' values of polygon of _n_ vertices is equal to 180 × (_n_ - 2), then_x_ × 270 + (_n_ - _x_) × 90 equals to 180 × (_n_ - 2). This leads us to  , being the answer for the problem calculated in _O_(1).