P3084 [USACO13OPEN]照片Photo

题目描述

Farmer John has decided to assemble a panoramic photo of a lineup of his N cows (1 <= N <= 200,000), which, as always, are conveniently numbered from 1..N. Accordingly, he snapped M (1 <= M <= 100,000) photos, each covering a contiguous range of cows: photo i contains cows a_i through b_i inclusive. The photos collectively may not necessarily cover every single cow.

After taking his photos, FJ notices a very interesting phenomenon: each photo he took contains exactly one cow with spots! FJ was aware that he had some number of spotted cows in his herd, but he had never actually counted them. Based on his photos, please determine the maximum possible number of spotted cows that could exist in his herd. Output -1 if there is no possible assignment of spots to cows consistent with FJ's photographic results.

农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N。

于是约翰拍摄了M(1 <= M <= 100,000)张照片,每张照片都覆盖了连续一段奶牛:第i张照片中包含了编号\(a_i\)\(b_i\)的奶牛。但是这些照片不一定把每一只奶牛都拍了进去。

在拍完照片后,约翰发现了一个有趣的事情:每张照片中都有且仅有一只身上带有斑点的奶牛。约翰意识到他的牛群中有一些斑点奶牛,但他从来没有统计过它们的数量。 根据照片,请你帮约翰估算在他的牛群中最多可能有多少只斑点奶牛。如果无解,输出“-1”。

Input

输入输出格式

输入格式:

  • Line 1: Two integers N and M.

  • Lines 2..M+1: Line i+1 contains a_i and b_i.

输出格式:

  • Line 1: The maximum possible number of spotted cows on FJ's farm, or -1 if there is no possible solution.

输入输出样例

输入样例#1: 复制

5 3
1 4
2 5
3 4

输出样例#1: 复制

1

说明

There are 5 cows and 3 photos. The first photo contains cows 1 through 4, etc.

From the last photo, we know that either cow 3 or cow 4 must be spotted. By choosing either of these, we satisfy the first two photos as well.


正解dp,但是差分约束也可以做

假如\(f[i]\)表示前 \(i\)个奶牛中有\(f[i]\)只斑点奶牛,所以对于\(\forall i\)\(f[l_i]-f[r_i]=1\)
所以\(f[l_i]-f[r_i]\leq -1 \ |\ f[r_i]-f[l_i]\leq 1\) 且对于 \(i\in[2,n]\)\(f[i]-f[i-1]\leq 1 \ \ |\ \ f[i-1]-f[i]\leq1\)

虽然数据卡spfa,但是加上线段树优化跑的还是很快的

#include<iostream>
#include<cstdio>
#include<cstring>
#define update(now) c[now]=a[c[now*2]]<a[c[now*2+1]] ? c[now*2]: c[now*2+1]
using namespace std;

int i,m,n,j,k,a[1000001],x,y,ver[1000001],nex[1000001],head[1000001],cnt,edge[1000001],w[1000001],c[5000001],sum;
bool inq[1000001];

void built(int now,int l,int r)
{
    if(r==l) {c[now]=l; return ;}
    built(now*2,l,(l+r)/2);
    built(now*2+1,(l+r)/2+1,r);
    update(now);
}

void gai(int now,int l,int r,int w)
{
    if(r==l) { c[now]=w; return;}
    int mid=(l+r)/2;
    if(w<=mid) gai(now*2,l,mid,w);
    else gai(now*2+1,mid+1,r,w);
    
    update(now);
}

void add(int x,int y,int z)
{
    cnt+=1;
    ver[cnt]=y; nex[cnt]=head[x]; head[x]=cnt; edge[cnt]=z;
}

inline int gtt()
{
    char ch;
    int res=0;
    while(ch=getchar(),ch<'0'||ch>'9');
    res=ch-48;
    while(ch=getchar(),ch>='0'&&ch<='9')
    res=(res<<3)+(res<<1)+ch-48;
    return res;
}


int spfa()
{
    inq[0]=1;
    sum=1;
    while(sum)
    {
        int t=c[1]; 
        for(int i=head[t];i;i=nex[i])
            if(w[ver[i]]>w[t]+edge[i]) 
            {
                cnt+=1;
                if(cnt>=800000) return -1;
                if(!inq[ver[i]]) inq[ver[i]]=1, sum+=1;
                w[ver[i]]=w[t]+edge[i];
                a[ver[i]]=w[t]+edge[i];
                gai(1,0,n,ver[i]);
            }
        a[t]=0x3f3f3f3f; sum-=1; inq[t]=0;
        gai(1,0,n,t);
    }
    return w[n];
}

int main()
{
    scanf("%d%d",&n,&m);
    memset(w,0x3f,sizeof(w));
    memset(a,0x3f,sizeof(a));
    w[0]=0; a[0]=0;
    built(1,0,n);
    for(i=1;i<=m;i++) 
    {
        x=gtt();y=gtt();
        add(x-1,y,1);
        add(y,x-1,-1);
    }
    for(i=0;i<n;i++) 
        add(i+1,i,0),add(i,i+1,1);
    cnt=0;
    printf("%d\n",spfa());
}

猜你喜欢

转载自www.cnblogs.com/ZUTTER/p/9610207.html