Codeforces 593B Anton and Lines
来源:互联网 发布:如何评价李舒 知乎 编辑:程序博客网 时间:2024/04/29 08:05
题目链接:http://codeforces.com/problemset/problem/593/B
题意:直线:y=kx+b,给出n个(k,b),问这n条直线是否在(x1,x2)中有交点
思路:处理处每条直线与x1,x2的交点l和r,存储结构体数组s中,如果li < lj 且 ri>rj则相交,由于数据比较大,需要有更好的查询方式,我们先以l从小到大排一遍序,先以s[0]为基准tmp进行比较,s[tmp].r<=s[i].r的时候,我可以得知在(x1,x2)内与直线tmp有交点,必须先与i有交点,tmp=i,以i为基准继续比较,时间复杂度是O(n)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define maxn 1000300using namespace std;struct Node{ long long l,r;}s[maxn];bool cmp(Node p,Node q){ if (p.l!=q.l) return p.l<q.l; else return p.r<q.r;}int main(){ int n,x1,x2; while (scanf("%d",&n)!=EOF) { scanf("%d%d",&x1,&x2); for (int i=0;i<n;i++) { int k,b; scanf("%d%d",&k,&b); s[i].l=(long long)k*x1+b; s[i].r=(long long)k*x2+b; } sort(s,s+n,cmp); int tmp=0,flag=0; for (int i=1;i<n;i++) { if (s[tmp].l<s[i].l && s[tmp].r>s[i].r) { flag=1; break; } else { tmp=i; } } if (flag) printf("YES\n"); else printf("NO\n"); }}
0 0
- Codeforces 593B Anton and Lines 【思维】
- codeforces 593B Anton and Lines
- Codeforces 593B Anton and Lines
- CodeForces 593B - Anton and Lines(思维)
- Codeforces Round #329 (Div. 2) 593B - Anton and Lines
- CodeForces 593B Anton and Lines【数学+排序】
- Codeforces Round #329 (Div. 2)B. Anton and Lines
- Codeforces Round #329 (Div. 2)B. Anton and Lines
- Codeforces Round #329 (Div. 2)B. Anton and Lines
- CodeForces - 593B Anton and Lines (数学方程&技巧) 判断直线是否相交
- Code Forces 593 B. Anton and Lines(水~)
- codeforce #329div2 B. Anton and Lines
- cf B. Anton and Lines (STL)
- Codeforces593B. Anton and Lines
- Codeforces Round #329 (Div. 2) B. Anton and Lines ([好题] 计算直线在区间是否有交点)
- 【81.37%】【codeforces 734B】Anton and Digits
- codeforces 734B.Anton and Digits
- 【codeforces 785B】Anton and Classes
- 船长 | 我们在Club全栈创业
- 2.4 Android编码规范(微博官方)
- B/S学习之旅
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十二)水情雨情模块
- 新PaaS平台上线数据初始化经验
- Codeforces 593B Anton and Lines
- CSS 格式化排版
- Team Formation 浙江省省赛原题
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十三)台风模块
- MyAccountBook
- Maven集成
- java中public protected private修饰符
- UML全家福
- LeetCode Combination Sum III