/// Raihan Ruhin /// CSE, Jahangirnagar University. /// Dhaka-Bangladesh. /// id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj) /// mail: raihanruhin@ (yahoo / gmail / facebook) /// blog: ruhinraihan.blogspot.com #include<bits/stdc++.h> using namespace std; #define SET(a) memset(a,-1,sizeof(a)) #define CLR(a) memset(a,0,sizeof(a)) #define PI acos(-1.0) #define MOD 1000000007 #define MX 100010 long long arr[MX]; int main() { ios_base::sync_with_stdio(0);cin.tie(0); int tc,kk=1, n; cin>>n; for(int i=0;i<n;i++) cin>>arr[i]; //forward increasing. stack<long long>fi; fi.push(arr[0]); fi.push(arr[1]); long long mx=arr[0]^arr[1]; for(int i=2;i<n;i++) { while(!fi.empty() && fi.top()<arr[i]) fi.pop(); fi.push(arr[i]); long long last=fi.top(); fi.pop(); if(!fi.empty()) { long long prev=fi.top(); mx=max(mx, prev^last); } fi.push(last); } //backward increasing stack<long long>bi; bi.push(arr[n-1]); bi.push(arr[n-2]); mx=max(mx, arr[n-1]^arr[n-2]); for(int i=n-3;i>=0;i--) { while(!bi.empty() && bi.top()<arr[i]) bi.pop(); bi.push(arr[i]); long long last=bi.top(); bi.pop(); if(!bi.empty()) { long long prev=bi.top(); mx=max(mx, prev^last); } bi.push(last); } cout<<mx; return 0; }
Friday, 20 February 2015
Codeforces Round #172 (Div. 2) Maximum Xor Secondary
Problem: Maximum Xor Secondary
Subscribe to:
Post Comments (Atom)
can you please explain the logic behind this code?
ReplyDelete