CodeForces 612D - The Union of k-Segments(模拟)
2016-09-14 18:07
357 查看
D. The Union of k-Segments
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 2
0 5
-3 2
3 8
output
2
0 2
3 5
input
3 2
0 5
-3 3
3 8
output
1
0 5
题意:
给出n个线段和k,然后看在数轴上哪些区间的线段覆盖度大于等于k.
解题思路:
如果一个区间的覆盖度是k,那么他前面的线段起点必然大于等于k.
记录下每个线段的起点和终点.然后对所有点进行排序,如果遇到的是起点,那么cnt++,否则cnt–.
当cnt == k的时候,是这个区间的起点,再次遇到cnt == k同时该点是线段的终点时,这个点就是这个区间的终点.
AC代码:
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 2
0 5
-3 2
3 8
output
2
0 2
3 5
input
3 2
0 5
-3 3
3 8
output
1
0 5
题意:
给出n个线段和k,然后看在数轴上哪些区间的线段覆盖度大于等于k.
解题思路:
如果一个区间的覆盖度是k,那么他前面的线段起点必然大于等于k.
记录下每个线段的起点和终点.然后对所有点进行排序,如果遇到的是起点,那么cnt++,否则cnt–.
当cnt == k的时候,是这个区间的起点,再次遇到cnt == k同时该点是线段的终点时,这个点就是这个区间的终点.
AC代码:
#include<stdio.h> #include<algorithm> using namespace std; const int maxn = 2000005; struct node { int val; int op; }; node qdu[maxn]; int res[maxn]; bool cmp(node a,node b) { if(a.val != b.val) return a.val < b.val; else return a.op > b.op; } int main() { int n; int k; scanf("%d%d",&n,&k); int top = 0; for(int i = 0;i < n;i++) { int l; int r; scanf("%d%d",&l,&r); qdu[top].val = l; qdu[top++].op = 1; qdu[top].val = r; qdu[top++].op = 0; } sort(qdu,qdu+top,cmp); int have = 0; int cnt = 0; for(int i = 0;i < top;i++) { if(qdu[i].op) { have++; if(have == k) res[cnt++] = qdu[i].val; } else { if(have == k) res[cnt++] = qdu[i].val; have--; } } printf("%d\n",cnt>>1); for(int i = 0;i < cnt;i += 2) printf("%d %d\n",res[i],res[i+1]); return 0; }
相关文章推荐
- The Union of k-Segments CodeForces - 612D
- CodeForces 612 D. The Union of k-Segments(水~)
- The Union of k-Segments CodeForces - 612D (思维)
- Codeforces 612 D. The Union of k-Segments (非递归线段树+离散化)
- (CodeForces - 612D)The Union of k-Segments
- Codeforces 612D Educational Codeforces Round 4 D. The Union of k-Segments 离散化+map
- The Union of k-Segments CodeForces - 612D 什么技巧还有数据结构
- codeforces D. The Union of k-Segments 排序
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- CF#The Union of k-Segments -离散化+树状数组
- codeforces 251 div2 C. Devu and Partitioning of the Array 模拟
- [CF612D] The Union of k-Segments(排序,扫描线)
- CodeForces-732B-Cormen — The Best Friend Of a Man(简单模拟)
- cf612D-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(扫描线)
- D. The Union of k-Segments----扫描线初步
- Educational Codeforces Round 4-D. The Union of k-Segments