cf612D-The Union of k-Segments(思路题(类似扫描线))
来源:互联网 发布:搭配发型的软件 编辑:程序博客网 时间:2024/06/05 23:47
题目来源:http:///contest/612/problem/D
题意
在一个Ox的横轴上给出好多闭区间,问被这些闭区间覆盖k次或者k次以上的点的最少的区间的个数,以及区间。
思路
假设存在与y轴平行的一条线(无穷远处),然后从至右依次遍历这些点,遇到一个左边界,就+1,遇到右边界就-1,一旦有大于等于k的就存起来,由于不知道他有多少段符合条件的区间,所以用vector存下较为合适,其中pair的作用类似于结构体。。。
代码
#include<vector>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct pp{ int l,r;}s[2000000+10];vector<pair<int,int> > v;bool cmp(pp a,pp b){ if(a.l==b.l) { return a.r>b.r; } return a.l<b.l;}int main(){ int n,k; while(~scanf("%d%d",&n,&k)) { for(int i=0;i<n;i++) { int l ,r; scanf("%d%d",&l,&r); s[2*i].l=l; s[2*i].r=1; s[2*i+1].l=r; s[2*i+1].r=-1; } sort(s,s+2*n,cmp); for(int i=1;i<2*n;i++) { s[i].r+=s[i-1].r; } for(int i=0;i<2*n;i++) { if(s[i].r>=k) { int x=s[i++].l,y; while(s[i].r>=k) i++; y=s[i].l; v.push_back(make_pair(x,y)); } } int len=v.size(); printf("%d\n",len); for(int i=0;i<v.size();i++) { printf("%d %d\n",v[i].first,v[i].second); } }}
阅读全文
0 0
- cf612D-The Union of k-Segments(思路题(类似扫描线))
- D. The Union of k-Segments(扫描线)
- ★codeforces 612D The Union of k-Segments (思维or类扫描线)
- CodeForces 612D The Union of k-Segments(排序+区间计数、扫描线)
- D. The Union of k-Segments----扫描线初步
- CodeForces 612 D. The Union of k-Segments(水~)
- codeforces_612D. The Union of k-Segments(排序)
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- 排序 Codeforces612D The Union of k-Segments
- The Union of k-Segments CodeForces
- The Union of k-Segments CodeForces
- Codeforce 题目612 D The Union of k-Segments(暴力,排序,技巧)
- codeforces 612D The Union of k-Segments (前缀和处理区间)
- codeforces D. The Union of k-Segments 排序
- CF#The Union of k-Segments -离散化+树状数组
- Codeforces 612D The Union of k-Segments 【思维】
- Codeforces 612D The Union of k-Segments
- codeforces 612D The Union of k-Segments (sorting)
- springboot整合mybatis xml方式配置SQL
- 删除null 字段
- Android socket通过UDP的方式发送,接收数据
- equals与==的区别
- 20171031
- cf612D-The Union of k-Segments(思路题(类似扫描线))
- 【LPC54100】GPIO驱动TFT LCD
- Erlang 并发编程基础二
- luogu 2700 逐个击破
- 【数据平台】pandas将一列中的文本拆分成多行
- VirtualBox中克隆出现的eth0错误的问题
- std::any——C++弱类型特性
- 99. Recover Binary Search Tree
- 【BZOJ1055】【HAOI2008】玩具取名