51nod1423 最大二“货”

来源:互联网 发布:mac系统怎么关闭程序 编辑:程序博客网 时间:2024/04/30 05:30

题面在这里

我们维护一个单调递减的栈,然后发现每次一个数进栈的时候,那些弹出栈的数都对应某一个区间的次大值(最大值就是要进栈的数),所以我们每次弹栈的时候更新一下答案就好了。

然后这个只是次大值在最大值之前的情况,之后的情况再把a数组反过来操作一遍就行。


/*************************************************************Problem: 51nod 1423 最大二“货”User: bestFyLanguage: C++Result: AcceptedTime: 234 msMemory: 2700 KBSubmit_Time: 2017/11/15 16:55:21*************************************************************/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 100010;int n, ans, top;int a[N], st[N];inline void solve(){st[top = 0] = 0;for (int i = 1; i <= n; i ++){while (top && a[st[top]] <= a[i]) ans = max(ans, a[i]^a[st[top --]]);st[++ top] = i;}}int main(){scanf("%d", &n);for (int i = 1; i <= n; i ++) scanf("%d", &a[i]);solve();reverse(a+1, a+1+n);solve();printf("%d\n", ans);return 0;}


原创粉丝点击