D - Bicycle Race

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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
6
0 0
0 1
1 1
1 2
2 2
2 0
0 0
Output
1
Input
16
1 1
1 5
3 5
3 7
2 7
2 9
6 9
6 7
5 7
5 3
4 3
4 4
3 4
3 2
5 2
5 1
1 1
Output
6

题意:一个多边形围成的湖,一个人绕着走。如果在某个位置不转弯直走的话会掉入湖里,

   则认为这个弯是危险的。问有多少个这样的弯。

题解:第一种解法:从题目中的加粗字体可知人是从左下角往上走的,即顺时针走,画顺时针走的图可知向量b在向量a的逆时针方向,

   即叉积大于0。注意千万不能看做逆时针走,一定要注意方向,否则结果会是n-ans。

   第二种解法:可以发现,在内角为270°的地方才有可能掉到水里,设这样的地方有x个,

   则内角和 180 * (n - 2) = 270 * x + (n - x) * 90,得到 x=(n-4)/2 。

代码一:

#include <iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int i,ans=;
int a[],b[];
for(i=;i<=n;i++)
cin>>a[i]>>b[i];
for(i=;i<=n;i++)
if(((a[i-]-a[i-])*(b[i]-b[i-])-(a[i]-a[i-])*(b[i-]-b[i-]))>)
ans++;
cout<<ans<<endl;
}
return ;
}

代码二:

#include <cstdio>
using namespace std;
int main(){
int n;
scanf("%d",&n);
printf("%d\n",(n - )/);
return ;
}

CodeForces 659D Bicycle Race (判断点是否为危险点)的更多相关文章

  1. Codeforces 659D Bicycle Race【计算几何】

    题目链接: http://codeforces.com/contest/659/problem/D 题意: 若干条直线围城的湖,给定直线的端点,判断多少个转点会有危险?(危险是指直走的的话会掉进水里) ...

  2. codeforces 659D . Bicycle Race 几何

    题目链接 对相邻的三个点叉积判断一下就好. #include <iostream> #include <vector> #include <cstdio> #inc ...

  3. [ An Ac a Day ^_^ ] CodeForces 659D Bicycle Race 计算几何 叉积

    问有多少个点在多边形内 求一遍叉积 小于零计数就好了~ #include<stdio.h> #include<iostream> #include<algorithm&g ...

  4. codeforces 659D D. Bicycle Race(水题)

    题目链接: D. Bicycle Race time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. Codeforces Round #346 (Div. 2) D Bicycle Race

    D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...

  6. (求凹包) Bicycle Race (CF 659D) 简单题

    http://codeforces.com/contest/659/problem/D     Maria participates in a bicycle race. The speedway t ...

  7. Codeforces Round #346 (Div. 2) D. Bicycle Race 叉积

    D. Bicycle Race 题目连接: http://www.codeforces.com/contest/659/problem/D Description Maria participates ...

  8. 2016NEFU集训第n+3场 D - Bicycle Race

    Description Maria participates in a bicycle race. The speedway takes place on the shores of Lake Luc ...

  9. CodeForces 8D Two Friends 判断三个圆相交

    题意: 有两个人\(Alan\)和\(Bob\),他们现在都在\(A\)点,现在\(Bob\)想去\(B\)点,\(Alan\)想先到\(C\)点再去\(B\)点. \(Alan\)所走的总路程不能超 ...

随机推荐

  1. QToolBox学习笔记

    抽屉控件效果类似于QQ界面 最外面一层叫工具盒QToolBox QToolBox中装的是QGroupBox,分组的盒子 在分组的盒子QGroupBox中装的是QToolButton.

  2. python基础之函数参数、嵌套、返回值、对象、命名空间和作用域

    函数的使用原则 函数的使用必须遵循:先定义后使用的原则 函数的定义,与变量的定义是相似的,如果没有事先定义函数而直接引用就相当于在引用一个不存在变量名 定义阶段:只检测语法,不执行代码,当出现语法错误 ...

  3. spark练习--mysql的读取

    前面我们一直操作的是,通过一个文件来读取数据,这个里面不涉及数据相关的只是,今天我们来介绍一下spark操作中存放与读取 1.首先我们先介绍的是把数据存放进入mysql中,今天介绍的这个例子是我们前两 ...

  4. Android 面试收集录5 消息机制

    1.消息机制概述 1.1.消息机制的简介 在Android中使用消息机制,我们首先想到的就是Handler. 没错,Handler是Android消息机制的上层接口. Handler的使用过程很简单, ...

  5. Android 数据库中的数据给到ListView

    前言:因为之前学的都是用一个自己定义的类,完成将某一个bean中的数据直接获取,而实际中通常是通过数据库来得到的,总之,最终就是要得到数据.提一下最重要的东西,我把它叫做代理,如同一个校园代理,没有他 ...

  6. Eclipse 安装(Neon 版本2016年)---Eclipse教程第01课

    Eclipse 最新版本 Eclipse Neon,这个首次鼓励用户使用 Eclipse Installer 来做安装,这是一种由Eclipse Oomph提供的新技术,它通过提供一个很小的安装器来使 ...

  7. android gesture检测

    1.关于on<TouchEvent>的返回值 a return value of true from the individual on<TouchEvent> methods ...

  8. 《Cracking the Coding Interview》——第17章:普通题——题目4

    2014-04-28 22:32 题目:不用if语句或者比较运算符的情况下,实现max函数,返回两个数中更大的一个. 解法:每当碰见这种无聊的“不用XXX,给我XXX”型的题目,我都默认处理的是int ...

  9. soapUI的简单使用(webservice接口功能测试)

    1.soapUI支持什么样的测试? 功能测试.性能测试.负载.回归测试等,它不仅仅可以测试基于 SOAP 的 Web 服务,也可以测试 REST 风格的 Web 服务. 1.SoapUI安装注意事项 ...

  10. Python之进程、线程、协程

    进程和线程的目的 进程和线程目的是为了:提高执行效率 现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务“呢?简单地说,就是操作系 ...