又是一顿魔改,但是就是 A 不掉/ll
代码可读性还行。
#include<bits/stdc++.h>
using namespace std;
deque<pair<int, int> > q1, q2;
int t, n, a[1000005];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> t;
for (int _ = 1; _ <= t; _++){
if (_ == 1){
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
}else{
int qwqwq;
cin >> qwqwq;
for (int i = 1; i <= qwqwq; i++){
int u, v;
cin >> u >> v;
a[u] = v;
}
}
for (int i = 1; i <= n; i++)
q1.push_back(make_pair(a[i], i));
int ans = 0;
pair<int, int> xz;
while (1){
if (q1.size() + q2.size() <= 2){
ans = 1;
break;
}
int mx, id, mn, md;
mn = q1.front().first;
md = q1.front().second;
q1.pop_front();
if (q2.empty() || !q1.empty() && q1.back() > q2.back()){
mx = q1.back().first;
id = q1.back().second;
q1.pop_back();
}else{
mx = q2.back().first;
id = q2.back().second;
q2.pop_back();
}
if (q1.empty() || make_pair(mx - mn, id) < q1.front()){
xz.first = mx - mn;
xz.second = id;
break;
}else
q2.push_front(make_pair(mx - mn, id));
}
if (ans == 1){
cout << "1\n";
while (!q1.empty())
q1.pop_back();
while (!q2.empty())
q2.pop_back();
continue;
}
ans = q1.size() + q2.size() + 2;
// cout << ans << "\n";
int cnt = 0;
while (1){
cnt++;
if (q1.size() + q2.size() == 1){
if (cnt % 2 == 0)
ans--;
break;
}
int mx, id;
if (q2.empty() || !q1.empty() && q1.back() > q2.back()){
mx = q1.back().first;
id = q1.back().second;
q1.pop_back();
}else{
mx = q2.back().first;
id = q2.back().second;
q2.pop_back();
}
xz.first = mx - xz.first;
if (!(q1.empty() || xz < q1.front()) || !(q2.empty() || xz < q2.front())){
if (cnt % 2 == 0)
ans--;
break;
}
}
while (!q1.empty())
q1.pop_back();
while (!q2.empty())
q2.pop_back();
cout << ans << "\n";
}
return 0;
}