(CodeForces - 612D)The Union of k-Segments
2017-09-28 21:21
399 查看
(CodeForces - 612D)The Union of k-Segments
time limit per test:4 secondsmemory limit per test:256 megabytes
input:standard input
output:standard output
You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others.
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k.The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order.
Output
First line contains integer m — the smallest number of segments.Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right.
Examples
Input
3 20 5
-3 2
3 8
Output
20 2
3 5
Input
3 20 5
-3 3
3 8
Output
10 5
题目大意:给出n个线段问至少被覆盖k次的点的集合。
思路:将线段排序,用cnt从左到右计数,碰到左括号+1,碰到右括号-1,如果cnt等于k时输出左右括号的端点值就是所要的答案。(可以在草稿纸上划一下验证)
#include<cstdio> #include<algorithm> using namespace std; const int maxn=1000005; int ans[maxn<<1]; struct node { int left,x; }a[maxn<<1]; bool cmp(node a,node b) { return ((a.x<b.x)||((a.x==b.x)&&a.left>b.left)); } int main() { int n,k; while(~scanf("%d%d",&n,&k)) { int tot=0; for(int i=0;i<n;i++) { int l,r; scanf("%d%d",&l,&r); a[tot].x=l; a[tot++].left=1; a[tot].x=r; a[tot++].left=0; } sort(a,a+tot,cmp); int cnt=0,tot1=0; for(int i=0;i<tot;i++) { if(a[i].left) { cnt++; if(cnt==k) ans[tot1++]=a[i].x; } else { if(cnt==k) ans[tot1++]=a[i].x; cnt--; } } printf("%d\n",tot1/2); for(int i=0;i<tot1;i+=2) printf("%d %d\n",ans[i],ans[i+1]); } return 0; }
相关文章推荐
- Codeforces 612 D. The Union of k-Segments (非递归线段树+离散化)
- The Union of k-Segments CodeForces - 612D 什么技巧还有数据结构
- The Union of k-Segments CodeForces - 612D (思维)
- CodeForces 612 D. The Union of k-Segments(水~)
- Codeforce 题目612 D The Union of k-Segments(暴力,排序,技巧)
- The Union of k-Segments CodeForces - 612D
- CodeForces 612D - The Union of k-Segments(模拟)
- Codeforces 612D Educational Codeforces Round 4 D. The Union of k-Segments 离散化+map
- 【codefores 612】 The Union of k-Segments 【思维+ 线段处理】
- D. The Union of k-Segments----扫描线初步
- Educational Codeforces Round 4 D. The Union of k-Segments(★)
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
- Educational Codeforces Round 4 D. The Union of k-Segments 排序,思维
- D. The Union of k-Segments(扫描线)
- Educational Codeforces Round 4-D. The Union of k-Segments
- codeforces D. The Union of k-Segments 排序
- [CF612D] The Union of k-Segments(排序,扫描线)
- 【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments
- cf612D-The Union of k-Segments(思路题(类似扫描线))
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)