题目链接:http://codeforces.com/contest/1145/problem/A
愚人节题目,求最长不下降连续序列,序列还得是去一半,去一半得到的。
递归
1 import java.util.Scanner; 2 3 public class pro1145A { 4 5 public static int rise(int[] R,int ind,int len){ 6 if(len==1) return 1; 7 boolean flag = true; 8 for(int i=ind;i<ind+len-1;++i){ 9 if(R[i]>R[i+1]){ 10 flag = false; 11 break; 12 } 13 } 14 if(flag) 15 return len; 16 else{ 17 return Math.max(rise(R,ind,len/2),rise(R,ind+len/2,len/2)); 18 } 19 } 20 21 public static void main(String[] args){ 22 Scanner input = new Scanner(System.in); 23 24 int N; 25 N = input.nextInt(); 26 27 int[] raw = new int[N]; 28 for(int i=0;i<N;++i){ 29 raw[i] = input.nextInt(); 30 } 31 32 int ans = rise(raw,0,N); 33 System.out.println(ans); 34 } 35 }
分治
1 package com.jetbrains; 2 3 import java.util.Scanner; 4 5 public class pro1145A { 6 7 public static int rise(int[] arr,int L,int R){ 8 if(L + 1 == R) return 1; 9 10 int mid = L+(R-L)/2; 11 int Lans = rise(arr,L,mid), 12 Rans = rise(arr,mid,R); 13 14 if(arr[mid-1] <= arr[mid]&& Lans == mid-L 15 && Rans == R-mid) 16 return Lans+Rans; 17 else 18 return Math.max(Lans,Rans); 19 } 20 21 public static void main(String[] args){ 22 Scanner input = new Scanner(System.in); 23 24 int N; 25 N = input.nextInt(); 26 27 int[] raw = new int[N]; 28 for(int i=0;i<N;++i){ 29 raw[i] = input.nextInt(); 30 } 31 32 int ans = rise(raw,0,N); 33 System.out.println(ans); 34 } 35 }