第一行一个整数n,即序列的长度。(2<= n <= 100000) 第二行n个数,依次表示这个序列每个数值V[i], (1 ≤ V[i] ≤ 10^8)且保证V[1]到V[n]中至少存在不同的两个值.
输出一个整数,即最大的幸运值
5 5 2 1 4 3
7
import java.util.*; public class try2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { Cal(sc); } } public static void Cal(Scanner sc) { int n = sc.nextInt(); int[] V = new int[n]; ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { V[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { int l=-1, r=-1; for (int x = i - 1; x >= 0; x--) { if (V[x] > V[i]) { l = x; break; } } for (int y = i + 1; y < n; y++) { if (V[y] > V[i]) { r = y; break; } } if (l!=-1) { // System.out.println("l="+V[l]+" i="+V[i]); arr.add(V[l]^V[i]); } if (r!=-1) { // System.out.println("i="+V[i]+" r="+V[r]); arr.add(V[i]^V[r]); } } Collections.sort(arr, (a, b) -> { return b - a; }); Iterator<Integer> it = arr.iterator(); if (it.hasNext()) { System.out.println(it.next()); } } }
/*