D - Bicycle Race CodeForces 659D

来源:互联网 发布:macbook软件卸载不了 编辑:程序博客网 时间:2024/05/01 23:45
D - Bicycle Race
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 659D

Description

Maria participates in a bicycle race.

The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.

Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).

Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.

Help Maria get ready for the competition — determine the number of dangerous turns on the track.

Input

The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track.

The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1).

It is guaranteed that:

  • the first straight section is directed to the north;
  • the southernmost (and if there are several, then the most western of among them) point of the track is the first point;
  • the last point coincides with the first one (i.e., the start position);
  • any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point);
  • no pair of points (except for the first and last one) is the same;
  • no two adjacent straight sections are directed in the same direction or in opposite directions.

Output

Print a single integer — the number of dangerous turns on the track.

Sample Input

Input
60 00 11 11 22 22 00 0
Output
1
Input
161 11 53 53 72 72 96 96 75 75 34 34 43 43 25 25 11 1
Output
6

FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
All Copyright Reserved ©2010-2014 HUST ACM/ICPC TEAM

题意:

若干条直线围城的湖,给定直线的端点,判断多少个转点会有危险?(危险是指直走的的话会掉进水里)

分析:

  • 观察法:减去竖直水平的四条边,剩下的每两条边的交点就是答案。
  • 求叉积:看两个向量夹角,如果夹角小于90度,则直走的话会掉进水里。


2
3
5
46
9
78
12
1011
15
131416
20
17181921
26
22232425
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<cctype>
#define max(a,b)(a>b?a:b)
#define min(a,b)(a<b?a:b)
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
int n,a,b;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<=n;i++)
{
scanf("%d%d",&a,&b);
}
printf("%d\n",(n-4)/2);
}
return 0;
}
234567891011121314151617181920212223242526272829
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<cctype>#define max(a,b)(a>b?a:b)#define min(a,b)(a<b?a:b)#define INF 0x3f3f3f3fusing namespace std;int main(){    int n,a,b;    while(scanf("%d",&n)!=EOF)    {        for(int i=0;i<=n;i++)        {            scanf("%d%d",&a,&b);        }        printf("%d\n",(n-4)/2);    }    return 0;}

1136242673 's source code for D
Memory: 2192 KB Time: 15 MSLanguage: GNU G++ 4.9.2 Result: AcceptedPublic:  
Select All
123456789101112131415161718192021222324252627282930313233343536
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<cctype>#define max(a,b)(a>b?a:b)#define min(a,b)(a<b?a:b)#define INF 0x3f3f3f3fusing namespace std;int a[1100],b[1100];int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        for(int i=0;i<=n;i++)        {            scanf("%d%d",&a[i],&b[i]);        }        int ans=0;        for(int i=1;i<n;i++)        {            if((a[i]-a[i-1])*(b[i+1]-b[i])-(b[i]-b[i-1])*(a[i+1]-a[i])>0)  ///(x2-x1)/(x3-x2) > (y2-y1)/(y3-y2) 时 满足危险点条件                ans++;        }            printf("%d\n",ans);    }    return 0;}


234567891011121314151617181920212223242526272829
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<cctype>#define max(a,b)(a>b?a:b)#define min(a,b)(a<b?a:b)#define INF 0x3f3f3f3fusing namespace std;int main(){    int n,a,b;    while(scanf("%d",&n)!=EOF)    {        for(int i=0;i<=n;i++)        {            scanf("%d%d",&a,&b);        }        printf("%d\n",(n-4)/2);    }    return 0;}
0 0
原创粉丝点击