【Luogu3602】Koishi Loves Segments(贪心)

题面

洛谷

题解

离散区间之后把所有的线段挂在左端点上,从左往右扫一遍。

对于当前点的限制如果不满足显然会删掉右端点最靠右的那根,拿一个堆维护一下就好了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 400400
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int n,m,ans,S[MAX<<1],tot,L[MAX],R[MAX],P[MAX],X[MAX];
int Bound(int x){return lower_bound(&S[1],&S[tot+1],x)-S;}
int c[MAX<<1],lim[MAX<<1];
vector<int> lk[MAX<<1];
struct Heap
{
int H[MAX],top;
void push(int x){H[++top]=x;push_heap(&H[1],&H[top+1]);}
void pop(){pop_heap(&H[1],&H[top--]);}
int Top(){return H[1];}
}Q;
int main()
{
n=read();m=read();
for(int i=1;i<=n;++i)L[i]=read(),R[i]=read(),S[++tot]=L[i],S[++tot]=R[i];
for(int i=1;i<=m;++i)P[i]=read(),X[i]=read(),S[++tot]=P[i];
sort(&S[1],&S[tot+1]);tot=unique(&S[1],&S[tot+1])-S-1;
for(int i=1;i<=n;++i)lk[Bound(L[i])].push_back(Bound(R[i]));
for(int i=1;i<=tot;++i)lim[i]=1e9;
for(int i=1;i<=m;++i)P[i]=Bound(P[i]),lim[P[i]]=min(lim[P[i]],X[i]);
for(int i=1,t=0;i<=tot;++i)
{
t+=c[i];
for(int r:lk[i])c[r+1]-=1,t+=1,Q.push(r),++ans;
while(t>lim[i])c[Q.Top()+1]+=1,t-=1,Q.pop(),--ans;
}
printf("%d\n",ans);
return 0;
}

【Luogu3602】Koishi Loves Segments(贪心)的更多相关文章

  1. D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

    题目描述 Koishi喜欢线段. 她的条线段都能表示成数轴上的某个闭区间.Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了. Flandre看她和线段玩得很起开心,就抛给她一个 ...

  2. 洛谷P3602 Koishi Loves Segments 贪心

    正解:贪心 解题报告: 传送门! 首先在学习贪心的入门题的时候我们就知道,当x=1的时候,也就是每条线段不能相交的时候的做法——就按右端点排序然后能选就选,也就是会议安排问题,原因显然?就你选右端点更 ...

  3. 洛谷P3602 Koishi Loves Segments(贪心,multiset)

    洛谷题目传送门 贪心小水题. 把线段按左端点从小到大排序,限制点也是从小到大排序,然后一起扫一遍. 对于每一个限制点实时维护覆盖它的所有线段,如果超过限制,则贪心地把右端点最大的线段永远删去,不计入答 ...

  4. LuoguP3602 Koishi Loves Segments

    题面 n个区间和数轴上的m个关键点 (0<=n,m<=4*1e5,数轴范围 \(-1^7\) ~ \(1^7\))每个关键点有被区间区间覆盖的次数上限,求最多能放多少个区间到数轴上 传送门 ...

  5. Luogu P3602 Koishi Loves Segments

    传送门 题解 既然是选取区间,没说顺序 肯定先排遍序 都是套路 那么按什么排序呢??? 为了方便处理 我们把区间按左端点从小到大排序 把关键点也按从小到大排序 假设当扫到 \(i\) 点时,i 点之前 ...

  6. 【题解】P3599 Koishi Loves Construction

    [题解]P3599 Koishi Loves Construction \(\mod n\) 考虑如何构造,发现\(n\)一定在第一位,不然不行.\(n\)一定是偶数或者是\(1\),不然 \(n|\ ...

  7. 题解-Koishi Loves Construction

    题解-Koishi Loves Construction 前缀知识 质数 逆元 暴搜 Koishi Loves Construction 给定 \(X\),\(T\) 组测试数据,每次给一个 \(n\ ...

  8. CF447B DZY Loves Strings 贪心

    DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...

  9. HDU 4882 ZCC Loves Codefires(贪心)

     ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. Python_迭代器、生成器、列表推导式,生成器表达式

    1.迭代器 (1)可迭代对象 s1 = ' for i in s1: print(i) 可迭代对象 示例结果: D:\Python36\python.exe "E:/Python/课堂视频/ ...

  2. apply和call方法

    真伪数组转换 /* apply和call方法的作用: 专门用于修改方法内部的this 格式: call(对象, 参数1, 参数2, ...); apply(对象, [数组]); */ function ...

  3. react 路由 react-router-dom

    import React from 'react'; import DataList from './data' import Tr from './Tr' // import One from '. ...

  4. [转帖]Centos7 yum安装Chrome浏览器

    Centos7 yum安装Chrome浏览器 https://www.cnblogs.com/ianduin/p/8727333.html以及https://blog.csdn.net/libaine ...

  5. 372.Definition of ListNode

    单项列表只能把后一个node中的所有数据copy到当前node再delete后一node. /** * Definition of ListNode * class ListNode { * publ ...

  6. 基于vue-cli,sass,vant的移动端项目

    项目架构 开始 vue init webpack    项目名称         //新建项目,cd进入新项目 npm install axios                    //先安装! ...

  7. Python时间的简单使用

    1.time.strptime(string[, format]),string -- 时间字符串.format -- 格式化字符串.返回struct_time对象.     把字符串转换为时间格式, ...

  8. C#中decimal,double和float的区别

    float 单精度浮点 32bit,double 双精度浮点64bit,decimal是高精度 128bit,浮点型.float double 是 基本类型(primitive type),decim ...

  9. 小米Note3 MIUI9可以用的XPosed框架

    资源来自论坛里的:http://www.miui.com/thread-6449305-1-1.html 首先需要刷入TWRP,资源在这个帖子里:http://www.miui.com/thread- ...

  10. 9.Pod控制器概念和基本操作2

    利用一个简单的例子来启动一个deployment的Pod控制器 [root@master song]# cat deploy.yml apiVersion: apps/v1 kind: Deploym ...