1 条题解

  • 0
    @ 2021-6-14 23:29:08

    C :

    #include<stdio.h>
    int main()
    {int a=getchar();if (a=='3') printf("5\n2\n");if (a=='1') printf("1\n7\n");if (a=='4') printf("7\n4\n");if (a=='2') printf("5\n8\n");return 0;}
    

    C++ :

    #include <iostream>
    #include <cassert>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <time.h>
    using namespace std;
    
    #define MAX 100000
    //最长降序列,最长升序列
    //389 207 155 300 299 170 158 65
    // 1   2   3   2   3   4   5   6
    // 1   1   1   2   2   2   2   1
    int T[MAX] = {0};
    int DP[MAX] = {0};
    int main()
    {
        int len=0;
        while(scanf("%d",&T[len]) != EOF) 
            ++len;  
        DP[0] = 1;
        int down_max = DP[0];
        for(int i=1; i<len; ++i)
        {
            //DP[i] = max{DP[k]+1,if(T[k]>T[i] && k<i)}
            int max_index = i; 
            for(int k=0; k<i; ++k)
                if(T[k]>T[i] && 
                        (max_index==i || DP[k]>DP[max_index]))
                    max_index = k;
            if(max_index == i)
                DP[i] = 1;
            else
                DP[i] = DP[max_index] + 1;
            if(DP[i] > down_max)
                down_max = DP[i];
        }
        DP[0] = 1;
        int up_max = DP[0];
        for(int i=1; i<len; ++i)
        {
            //DP[i] = max{DP[k]+1,if(T[k]<T[i] && k<i)}
            int max_index = i; 
            for(int k=0; k<i; ++k)
                if(T[k]<T[i] && 
                        (max_index==i || DP[k]>DP[max_index]))
                    max_index = k;
            if(max_index == i)
                DP[i] = 1;
            else
                DP[i] = DP[max_index] + 1;
            if(DP[i] > up_max)
                up_max = DP[i];
        }
        printf("%d\n%d\n",down_max,up_max);
        return 0;
    }
    
    

    Java :

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    public class Main{
    	public static void main(String[] args) throws IOException {
    //		Scanner sc = new Scanner(System.in);
    //		String str = sc.nextLine();
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		String str = br.readLine();
    		br.close();
    		deal(str);
    		
    	}
    	public static void deal(String str) {
    		if(str.equals(""))
    			return ;
    		String[] arr = str.split(" ");
    		int arrLen = arr.length;
    		int [] num1 = new int[arrLen];
    		int [] num2 = new int[arrLen];
    		int [] num3 = new int[arrLen];
    		
    		for(int i = 0; i < arrLen; i ++) {
    			num1[i] = Integer.parseInt(arr[i]);
    			num2[i] = 1;num3[i] = 1;
    		}
    		
    		int max = Integer.MIN_VALUE;
    		int min = Integer.MIN_VALUE;
    		for(int i = 0; i < arrLen; i ++) 
    			for(int j = 0; j < i; j ++) {
    				if(num1[i]<num1[j] && num2[i]<num2[j]+1) 
    					num2[i] = num2[j]+1;
    				max = Math.max(max, num2[i]);
    			}
    		
    		for(int i = 0; i < arrLen; i ++) 
    			for(int j = 0; j < i; j ++) {
    				if(num1[i]>num1[j] && num3[i]<num3[j]+1) 
    					num3[i] = num3[j]+1;
    				min = Math.max(min, num3[i]);
    			}
    		
    		System.out.println(max);
    		System.out.println(min);
    	}
    }
    
    
    • 1

    信息

    ID
    195
    时间
    1000ms
    内存
    125MiB
    难度
    10
    标签
    递交数
    3
    已通过
    3
    上传者