zoukankan      html  css  js  c++  java
  • Constructing Roads In JGShining's Kingdom


    点击打开题目链接

      本题目是考察  最长递增子序列的  有n^2     n(logn)  n^2  会超时的

    下面两个方法的代码  思路  可以百度LIS  LCS

    dp里面存子序列


    n(logn)   代码

    <span style="font-size:18px;">#include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<stdlib.h>
    #define N 500000
    using namespace std;
    
    int road[N],dp[N],n,len;
    
    int two_part(int *a,int L,int R, int aim)  //二分法找更新的位置
    {
        int zz;
        if(len==1&&dp[1]==0) return 1;
        while(L<=R)
        {
            int mid=(L+R)/2;
            if(aim>a[mid]&&aim<a[mid+1]) return mid+1;
            else if(aim>a[mid])   L=mid+1;
            else if(aim<a[mid])  R=mid-1;
        }
        if(aim<=dp[1]) return 1;   //目标数字比第一个数小则更新dp[1]
        return ++len;  //找不到则在末尾更新
    }    
    int ans()
    {
        int i;
        for(i=1; i<=n; i++)  //把每个数字在dp数组中更新
        {
            int wz=two_part(dp,1,len,road[i]);
            dp[wz]=road[i];
            // print();
        }
        return len;
    }
    
    int main()
    {
        int t=1;
        while(scanf("%d",&n)!=EOF)
        {
            int i;
            len=1;
            memset(road,0,sizeof(road));
            memset(dp,0,sizeof(dp));
            for(i=1; i<=n; i++)
            {
                int ra,rb;
                scanf("%d%d",&ra,&rb);
                road[ra]=rb;
            }
            int answer=ans();
            // printf("%d
    ",ans());
            printf("Case %d:
    ",t++);
            if(answer==1) printf("My king, at most %d road can be built.
    
    ",answer);
            else  printf("My king, at most %d roads can be built.
    
    ",answer);
        }
        return 0;
    }</span>
    <span style="font-size:18px;">
    </span>


    n^2代码

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<stdlib.h>
    #define N 500000
    using namespace std;
    
    int road[N],dp[N],n;
    
    //int two_part(int *a,int L,int R, int aim)
    //{
    //    while(L<=R)
    //    {
    //        int mid=(L+R)/2;
    //        if(aim==a[mid]) return mid;
    //        else if(aim>mid)
    //        {
    //            L=mid+1;
    //        }
    //        else
    //        {
    //            R=mid-1;
    //        }
    //    }
    //    return -1;
    //}
    
    int ans()
    {
        int sum=0;
        int i,j;
        for(i=0;i<n;i++)
        {
            j=0;
            while(1)
            {
                if(road[i]<dp[j]||!dp[j])
                {
                    dp[j]=road[i];
                    break;
                }
                j++;
            }
        }
        for(i=0;i<n;i++)
           if(dp[i]) sum++;
        return sum;
    }
    
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            int i;
            memset(road,0,sizeof(road));
            memset(dp,0,sizeof(dp));
            for(i=0;i<n;i++)
            {
                int ra,rb;
                scanf("%d%d",&ra,&rb);
                road[ra]=rb;
            }
            int answer=ans();
           // printf("%d
    ",ans());
            if(answer==1) printf("My king, at most %d road can be built.
    
    ",answer);
            else  printf("My king, at most %d roads can be built.
    
    ",answer);
        }
        return 0;
    }



  • 相关阅读:
    Hadoop 启动脚本分析与实战经验
    mac appium-Andriod sdk安装
    windows解决appium-doctor报gst-launch-1.0.exe and/or gst-inspect-1.0.exe cannot be found
    windows解决appium-doctor报opencv4nodejs cannot be found
    windows解决appium-doctor报 mjpeg-consumer cannot be found.
    windows解决appium-doctor报ffmpeg cannot be found问题
    windows解决appium-doctor报 bundletool.jar cannot be found
    内存分配和垃圾回收调优
    内存分配与回收策略
    JVM老年代和新生代的比例
  • 原文地址:https://www.cnblogs.com/coded-ream/p/7208026.html
Copyright © 2011-2022 走看看