Cattle off winter 6-C | binary optimization dp, LIS

Thinking

The maximum length sequence increase
DP [i] expressed in the sub-sequence does not rise up to the end of the i-th element of
the transfer equation dp [i] = max (dp [i], dp [j] + 1: if (h [j ]> [i i-1 h ]) j∈1 ~
optimization ideas: the value of dp is the same, a relatively large number (here refers y) is clearly better, as far as possible to the front so that the large number of
all the lead-d array may be used to decrease monotonically bipartite

Examples The following diagram complement the introduction of LIS-half optimized: reprint garlic passenger count

Code

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100010;
int n;

struct node{
    int x,y;
    int pos;
}a[maxn];
int len = 0;
int d[maxn];
int b[maxn];

bool cmp(node u,node v){
    if(u.x == v.x) return u.y < v.y;
    return u.x < v.x;
}

int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].x>>a[i].y;
        a[i].pos = i;
    } 
    sort(a+1,a+n+1,cmp);
    d[++len] = a[1].y;
    b[a[1].pos] = 1;
    for(int i=2;i<=n;i++){
        if(a[i].y < d[len]) d[++len] = a[i].y,b[a[i].pos] = len;
        else{
            //找第一个小于的 
            int pos = upper_bound(d+1,d+len+1,a[i].y,greater<int>()) - d;
            d[pos] = a[i].y;
            b[a[i].pos] = pos;
        }
    }
    cout<<len<<endl;
    for(int i=1;i<=n;i++) cout<<b[i]<<" ";
    return 0;
} 

Guess you like

Origin www.cnblogs.com/fisherss/p/12329747.html