Educational Codeforces Round 4 D 扫描线思想
来源:互联网 发布:天正不能访问到网络锁 编辑:程序博客网 时间:2024/05/22 13:32
链接:戳这里
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个线段[l,r],问哪些区间正好被线段覆盖了k次。输出这样的区间个数,以及区间
思路:
对于每一条线段,l即为进入,r即为出去。如果当前l进入了之后,使得区间覆盖值==k,那么这个l肯定是可以匹配相应的一个r的。对于r出去以后,相应的之前肯定存在一个l与之匹配
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define mst(ss,b) memset((ss),(b),sizeof(ss))///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double ld;#define INF (1ll<<60)-1#define Max 1e9using namespace std;vector<pair<ll,int> > V;vector<ll> anw;int n,k;int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++){ ll l,r; scanf("%I64d%I64d",&l,&r); V.push_back(make_pair(l,-1)); V.push_back(make_pair(r,1)); } sort(V.begin(),V.end()); int num=0; for(int i=0;i<V.size();i++){ if(V[i].second==-1){ num++; if(num==k) anw.push_back(V[i].first); } else { if(num==k) anw.push_back(V[i].first); num--; } } printf("%d\n",anw.size()/2); for(int i=0;i<anw.size();i+=2) printf("%I64d %I64d\n",anw[i],anw[i+1]); return 0;}
0 0
- Educational Codeforces Round 4 D 扫描线思想
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- Codeforces Educational Rouund #4 D (扫描线)
- Educational Codeforces Round 4 D. Array GCD
- Educational Codeforces Round 21-D
- Educational Codeforces Round 5 D
- Educational Codeforces Round 26 D
- Educational Codeforces Round 26 D Round Subset
- Educational Codeforces Round 4 D. The Union of k-Segments
- Educational Codeforces Round 4-D. The Union of k-Segments
- Codeforces Educational Round 2 D题
- Educational Codeforces Round 10 D. Nested Segments
- Educational Codeforces Round 10 D Nested Segments
- Educational Codeforces Round 10 D.Nested Segments
- Educational Codeforces Round 12 D. Simple Subset
- Educational Codeforces Round 12 D. Simple Subset
- Educational Codeforces Round 15 D 数学推导
- Educational Codeforces Round 11 D 计算几何
- 【杭电1285】确定比赛名次
- 省市县联动 php处理 生成js文件 并且jquery 调用
- 人生经验 C#图解教程 学后感悟总结
- HDU:2647 Reward(链表型拓扑排序)
- (二)创建,修改,删除表空间
- Educational Codeforces Round 4 D 扫描线思想
- Spring框架——AOP前置、后置、环绕、异常通知
- 【杭电2647】Reward
- 查看spark on yarn的日志和程序状态的方法
- 1
- 一个零基础体验scala的隐式转换
- 数据结构与算法大全 目录索引
- 指针学习笔记
- poj1840 Eqs