structSnake { int val, id; Snake() {} Snake(int _val, int _id) : val(_val), id(_id) {} booloperator <(const Snake &t) const { return val == t.val ? id > t.id : val > t.val; } };
int T, n, k; int a[N]; set<Snake> s;
inlinevoidmain(){ read(T); for (int c = 1; c <= T; ++c) { s.clear(); if (c == 1) { read(n); for (int i = 1, x; i <= n; ++i) read(a[i]); } else { read(k); for (int i = 1, x, y; i <= k; ++i) read(x), read(y), a[x] = y; } for (int i = 1; i <= n; ++i) s.insert(Snake(a[i], i)); int ans = 0, flag = 0; while (true) { if (s.size() == 2) { s.erase(--s.end()); if (flag) { if ((flag - s.size()) % 2) ans = flag + 1; else ans = flag; } else ans = 1; break; } set<Snake>::iterator ma = s.begin(), mi = --s.end(); int val = ma -> val - mi -> val, id = ma -> id; s.erase(ma), s.erase(mi); s.insert(Snake(val, id)); if ((--s.end()) -> id == id) { if (!flag) flag = s.size(); } else { if (flag) { if ((flag - s.size()) % 2) ans = flag + 1; else ans = flag; break; } } } printf("%d\n", ans); } }
structSnake { int val, id; Snake() {} Snake(int _val, int _id) : val(_val), id(_id) {} booloperator <(const Snake &t) const { return val == t.val ? id < t.id : val < t.val; } };
int T, n, k; int a[N]; deque<Snake> q1, q2;
inline Snake get_max(){ Snake res; if (q1.empty()) res = q2.back(), q2.pop_back(); elseif (q2.empty()) res = q1.back(), q1.pop_back(); else { if (q1.back() < q2.back()) res = q2.back(), q2.pop_back(); else res = q1.back(), q1.pop_back(); } return res; }
inline Snake get_min(){ Snake res; if (q1.empty()) res = q2.front(), q2.pop_front(); elseif (q2.empty()) res = q1.front(), q1.pop_front(); else { if (q1.front() < q2.front()) res = q1.front(), q1.pop_front(); else res = q2.front(), q2.pop_front(); } return res; }
inlinevoidmain(){ read(T); for (int c = 1; c <= T; ++c) { if (c == 1) { read(n); for (int i = 1, x; i <= n; ++i) read(a[i]); } else { read(k); for (int i = 1, x, y; i <= k; ++i) read(x), read(y), a[x] = y; } q1.clear(), q2.clear(); for (int i = 1; i <= n; ++i) q1.push_back(Snake(a[i], i)); int ans = 0, flag = 0; while (true) { if (q1.size() + q2.size() == 2) { if (flag) { if ((flag - 1) % 2) ans = flag + 1; else ans = flag; } else ans = 1; break; } Snake ma = get_max(), mi = get_min(), u(ma.val - mi.val, ma.id); if ((q1.empty() || u < q1.front()) && (q2.empty() || u < q2.front())) { if (!flag) flag = q1.size() + q2.size() + 1; } else { if (flag) { if ((flag - (q1.size() + q2.size() + 1)) % 2) ans = flag + 1; else ans = flag; break; } } q2.push_front(u); } printf("%d\n", ans); } }