第一行一个整数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.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int max = Integer.MIN_VALUE;
for(int j = 0; j < n; j++) {
// 标志每个节点都为次最大值
int secondIndex = arr[j];
// 向前找 找出最大值
int first = 0;
int firstMax = 0;
int index1 = j;
while(index1 != 0) {
if(arr[index1 - 1] > arr[j]) {
first = arr[index1 - 1];
break;// 如果找到了就退出循环
}
index1--;
}
firstMax = first ^ secondIndex;
max = Math.max(max, firstMax);
// 向后找 找出最大值
int second = 0;
int secondMax = 0;
int index2 = j;
while(index2 != arr.length - 1) {
if(arr[index2 + 1] > arr[j]) {
second = arr[index2 + 1];
break;// 如果找到了就退出循环
}
index2++;
}
secondMax =second ^ secondIndex;
max = Math.max(secondMax, max);
}
System.out.println(max);
sc.close();
}
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, s=0, x;
stack<int> S;
scanf("%d", &n);
for(int i=0;i<n;i++){
scanf("%d", &x);
while(!S.empty() && S.top()<=x){
s = max(s, x^S.top());
S.pop();
}
if(!S.empty())
s = max(s, x^S.top());
S.push(x);
}
printf("%d\n", s);
return 0;
} n=int(input()) s=list(map(int,input().split())) xor_list=0 for x in range(n): for y in range(x+1,n): #向右循环遍历 if s[y]>s[x]: xor_list=max(xor_list,s[y]^s[x]) break for y in range(x-1,-1,-1): #向左循环遍历 if s[y]>s[x]: xor_list=max(xor_list,s[y]^s[x]) break print(xor_list)
''' ## 列举出所有连续子序列,时间复杂度过大,O(n^2),未通过 n = int(input()) inp = [int(x) for x in input().split()] max_num = 0 for i in range(2,n+1) : for j in range(n-i+1) : temp_sublist = list(set(inp[j:j+i])) if len(temp_sublist) == 1: continue else : max_value = max(temp_sublist) temp_sublist.remove(max_value) submax_value = max(temp_sublist) temp = max_value ^ submax_value if temp > max_num: max_num = temp print(max_num) ''' ## 将max_num和submax_num“互换”,不通过 n = int(input()) inp = [int(x) for x in input().split()] max_luck_num = 0 for i in range(n): submax_num = inp[i] for j in range(i,-1,-1): if inp[j] > inp[i] : max_num = inp[j] temp = max_num ^ submax_num if temp > max_luck_num : max_luck_num = temp break for j in range(i+1,n): if inp[j] > inp[i] : max_num = inp[j] temp = max_num ^ submax_num if temp > max_luck_num : max_luck_num = temp break print(max_luck_num)
#include<stdio.h>
#include<stack>
#include<algorithm>
using namespace std;
int main(){
int n,i,res=0,x;
stack<int> s;
for(scanf("%d",&n),i=0;i<n;i++){
scanf("%d",&x);
while(s.size()&&s.top()<=x)
res=max(res,x^s.top()),s.pop();
if(s.size()) res=max(res,x^s.top());
s.push(x);
}
printf("%d\n",res);
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Stack<Integer> stack = new Stack<>();
int res = 0;
for (int i = 0; i < n; i++){
int x = sc.nextInt();
// stack维持的是一个单调递减的栈,
// 如果当前元素比栈顶大,则当前元素作为最大值向前组成子序列,次大值依次为出栈的栈顶元素
while (!stack.isEmpty() && stack.peek() <= x){
res = Math.max(res, x ^ stack.pop());
}
// 如果stack非空,此时栈顶元素大于当前元素x,
// 当前元素为次大值,栈顶为最大值计算异或值
if (!stack.isEmpty()){
res = Math.max(res, x ^ stack.peek());
}
stack.push(x);
}
System.out.println(res);
}
} 感觉到自己的渺小 与大神的差距让人绝望
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());
}
}
}
n = int(input()) v = list(map(int, input().split())) luck = [] for i in range(n): j = i arr = [] maxluck = 0 while j >= 0: arr.append(v[j]) zuida = max(arr) if len(arr) == 1: luckzhi = zuida ^ zuida elif len(arr) > 1: arr1 = arr[:] arr1.remove(zuida) cida = max(arr1) luckzhi = zuida ^ cida if luckzhi > maxluck: maxluck = luckzhi luck.append(maxluck) j = j - 1 print(max(luck))
n = int(input()) v =[int(i) for i in input().split()] luck = [] for i in range(len(v)): j = i zuida = v[i] while j >= 0: if v[j] > v[i]: zuida = v[j] res = zuida ^ v[i] break else: res = zuida ^ v[i] j = j - 1 k = i cida = v[i] while k < len(v): if v[k] > v[i]: cida = v[k] res = max(res, cida ^ v[i]) break else: res = max(res, cida ^ v[i]) k = k + 1 luck.append(res) print(max(luck))
## 单调栈做法 def getLuckNum2(arr,n):#单调减栈 遇到不符合的情况弹栈计算结果 # 找到每个元素 左边第一个比它大的 右边第一个比它大的 计算两者异或 然后更新结果就好了 等于都不可以 s = [] ans = -(1<<31) for idx in range(n): while s and arr[s[-1]] <= arr[idx]: #median = s.pop() #弹出最后一个 中心 ans = max(ans,arr[s[-1]]^arr[idx]) s.pop() if s: ans = max(ans,arr[s[-1]]^arr[idx]) #即使最后栈还有 已经计算进去了 s.append(idx) print(ans) getLuckNum2(arr,n)
/*
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
/**
* @author wylu
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] strs = br.readLine().split(" ");
int[] v = new int[n];
for (int i = 0; i < n; i++) {
v[i] = Integer.parseInt(strs[i]);
}
int res = 0;
LinkedList<Integer> stack = new LinkedList<>();
//遍历所有元素作为子串最大值和次大值的情况
for (int i = 0; i < n; i++) {
//当前元素作为连续子序列(也即子串)的最大值的情况
while (!stack.isEmpty() && stack.peek() <= v[i]) {
res = Math.max(res, v[i] ^ stack.pop());
}
//当前元素作为连续子序列的次大值的情况
if (!stack.isEmpty()) {
res = Math.max(res, v[i] ^ stack.peek());
}
stack.push(v[i]);
}
System.out.println(res);
}
}
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin>>n;
stack<long long> s;
long long res = 0;
for(int i=0;i<n;i++)
{
long long v;
cin>>v;
while(s.size()>0 && s.top()<=v)
{
res = max(res,v^s.top());
s.pop();
}
if(s.size()>0)
{ res = max(res,v^s.top()); }
s.push(v);
}
cout<<res<<endl;
return 0;
}