# Gym 101170F Free Weights（二分）题解

#include<stack>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + 10;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, w1[maxn], w2[maxn];
bool check(int m){
stack<int> s;
while(!s.empty()) s.pop();
for(int i = 1; i <= n; i++){
if(w1[i] > m){
if(s.empty()) s.push(w1[i]);
else if(!s.empty() && s.top() == w1[i]) s.pop();
else return false;
}
}
if(!s.empty()) return false;
for(int i = 1; i <= n; i++){
if(w2[i] > m){
if(s.empty()) s.push(w2[i]);
else if(!s.empty() && s.top() == w2[i]) s.pop();
else return false;
}
}
if(!s.empty()) return false;
return true;
}
int main(){
scanf("%d", &n);
int Max = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &w1[i]);
Max = max(Max, w1[i]);
}
for(int i = 1; i <= n; i++){
scanf("%d", &w2[i]);
Max = max(Max, w2[i]);
}
int l = 0, r = Max;
int ans = INF;
while(l <= r){
int m = (l + r) >> 1;
if(check(m)){
ans = m;
r = m - 1;
}
else{
l = m + 1;
}
}
printf("%d\n", ans);
return 0;
}

04-28 100

10-18 115
07-31 108
07-21 776
07-27 877
03-01 690
07-22 916
07-16 21
11-01 5450
05-27 60
08-16 1163
12-19 836
11-10 977

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助