题目描述

Koishi喜欢线段。

她的D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]条线段都能表示成数轴上的某个闭区间D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]。Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了。

Flandre看她和线段玩得很起开心,就抛给她一个问题:

数轴上有D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]个点突然兴奋,如果自己被身上覆盖了超过D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]条线段,这个点就会浑身难受然后把Koishi批判一番。

Koishi十分善良,为了不让数轴上的点浑身难受,也为了让自己开心,她想在数轴上放入尽量多的线段。

按照套路,Koishi假装自己并不会做这道题,所以她就来求你帮忙。并承诺如果你解决了问题就给你打一通电话w。

输入输出格式

输入格式:

 

第一行两个个整数D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆],分别表示插入的线段数和关键点数。

接下来D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]行,每行两个整数D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆],表示线段D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]的端点。

接下来D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]行,每行两个整数D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆],表示有个位于D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]的点突然兴奋,并认为自己身上不得覆盖超过D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]条线段

 

输出格式:

 

一个整数,表示最多能放入的线段数

 

输入输出样例

输入样例#1:
4 3
1 3
2 4
5 7
6 8
2 5
3 1
6 2
输出样例#1:
3

说明

对于20%的数据,满足D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

对于60%的数据,满足D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

对于80%的数据,满足D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

对于100%的数据,满足D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

如果一个点兴奋了两次,那么Koishi应当满足它的*较严苛的要求*(也就是D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]相同时D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]取最小值啦)

请适当使用读入优化


 

比赛时交了一个网络流60分

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=1005,M=1e6,INF=1e9;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int n,m,s,t;
struct rec{
    int l,r;
}a[N];
struct point{
    int p,k;
    bool operator <(const point a)const{
        if(p==a.p) return k<a.k;
        else return p<a.p;
    }
}b[N];

struct edge{
    int v,c,f,ne;
}e[M<<1];
int cnt,h[N];
inline void ins(int u,int v,int c){
    cnt++;
    e[cnt].v=v;e[cnt].c=c;e[cnt].f=0;e[cnt].ne=h[u];h[u]=cnt;
    cnt++;
    e[cnt].v=u;e[cnt].c=0;e[cnt].f=0;e[cnt].ne=h[v];h[v]=cnt;
}
int cur[N],d[N],vis[N];
int q[N],head,tail;
bool bfs(){
    head=tail=1;
    memset(vis,0,sizeof(vis));
    d[s]=1;vis[s]=1;q[tail++]=s;
    while(head!=tail){
        int u=q[head++];
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v;
            if(!vis[v]&&e[i].c>e[i].f){
                vis[v]=1;d[v]=d[u]+1;
                q[tail++]=v;
                if(v==t) return true;
            }
        }
    }
    return false;
}
int dfs(int u,int a){
    if(u==t||a==0) return a;
    int flow=0,f;
    for(int &i=cur[u];i;i=e[i].ne){
        int v=e[i].v;
        if(d[v]==d[u]+1&&(f=dfs(v,min(e[i].c-e[i].f,a)))>0){
            flow+=f;
            e[i].f+=f;
            e[((i-1)^1)+1].f-=f;
            a-=f;
            if(a==0) break;
        }
    }
    if(a) d[u]=-1;
    return flow;
}
int dinic(){
    int flow=0;
    while(bfs()){
        for(int i=s;i<=t;i++) cur[i]=h[i];
        flow+=dfs(s,INF);
    }
    return flow;
}
//int Bin(int v){
//    int l=1,r=m;
//    while(l<r){
//        int mid=(l+r)>>1;
//        if(v<=b[mid].p) r=mid;
//        else if(v>b[mid].p) l=mid+1;
//    }
//    return l;
//}


void buildGraph(){
    for(int i=1;i<=m;i++) ins(n+n+i,n+n+m+i,b[i].k);
    for(int i=1;i<=n;i++){
        ins(s,i,1);ins(n+i,t,1);
        int now=i;
        for(int j=1;j<=m;j++){
            if(b[j].p<a[i].l) continue;
            if(b[j].p>a[i].r) break;
            ins(now,n+n+j,1);
            now=n+n+m+j;
        }
        ins(now,n+i,1);
    }
}
void getMP(){
    sort(b+1,b+1+n);
    int p=0;b[++p]=b[1];
    for(int i=2;i<=m;i++)
        if(b[i].p!=b[i-1].p) b[++p]=b[i];
    m=p;
}
int main(int argc, const char * argv[]){
    n=read();m=read();
    for(int i=1;i<=n;i++) a[i].l=read(),a[i].r=read();
    for(int i=1;i<=m;i++) b[i].p=read(),b[i].k=read();
    getMP();s=0;t=n+n+m+m+1;
    buildGraph();
    printf("%d",dinic());
    return 0;
}
网络流

相关文章:

  • 2021-11-12
  • 2022-12-23
  • 2021-06-27
  • 2022-12-23
  • 2021-09-30
  • 2021-08-20
  • 2021-10-26
  • 2021-07-22
猜你喜欢
  • 2021-11-11
  • 2021-09-27
  • 2022-02-13
  • 2021-09-06
  • 2021-06-26
  • 2021-08-27
  • 2021-10-16
相关资源
相似解决方案