Luo Gu topic - Linear Dynamic Programming

P1020 missile interceptor (the longest sequence rise / fall maximum length sequence - O (N 2 ) to O (NlogN) approach)

Meaning of the questions:

There are n number of missiles flying at different heights, an interceptor missile can intercept some missiles are highly rising, ask an interceptor missile can intercept the maximum number of missiles, missile intercept all interceptor missiles to the number of stars

Ideas:

The first question: a missile interceptor can intercept the maximum number of missiles, this is the longest a request did not fall subsequence problem

Consider O (N 2 ) approach, for each missile, each of our previous missile forward traversal, if the height is less than or equal to, can try to update dp [i] = max (dp [i], dp [j] +1)

dp [i] of each element is initialized to 1

Let's look traverse before O (nlogn) approach, the team moved forward approach each element here, we can choose to optimize

Establishing a tree array, made by numerical subscripts, maintain the maximum length, from the forward cycle, the number of the tree has been placed inside the array before each query to this number does not rise up to the end of the sequence length maximum, then this maximum length + 1 as the longest sequence does not rise to its own end, inside the array into tree

The second question: How many missiles to ask a minimum of

To use a Dilworth Theorem

Dilworth theorem: dividing the minimum number of anti-chain Poset equal to the length of the longest chain

We are simply seeking a rise longest sequence, and try to prove it simple

(1) Assuming that the missile hit the method is such that: a take any missile, the missile from the missile begins to play all finished. These missiles all referred to the same group, and then optionally repeating the above steps in a down missile did not play until all missiles kick

(2) Suppose we obtained a minimum division K groups missile, from a (1 <= a <= K) group Missile either take a missile, it should be possible to find a height of the missile than the missile from a + 1 group high (because if not found, then it is more than a + 1 group on arbitrary a higher conductivity, should put a + lay together in a group playing the first group of missiles rather than all the other classified as a group 1 + ), the same group from a + 1 a + 2 set to true. Then you can find in each group from front to back in a missile up even higher, even as a rising sequence, which is the length K

(3) increased provided the longest sequence length is P, there are K <= P; increased and because the longest sequence in any of the two groups are not the same (or is not satisfied monotonically liters), there are P> = K , so K = P

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define lowbit(x) (x&(-x))
 using namespace std;
 const int maxn=100100;
 int n=0,len;
 int a[maxn],mx[maxn];
 void add(int x,int val)
 {
     while(x<=maxn){
         mx[x]=max(mx[x],val);
         x+=lowbit(x);
     }
 }
 int query(int x)
 {
     int ans=0;
     while(x>=1){
         ans=max(mx[x],ans);
         x-=lowbit(x);
     }
    return ans;
 }
 int main()
 {
     while(scanf("%d",&a[++n])!=EOF); n--;
     int ans=0;
     for(int i=n;i>=1;i--){
         int x=query(a[i])+1;
        add(a[i],x);
        ans=max(ans,x); 
     }
     cout<<ans<<endl;
     memset(mx,0,sizeof(mx));
     ans=0;
     for(int i=1;i<=n;i++){
         int x=query(a[i]-1)+1;
         add(a[i],x);
         ans=max(ans,x);
     }
    cout<<ans<<endl;
     return 0;
 }
View Code

 

Guess you like

Origin www.cnblogs.com/overrate-wsj/p/12313541.html