voidsolve(){ int n = read(); vector<int> a(n + 1); for (int i = 1; i <= n; i++) { a[i] = read(); } int ans = 2; int l = 0; while (l + 1 <= n and a[l + 1] == 0) l++; int r = n + 1; while (r - 1 >= 1and a[r - 1] == 0) r--;
bool f = 1; for (int i = l + 1; i < r; i++) { if (a[i] == 0) { f = 0; break; } }
if (f) ans = 1; else ans = 2;
if (l == n and r == 1) ans = 0;
cout << ans << '\n'; }
B
Code >folded
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
voidsolve(){ int n = read(); string s; cin >> s; bool ans = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s[i] == 's'and i != 0and s[j] == 'p'and j != n - 1) { ans = 0; break; } } }
intgetmex(const vector<int>& a){ std::unordered_set<int> s(a.begin(), a.end()); int mex = 0; while (s.find(mex) != s.end()) { mex++; } return mex; }
voidsolve(){ int n = read(); int x = read(), y = read();
vector<vector<int>> g(n + 1); for (int i = 1; i <= n; i++) { int prev = (i == 1) ? n : i - 1; int next = (i == n) ? 1 : i + 1; g[i].eb(prev); g[i].eb(next); } bool f = false; for (auto v : g[x]) { if(v == y) { f = true; break; } } if (!f) g[x].eb(y), g[y].eb(x); vector<int> a(n + 1); std::queue<int> q; vector<bool> inq(n + 1, 0); for (int i = 1; i <= n; i++) { q.ep(i); inq[i] = true; } while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; vector<int> t; for (auto v : g[u]) { t.push_back(a[v]); } int mex = getmex(t); if (a[u] != mex) { a[u] = mex; for (auto v : g[u]) { if (!inq[v]) { q.push(v); inq[v] = 1; } } } } f = 1; for (int i = 1; i <= n; i++) { vector<int> t; for(auto v : g[i]) { t.push_back(a[v]); } int mex = getmex(t); if (a[i] != mex){ f = 0; break; } } if (!f) { cout << "NO\n"; return; } for (int i = 1; i <= n; i++) { cout << a[i] << " \n"[i == n]; } }
for (int j = 0; j < m; j++) { for (int s = 0; s < m; s++) { if (dp[j][s] < pre[j]) { pre[j] = dp[j][s]; } } }
for (int s = 0; s < m; s++) { for (int j = 0; j < m; j++) { int val = a[i][(j + s) % m]; if (j == 0) { ndp[j][s] = min(ndp[j][s], pre[j] + k * s + val); } else { ndp[j][s] = min({ ndp[j][s], pre[j] + k * s + val, ndp[j - 1][s] + val }); } } }
dp = std::move(ndp); } int ans = INF; for (int s = 0; s < m; s++) { ans = min(ans, dp[m - 1][s]); } cout << ans << '\n'; }
auto ask = [&](int l, int r) { cout << "? " << l << " " << r << "\n"; fflush(stdout); int res; cin >> res; fflush(stdout); return res; };
int ans = 0;
if (ask(1, n / 4) != ask(n / 4 + 1, n / 2)) { // 1 in [1, n / 2] // sum[1, n / 2] = 1 // sum(n / 2, n] = 0 if (ask(1, n / 2) == 1) { // k > n / 2 for (int l = n / 2 + 1, r = n - 1; l <= r; ) { int mid = l + r >> 1; if (ask(1, mid) == 0) { ans = mid, r = mid - 1; } else { l = mid + 1; } } } else { // k <= n / 2 for (int l = 2, r = n / 2; l <= r; ) { int mid = l + r >> 1; if (ask(n / 2 + 1, n / 2 + mid) == 1) { ans = mid, r = mid - 1; } else { l = mid + 1; } } } } else { // 1 in (n / 2, n] // sum[1, n / 2] = 0 // sum(n / 2, n] = 1 if (ask(1, n / 2) == 0) { // k > n / 2 for (int l = n / 2 + 1, r = n - 1; l <= r; ) { int mid = l + r >> 1; if (ask(n - mid + 1, n) == 0) { ans = mid, r = mid - 1; } else { l = mid + 1; } } } else { // k <= n / 2 for (int l = 2, r = n / 2; l <= r; ) { int mid = l + r >> 1; if (ask(1, mid) == 1) { ans = mid, r = mid - 1; } else { l = mid + 1; } } } }