[SimpleOJ233]a xor b

题目大意:
  给你一个数列,求所有区间最大值和次大只异或的最大值。

思路:
  很容易想到一个O(n^2)的暴力。
  O(n)的单调栈做法似乎也很好想,不过考场上没想出来。
  对于数列上的某一个数,我们维护在它左边的比它大的单调递减序列。
  对于新加进来的一个数,我们把它作为最大值,对栈中比它小的数(次大值)取异或,并弹出这些数。

 1 #include<stack>
 2 #include<cstdio>
 3 #include<cctype>
 4 inline int getint() {
 5     register char ch;
 6     while(!isdigit(ch=getchar()));
 7     register int x=ch^'0';
 8     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
 9     return x;
10 }
11 std::stack<unsigned> s;
12 int main() {
13     int n=getint();
14     s.push(getint());
15     unsigned ans=0;
16     for(register int i=1;i<n;i++) {
17         const unsigned &x=getint();
18         while(!s.empty()&&s.top()<=x) {
19             ans=std::max(ans,s.top()^x);
20             s.pop();
21         }
22         if(!s.empty()) ans=std::max(ans,s.top()^x);
23         s.push(x);
24     }
25     printf("%u\n",ans);
26     return 0;
27 }

 

转载于:https://www.cnblogs.com/skylee03/p/7637482.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值