首页 > 试题广场 >

幸运子序列

[编程题]幸运子序列
  • 热度指数:816 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛得到一个长度为n的整数序列V,牛牛定义一段连续子序列的幸运值为这段子序列中最大值和次大值的异或值(次大值是严格的次大)。牛牛现在需要求出序列V的所有连续子序列中幸运值最大是多少。请你帮帮牛牛吧。

输入描述:
第一行一个整数n,即序列的长度。(2<= n <= 100000)
第二行n个数,依次表示这个序列每个数值V[i], (1 ≤ V[i] ≤ 10^8)且保证V[1]到V[n]中至少存在不同的两个值.


输出描述:
输出一个整数,即最大的幸运值
示例1

输入

5
5 2 1 4 3

输出

7
import java.util.*;

public class Main {

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a=new int[n];
        for(int i=0;i<n;i++)
            a[i]=sc.nextInt();
        int max=0;
        int temp;
        for(int i=0;i<n;i++)
        {
            for(int j=i-1;j>=0;j--)
                if(a[j]>=a[i])
                {
                    temp=a[i]^a[j];
                    max=max>temp?max:temp;
                    break;
                }
            for(int j=i+1;j<n;j++)
                if(a[j]>=a[i])
                {
                    temp=a[i]^a[j];
                    max=max>temp?max:temp;
                    break;
                }
        }
        System.out.println(max);
	}
	
}



发表于 2019-08-30 23:34:43 回复(0)