zoukankan      html  css  js  c++  java
  • 最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom

    最长上升子序列o(nlongn)写法

    dp[1]=a[1];
    int len=1;
    for(int i=1;i<=n;i++){
        if(a[i]>dp[len]) dp[++len]=a[i];
        else *lower_bound(dp+1,dp+1+len,a[i])=a[i];
    }

    数组dp[len]描述的是长度为len时的结尾的最小元素。

    怎么样才能不交叉呢?i和j相连,i1和j1想连,只有i<i1&&j<j1时才不会交叉,所以让第行单调递增,然后第而行求他的LIS

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=5E5+7;
    const int INF=1E9+7;
    struct stu{
        int a,b;
        bool friend operator <(const stu x,const stu y){
            return x.a<y.a;
        }
    }arr[N];
    int a[N],dp[N];
    int main(){
        ios::sync_with_stdio(0);
        int n;
        int t=0;
        while(cin>>n){
            for(int i=1;i<=n;i++) cin>>arr[i].a>>arr[i].b;
            sort(arr+1,arr+1+n);
            for(int i=1;i<=n;i++){
                a[i]=arr[i].b;
            }
            dp[1]=a[1];
            int len=1;
            for(int i=1;i<=n;i++){
                if(a[i]>dp[len]) dp[++len]=a[i];
                else *lower_bound(dp+1,dp+1+len,a[i])=a[i];
            }
            cout << "Case " << ++t << ":
    ";
            cout << "My king, at most " << len << (len > 1 ? " roads" : " road") << " can be built.
    
    ";
        }
        return 0;
    }

    注:sb了。为什么要用struct呢?还得排序,,,直接a[x]=y就行.....

  • 相关阅读:
    SPOJ ORDERSET
    BZOJ 1109: [POI2007]堆积木Klo
    BZOJ 1112: [POI2008]砖块Klo
    BZOJ 4144: [AMPPZ2014]Petrol
    BZOJ 4385: [POI2015]Wilcze doły
    BZOJ 1124: [POI2008]枪战Maf
    BZOJ 1123: [POI2008]BLO
    BZOJ 1121: [POI2008]激光发射器SZK
    BZOJ 1131: [POI2008]Sta
    BZOJ 4551: [Tjoi2016&Heoi2016]树
  • 原文地址:https://www.cnblogs.com/Accepting/p/12410110.html
Copyright © 2011-2022 走看看