1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <bits/stdc++.h> using namespace std; const int N = 1000000 + 10, P = 998244353;
int a[N]; int main() { int T; scanf("%d", &T); int n; for (int cas = 1; cas <= T; cas++) { if (cas == 1) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); } else { int k; scanf("%d", &k); while (k--) { int x, y; scanf("%d%d", &x, &y); a[x] = y; } } deque<pair<int, int>> q1, q2; for (int i = 1; i <= n; i++) { q1.push_back({a[i], i}); } int ans; while (1) { if (q1.size() + q2.size() == 2) { ans = 1; break; } int x, id, y; y = q1.front().first, q1.pop_front(); if (q2.empty() || !q1.empty() && q1.back() > q2.back()) x = q1.back().first, id = q1.back().second, q1.pop_back(); else x = q2.back().first, id = q2.back().second, q2.pop_back(); pair<int, int> now = make_pair(x - y, id); if (q1.empty() || q1.front() > now) { ans = q1.size() + q2.size() + 2; int cnt = 0; while (1) { cnt++; if (q1.size() + q2.size() + 1 == 2) { if (cnt % 2 == 0) ans--; break; } int x, id; if (q2.empty() || !q1.empty() && q1.back() > q2.back()) x = q1.back().first, id = q1.back().second, q1.pop_back(); else x = q2.back().first, id = q2.back().second, q2.pop_back(); now = {x - now.first, id}; if (!((q1.empty() || now < q1.front()) && (q2.empty() || now < q2.front()))) { ans = ans - !(cnt % 2); break; } } break; } else { q2.push_front(now); } } printf("%d\n", ans); } return 0; }
|