【codefores 612】 The Union of k-Segments 【思维+ 线段处理】
2017-09-19 18:17
459 查看
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
写这道题 之前是先写了 这道题 C 题
都是对 线段问题的处理 ,这种处理方式可以 记录一下。
处理完之后,找一下规律就知道,cnt表示以当前点为起点之后的点被线段的重叠个数。 之后枚举所有端点,一个左端点cnt++,一个右端点cnt–。 同时记录一下 最后答案的左点和右点就行。
代码
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
写这道题 之前是先写了 这道题 C 题
都是对 线段问题的处理 ,这种处理方式可以 记录一下。
处理完之后,找一下规律就知道,cnt表示以当前点为起点之后的点被线段的重叠个数。 之后枚举所有端点,一个左端点cnt++,一个右端点cnt–。 同时记录一下 最后答案的左点和右点就行。
代码
#include<bits/stdc++.h> using namespace std; #define _int64 LL const int MAXN = 1e6+10; const int MAXM = 1e6; const int mod = 1e9+7; const int inf = 0x3f3f3f3f; struct Node{ int p,t; Node(){} Node (int a,int b) { p=a; t=b; } bool operator<(Node b){ if(b.p==p) return t<b.t; else return p<b.p; } }P[MAXN*2]; vector < pair<int,int> > ve ; int main(){ int n,k;scanf("%d%d",&n,&k); int a,b,c;int tot=0; for(int i=1;i<=n;i++){ scanf("%d%d",&a,&b); P[tot++]={a,-1}; P[tot++]={b,1}; } sort(P,P+tot); int l,r;int cnt=0; for(int i=0;i<tot;i++){ if(P[i].t==-1) { cnt++; if(cnt==k) l=P[i].p; }else { cnt--; if(cnt==k-1) { r=P[i].p; ve.push_back({l,r}); } } } printf("%d\n",ve.size()); for(int i=0;i<ve.size();i++) printf("%d %d\n",ve[i].first,ve[i].second); return 0; }
相关文章推荐
- Codeforce 题目612 D The Union of k-Segments(暴力,排序,技巧)
- Educational Codeforces Round 4 D. The Union of k-Segments 排序,思维
- The Union of k-Segments CodeForces - 612D (思维)
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
- 【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments
- The Union of k-Segments CodeForces - 612D 什么技巧还有数据结构
- Codeforces 612 D. The Union of k-Segments (非递归线段树+离散化)
- Educational Codeforces Round 4 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 (扫描线)
- Educational Codeforces Round 4-D. The Union of k-Segments
- codeforces D. The Union of k-Segments 排序
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
- The Union of k-Segments CodeForces - 612D
- Codeforces Round #428 (Div. 2) B. Game of the Rows (思维)
- CodeForces 612D - The Union of k-Segments(模拟)
- Codeforce 839 -- B Game of the Rows 【思维 + 贪心】
- Codeforces 612D Educational Codeforces Round 4 D. The Union of k-Segments 离散化+map
- 【思维+贪心】codeforces Game of the Rows