[USACO13OPEN] Photo

This is a great dynamic programming problem

Description

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.

Input

  • Line 1: Two integers \(N\)and \(M\).

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

Output

  • Line 1: The maximum possible number of spotted cows on FJ's farm, or \(-1\) if there is no possible solution.
Examples of input and output

Input #1
5 3
1 4
2 5
3 4
Output #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.




The solution of the problem

See discussion board said differential constraint can be done, butBecause it is not the reasonI had no choice differential constraints.

Recap subject to the effect, given a number of intervals: \ (R_i = [L_i, R_i] \) , each section has one and only one point. Q. up to a few points.


emmmmmmmmmmm ......
easy to get the state transition equation:

(In fact, that is not really easy, I would like to not know how long ...... finally peeked a few lines of explanations)
\ [F [i] = f_ {max} [j] + 1, j \ in \ { certain conditions \} \]
where, \ (F [I] \) represents (I \) \ placed at a point , a count this point, the \ (1 \) to \ (I \) position to place up to several points.



Straightforward equation, the problem is that this "certain conditions."

Consider "certain conditions," the two endpoints.


\ (1 ^ {\ circ} \) on the left where most go away?
We want \ (j \) values as small as possible, but not too small. For example, the \ (i \) to \ (j \) actually crossed an entire range, which is impossible. Because the definition of the state transition equation, \ ((J, I] \) intermediate point is not placed.
Therefore,probablyThis point can know the value of the left can go to a minimum:

  1. Does not cover \ (I \) a;
  2. A left end point value as large as possible;

The left point range. It denoted \ (MaxL [i] \) .


\ (2 ^ {\ circ} \) where the far right away?
We want \ (j \) value the bigger the better, but not too much. For example, the \ (i \) to \ (j \) is actually in the same range, this is impossible. Because the same interval has one and only one point.
and so,probablyThis value may know the right spot can go to a maximum of:

  1. Covering \ (I \) a;
  2. A left end point value as small as possible;

Position and then to the left of the left end point of a unit interval. It referred to as \ (minL [I] \) .

About \ (maxl [i] \) and \ (minl [i] \) method of seeking, to recurrence.

Read into \ ([L_i, R_i] \ ) when:

    minl[r]=min(minl[r],l);
    maxl[r+1]=max(maxl[r+1],l);

Then scan:

    for(int i=1;i<=n+1;i++)
        maxl[i]=max(maxl[i-1],maxl[i]);
    for(int i=n ;i>=0;i--)
        minl[i]=min(minl[i+1],minl[i]);

Because every time we are taking \ ([maxl [i], minl [i]) \) the maximum state transition, also noted \ (maxl [] \) and \ (minl [] \) is incremented sequence;

This very happy, directly on the monotonous queue optimization.

On illegal situation, i.e., be transferred to the \ (f [i] \) a \ (J \) does not exist, i.e., queue is empty, then the \ (F [I] = -. 1 \) ;

Finally, get hold of a fake point \ (the n-+ 1 \) , pay attention to this point it is time to leave again so the state can not transfer +1.

The final state transition equation:
\ [F [I] = F [Q.front ()] + (! (N-I + ==. 1)), \ Q.front () \ IS \ The \ maximum \ value \ in [maxl [i], minl [ i]). \]

* About the state transition equation In fact, I still doubt. In the evaluation OJ written as \ (f [i] = f [Q.back ()] + (! (I == n + 1)) \) it is also possible in the AC. This may be a silly question, but be sure people know the truth of the message.

Codeword tired ah ......




code:

#include<bits/stdc++.h>
using namespace std;
const int N=200005,INF=0x3f3f3f3f,M=100005;
int maxl[N],minl[N],f[N];
int tot=0,ans=0;
int n,m;
bool ban[N];
deque<int> Q;
inline int read()
{
    char c=getchar();int x=0;
    for(;!isdigit(c);c=getchar());
    for(;isdigit(c);c=getchar())
        x=x*10+c-'0';
    return x;
}


int main()
{
    n=read();m=read();
    for(int i=1;i<=n+1;i++)minl[i]=i;
    for(int i=1;i<=m;i++)
    {
        int l=read();
        int r=read();
        minl[r]=min(minl[r],l);
        maxl[r+1]=max(maxl[r+1],l);
    }
    for(int i=1;i<=n+1;i++)
        maxl[i]=max(maxl[i-1],maxl[i]);
    for(int i=n ;i>=0;i--)
        minl[i]=min(minl[i+1],minl[i]);
    
    int j=1;
    Q.push_back(0);
    for(int i=1;i<=n+1;i++)
    {
        for(;j<minl[i];j++)
        {
            if(f[j]!=-1)
            {
                while(!Q.empty()&&f[Q.back()]<f[j])Q.pop_back();
                Q.push_back(j);
            }
        }
        while(!Q.empty()&&Q.front()<maxl[i])Q.pop_front();
        if(!Q.empty())
            f[i]=f[Q.front()]+(!(i==n+1));
        else
            f[i]=-1;
    }
    cout<<f[n+1];
    return 0;
}

Guess you like

Origin www.cnblogs.com/kion/p/11819379.html