C - Crazy Town
Problem description
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 ≤ x2, y2 ≤ 106) — the coordinates of the university you are studying at.
The third line contains an integer n (1 ≤ n ≤ 300) — the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≤ ai, bi, ci ≤ 106; |ai| + |bi| > 0) — the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
解题思路:题目的意思就是有n条直线(它们互不重合且其方程满足ax+by+c=0)把一个平面分成很多块,家和学校这两个坐标点必在其中一个块中,规定从一个块到与之相邻的另一个块所走步数为1(不能走没有相邻的块),求从家到学校走的最小步数。分析一下走法可知,从家到学校路途中,跨过相邻的块必经过一条直线,且这条直线在A、B两点之间,因此可以得出最小步数为A、B两点之间直线的条数。怎么判断条数呢?回想一下,高中学过的线性规划,在判断可行域的时候,通常以某个点例如(0,0)来判断可行域属于直线上方还是下方,上方为正,下方为负。因此这道题刚好就运用到这一知识点:把A(x1,y1),B(x2,y2)分别代到n个方程中,只要每个方程的两个结果满足一正一负(直线的两侧),就将步数加1,最后即为最小步数。注意本题判断方法中的相乘可能导致数据溢出,因此将所有数据类型定义为long long,避免出错。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
LL x1,y1,x2,y2,n,a,b,c,step=;
cin>>x1>>y1>>x2>>y2>>n;
while(n--){
cin>>a>>b>>c;
if((a*x1+b*y1+c>)&&(a*x2+b*y2+c<))step++;
if((a*x1+b*y1+c<)&&(a*x2+b*y2+c>))step++;
}
cout<<step<<endl;
return ;
}
C - Crazy Town的更多相关文章
- #284 div.2 C.Crazy Town
C. Crazy Town Crazy Town is a plane on which there are n infinite line roads. Each road is defined ...
- Codeforces Round #284 (Div. 1) A. Crazy Town 计算几何
A. Crazy Town 题目连接: http://codeforces.com/contest/498/problem/A Description Crazy Town is a plane on ...
- Codeforces 498A Crazy Town
C. Crazy Town time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- A. Crazy Town
Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation ...
- Codeforces_499C:Crazy Town(计算几何)
题目链接 给出点A(x1,y1),B(x2,y2),和n条直线(ai,bi,ci,aix + biy + ci = 0),求A到B穿过多少条直线 枚举每条直线判断A.B是否在该直线两侧即可 #incl ...
- Codeforces 499C:Crazy Town(计算几何)
题目链接 给出点A(x1,y1),B(x2,y2),和n条直线(ai,bi,ci,aix + biy + ci = 0),求A到B穿过多少条直线 枚举每条直线判断A.B是否在该直线两侧即可 #incl ...
- 【codeforces 499C】Crazy Town
[题目链接]:http://codeforces.com/problemset/problem/499/C [题意] 一个平面,被n条直线分成若干个块; 你在其中的某一块,然后你想要要到的终点在另外一 ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #284 (Div. 1)
A. Crazy Town 这一题只需要考虑是否经过所给的线,如果起点和终点都在其中一条线的一侧,那么很明显从起点走点终点是不需要穿过这条线的,否则则一定要经过这条线,并且步数+1.用叉积判断即可. ...
随机推荐
- 微信小程序——weui的使用
使用在根目录中复制weui.wxss,app.wxss中引入 在weui.io中查看到自己想要的样式表后,到第二个网站复制代码,复制到自己的项目中即可 <!--pages/register/re ...
- JQ + PHP + TrackMore物流信息跟踪
在使用之前,您需要先去trackmore官方网站申请API_KEY,传送门:TrackMore html <script type="text/javascript" src ...
- 腾讯云,搭建 Discuz 个人论坛
准备 LAMP 环境 任务时间:30min ~ 60min LAMP 是 Linux.Apache.MySQL 和 PHP 的缩写,是 Discuz 论坛系统依赖的基础运行环境.我们先来准备 LAMP ...
- 【BestCoder Round #93 1001】MG loves gold
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=6019 [题意] 每次选择一段连续的段,使得这一段里面没有重复的元素; 问你最少选多少次; [题解] ...
- HDU 5343 MZL's Circle Zhou
MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Orig ...
- [Codeforces 872]比赛记录
强行打了$cf$上的第一场比赛,现在感觉自己的$rating$会炸飞= = A 这是练习输入输出吗QAQ,竟然$WA$了两遍QAQ,我$WA$的一声就哭了出来啊QAQ B 好像很水的乱扫就好了,m ...
- [POJ1733]Parity game(并查集 + 离散化)
传送门 题意:有一个长度已知的01串,给出[l,r]这个区间中的1是奇数个还是偶数个,给出一系列语句问前几个是正确的 思路:如果我们知道[1,2][3,4][5,6]区间的信息,我们可以求出[1,6] ...
- 用Grails写单元测试
新的领域,多练练,这样写出的程序,确实坚固些. 也要理解集成测试与数据库相关,单元测试与类方法有关. 如果测试文件没有建立,按如下操作: Unit tests are generated automa ...
- 相克军_Oracle体系_随堂笔记 PPT
http://www.cnblogs.com/jyzhao/category/581259.html http://download.csdn.net/detail/yzj149286454/8960 ...
- gap lock/next-key lock浅析 Basic-Paxos协议日志同步应用
http://www.cnblogs.com/renolei/p/4673842.html 当InnoDB在判断行锁是否冲突的时候, 除了最基本的IS/IX/S/X锁的冲突判断意外, InnoDB还将 ...