区间 桂林电子科技大学第三届ACM程序设计竞赛

链接:https://ac.nowcoder.com/acm/contest/558/E
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述 
小猫在研究序列。
小猫在研究单调性。
给定一个长度为N的序列a1,a2,…,aN,请你选出一个最长的区间[l,r](1≤l≤r≤N),满足al≤al+1≤…≤ar。
如果有多个,请输出l最小的。

输入描述:

第一行一个正整数T,表示数据组数。

每组数据的第一行一个正整数N。

接下来一行N个正整数a1,a2,…,aN。

输出描述:

T行,每行两个正整数l,r,表示选出的区间。
示例1

输入

4
5
1 2 3 4 5
5
5 4 3 2 1
5
5 3 4 1 2
5
3 4 5 1 2

输出

1 5
1 1
2 3
1 3

备注:

1≤T,N,ai≤1000

求 array[] 序列的单调递增连续最长子序列, A[i] 表示以 array[i] 结尾的最长递增连续子序列的长度。

Accept
import java.util.Scanner;

public class Main {
    
    public static int[] findLengthOfLCIS(int[] nums) {
        int len = nums.length;
        if(len==0) return nums;
        int[] a = new int[len];
        a[0] = 1;
        for(int i=1;i<len;i++){
            if(nums[i]>=nums[i-1]){
                a[i] = a[i-1]+1;
            } else {
                a[i] = 1;
            }
        }
        return a;
    }
    
    public static void main(String[] args) {
        Scanner reader=new Scanner(System.in);
        int T=reader.nextInt();
        while(T>=1){
            int N=reader.nextInt();
            int array[]=new int[N];
            for(int i=0;i<array.length;i++){
                array[i]=reader.nextInt();
            }
            int calulate[]=findLengthOfLCIS(array);
            int max=-1;
            int indexOfMax=0;
            for(int i=0;i<calulate.length;i++){
                if(calulate[i]>max){
                    max=calulate[i];
                    indexOfMax=i;
                }
            }
            int right=indexOfMax;
            int left=indexOfMax-max+1;
            System.out.println(left+1+" "+(right+1));
            T--;
        }
        
    }
}

猜你喜欢

转载自www.cnblogs.com/chiweiming/p/10705775.html