CF-612D The Union of k-Segments 差分
D. The Union of k-Segments
题意
给出n个线段,以及一个数字k,让求出有哪些线段:线段上所有的点至少被覆盖了k次。
思路
假如忽略掉线段的左右端点范围,肯定是使用差分来维护每个点被覆盖的次数,遍历一遍统计区间。
对于这题,可以发现所有被覆盖k次以及以上的线段,端点肯定是已知的端点。
所以思路就和差分差不多,我们把所有的端点按坐标小到大排序,左端点赋值+1,右端点赋值为-1。如果坐标相同+1排在前面,开始累加,第一个==k的就是一个区间的起点,第一个<k的就是一个区间的终点。
代码
#include<bits/stdc++.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<string>
#include<math.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double eps=1e-14;
struct note
{
int pos,val;
note(int a,int b):pos(a),val(b){}
note(){}
bool operator <(const note&a)const
{
if(pos==a.pos) return val>a.val;
return pos<a.pos;
}
}arr[N*2];
int aga[N*2],en[N*2],tot;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
int l,r;
scanf("%d%d",&l,&r);
arr[i]=note(l,1);
arr[n+i]=note(r,-1);
}
sort(arr+1,arr+1+2*n);
int num=0,flag=0;
for(int i=1;i<=2*n;i++)
{
num+=arr[i].val;
if(num==k&&flag==0)
{
flag=1;
aga[++tot]=arr[i].pos;
}
else if(flag&&num<k)
{
en[tot]=arr[i].pos;
flag=0;
}
}
printf("%d\n",tot);
for(int i=1;i<=tot;i++)
printf("%d %d\n",aga[i],en[i]);
return 0;
}
CF-612D The Union of k-Segments 差分的更多相关文章
- codeforces 612D The Union of k-Segments (线段排序)
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- [CF 276C]Little Girl and Maximum Sum[差分数列]
题意: 给出n项的数列A[ ], q个询问, 询问 [ l, r ] 之间项的和. 求A的全排列中该和的最大值. 思路: 记录所有询问, 利用差分数列qd[ ], 标记第 i 项被询问的次数( 每次区 ...
- 612D The Union of k-Segments
传送门 题目大意 给定n个区间,问你被覆盖至少k次的区间(两端连续区间可以合并)最少有多少个,并输出. 分析 大水题呀QwQ,只需要将每个点的位置及它是左端点还是右端点这两个信息存起来然后进行一些简单 ...
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
D. The Union of k-Segments You re given n segments on the coordinate axis Ox and the number k. The ...
- Linq无聊练习系列6--Any/All/Contains/Concat/Union/Intersect/Except/take/skip/SqlMethods操作练习
/*********************Any/All/Contains/Concat/Union/Intersect/Except/take/skip/SqlMethods操作练习******* ...
- CF 441E Valera and Number
CF 441E Description 一共执行\(k\)次,每次有\(p\%\)把\(x * 2\),有\((100 - p)\%\)把\(x + 1\).问二进制下\(x\)末尾期望\(0\)的个 ...
- 差分形式的牛顿插值法(c++)
本程序对cosx函数进行插值,取步长为0.1,因此x的值为0.00,0.10,0.20,0.30,对应的y值为cos(0.00),cos(0.10),cos(0.20),cos(0.30),其实本程序 ...
- ZZNU-OJ-2098 : Drink coffee【线段树合并区间或者 差分 + 二分索引树】
: Drink coffee 时间限制: Sec 内存限制: MiB 提交: 答案正确: 提交 状态 讨论区 题目描述 为了在上课时保持清醒,凯伦需要一些咖啡.咖啡爱好者凯伦想知道最佳的温度来冲煮完美 ...
- LuoguP5488 差分与前缀和
题意 给定一个长为\(n\)的序列\(a\),求出其\(k\)阶差分或前缀和.结果的每一项都需要对\(1004535809\)取模. 打表找规律 先看前缀和,设\(n=5\),\(k=4\),按照阶从 ...
- 特殊的ASCII码对应的字符
Special Characters " " " quotation mark u+0022 ISOnum p:before { content:"\0022& ...
随机推荐
- Eugene and an array CodeForces - 1333C (思维)
题目大意:求好数组的个数,所谓好数组 1好数组是原数组的一段连续的子数组,2 好数组不包含元素和为0的子数组. 题解:唉,这个题目把我给些懵了....我一开始的想法求后缀和,保存位置,然后枚举前缀和, ...
- Cyclic Nacklace 杭电3746
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- copy模块中的copy与deepcopy的区别
前言 每空闲下来,就觉得以前写的博客很low........也许现在也很low~~~~好吧就当升级版的low吧~~~~ 如果要了解copy与deepcopy的区别,就需要了解Python的存储机制:P ...
- 玩转控件:Fucking ERP之流程图
前言 首先,跟守护在作者公众号和私信作者催更的朋友们道个歉.疫情的原因,公司从年初到现在一直处于996+的高压模式,导致公众号更新频率较低.而且作者每更新一篇原创公众号,既要对自己沉淀知识负责,也要对 ...
- java-锁膨胀的过程
先来看个奇怪的demo public class A { int i=0; // boolean flag =false; public synchronized void parse(){ i++; ...
- react: nextJs koa project basic structure
1.init nextJs project npm init npm install react react-dom next config script in package.json " ...
- 从一个数对列中找出素数对用于RAS加密的C++程序
#include <iostream> #include <cmath> using namespace std; const int n = 10000; int isPri ...
- Linux网络服务第五章NFS共享服务
1.笔记 NFS一般用在局域网中,网络文件系统c/s格式 服务端s:设置一个共享目录 客户端c:挂载使用这个共享目录 rpc:111远程过程调用机制 Showmount -e:查看共享目录信息 def ...
- Pytorch中自定义神经网络卷积核权重
1. 自定义神经网络卷积核权重 神经网络被深度学习者深深喜爱,究其原因之一是神经网络的便利性,使用者只需要根据自己的需求像搭积木一样搭建神经网络框架即可,搭建过程中我们只需要考虑卷积核的尺寸,输入输出 ...
- values/colors.xml
<color name="abcd">#FFC4C4C4</color> <color name="white">#FFFF ...