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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| #pragma warning(disable:4996) #include <bits/stdc++.h>
#define upf(i,a,n) for (int i=a;i<=n;i++) #define drf(i,a,n) for (int i=n;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; typedef long long ll; const ll INF(0x3f3f3f3f); const ll mod(998244353); const int maxn(1e5 + 5); ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a%mod; a = a * a%mod; }return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } int t, n; struct group { ll a, b, sum; }coins[maxn]; struct tmp2 { bool operator()(pair<int,int> a, pair<int,int> b) { return a.first < b.first; } }; ll marked[maxn], f[maxn * 2]; priority_queue<pair<int, int>, vector<pair<int, int> >, tmp2 > q1, q2; int main(){ #ifdef ACM_LOCAL freopen("acm.in", "r", stdin); freopen("acm.out", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; upf(_, 1, t) { cin >> n; while (!q1.empty()) q1.pop(); while (!q2.empty()) q2.pop(); upf(i, 0, n + 1) marked[i] = 0; upf(i, 1, n) { cin >> coins[i].a >> coins[i].b; coins[i].sum = coins[i].a + coins[i].b; q1.push(pair<int, int>{coins[i].a, i}); q2.push(pair<int, int>{coins[i].sum, i}); } f[0] = 0; f[1] = q1.top().first; int pos = q1.top().second; int ve = f[1]; marked[pos]++; q1.pop(); if (marked[pos] != 2) q1.push(pair<int, int> {coins[pos].b, pos}); upf(i, 2, 2 * n) { while (!q1.empty()) { if (marked[q1.top().second] == 2) q1.pop(); else break; } int poss = q1.top().second; int value = q1.top().first; q1.pop(); marked[poss]++; while (!q2.empty()) { if (marked[q2.top().second]) q2.pop(); else break; } pair<int, int> sm; if (!q2.empty()) { sm = q2.top(); } else { sm.first = -INF; } if (marked[poss] == 1) { if (sm.first < coins[poss].sum) { sm.first = coins[poss].sum; sm.second = poss; } } if (sm.first > value + ve) { f[i] = f[i - 2] + sm.first; if (!q2.empty()) { if (q2.top().first == sm.first) q2.pop(); } if (pos == poss) { marked[pos] = 0; marked[sm.second] = 2; q1.push(pair<int, int>{coins[pos].a, pos}); q2.push(pair<int, int>{coins[pos].sum, pos}); ve = coins[sm.second].b; pos = sm.second; continue; } if (marked[pos] == 1) { q1.push(pair<int, int>{coins[pos].a, pos}); } else { q1.push(pair<int, int>{coins[pos].b, pos}); } if (marked[poss] == 1) { q1.push(pair<int, int>{coins[poss].a, poss}); } else { q1.push(pair<int, int>{coins[poss].b, poss}); } marked[pos]--; marked[poss]--; marked[sm.second] = 2; if (marked[pos] == 0) { q2.push(pair<int, int>{coins[pos].sum, pos}); } if (marked[poss] == 0) { q2.push(pair<int, int> {coins[poss].sum, poss}); } ve = coins[sm.second].b; pos = sm.second; } else { f[i] = f[i - 1] + value; if (marked[poss] == 1) { q1.push(pair<int, int>{coins[poss].b, poss}); } pos = poss; ve = value; } } upf(i, 1, 2 * n) cout << f[i] << " \n"[i == 2 * n]; } }
|