|
| 1 | +#include <algorithm> |
| 2 | +#include <bitset> |
| 3 | +#include <cassert> |
| 4 | +#include <chrono> |
| 5 | +#include <cmath> |
| 6 | +#include <complex> |
| 7 | +#include <cstdio> |
| 8 | +#include <cstring> |
| 9 | +#include <ctime> |
| 10 | +#include <functional> |
| 11 | +#include <iomanip> |
| 12 | +#include <iostream> |
| 13 | +#include <iterator> |
| 14 | +#include <limits> |
| 15 | +#include <map> |
| 16 | +#include <numeric> |
| 17 | +#include <queue> |
| 18 | +#include <random> |
| 19 | +#include <set> |
| 20 | +#include <stack> |
| 21 | +#include <string> |
| 22 | +#include <tuple> |
| 23 | +#include <utility> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#define int long long |
| 27 | +#define double long double |
| 28 | +#define ff first |
| 29 | +#define ss second |
| 30 | +#define endl '\n' |
| 31 | +#define ii pair<int, int> |
| 32 | +#define mp make_pair |
| 33 | +#define mt make_tuple |
| 34 | +#define DESYNC \ |
| 35 | + ios_base::sync_with_stdio(false); \ |
| 36 | + cin.tie(0); \ |
| 37 | + cout.tie(0) |
| 38 | +#define pb push_back |
| 39 | +#define vi vector<int> |
| 40 | +#define vii vector<ii> |
| 41 | +#define all(x) x.begin(), x.end() |
| 42 | +#define EPS 1e-9 |
| 43 | +#define INF 1e18 |
| 44 | +#define ROOT 1 |
| 45 | +#define curtime chrono::steady_clock::now().time_since_epoch().count |
| 46 | +#define rep(i, beg, n, s) for (int i = beg; i < n; i += s) |
| 47 | +using ll = long long; |
| 48 | +using namespace std; |
| 49 | +const double PI = acos(-1); |
| 50 | +const int M = 1000000007; |
| 51 | +// const int M = 998244353; |
| 52 | +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); |
| 53 | +inline int mod(int n, int m = M) { |
| 54 | + int ret = n % m; |
| 55 | + if (ret < 0) ret += m; |
| 56 | + return ret; |
| 57 | +} |
| 58 | + |
| 59 | +int exp(int n, int k, int m = M) { |
| 60 | + if (k == 0) return 1; |
| 61 | + if (k == 1) return n; |
| 62 | + int ax = exp(n, k / 2, m); |
| 63 | + ax = mod(ax * ax, m); |
| 64 | + if (k % 2) ax = mod(ax * n, m); |
| 65 | + return ax; |
| 66 | +} |
| 67 | + |
| 68 | +int gcd(int a, int b) { |
| 69 | + if (a == 0) |
| 70 | + return b; |
| 71 | + else |
| 72 | + return gcd(b % a, a); |
| 73 | +} |
| 74 | + |
| 75 | +/* LIBRARY INCLUDES */ |
| 76 | + |
| 77 | +/* END OF LIBRARY INCLUDES */ |
| 78 | + |
| 79 | +/* TEMPLATE DEFINES */ |
| 80 | +// #define FILE_INPUT |
| 81 | +// #define PRINT_TEST_CASE |
| 82 | +#define MULTIPLE_TEST_CASE |
| 83 | + |
| 84 | +/* SOLUTION CODE */ |
| 85 | +void solution(int testcase) { |
| 86 | +#ifdef PRINT_TEST_CASE |
| 87 | + cout << "Case #" << testcase << ": "; |
| 88 | +#endif |
| 89 | + // Code starts here |
| 90 | + int n; |
| 91 | + cin >> n; |
| 92 | + vi v(n); |
| 93 | + for (int &x : v) cin >> x; |
| 94 | + |
| 95 | + vector<vi> buc; |
| 96 | + vector<ii> idx; |
| 97 | + vi current; |
| 98 | + int ini = 0; |
| 99 | + for (int i = 0; i < n; i++) { |
| 100 | + if (v[i] == 0) { |
| 101 | + if (current.size() > 0) { |
| 102 | + buc.push_back(current); |
| 103 | + idx.pb(ii(ini, (n - 1) - (i - 1))); |
| 104 | + } |
| 105 | + current.clear(); |
| 106 | + ini = i + 1; |
| 107 | + } else { |
| 108 | + current.pb(v[i]); |
| 109 | + } |
| 110 | + } |
| 111 | + if (current.size() > 0) { |
| 112 | + buc.push_back(current); |
| 113 | + idx.pb(ii(ini, 0)); |
| 114 | + } |
| 115 | + |
| 116 | + int max_ans = -1; |
| 117 | + ii ans; |
| 118 | + for (int i = 0; i < buc.size(); i++) { |
| 119 | + vi v = buc[i]; |
| 120 | + // cout << v.size() << " " << idx[i].ff << " " << idx[i].ss << endl; |
| 121 | + |
| 122 | + int negs = 0; |
| 123 | + int twos = 0; |
| 124 | + for (int x : v) { |
| 125 | + if (x < 0) negs++; |
| 126 | + if (abs(x) == 2) twos++; |
| 127 | + } |
| 128 | + |
| 129 | + if (negs % 2 == 0) { |
| 130 | + if (max_ans < twos) { |
| 131 | + ans = idx[i]; |
| 132 | + max_ans = twos; |
| 133 | + } |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + int pref = 0; |
| 138 | + int pref_deleted = 0; |
| 139 | + for (int x : v) { |
| 140 | + pref_deleted++; |
| 141 | + if (abs(x) == 2) pref++; |
| 142 | + if (x <= 0) { |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + reverse(all(v)); |
| 148 | + int suf = 0; |
| 149 | + int suf_deleted = 0; |
| 150 | + for (int x : v) { |
| 151 | + suf_deleted++; |
| 152 | + if (abs(x) == 2) suf++; |
| 153 | + if (x <= 0) { |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if (pref <= suf) { |
| 159 | + if (max_ans <= twos - pref) { |
| 160 | + ans = ii(idx[i].ff + pref_deleted, idx[i].ss); |
| 161 | + max_ans = twos - pref; |
| 162 | + } |
| 163 | + } else { |
| 164 | + if (max_ans <= twos - suf) { |
| 165 | + ans = ii(idx[i].ff, idx[i].ss + suf_deleted); |
| 166 | + max_ans = twos - suf; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (buc.size() == 0) { |
| 172 | + ans = ii(v.size(), 0); |
| 173 | + } |
| 174 | + cout << ans.ff << " " << ans.ss << endl; |
| 175 | +} |
| 176 | + |
| 177 | +#ifdef FILE_INPUT |
| 178 | +freopen("equal.in", "r", stdin); |
| 179 | +freopen("equal.out", "w", stdout); |
| 180 | +#endif |
| 181 | + |
| 182 | +int32_t main() { |
| 183 | + DESYNC; |
| 184 | + int t = 1; |
| 185 | +#ifdef MULTIPLE_TEST_CASE |
| 186 | + cin >> t; |
| 187 | +#endif |
| 188 | + for (int _testcase = 1; _testcase <= t; _testcase++) solution(_testcase); |
| 189 | +} |
0 commit comments