C - Line-line Intersection Gym - 102220C(线段相交)
There are n lines l1,l2,…,ln
on the 2D-plane.
Staring at these lines, Calabash is wondering how many pairs of (i,j)
that 1≤i<j≤n and li,lj
share at least one common point. Note that two overlapping lines also share common points.
Please write a program to solve Calabash's problem.
Input
The first line of the input contains an integer T(1≤T≤1000)
, denoting the number of test cases.
In each test case, there is one integer n(1≤n≤100000)
in the first line, denoting the number of lines.
For the next n
lines, each line contains four integers xai,yai,xbi,ybi(|xai|,|yai|,|xbi|,|ybi|≤109). It means li passes both (xai,yai) and (xbi,ybi). (xai,yai) will never be coincided with (xbi,ybi)
.
It is guaranteed that ∑n≤106
.
Output
For each test case, print a single line containing an integer, denoting the answer.
Example
3 2 0 0 1 1 0 1 1 0 2 0 0 0 1 1 0 1 1 2 0 0 1 1 0 0 1 1
1 0 1
题解:两条直线不平行必相交,若平行:若重合答案加1,否则不算。
#include<iostream> #include<cstring> #include<string> #include<queue> #include<stack> #include<algorithm> #include<stdio.h> #include<map> #include<set> using namespace std; typedef long long ll; typedef pair<ll,ll>P; typedef pair<pair<ll,ll>,ll>Pi; const int maxn=100010; map<pair<ll,ll>,ll>mp1;//两个参数a,b,代表形如a*x+b*y=c(c任意)的直线有多少个 map<pair<pair<ll,ll>,ll>,ll>mp2;//三个参数a,b,c,代表形如a*x+b*y=c的直线有多少个,即相同直线有多少个 ll cnt,ans,n; int main() {ios::sync_with_stdio(0);int T;cin>>T;while(T--){ans=cnt=0;cin>>n;mp1.clear(),mp2.clear();for(int i=1;i<=n;i++){ll x1,x2,y1,y2;cin>>x1>>y1>>x2>>y2;ll a=x1-x2,b=y1-y2,c=x1*y2-x2*y1;ll g=__gcd(a,b);a/=g;b/=g;c/=g;mp1[P(a,b)]++;mp2[Pi(P(a,b),c)]++;ans+=i-1+mp2[Pi(P(a,b),c)]-mp1[P(a,b)];//假设与前i-1条边都相交,需要减去与他平行而不重合的线段 }cout<<ans<<endl;}return 0; }
转载于:https://www.cnblogs.com/cherish-lin/p/11066282.html
C - Line-line Intersection Gym - 102220C(线段相交)相关推荐
- C++line segment intersection线段求交(交点)(附完整源码)
C++line segment intersection线段求交的实现 C++line segment intersection线段求交实现的完整源码(定义,实现,main函数测试) C++line ...
- 判断线段相交 + vector. erase迭代指针 的使用 poj 2653 Pick-up sticks
题目来源:http://poj.org/problem?id=2653 分析: 题意:按顺序给出一些木棍,输出在最上面的木棍标号. 用vector 存储木棍标号, 当前木棍与 vector 中的木棍 ...
- 如何判断2个线段相交
判断 2 个线段相交有很多方法,最直接的方法就是直接计算两条直线的交点,然后看看交点是否分别在这两条线段上.这样的方法很容易理解,但是代码实现比较麻烦. 还有一种常用的方法是通过向量叉积来判断的,这种 ...
- POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
- POJ-1556 The Doors 线段相交+最短路
题意:在一个矩形平面内,有若干道墙,现求从左部某一点到右部某一点的最短路径. 解法:有一个事实是线路一定是从门两边的点上通过的,不可能出现从中间穿过的可能.因此我们就枚举两两点之间是否可达,这里就要使 ...
- [51nod1264]线段相交
给定两个点: typedef struct { double x, y; } Point; Point A1,A2,B1,B2; 首先引入两个实验: a.快速排斥实验 设以线段A1A2和线段B1B ...
- 【POJ - 1556】The Doors (计算几何,线段相交)
题干: You are to find the length of the shortest path through a chamber containing obstructing walls. ...
- 【POJ - 3304 】Segments(计算几何,思想转化,直线和线段相交)
题干: Given n segments in the two dimensional space, write a program, which determines if there exists ...
- HDU1086You can Solve a Geometry Problem too(判断线段相交)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
最新文章
- SQL Server 中各个系统表的作用
- Android系统移植与调试之-------如何修改Android设备添加重启、飞行模式、静音模式等功能(一)...
- 斯坦福证明神经网络能直接在光学芯片上训练
- scrapy配置文件操作核心API解读
- tomcat jsvc 调优及JMX监控
- poj 2689 大范围素数
- Django - app
- 【Python】图解Pandas数据合并:concat、join、append
- Android 高级Drawable资源---复合Drawable----变换Drawable
- 飞鸽传书:浅谈 Scrum
- SpringBoot——@Scheduled的自定义周期性线程池解决任务延时执行问题
- 深入理解springboot starter
- golang变量的注意
- CodeProject上的两个简单绘图程序
- 烧钱圈地后,医疗大数据如何变现?
- ArcGIS导出辖区边界点坐标
- QT资源库中图片的调用
- 关于3D打印文件格式:STL、OBJ、AMF、3MF的详解
- 《唯有时间能证明伟大:极客之王特斯拉传》读后感
- Angularjs之模块(angular.module('myApp', [])参数)