CodeForces 612D The Union of k-Segments
2016-04-22 23:52
411 查看
题意:
给你一堆区间,然后让你把覆盖k次及k次以上的区间都输出出来
思路:
直接暴力扫分界点就好了
分界点是正向覆盖k次的就加进左端点,是反向,就加进右端点,然后输出就好了
Description
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.
Sample Input
Input
Output
Input
Output
给你一堆区间,然后让你把覆盖k次及k次以上的区间都输出出来
思路:
直接暴力扫分界点就好了
分界点是正向覆盖k次的就加进左端点,是反向,就加进右端点,然后输出就好了
#include<bits/stdc++.h> using namespace std; #define maxn 3000006 pair<int,int> Line[maxn]; int tot = 1; int t[maxn]; int main() { int n,k;scanf("%d%d",&n,&k); for(int i=0;i<n;i++) { int x,y;scanf("%d%d",&x,&y); Line[tot++]=make_pair(x,-1); Line[tot++]=make_pair(y,1); } sort(Line+1,Line+tot); int flag1 = 0,flag2 = 0; vector<int> ans1; vector<int> ans2; for(int i=1;i<tot;i++) { t[i] = t[i-1] - Line[i].second; if(t[i]==k&&t[i-1]==k-1) ans1.push_back(Line[i].first); } memset(t,0,sizeof(t)); for(int i=1;i<tot;i++) { t[i] = t[i-1] - Line[i].second; if(t[i]==k-1&&t[i-1]==k) ans2.push_back(Line[i].first); } if(ans1.size()!=ans2.size()) ans2.push_back(Line[tot-1].first); cout<<ans1.size()<<endl; for(int i=0;i<ans1.size();i++) cout<<ans1[i]<<" "<<ans2[i]<<endl; }
Description
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.
Sample Input
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
相关文章推荐
- 20150203+JS巩固与加强1-01
- (转载)Python学习–数据分析路线
- protobuf中文教程(第一篇)
- hdu1061Rightmost Digit
- 从程序员到CTO的Java技术路线图
- 【递推】【NOIP模拟】彩灯的问题 Lights
- 从源代码看TreeMap、HashMap、Hashtable、ConcurrentHashMap、LinkedHashMap特性
- 数据结构的二叉链表中序遍历
- 聊聊Android的缓存(一)
- ubuntu sublime 支持拼音输入的方法
- [Toddler's Bottle]-[fd]
- CodeForces 612C Replace To Make Regular Bracket Sequence
- 20150202--XML下02
- 四、类的结构及声明
- Java序列化与ProtocalBuffer序列化之深入分析(转)
- 一个较完整的网页布局
- 久违的锻炼
- PHP URL 重定向 的三种方法
- 白话空间统计二十一:密度分析(一)
- CodeForces 612B HDD is Outdated Technology