Educational Codeforces Round 4-D. The Union of k-Segments
2016-10-28 11:10
543 查看
原题链接
D. The Union of k-Segments
time limit per test
4 seconds
memory 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
output
input
output
#include <bits/stdc++.h>
#define maxn 1000005
using namespace std;
typedef long long ll;
struct Node{
Node(){
}
Node(int a, int b){
l = a;
r = b;
}
int l, r;
}node[maxn];
vector<Node> v;
int n, k, d[maxn<<1], p[maxn<<1];
int main(){
// freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++){
scanf("%d%d", &node[i].l, &node[i].r);
d[2*i] = node[i].l;
d[2*i+1] = node[i].r;
}
sort(d, d+2*n);
int cnt = 2 * n;
for(int i = 0; i < n; i++){
int k1 = lower_bound(d, d+cnt, node[i].l) - d;
p[k1+1]++;
int k2 = upper_bound(d, d+cnt, node[i].r) - d;
p[k2]--;
}
for(int i = 1; i <= cnt; i++)
p[i] += p[i-1];
int l = -1, r = -1;
for(int i = 0; i <= cnt; i++){
if(l == -1 && p[i] >= k)
l = i;
if(l != -1 && p[i] < k){
r = i;
v.push_back(Node(d[l-1], d[r-1]));
l = -1;
}
}
printf("%d\n", v.size());
for(int i = 0; i < v.size(); i++)
printf("%d %d\n", v[i].l, v[i].r);
return 0;
}
D. The Union of k-Segments
time limit per test
4 seconds
memory 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 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
#include <bits/stdc++.h>
#define maxn 1000005
using namespace std;
typedef long long ll;
struct Node{
Node(){
}
Node(int a, int b){
l = a;
r = b;
}
int l, r;
}node[maxn];
vector<Node> v;
int n, k, d[maxn<<1], p[maxn<<1];
int main(){
// freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++){
scanf("%d%d", &node[i].l, &node[i].r);
d[2*i] = node[i].l;
d[2*i+1] = node[i].r;
}
sort(d, d+2*n);
int cnt = 2 * n;
for(int i = 0; i < n; i++){
int k1 = lower_bound(d, d+cnt, node[i].l) - d;
p[k1+1]++;
int k2 = upper_bound(d, d+cnt, node[i].r) - d;
p[k2]--;
}
for(int i = 1; i <= cnt; i++)
p[i] += p[i-1];
int l = -1, r = -1;
for(int i = 0; i <= cnt; i++){
if(l == -1 && p[i] >= k)
l = i;
if(l != -1 && p[i] < k){
r = i;
v.push_back(Node(d[l-1], d[r-1]));
l = -1;
}
}
printf("%d\n", v.size());
for(int i = 0; i < v.size(); i++)
printf("%d %d\n", v[i].l, v[i].r);
return 0;
}
相关文章推荐
- 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 (扫描线)
- 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 排序
- Educational Codeforces Round 26 G. Functions On The Segments 主席树
- codeforces D. The Union of k-Segments 排序
- Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法
- [Educational Codeforces Round 7]F. The Sum of the k-th Powers
- Educational Codeforces Round 2D. Area of Two Circles' Intersection(计算几何+圆相交的面积)
- Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game
- Educational Codeforces Round 3 B. The Best Gift
- Educational Codeforces Round 2 D.Area of Two Circles' Intersection(计算几何)
- Codeforces Educational Codeforces Round 3 B. The Best Gift(签到)
- Educational Codeforces Round 4 A. The Text Splitting
- Educational Codeforces Round 5 E. Sum of Remainders(数学)
- Educational Codeforces Round 1 D.Igor In the Museum(DFS)
- Educational Codeforces Round 4 A. The Text Splitting 水题
- 【Educational Codeforces Round 3 B】【水题 基本容斥】The Best Gift n本书任选两个不能同一类型的方案数