51nod-1423 最大二“货”(单调栈)
2017-01-09 11:40
267 查看
原题链接
1423 最大二“货”
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
收藏
关注
白克喜欢找一个序列中的次大值。对于一个所有数字都不同的序列 x1, x2, ..., xk (k > 1) ,他的次大值是最大的 xj ,并且满足 xj ≠maxki=1 xi
对于一个所有数字都不同的序列 x1, x2, ..., xk (k > 1) ,他的幸运数字是最大值和次大值的异或值(Xor)。
现在有一个序列 s1, s2, ..., sn (n > 1) 。 s[l,r] 表示子段 sl, sl+1, ..., sr 。你的任务是找出所有子段的最大幸运数字。
注意,序列s中的所有数字都是不同的。
Input
Output
Input示例
Output示例
从左到右遍历数组,维护一个单调递减栈,若num[i] > Stack[p-1] , ans = max(ans, num[i] ^ Stack[p-1]);再从右到左遍历整个数组,执行相同操作
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#define maxn 100005
#define MOD 1000000007
using namespace std;
typedef long long ll;
int num[maxn], Stack[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", num+i);
int ans = 0;
int p = 1;
Stack[0] = num[0];
for(int i = 1; i < n; i++){
while(p && num[i] > Stack[p-1]){
ans = max(ans, num[i] ^ Stack[p-1]);
p--;
}
Stack[p++] = num[i];
}
Stack[0] = num[0];
p = 1;
for(int i = n - 1; i >= 0; i--){
while(p && num[i] > Stack[p-1]){
ans = max(ans, num[i] ^ Stack[p-1]);
p--;
}
Stack[p++] = num[i];
}
printf("%d\n", ans);
return 0;
}
1423 最大二“货”
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
收藏
关注
白克喜欢找一个序列中的次大值。对于一个所有数字都不同的序列 x1, x2, ..., xk (k > 1) ,他的次大值是最大的 xj ,并且满足 xj ≠maxki=1 xi
对于一个所有数字都不同的序列 x1, x2, ..., xk (k > 1) ,他的幸运数字是最大值和次大值的异或值(Xor)。
现在有一个序列 s1, s2, ..., sn (n > 1) 。 s[l,r] 表示子段 sl, sl+1, ..., sr 。你的任务是找出所有子段的最大幸运数字。
注意,序列s中的所有数字都是不同的。
Input
单组测试数据。 第一行有一个整数n (1 < n ≤ 10^5)。 第二行包含n个不同的整数 s1, s2, ..., sn (1 ≤ si ≤ 10^9)。
Output
输出所有子段的最大幸运值。
Input示例
5 5 2 1 4 3 5 9 8 3 5 7
Output示例
7 15
从左到右遍历数组,维护一个单调递减栈,若num[i] > Stack[p-1] , ans = max(ans, num[i] ^ Stack[p-1]);再从右到左遍历整个数组,执行相同操作
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#define maxn 100005
#define MOD 1000000007
using namespace std;
typedef long long ll;
int num[maxn], Stack[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", num+i);
int ans = 0;
int p = 1;
Stack[0] = num[0];
for(int i = 1; i < n; i++){
while(p && num[i] > Stack[p-1]){
ans = max(ans, num[i] ^ Stack[p-1]);
p--;
}
Stack[p++] = num[i];
}
Stack[0] = num[0];
p = 1;
for(int i = n - 1; i >= 0; i--){
while(p && num[i] > Stack[p-1]){
ans = max(ans, num[i] ^ Stack[p-1]);
p--;
}
Stack[p++] = num[i];
}
printf("%d\n", ans);
return 0;
}
相关文章推荐
- 单调栈——51nod 1423 最大二“货”
- 51nod 1423 最大“二货”【单调栈】
- 【单调栈】51Nod 1423 最大二“货”
- 51nod 1423:最大二“货”
- 51nod 1423:最大二“货”
- 51nod 1272 最大距离 (单调栈)
- 51nod 1102 面积最大的矩形(单调栈)
- 51nod 1102 面积最大的矩形(单调栈)
- 51nod-1158 全是1的最大子矩阵(单调栈)
- 51nod-1158 . 全是1的最大子矩阵 &&CODEVS-2491 玉蟾宫(枚举+单调栈 or dp)
- 51nod 1102 面积最大的矩形【单调栈、预处理】
- 51nod 1349 最大值(单调栈)
- 51Nod-1423-最大二"货"
- 51nod 1349 最大值【单调栈】
- 【单调栈】51Nod 1349 最大值
- 51nod 1272 最大距离 (贪心或单调栈)
- 51Nod - 1349 最大值(单调栈)
- 51Nod 1158 全是1的最大子矩阵【单调栈】
- 51nod 1423 最大二“货”
- 【51Nod】1102 - 面积最大的矩形面积(单调栈)