Line-line Intersection Gym - 102220C
题目链接:https://vjudge.net/problem/Gym-102220C
题意:求n 条直线两两相交有几对(也可以重合)。
思路:用map和pair存所有直线的斜率和与X轴的交点,假设与前面i条直线都相交,那么要减去与这条直线平行而不重合的直线。
1 #include <bits/stdc++.h>
2 #include <time.h>
3 #include <set>
4 #include <map>
5 #include <stack>
6 #include <cmath>
7 #include <queue>
8 #include <cstdio>
9 #include <string>
10 #include <vector>
11 #include <cstring>
12 #include <utility>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 #include <list>
17 using namespace std;
18 //cout<<setprecision(10)<<fixed;
19 #define eps 1e-6
20 #define PI acos(-1.0)
21 #define lowbit(x) ((x)&(-x))
22 #define zero(x) (((x)>0?(x):-(x))<eps)
23 #define mem(s,n) memset(s,n,sizeof s);
24 #define rep(i,a,b) for(int i=a;i<=b;i++)
25 #define rep2(i,a,b) for(int i=a;i>=b;i--)
26 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
27 typedef long long ll;
28 typedef unsigned long long ull;
29 const int maxn=1e6+5;
30 const ll Inf=0x7f7f7f7f7f7f7f;
31 const ll mod=1e6+3;
32 //const int N=3e3+5;
33 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判断一个数是不是 2 的正整数次幂
34 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//对 2 的非负整数次幂取模
35 int getBit(int a, int b) { return (a >> b) & 1; }// 获取 a 的第 b 位,最低位编号为 0
36 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 为 0,否则为 -1
37 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); }
38 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
39 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
40 inline int read()
41 {
42 int X=0; bool flag=1; char ch=getchar();
43 while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
44 while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
45 if(flag) return X;
46 return ~(X-1);
47 }
48 inline void write(int X)
49 {
50 if(X<0) {X=~(X-1); putchar('-');}
51 if(X>9) write(X/10);
52 putchar(X%10+'0');
53 }
54 /*
55 inline int write(int X)
56 {
57 if(X<0) {putchar('-'); X=~(X-1);}
58 int s[20],top=0;
59 while(X) {s[++top]=X%10; X/=10;}
60 if(!top) s[++top]=0;
61 while(top) putchar(s[top--]+'0');
62 }
63 */
64 int Abs(int n) {
65 return (n ^ (n >> 31)) - (n >> 31);
66 /* n>>31 取得 n 的符号,若 n 为正数,n>>31 等于 0,若 n 为负数,n>>31 等于 -1
67 若 n 为正数 n^0=n, 数不变,若 n 为负数有 n^(-1)
68 需要计算 n 和 -1 的补码,然后进行异或运算,
69 结果 n 变号并且为 n 的绝对值减 1,再减去 -1 就是绝对值 */
70 }
71 ll binpow(ll a, ll b) {
72 ll res = 1;
73 while (b > 0) {
74 if (b & 1) res = res * a%mod;
75 a = a * a%mod;
76 b >>= 1;
77 }
78 return res%mod;
79 }
80 void extend_gcd(ll a,ll b,ll &x,ll &y)
81 {
82 if(b==0) {
83 x=1,y=0;
84 return;
85 }
86 extend_gcd(b,a%b,x,y);
87 ll tmp=x;
88 x=y;
89 y=tmp-(a/b)*y;
90 }
91 ll mod_inverse(ll a,ll m)
92 {
93 ll x,y;
94 extend_gcd(a,m,x,y);
95 return (m+x%m)%m;
96 }
97 ll eulor(ll x)
98 {
99 ll cnt=x;
100 ll ma=sqrt(x);
101 for(int i=2;i<=ma;i++)
102 {
103 if(x%i==0) cnt=cnt/i*(i-1);
104 while(x%i==0) x/=i;
105 }
106 if(x>1) cnt=cnt/x*(x-1);
107 return cnt;
108 }
109 map<pair<ll,ll>,ll>k;//斜率相同的直线;
110 map<pair<pair<ll,ll>,ll>,ll>c;//相同直线;
111 int main()
112 {
113 int n,t;
114 t=read();
115 while(t--)
116 {
117 n=read();
118 ll ans=0;
119 k.clear();
120 c.clear();
121 for(int i=0;i<n;i++)
122 {
123 ll x1,y1,x2,y2,x;
124 scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
125 x=x1*y2-x2*y1;
126 ll xx=x2-x1;
127 ll yy=y2-y1;
128 ll d=__gcd(xx,yy);
129 xx/=d;
130 yy/=d;
131 x/=d;
132 ans+=i-k[{xx,yy}]+c[{{xx,yy},x}];//减掉与其平行而不重合的直线;
133 k[{xx,yy}]++;
134 c[{{xx,yy},x}]++;
135 }
136 printf("%lld\n",ans);
137 }
138 return 0;
139 }
//假设与前i-1条边都相交,需要减去与他平行而不重合的线段
Line-line Intersection Gym - 102220C的更多相关文章
- 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 ...
- [CareerCup] 7.6 The Line Passes the Most Number of Points 经过最多点的直线
7.6 Given a two-dimensional graph with points on it, find a line which passes the most number of poi ...
- Poj 2074 Line of Sight
地址:http://poj.org/problem?id=2074 题目: Line of Sight Time Limit: 1000MS Memory Limit: 30000K Total ...
- 简单几何(直线求交点) POJ 2074 Line of Sight
题目传送门 题意:从一条马路(线段)看对面的房子(线段),问连续的能看到房子全部的最长区间 分析:自己的思路WA了:先对障碍物根据坐标排序,然后在相邻的障碍物的间隔找到区间,这样还要判断是否被其他障碍 ...
- [CC150] Find a line passing the most number of points
Problem: Given a two-dimensional graph with points on it, find a line which passes the most number o ...
- 《MATLAB从入门到放弃》二维曲线和图形绘制基础(二):使用Help文档学习line、plot、plotyy、subplot、hold绘图函数
目录: » plot 最常用的二维曲线绘图函数 > 帮助文档 > 基本使用语法 > 线条的样式.符号和颜色调整 > 图形属性调整 > 使用图形句柄进行设置 » ...
- Problem E: 平面上的点和线——Point类、Line类 (V)
Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定,两点确定一条线段.现在我们封装一个“Point类”和“Line类”来实现平面上的点的操作. 根据“append ...
- Problem D: 平面上的点和线——Point类、Line类 (IV)
Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定,两点确定一条线段.现在我们封装一个“Point类”和“Line类”来实现平面上的点的操作. 根据“append ...
- Problem C: 平面上的点和线——Point类、Line类 (III)
Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定,两点确定一条线段.现在我们封装一个“Point类”和“Line类”来实现平面上的点的操作. 根据“append ...
随机推荐
- Kattis amazingadventures Amazing Adventures(费用流路径)题解
题意: 在一个\(100*100\)的方格中,要求从\(b\)走到\(g\),途中经过\(c\)但不经过\(u\),并且不能走已经做过的路.如果可以,就求出路径. 思路: 拆点建费用流,看能不能从\( ...
- μC/OS-III---I笔记5---多值信号量
多值信号量 操作系统中利用信号量解决进程间的同步和互斥(互斥信号量)的问题,在多道程序环境下,操作系统如何实现进程之间的同步和互斥显得极为重要.比如对同一部分资源的访问是要互斥,不能在另一个进程A在访 ...
- CSS 弹性盒子模型
CSS 弹性盒子模型 https://www.w3.org/TR/2016/CR-css-flexbox-1-20160526/ CSS Flexible Box Layout Module Leve ...
- 如何使用 js 实现一个 debounce 函数
如何使用 js 实现一个 debounce 函数 原理 防抖: 是指在指定的单位时间内,如果重复触发了相同的事件,则取消上一次的事件,重新开始计时! 实现方式 "use strict&quo ...
- 如何用 js 实现一个 bind 函数
如何用 js 实现一个 bind 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...
- JavaScript 如何使用 setTimeout 实现 setInterval
JavaScript 如何使用 setTimeout 实现 setInterval website multi content page setIntervalSimulator "use ...
- React Hooks & React v16.8.6
React Hooks Hooks are a new addition in React 16.8 const [state, setState] = useState(initialState); ...
- DHCP (Dynamic Host Configuration Protocol )协议的探讨与分析
DHCP (Dynamic Host Configuration Protocol )协议的探讨与分析 问题背景 最近在工作中遇到了连接外网的交换机在IPv6地址条件下从运营商自动获取的DNS地址与本 ...
- 联童科技基于incubator-dolphinscheduler从0到1构建大数据调度平台之路
联童科技是一家智能化母婴童产业平台,从事母婴童行业以及互联网技术多年,拥有丰富的母婴门店运营和系统开发经验,在会员经营和商品经营方面,能够围绕会员需求,深入场景,更贴近合作伙伴和消费者,提供最优服务产 ...
- 在.NET中使用Apache Kafka(一)
曾经在你的应用程序中使用过异步处理吗?在处理不需要立即执行的任务时,异步代码似乎是不可避免的.Apache Kafka是最常用和最健壮的开源事件流平台之一.许多公司和开发者利用它的强大功能来创建高性 ...