CodeForces 612 D. The Union of k-Segments(水~)
来源:互联网 发布:大众软件 故事合集 编辑:程序博客网 时间:2024/05/17 04:23
Description
给出n个区间[li,ri]以及一个整数k,称一个数的令人满意的如果它至少被这n个区间中的k个区间覆盖,输出所有令人满意的数构成的区间
Input
第一行两个整数n和k,之后n行每行两个整数li和ri表示一个区间(1<=k<=n<=10^6,-10^9<=li<=ri<=10^9)
Output
输出所有令人满意的数构成的区间的数量和这些区间的端点
Sample Input
3 2
0 5
-3 2
3 8
Sample Output
2
0 2
3 5
Solution
简单题,每个区间左端点贡献为1,右端点贡献为-1,给区间按左右端点排序后从左到右累加贡献,贡献等于k时,当前区间左端点就是一个令人满意的区间左端点,之后一直往后找右端点使得贡献小于k即为右端点
Code
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define maxn 2222222struct node{ int v,k;}a[maxn];int k,n,l,r,res,ans[maxn][2];int cmp(node x,node y){ if(x.v!=y.v)return x.v<y.v; return x.k>y.k;}int main(){ while(~scanf("%d%d",&n,&k)) { for(int i=0;i<n;i++) { scanf("%d%d",&l,&r); a[2*i].v=l,a[2*i].k=1; a[2*i+1].v=r,a[2*i+1].k=-1; } sort(a,a+2*n,cmp); for(int i=1;i<2*n;i++)a[i].k+=a[i-1].k; res=0; for(int i=0;i<2*n;i++) if(a[i].k==k) { ans[res][0]=a[i].v; while(i<2*n&&a[i].k>=k)i++; if(a[i].k==k-1)ans[res++][1]=a[i].v; } printf("%d\n",res); for(int i=0;i<res;i++)printf("%d %d\n",ans[i][0],ans[i][1]); } return 0;}
0 0
- CodeForces 612 D. The Union of k-Segments(水~)
- Codeforces 612D The Union of k-Segments 【思维】
- Codeforces 612D The Union of k-Segments
- codeforces 612D The Union of k-Segments (sorting)
- CodeForces 612D The Union of k-Segments
- CodeForces 612D - The Union of k-Segments(模拟)
- Codeforces 612D The Union of k-Segments
- ★codeforces 612D The Union of k-Segments (思维or类扫描线)
- codeforces 612D 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
- codeforces 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
- Codeforces 612 D. The Union of k-Segments (非递归线段树+离散化)
- Educational Codeforces Round 4 612D The Union of k-Segments(stl)
- The Union of k-Segments CodeForces
- The Union of k-Segments CodeForces
- 绘画基础研究(一)-画面的对象组成与形状组成
- 如何获取 yum 安装的 rpm 包位置---详解
- C语言中的类型转换
- 猴子吃桃子的问题
- Android中使用OrmLite(一):表创建及增删改查
- CodeForces 612 D. The Union of k-Segments(水~)
- 菜鸟在线教你用Unity3D开发VR版的Hello World
- 代码
- spring学习笔记 - 面试题
- UVA 439 Knight Moves
- Openlayers离线加载天地图
- ZOJ 2110 Tempter of the Bone (DFS)
- Android 实现形态各异的双向侧滑菜单 自定义控件来袭
- Centos编译安装PHP7+Nginx1.9+MySQL5.7