CF 280B - Maximum Xor Secondary(单调栈)

题目链接:Click here~~

题意:

给一个长度为 n 的序列,元素不重复,求 max {a[i] ^ a[j]} (a[i] 和 a[j] 为某个区间中的最大值和次大值,区间长度任意)。

解题思路:

接上篇。

考虑虽然区间的个数为 O(n ^ 2) 的,但是很多区间的最大值和次大值是重复的。

类似的思路,考虑 a[i] 作为次大值时,向前能扩展到的最大值,也需要同样向前找到第一个比它大的元素。

同样维护一个单调栈,可以 O(n) 解决掉。

然后别忘了还要向后扩展一次。

#include 
#include 
#include 

using namespace std;

const int N = 1e5 + 5;

int n,a[N],s[N];

int solve()
{
    int ret = 0 , top = 0;
    for(int i=0;i


你可能感兴趣的:(◆点点滴滴,【动态规划】)