func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; int arr[(int)1e5 + 1]; int q[(int)1e5 + 1]; int sol[(int)1e5 + 1]; int main() { int n, m; cin >> n; cin >> arr[0]; for (int i = 1; i < n; i++) { int x; cin >> x; arr[i] = arr[i - 1] + x; } cin >> m; for (int i = 0; i < m; i++) { cin >> q[i]; } for (int i = 0; i < m; i++) { int lo = 0, hi = n - 1, res = -1; while (lo <= hi) { int md = (hi + lo) >> 1; if (arr[md] >= q[i] && q[i] >= arr[md - 1]) { hi = md - 1; res = md; } else if (arr[md] >= q[i]) hi = md - 1; else lo = md + 1; } sol[i] = res + 1; } for (int i = 0; i < m; i++) { cout << sol[i] << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int m, n, j, k, l, i, o, p, __t; int V[200][2000], tim[200][2000], T[200], cop[200][2000]; char ch; void read(int &a) { for (ch = getchar(); (ch < 0 || ch > 9 ) && (ch != - ); ch = getchar()) ; if (ch == - ) a = 0, __t = -1; else a = ch - 0 , __t = 1; for (ch = getchar(); ch >= 0 && ch <= 9 ; ch = getchar()) a = a * 10 + ch - 0 ; a *= __t; } int main() { read(m), read(n); for (int i = 1; i <= m; i++) { read(T[i]); for (int j = 1; j <= T[i]; j++) read(V[i][j]), read(tim[i][j]), cop[i][j] = tim[i][j]; } int ans = 0; for (int i = 1; i <= m; i++) { for (int j = i + 1; j <= m; j++) { for (int k = 1; k <= T[i]; k++) tim[i][k] = cop[i][k]; for (int k = 1; k <= T[j]; k++) tim[j][k] = cop[j][k]; int x0 = 0, x1 = 0, who = 0; if (V[i][1] > V[j][1]) who = -1; else who = 1; for (int t1 = 1, t2 = 1; t1 <= T[i] && t2 <= T[j];) { int dece = min(tim[i][t1], tim[j][t2]); int y0 = x0 + dece * V[i][t1]; int y1 = x1 + dece * V[j][t2], nexwho; if (y0 == y1) nexwho = who; else if (y0 < y1) nexwho = 1; else nexwho = -1; if (nexwho != who) ans++; who = nexwho; x0 = y0, x1 = y1; tim[i][t1] -= dece; tim[j][t2] -= dece; if (tim[i][t1] == 0) t1++; if (tim[j][t2] == 0) t2++; } } } cout << ans << endl; }
|
#include <bits/stdc++.h> using namespace std; int n, k, t, cur; void solve() { scanf( %d %d , &n, &k); t = n - k; for (int i = 1; i <= t; i++) printf( %d , i); for (int i = k, j = 1; i >= 1; i--, j *= -1) { t += i * j; printf( %d , t); } } int main(void) { solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int ans, n; vector<pair<int, int> > vec; string s, t; int lcp1[5005][5005]; int lcp2[5005][5005]; void LCP(string &s, string &t, int lcp[5005][5005]) { for (int i = (int)s.size() - 1; i >= 0; i--) { for (int j = (int)t.size() - 1; j >= 0; j--) { if (s[i] == t[j]) { lcp[i][j] = lcp[i + 1][j + 1] + 1; } else { lcp[i][j] = 0; } } } } void solve() { LCP(s, t, lcp1); reverse(s.begin(), s.end()); LCP(s, t, lcp2); for (int ti = 0; ti < t.size();) { int mx1 = 0, mx1_si = -1; int mx2 = 0, mx2_si = -1; for (int si = 0; si < s.size(); si++) { if (mx1 < lcp1[si][ti]) { mx1 = lcp1[si][ti]; mx1_si = si; } if (mx2 < lcp2[si][ti]) { mx2 = lcp2[si][ti]; mx2_si = si; } } if (mx1 == 0 && mx2 == 0) { ans = -1; break; } else if (mx1 >= mx2) { ans++; vec.push_back(pair<int, int>(mx1_si + 1, mx1_si + mx1 - 1 + 1)); ti += mx1; } else { ans++; vec.push_back(pair<int, int>(n - mx2_si, n - (mx2_si + mx2 - 1))); ti += mx2; } } cout << ans << endl; for (int i = 0; i < (ans); i++) { cout << vec[i].first << << vec[i].second << endl; } } int main() { cin >> s; cin >> t; n = (int)s.size(); solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; string s1, s2; cin >> n >> s1 >> s2; long long count = 0; for (long long i = 0; i < n / 2; i++) { char a[4] = {s1[i], s1[n - i - 1], s2[i], s2[n - i - 1]}; if ((a[0] == a[1] and a[2] == a[3]) || (a[0] == a[3] and a[1] == a[2]) || (a[0] == a[2] and a[1] == a[3])) count += 0; else if ((a[0] == a[2] and a[1] != a[3]) || (a[0] == a[3] and a[1] != a[2]) || (a[0] != a[2] and a[1] == a[3]) || (a[0] != a[3] and a[1] == a[2]) || (a[0] != a[1] and a[2] == a[3])) count += 1; else count += 2; } if (n % 2 == 1) count += (s1[n / 2] != s2[n / 2]); cout << count; return 0; }
|
#include <bits/stdc++.h> void doubleIntSort(double d[], int m[], int s) { int i, j, r; double k1, k2, t; if (s <= 1) return; k1 = (d[0] + d[s - 1]) / 2.0; k2 = k1 + 1e-10; k1 -= 1e-10; i = -1; j = s; for (;;) { while (d[++i] < k1) ; while (d[--j] > k2) ; if (i >= j) break; t = d[i]; d[i] = d[j]; d[j] = t; r = m[i]; m[i] = m[j]; m[j] = r; } doubleIntSort(d, m, i); doubleIntSort(d + j + 1, m + j + 1, s - j - 1); } int n; int nf, ne, ns; int rf, re, rs; double Rf, Re, Rs; int df, de; int h[21]; double res; double x[100]; int ev[100]; int F, E, S; void calc(void) { int i, j, k; double w, tmp; if (n % 2 == 0) { i = h[0] + h[1] * 10; j = h[n - 2] + h[n - 1] * 10; if (i > j) return; } F = E = S = 0; for (i = 0; i < n; i++) { if (h[i] == 0) { x[2 * i] = (i / 2) - Rf; x[2 * i + 1] = (i / 2) + Rf; ev[2 * i] = 1; ev[2 * i + 1] = -1; } if (h[i] == 1) { x[2 * i] = (i / 2) - Re; x[2 * i + 1] = (i / 2) + Re; ev[2 * i] = 2; ev[2 * i + 1] = -2; } if (h[i] == 2) { x[2 * i] = (i / 2) - Rs; x[2 * i + 1] = (i / 2) + Rs; ev[2 * i] = 3; ev[2 * i + 1] = -3; } } doubleIntSort(x, ev, 2 * n); tmp = 0; for (i = 0; i < 2 * n - 1; i++) { w = x[i + 1] - x[i]; if (ev[i] == 1) F++; if (ev[i] == -1) F--; if (ev[i] == 2) E++; if (ev[i] == -2) E--; if (ev[i] == 3) S++; if (ev[i] == -3) S--; tmp += (F * df + E * de) * (w * (S + 1)); } if (res < tmp) res = tmp; } void brute(int depth) { if (depth == n) calc(); if (nf > 0) { h[depth] = 0; nf--; if (depth % 2 == 0 || h[depth - 1] <= h[depth]) brute(depth + 1); nf++; } if (ne > 0) { h[depth] = 1; ne--; if (depth % 2 == 0 || h[depth - 1] <= h[depth]) brute(depth + 1); ne++; } if (ns > 0) { h[depth] = 2; ns--; if (depth % 2 == 0 || h[depth - 1] <= h[depth]) brute(depth + 1); ns++; } } int main() { int i, j, k, l, m; res = 0; scanf( %d%d%d , &nf, &ne, &ns); n = nf + ne + ns; scanf( %d%d%d , &rf, &re, &rs); scanf( %d%d , &df, &de); Rf = sqrt(rf * rf - 1); if (rf == 1) Rf = 0; Re = sqrt(re * re - 1); if (re == 1) Re = 0; Rs = sqrt(rs * rs - 1); if (rs == 1) Rs = 0; brute(0); printf( %.15f n , res); return 0; }
|
#include <bits/stdc++.h> struct data { int val; data(int val) : val(val) {} }; bool operator<(const data lhs, const data rhs) { std::cout << ? << lhs.val << << rhs.val << std::endl; char c; std::cin >> c; return c == < ; } int t, n; std::set<data> a, b; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); std::cin >> t; while (t--) { std::cin >> n; a.clear(); b.clear(); for (int i = 1; i <= 2 * n; ++i) { if (a.size() < b.size()) a.emplace(i); else b.emplace(i); if (a.size() + b.size() > n) if (*a.begin() < *b.begin()) a.erase(a.begin()); else b.erase(b.begin()); } std::cout << ! << std::endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long int n; long long int f(long long int x) { long long int sq = sqrt(x); for (long long int i = 2; i <= sq; i++) if (x % i == 0) return 0; return 1; } int main() { cin >> n; if (f(n)) { cout << 1 << endl << n << endl; return 0; } cout << 3 << endl; cout << 3 << ; n -= 3; for (long long int i = 2; i < n; i++) { if (f(i) && f(n - i)) return cout << i << << n - i << endl, 0; } }
|
#include <bits/stdc++.h> using namespace std; int n, q; const int N = 2e5 + 10; const int inf = 1e9; char s[N]; struct Node { Node() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { dp[i][j] = (i == j) ? 0 : inf; } } } int dp[5][5]; }; Node operator*(const Node& u, const Node& v) { Node ans; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { ans.dp[i][j] = inf; for (int k = 0; k < 5; k++) { ans.dp[i][j] = min(u.dp[i][k] + v.dp[k][j], ans.dp[i][j]); } } } return ans; } Node nodes[N * 4]; void build_tree(int l, int r, int rt) { Node& v = nodes[rt]; if (l == r) { char c = s[l - 1]; if (c == 2 ) { v.dp[0][0] = 1; v.dp[0][1] = 0; } else if (c == 0 ) { v.dp[1][1] = 1; v.dp[1][2] = 0; } else if (c == 1 ) { v.dp[2][2] = 1; v.dp[2][3] = 0; } else if (c == 7 ) { v.dp[3][3] = 1; v.dp[3][4] = 0; } else if (c == 6 ) { v.dp[3][3] = 1; v.dp[4][4] = 1; } return; } int m = (l + r) >> 1; build_tree(l, m, 2 * rt); build_tree(m + 1, r, 2 * rt + 1); v = nodes[2 * rt] * nodes[2 * rt + 1]; } Node query(int L, int R, int l, int r, int rt) { Node& v = nodes[rt]; if (L <= l && R >= r) return v; int m = (l + r) >> 1; bool left = false, right = false; Node ltemp, rtemp; if (L <= m) { ltemp = query(L, R, l, m, rt * 2); left = true; } if (R > m) { rtemp = query(L, R, m + 1, r, rt * 2 + 1); right = true; } if (left && right) return ltemp * rtemp; if (left) return ltemp; if (right) return rtemp; } int main() { scanf( %d %d , &n, &q); scanf( %s , s); build_tree(1, n, 1); for (int i = 0; i < q; i++) { int l, r; scanf( %d %d , &l, &r); Node v = query(l, r, 1, n, 1); if (v.dp[0][4] == inf) cout << -1 << endl; else cout << v.dp[0][4] << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; struct Point { int x; int y; Point(int x_, int y_) : x(x_), y(y_) {} }; template <class T> T sqr(const T& t) { return t * t; } template <class T> T abs(const T& t) { return ((t > 0) ? (t) : (-t)); } void initialize() { freopen( _.in , r , stdin); freopen( _.out , w , stdout); } bool isSqr(int x) { int a = static_cast<int>(sqrt(static_cast<double>(x))); return (a * a == x); } int sqrt(int x) { return static_cast<int>(sqrt(static_cast<double>(x))); } int sign(int x) { if (x < 0) return 0; return 1; } int main() { int x, y; cin >> x >> y; int r = x * x + y * y; bool ok = true; int b = sqrt(r); int val = 2 + sign(x * y) + sqrt(r); if (isSqr(r) || (sign(x * y) + sqrt(r)) % 2 == 1) { cout << black ; } else { cout << white ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int res[100005], sz; bool prime[100005], used[100005]; int main() { int n; cin >> n; for (int p = 2; p <= n / 2; ++p) { if (prime[p]) continue; for (int j = p; j <= n; j += p) prime[j] = 1; if (p == 2) continue; int len = 0; for (int j = p; j <= n; j += p) { if (j == 2 * p) continue; if (used[j]) continue; used[j] = 1; res[sz++] = j; len++; } if (len & 1) { res[sz++] = 2 * p; used[2 * p] = 1; } } for (int i = 2; i <= n; i += 2) if (!used[i]) res[sz++] = i; cout << sz / 2 << endl; for (int i = 0; i + 1 < sz; i += 2) printf( %d %d n , res[i], res[i + 1]); return 0; }
|
#include <bits/stdc++.h> using namespace std; long long mul(long long a, long long b) { return (a * b) % 1000000007; } long long add(long long a, long long b) { a += b; if (a >= 1000000007) a -= 1000000007; return a; } long long binary_expo(long long base, long long expo) { long long res = 1; while (expo) { if (expo % 2) { res = mul(res, base); } expo /= 2; base = mul(base, base); } return res; } void solve() { long long n; cin >> n; cout << n << n ; for (long long i = 1; i <= n; i++) { cout << 1 ; } cout << n ; } signed main() { long long t; ios_base::sync_with_stdio(false); t = 1; while (t--) { solve(); } }
|
#include <bits/stdc++.h> using namespace std; long long n, a[3][333333], x, y, sum[4][333333], r[4][333333]; set<pair<long long, long long> > s[333333]; map<pair<long long, long long>, long long> d; map<long long, long long> last[4]; void read(long long &x) { scanf( %lld , &x); } int main() { cin >> n; for (long long i = 1; i <= 2; i++) for (long long j = 1; j <= n; j++) read(a[i][j]); for (long long i = 1; i <= 2; i++) for (long long j = n; j >= 1; j--) { sum[i][j] = sum[i][j + 1] + a[i][j]; sum[3][j] += sum[i][j]; } last[1][0] = last[2][0] = last[3][0] = n + 1; r[1][n + 1] = r[2][n + 1] = r[3][n + 1] = n + 1; for (long long i = 1; i <= 3; i++) for (long long j = n; j >= 1; j--) { r[i][j] = r[i][j + 1]; if (last[i].count(sum[i][j])) r[i][j] = min(r[i][j], last[i][sum[i][j]] - 1); last[i][sum[i][j]] = j; } s[0].insert(make_pair(0, 0)); d[make_pair(0, 0)] = 0; for (long long i = 0; i < n; i++) { for (pair<long long, long long> cur : s[i]) { x = cur.first; y = cur.second; if (x <= y) { s[min(x + 1, y)].insert(make_pair(x + 1, y)); d[make_pair(x + 1, y)] = max(d[make_pair(x + 1, y)], d[make_pair(x, y)]); s[min(r[1][x + 1], y)].insert(make_pair(r[1][x + 1], y)); d[make_pair(r[1][x + 1], y)] = max(d[make_pair(r[1][x + 1], y)], d[make_pair(x, y)] + 1); } if (y <= x) { s[min(x, y + 1)].insert(make_pair(x, y + 1)); d[make_pair(x, y + 1)] = max(d[make_pair(x, y + 1)], d[make_pair(x, y)]); s[min(r[2][y + 1], x)].insert(make_pair(x, r[2][y + 1])); d[make_pair(x, r[2][y + 1])] = max(d[make_pair(x, r[2][y + 1])], d[make_pair(x, y)] + 1); } if (x == y) { s[r[3][x + 1]].insert(make_pair(r[3][x + 1], r[3][x + 1])); d[make_pair(r[3][x + 1], r[3][x + 1])] = max(d[make_pair(r[3][x + 1], r[3][x + 1])], d[make_pair(x, y)] + 1); } d.erase(make_pair(x, y)); } s[i].clear(); } assert(d.size() <= 4 * n); cout << d[make_pair(n, n)]; }
|
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a > b) swap(a, b); if (a == 0) return b; return gcd(b % a, a); } int log2(int x, int max) { return max - __builtin_clz(x) - 1; } long long powerMod(long long x, long long y) { long long res = 1; x %= 1000000007; while (y > 0) { if (y & 1) res = (res * x) % 1000000007; y = y >> 1; x = (x * x) % 1000000007; } return res; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; int n, m, i, j, k, l, x, y, c; vector<pair<int, int> > v, dir; cin >> n >> m; string s[n]; int row[n][m]; int col[n][m]; memset((row), 0, sizeof((row))); memset((col), 0, sizeof((col))); for (i = 0; i < n; i++) { row[i][0] = 1; } for (i = 0; i < m; i++) { col[0][i] = 1; } for (i = 0; i < n; i++) { cin >> s[i]; for (j = 0; j < m; j++) { if (s[i][j] >= A && s[i][j] <= Z ) v.push_back(make_pair(i, j)); if (j > 0) { if (s[i][j] == # ) row[i][j] = row[i][j - 1] + 1; else row[i][j] = row[i][j - 1]; } if (i > 0) { if (s[i][j] == # ) col[i][j] = col[i - 1][j] + 1; else col[i][j] = col[i - 1][j]; } } } cin >> k; string st; for (i = 0; i < k; i++) { cin >> st >> l; if (st[0] == N ) dir.push_back(make_pair(0, l)); if (st[0] == S ) dir.push_back(make_pair(1, l)); if (st[0] == E ) dir.push_back(make_pair(2, l)); if (st[0] == W ) dir.push_back(make_pair(3, l)); } st = ; for (l = 0; l < v.size(); l++) { x = v[l].first, y = v[l].second; for (i = 0; i < k; i++) { j = dir[i].first; c = dir[i].second; if (j == 0) { if (x < c) goto loop; if (col[x][y] - col[x - c][y] == 0 && s[x - c][y] != # ) x -= c; else goto loop; } if (j == 1) { if (x + c >= n) goto loop; if (col[x + c][y] - col[x][y] == 0) x += c; else goto loop; } if (j == 2) { if (y + c >= m) goto loop; if (row[x][y + c] - row[x][y] == 0) y += c; else goto loop; } if (j == 3) { if (y < c) goto loop; if (row[x][y] - row[x][y - c] == 0 && s[x][y - c] != # ) y -= c; else goto loop; } } st += s[v[l].first][v[l].second]; loop:; } if (st == ) { cout << no solution n ; return 0; } sort(st.begin(), st.end()); cout << st << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9; const long long base = 1e9 + 7; const int N = 5e3 + 2; int n, m, k; int a[N][N]; pair<int, int> h[N], c[N]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> k; long long x, y, z; for (int i = (1), b_ = (k); i <= b_; ++i) { cin >> x >> y >> z; if (x == 1) { h[y].first = z; h[y].second = i; } else { c[y].first = z; c[y].second = i; } } for (int i = (1), b_ = (n); i <= b_; ++i) { for (int j = (1), b_ = (m); j <= b_; ++j) { if (h[i].second == 0 && c[j].second == 0) cout << 0 << ; else if (h[i].second > c[j].second) cout << h[i].first << ; else cout << c[j].first << ; } cout << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int S[1000007], X[1000007], Y[1000007], W[1000007], C[1000007]; char s[10]; void dfs(int x) { if (!S[x]) { W[x] = X[x]; return; } dfs(X[x]); if (S[x] > 1) dfs(Y[x]); if (S[x] == 1) W[x] = W[X[x]] ^ 1; if (S[x] == 2) W[x] = W[X[x]] & W[Y[x]]; if (S[x] == 3) W[x] = W[X[x]] | W[Y[x]]; if (S[x] == 4) W[x] = W[X[x]] ^ W[Y[x]]; } void change(int x) { if (!S[x]) return; if (S[x] == 1) C[X[x]] = C[x]; if (S[x] == 2) { if (!W[X[x]] && !W[Y[x]]) C[X[x]] = C[Y[x]] = 0; else if (W[X[x]] && W[Y[x]]) C[X[x]] = C[Y[x]] = C[x]; else C[X[x]] = W[X[x]] ? 0 : C[x], C[Y[x]] = W[Y[x]] ? 0 : C[x]; } if (S[x] == 3) { if (!W[X[x]] && !W[Y[x]]) C[X[x]] = C[Y[x]] = C[x]; else if (W[X[x]] && W[Y[x]]) C[X[x]] = C[Y[x]] = 0; else C[X[x]] = W[X[x]] ? C[x] : 0, C[Y[x]] = W[Y[x]] ? C[x] : 0; } if (S[x] == 4) C[X[x]] = C[Y[x]] = C[x]; change(X[x]); if (S[x] > 1) change(Y[x]); } int main() { int n, m, x, u, v, w, q; scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %s%d , s, &X[i]); if (s[0] == I ) S[i] = 0; if (s[0] == N ) S[i] = 1; if (s[0] == A ) S[i] = 2; if (s[0] == O ) S[i] = 3; if (s[0] == X ) S[i] = 4; if (S[i] > 1) scanf( %d , &Y[i]); } C[1] = 1; dfs(1); change(1); for (int i = 1; i <= n; i++) { if (!S[i]) printf( %d , W[1] ^ C[i]); } printf( n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 5, INF = (int)1e9; const long double EPS = 1e-9; int t[N], h[N], m[N]; bool used[N]; int main() { int n, cur, ans = 0, ans1 = 0, tp, v = -1, first; cin >> n >> first; cur = first; for (int i = 0; i < n; i++) cin >> t[i] >> h[i] >> m[i]; for (int i = 0; i < n; i++) if (t[i] == 0 && h[i] <= cur && (v == -1 || m[i] > m[v])) v = i; tp = 0; if (v != -1) { used[v] = 1; cur += m[v]; ans++; } for (int i = 0; i < n; i++) { v = -1; for (int j = 0; j < n; j++) if (!used[j] && t[j] != tp && h[j] <= cur && (v == -1 || m[j] > m[v])) v = j; if (v == -1) break; else { tp = 1 - tp; used[v] = 1; cur += m[v]; ans++; } } tp = 1; v = -1; cur = first; for (int i = 0; i < n; i++) used[i] = 0; for (int i = 0; i < n; i++) if (t[i] == 1 && h[i] <= cur && (v == -1 || m[i] > m[v])) v = i; if (v != -1) { used[v] = 1; cur += m[v]; ans1++; } for (int i = 0; i < n; i++) { v = -1; for (int j = 0; j < n; j++) if (!used[j] && t[j] != tp && h[j] <= cur && (v == -1 || m[j] > m[v])) v = j; if (v == -1) break; else { tp = 1 - tp; used[v] = 1; cur += m[v]; ans1++; } } cout << max(ans, ans1) << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; long long arr[maxn]; string s; int main() { std::ios::sync_with_stdio(false); int n; cin >> n; long long tot = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; arr[i] *= 2; tot += arr[i]; } cin >> s; long long W = 0, G = 0, flag = 5; for (int i = 0; i < n; i++) { if (s[i] == G ) G += arr[i]; else if (s[i] == W ) W += arr[i], flag = 3; long long cur = arr[i], sta; sta = min(cur / 2, W), tot += sta * 2, cur -= sta * 2, W -= sta; sta = min(cur / 2, G), tot += sta * 4, cur -= sta * 2, G -= sta; tot += flag * cur; } cout << tot / 2 << endl; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s, A( a ); cin >> s; vector<vector<int> > a(26); for (int i = 0; i < k; i++) a[s[i] - A[0]].push_back(i); for (int i = 0; i < n; i++) { cin >> s; long long ans = 0; for (int j = 0; j < s.size(); j++) { int b = (int)s[j] - A[0]; int r = (int)a[b].size(); if (r == 0) ans += s.size(); else { r--; int l = 0; while (r - l > 1) { int m = (l + r) / 2; if (a[b][m] >= j) r = m; else l = m; } ans += min(abs(a[b][l] - j), abs(a[b][r] - j)); } } cout << ans << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); void print_time() {} template <typename T> void update_max(T& x, T y) { x = max(x, y); } template <typename T> void update_min(T& x, T y) { x = min(x, y); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << ( << pair.first << , << pair.second << ) ; } template <class T> ostream& operator<<(ostream& out, vector<T> vec) { out << ( ; for (auto& v : vec) out << v << , ; return out << ) ; } template <class T> ostream& operator<<(ostream& out, set<T> vec) { out << ( ; for (auto& v : vec) out << v << , ; return out << ) ; } template <class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out << ( ; for (auto& v : vec) out << v << , ; return out << ) ; } long long ask(long long t, long long i, long long j, long long k) { ++i, ++j, ++k; cout << t << << i << << j << << k << endl; long long res; cin >> res; return res; } void ans(vector<long long> p) { cout << 0 ; for (long long pp : p) { cout << pp + 1 << ; } cout << endl; } int main() { cout << fixed << setprecision(20); ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long n; cin >> n; vector<long long> area(n); vector<long long> sign(n); for (long long i = 2; i < n; ++i) { area[i] = ask(1, 0, 1, i); sign[i] = ask(2, 0, 1, i); } vector<long long> cnt_sign(2); vector<set<pair<long long, long long>>> pos(2); for (long long i = 2; i < n; ++i) { pos[(sign[i] + 1) / 2].insert({area[i], i}); ++cnt_sign[(sign[i] + 1) / 2]; } vector<vector<long long>> sorted(2); for (long long kek = 0; kek < 2; ++kek) { sorted[kek] = vector<long long>(cnt_sign[kek]); long long l = 0; long long r = cnt_sign[kek] - 1; while (!pos[kek].empty()) { pair<long long, long long> curr = *pos[kek].begin(); pos[kek].erase(pos[kek].begin()); long long i = 0; long long j = curr.second; if (pos[kek].empty()) { sorted[kek][l] = j; break; } long long k = pos[kek].begin()->second; long long q = ask(2, i, j, k); if (q > 0) { sorted[kek][l++] = j; } else { sorted[kek][r--] = j; } } } vector<long long> p; p.push_back(0); for (long long v : sorted[0]) p.push_back(v); p.push_back(1); for (long long v : sorted[1]) p.push_back(v); ans(p); print_time(); return 0; }
|
#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while(t--) { long long n; cin >> n; string a[n]; for(long long i=0;i<n;i++) { cin >> a[i]; } for(long long i=0;i<10;i++) { long long ans=0; std::vector<pair<long long,long long> > x,y; for(long long j=0;j<n;j++) { for(long long k=0;k<n;k++) { if(a[j][k]==((char)i+ 0 )) { x.push_back(make_pair(j,k) ); y.push_back(make_pair(k,j) ); } } } sort(x.begin(), x.end()); sort(y.begin(), y.end()); for(long long k=0;k<x.size();k++) { long long x1=x[0].first,y1=x[0].second,x2=x[k].first,y2=x[k].second; ans=max(ans,abs(x1-x2)*max(max(y1,n-y1-1),max(y2,n-y2-1))); ans=max(ans,abs(y1-y2)*max(max(x1,n-x1-1),max(x2,n-x2-1))); } for(long long k=0;k<x.size();k++) { long long x1=x[k].first,y1=x[k].second,x2=x[x.size()-1].first,y2=x[x.size()-1].second; ans=max(ans,abs(x1-x2)*max(max(y1,n-y1-1),max(y2,n-y2-1))); ans=max(ans,abs(y1-y2)*max(max(x1,n-x1-1),max(x2,n-x2-1))); } for(long long k=0;k<y.size();k++) { long long y1=y[0].first,x1=y[0].second,y2=y[k].first,x2=y[k].second; ans=max(ans,abs(x1-x2)*max(max(y1,n-y1-1),max(y2,n-y2-1))); ans=max(ans,abs(y1-y2)*max(max(x1,n-x1-1),max(x2,n-x2-1))); } for(long long k=0;k<y.size();k++) { long long y1=y[k].first,x1=y[k].second,y2=y[y.size()-1].first,x2=y[y.size()-1].second; ans=max(ans,abs(x1-x2)*max(max(y1,n-y1-1),max(y2,n-y2-1))); ans=max(ans,abs(y1-y2)*max(max(x1,n-x1-1),max(x2,n-x2-1))); } //} cout << ans << ; } cout << endl; } }
|
#include <bits/stdc++.h> using namespace std; string t; vector<string> query(3); void ask(string &q) { cout << ? << q << n ; fflush(stdout); cin >> q; } int main() { ios_base::sync_with_stdio(false); ; cin >> t; for (int i = 0; i < t.length(); i++) { query[0] += (char)( a + (i % 26)); query[1] += (char)( a + ((i % (26 * 26)) / (26))); query[2] += (char)( a + (i % (26 * 26 * 26)) / (26 * 26)); } ask(query[0]); ask(query[1]); ask(query[2]); string ans(t.length(), ); for (int i = 0; i < t.length(); i++) { int pos = (query[0][i] - a ) + (query[1][i] - a ) * 26 + (query[2][i] - a ) * 26 * 26; ans[pos] = t[i]; } cout << ! << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 500010, INF = MAXN + 1; struct Query { int pos, l, r; }; struct AddEvent { int pos, value, start_valid; }; int n, m; int as[MAXN]; int close[MAXN]; Query queries[MAXN]; AddEvent add_events[MAXN]; int results[MAXN]; int st[4 * MAXN]; void update(int v, int l, int r, int ind, int val) { if (l == r) { assert(st[v] == INF); st[v] = val; } else { int m = (l + r) >> 1; if (ind <= m) update(v + v, l, m, ind, val); else update(v + v + 1, m + 1, r, ind, val); st[v] = min(st[v + v], st[v + v + 1]); } } int get(int v, int l, int r, int tl, int tr) { assert(l <= tl && tr <= r && tl <= tr); if (l == tl && r == tr) return st[v]; int res = INF, m = (l + r) >> 1; if (tl <= m) res = get(v + v, l, m, tl, min(tr, m)); if (tr > m) res = min(res, get(v + v + 1, m + 1, r, max(m + 1, tl), tr)); return res; } int main() { for (int i = 0; i < int(4 * MAXN); ++i) st[i] = INF; scanf( %d%d , &n, &m); for (int i = 0; i < int(n); ++i) scanf( %d , &as[i]); for (int i = 0; i < int(m); ++i) { scanf( %d%d , &queries[i].l, &queries[i].r); --queries[i].l, --queries[i].r; queries[i].pos = i; } sort(queries, queries + m, [](Query a, Query b) { return a.r < b.r; }); map<int, int> last; for (int i = n - 1; i >= 0; i--) { auto it = last.find(as[i]); if (it == last.end()) { close[i] = INF; } else { close[i] = it->second - i; } last[as[i]] = i; } for (int i = 0; i < int(n); ++i) { add_events[i] = {i, close[i], i + close[i]}; } sort(add_events, add_events + n, [](AddEvent a, AddEvent b) { return a.start_valid < b.start_valid; }); int qp = 0, ap = 0; for (int i = 0; i < n; i++) { while (ap < n && add_events[ap].start_valid == i) { update(1, 0, n - 1, add_events[ap].pos, add_events[ap].value); ++ap; } while (qp < m && queries[qp].r == i) { results[queries[qp].pos] = get(1, 0, n - 1, queries[qp].l, queries[qp].r); ++qp; } } for (int i = 0; i < int(m); ++i) { if (results[i] == INF) results[i] = -1; printf( %d n , results[i]); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 11; int a[N], b[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a[i] >> b[i]; } vector<int> v; for (int p = 1; p <= 200; p++) { int x = 0; for (int i = 1; i <= m; i++) { int d = (a[i] / p); if (a[i] % p != 0) d++; if (b[i] != d) { x = 1; break; } } if (x == 0) { int d = (n / p); if (n % p != 0) d++; v.push_back(d); } } sort(v.begin(), v.end()); if (v[0] != v[v.size() - 1]) { cout << -1 << endl; } else cout << v[0] << endl; }
|
#include <bits/stdc++.h> using namespace std; vector<int> sieve(int n) { vector<bool> prime(n + 1, true); prime[0] = false; prime[1] = false; int m = sqrt(n); vector<int> ans; for (int p = 2; p <= m; p++) { if (prime[p]) { for (int i = p * 2; i <= n; i += p) prime[i] = false; } } for (int i = 0; i < n; i++) if (prime[i]) ans.push_back(i); return ans; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int mod = 1e9 + 7; int power(int a, long long b) { int res = 1; while (b) { if (b & 1) res = 1LL * res * a % mod; a = 1LL * a * a % mod; b >>= 1; } return res; } long long min(int a, long long b) { if (a > b) return b; return a; } long long max(int a, long long b) { if (a > b) return a; return b; } void solve() { long long n, m; cin >> n >> m; vector<pair<int, int>> p; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; p.push_back({x, y}); } long long ans = max(p[0].first + p[0].second - 1, p[m - 1].second + (n - p[m - 1].first)); for (int i = 1; i < m; i++) { if (abs(p[i].second - p[i - 1].second) > p[i].first - p[i - 1].first) { cout << IMPOSSIBLE ; return; } ans = max(ans, max(p[i].second, p[i - 1].second) + (p[i].first - p[i - 1].first - abs(p[i].second - p[i - 1].second)) / 2); } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int mul = 0; if (mul == 1) { int t; cin >> t; while (t--) { solve(); cout << n ; } } else { solve(); } }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; const int MOD = 1e9 + 7; int n; long long int a[MAXN], minn; bool have[MAXN], flag; int main() { while (scanf( %d , &n) != EOF) { a[1] = 0; minn = 0; memset(have, false, sizeof(have)); flag = true; for (int i = 1; i < n; ++i) { int num; scanf( %I64d , &num); a[i + 1] = a[i] + num; minn = min(minn, a[i + 1]); } for (int i = 1; i <= n; ++i) { a[i] += -minn + 1; if (a[i] > n || have[a[i]]) { printf( -1 n ); flag = false; break; } have[a[i]] = true; } if (flag) { for (int i = 1; i <= n; ++i) { i == n ? printf( %I64d n , a[i]) : printf( %I64d , a[i]); } } } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << ( ; os << v.first << , << v.second << ) ; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << [ ; long long sz = v.size(); for (long long i = 0; i < sz; ++i) { os << v[i]; if (i != sz - 1) os << , ; } os << ] n ; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { T last = *v.rbegin(); os << [ ; for (auto it : v) { os << it; if (it != last) os << , ; } os << ] n ; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto it : v) { os << it.first << : << it.second << n ; } return os; } long long power(long long a, long long b) { long long res = 1; while (b) { if (b % 2) { res = (res * a) % 1000000007; } b /= 2; a = (a * a) % 1000000007; } return res; } template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << : << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, , ); cerr.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } const long long N = 1005; long long arr[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; for (__typeof(1) i = 1; i <= n; i++) cin >> arr[i]; long long res = 0; for (__typeof(2) i = 2; i <= n - 1; i++) { if ((arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) || (arr[i] < arr[i - 1] && arr[i] < arr[i + 1])) ++res; } cout << res << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 7; int A[N], B[N], n, ans, pos[N], s1[N], s2[N], cnt; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> A[i], pos[A[i]] = i; for (int i = 1; i <= n; ++i) cin >> B[i], A[pos[B[i]]] = i; for (int i = 1; i <= n; ++i) pos[A[i]] = i; ZXY:; int st = 1; while (A[st] == st) st++; for (int i = st; i <= n; ++i) if (A[i] > i) { for (int j = i + 1; j <= n; ++j) if (A[j] <= i && j <= A[i]) { ans += j - i; s1[++cnt] = i, s2[cnt] = j; swap(A[i], A[j]); goto ZXY; } } cout << ans << endl << cnt << endl; for (int i = 1; i <= cnt; ++i) cout << s1[i] << << s2[i] << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long n, res = 0, a, b; cin >> n; vector<pair<long long, long long>> nums; for (int i = 0; i < n; i++) { cin >> a >> b; nums.push_back({a, b}); } for (int i = 0; i < n; i++) { bool l = false, r = false, u = false, d = false; for (int j = 0; j < n; j++) if (i != j) { if (nums[j].second == nums[i].second && nums[j].first > nums[i].first) r = true; if (nums[j].second == nums[i].second && nums[j].first < nums[i].first) l = true; if (nums[j].second > nums[i].second && nums[j].first == nums[i].first) u = true; if (nums[j].second < nums[i].second && nums[j].first == nums[i].first) d = true; } res += (bool)(l && r && u && d); } cout << res; return 0; }
|
#include <bits/stdc++.h> int main() { int a, b, c, d; while (scanf( %d%d%d%d , &a, &b, &c, &d) == 4) { if (c >= b - 1 && c <= 2 * (b + 1)) { printf( YES n ); continue; } if (d >= a - 1 && d <= 2 * (a + 1)) { printf( YES n ); continue; } printf( NO n ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int A[1000001]; int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } map<int, int> change_v; int above = 0, below = 0; long long sum = 0; long long best_i = 0; for (int i = 0; i < n; i++) { int xs = A[i] - i - 1; int x = (xs > 0) ? xs : -1 * xs; if (xs < 0) { change_v[x]++; } if (xs < 0) { below++; } else { above++; } sum += x; } long long best_sum = sum; for (int i = 0; i < n - 1; i++) { long long xs = A[i] - n; long long x = (xs > 0) ? xs : -1 * xs; long long cnt = change_v[i + 1]; if (xs < 0) { change_v[x + i + 1]++; } int cur_below_d = (A[i] - 1 < 0) ? 1 : 0; int cur_above_d = (A[i] - 1 >= 0) ? 1 : 0; sum -= below - cur_below_d; sum += above - cur_above_d; sum -= (A[i] - 1 > 0) ? A[i] - 1 : 1 - A[i]; sum += x; above += cnt; below -= cnt; if (xs < 0 && A[i] - 1 >= 0) { below++; above--; } if (sum < best_sum) { best_i = i + 1; best_sum = sum; } } if (best_i != 0) { best_i = n - best_i; } cout << best_sum << << best_i; }
|
#include <bits/stdc++.h> using namespace std; template <class T> T power(T a, T b) { T x; if (b == 0) x = 1; else x = a; for (size_t i = 1; i < b; i++) x *= a; return x; } long long int gcd(long long int a, long long int b) { return b == 0 ? a : gcd(b, a % b); } long long int n, m, a, b, c, ab, ba, x, y, z, avg, total, ans, sum; long long int arr2[300001], arr3[100001], arr4[100001], arr5[100001]; char c1, c2; vector<pair<string, long long int> > vec; vector<long long int> v; bool f, f1, f2, found; string str, s1, s2, s3, s4; map<long long int, long long int> mp, mp1; set<int> seto; double d, d1; long long int x1, x2, y11, y2; long long int mini = 10000000000, maxi = 0; set<string> sets; int main() { cin >> n >> str; for (long long int i = 0; i < str.size(); i++) { if (str[i] == > ) f1 = 1; else f = 1; } if (f && f1) { for (long long int i = 0; i < str.size(); i++) if (str[i] == < ) sum++; else break; reverse(str.rbegin(), str.rend()); for (long long int i = 0; i < str.size(); i++) if (str[i] == > ) sum++; else break; } else sum = str.size(); cout << sum; }
|
#include <bits/stdc++.h> using namespace std; const int nm = 2e5 + 3; const long long mod = 1e9 + 7; int n; int pos[nm]; int main() { ios::sync_with_stdio(0), cin.tie(0); while (cin >> n) { for (int i = 0; i < n; i++) cin >> pos[i]; sort(pos, pos + n); int step = n / 2 - 1; int res = pos[n - 1] - pos[0]; pair<double, int> loc; for (int i = 0; i <= step; i++) { if (pos[i + n - step - 1] - pos[i] < res) { res = pos[i + n - step - 1] - pos[i]; loc.first = i, loc.second = i + n - step - 1; } } cout << res << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 150; int n, dp[N][N * N], sum, cnt[N], x, u[N][N * N], res; int main() { cin >> n; for (int i = 1; i <= n; i++) scanf( %d , &x), sum += x, cnt[x]++; dp[0][0] = 1; for (int i = 1; i <= 100; i++) { res += (cnt[i] > 0); for (int j = n; j >= 1; j--) { for (int cn = 1; cn <= min(j, cnt[i]); cn++) { for (int l = i * cn; l <= sum; l++) { dp[j][l] += dp[j - cn][l - i * cn]; } } } } if (res == 1 || res == 2) { cout << n << endl; return 0; } for (int i = 1; i <= 100; i++) for (int cn = 1; cn <= cnt[i]; cn++) u[cn][i * cn] = 1; for (int i = n; i >= 1; i--) { for (int j = 0; j <= sum; j++) { if (dp[i][j] == 1 && u[i][j] == 1) { cout << i << endl; return 0; } } } }
|
#include <bits/stdc++.h> using namespace std; long long t, xiaoyud, dayu, n, x, pos, ans1, mo = 1e9 + 7, ans2, ans; int main() { cin >> n >> x >> pos; long long l = 0, r = n; while (l < r) { long long mid = (l + r) / 2; if (mid <= pos) { l = mid + 1; if (mid != pos) xiaoyud++; } else { r = mid; dayu++; } } ans1 = 1; for (long long i = 1; i <= xiaoyud; i++) { ans1 = (ans1 * (x - 1 - i + 1)) % mo; } ans2 = 1; for (long long i = 1; i <= dayu; i++) { ans2 = (ans2 * (n - x - i + 1)) % mo; } ans = (ans1 * ans2) % mo; for (long long i = 1; i <= n - xiaoyud - dayu - 1; i++) { ans = (ans * (n - xiaoyud - dayu - i)) % mo; } cout << ans; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, a, od = 0, ev = 0, ans = 0; cin >> n; vector<int> vec; vector<int>::iterator it; while (n--) { cin >> a; vec.push_back(a); if (a % 2 == 0) ev++; else od++; } for (it = vec.begin(); it != vec.end(); it++) { if (ev > od) { if (*it % 2 != 0) ans = distance(vec.begin(), it); } else if (od > ev) { if (*it % 2 == 0) ans = distance(vec.begin(), it); } } cout << ans + 1; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; string s, t; int cnt[10], n, Num, tmp[10], cntT[10], m, tmpCnt[10]; string lmao1 = , lmao2 = , lmao3 = ; int main() { cin.tie(0), ios_base::sync_with_stdio(0); cin >> s; cin >> t; m = (int)(t.length()); n = (int)(s.length()); for (int i = 0; i < m; i++) cntT[(int)(t[i] - 0 )]++; for (int i = 0; i < n; i++) cnt[(int)(s[i] - 0 )]++; if ((s == 01 || s == 10 ) && t == 0 ) { cout << 0 ; exit(0); } for (int i = 1; i <= n; i++) { for (int j = 0; j < 10; j++) tmp[j] = 0; int x = i, sumD = 0; while (x) { tmp[x % 10]++; x /= 10; sumD++; } if (sumD + i != n) continue; bool ok = 1; for (int j = 0; j < 10; j++) ok &= (cnt[j] >= tmp[j]); for (int j = 0; j < 10; j++) ok &= (cnt[j] - tmp[j] >= cntT[j]); int digits = 0; for (int j = 1; j < 10; j++) digits += (cnt[j] - tmp[j] > 0); ok &= (digits > 0); if (!ok) continue; Num = i; for (int j = 0; j < 10; j++) cnt[j] -= tmp[j]; break; } bool containT = 0; int lim = 0; for (int j = 0; j < 10; j++) tmpCnt[j] = cnt[j]; string ans = ; bool ok = 1; for (int i = 1; i <= Num; i++) { if (i <= lim) continue; bool ck = 0; for (int j = (i == 1); j <= 9; j++) { if (!cnt[j]) continue; cnt[j]--; bool uk = 1; if (!containT) { for (int k = 0; k <= 9; k++) uk &= (cnt[k] >= cntT[k]); if (i != 1) { if (j == t[0] - 0 ) { cnt[j]++; lmao1 += t; for (int k = 0; k <= 9; k++) cnt[k] -= cntT[k]; containT = 1; lim = i + m - 1; ck = 1; break; } } if (!uk) { cnt[j]++; continue; } } ck = 1; lmao1 += ( 0 + j); break; } ok &= ck; } if (!ok) lmao1 = ; containT = 0, lim = 0; ok = 1; for (int j = 0; j < 10; j++) cnt[j] = tmpCnt[j]; for (int i = 1; i <= Num; i++) { if (i <= lim) continue; bool ck = 0; for (int j = (i == 1); j <= 9; j++) { if (!cnt[j]) continue; cnt[j]--; bool uk = 1; if (!containT) { for (int k = 0; k <= 9; k++) uk &= (cnt[k] >= cntT[k]); if (j == t[0] - 0 ) { cnt[j]++; lmao3 += t; for (int k = 0; k <= 9; k++) cnt[k] -= cntT[k]; containT = 1; lim = i + m - 1; ck = 1; break; } if (!uk) { cnt[j]++; continue; } } ck = 1; lmao3 += ( 0 + j); break; } ok &= ck; } if (!ok) lmao2 = ; containT = 0, lim = 0; ok = 1; for (int j = 0; j < 10; j++) cnt[j] = tmpCnt[j]; for (int i = 1; i <= Num; i++) { if (i <= lim) continue; bool ck = 0; for (int j = (i == 1); j <= 9; j++) { if (!cnt[j]) continue; cnt[j]--; bool uk = 1; if (!containT) { for (int k = 0; k <= 9; k++) uk &= (cnt[k] >= cntT[k]); cnt[j]++; if (cnt[j] - cntT[j] == 0 && j == t[0] - 0 ) { lmao2 += t; for (int k = 0; k <= 9; k++) cnt[k] -= cntT[k]; containT = 1; lim = i + m - 1; ck = 1; break; } cnt[j]--; if (!uk) { cnt[j]++; continue; } } ck = 1; lmao2 += ( 0 + j); break; } ok &= ck; } if (!ok) lmao3 = ; if (lmao1 != ) ans = lmao1; if (lmao2 != ) ans = lmao2; if (lmao3 != ) ans = lmao3; if (lmao1 != ) ans = min(ans, lmao1); if (lmao2 != ) ans = min(ans, lmao2); if (lmao3 != ) ans = min(ans, lmao3); cout << ans; }
|
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) using namespace std; int a[510][510]; int mi[510][510]; int main() { ios_base::sync_with_stdio(0); int n, m, k, q; cin >> n >> m >> k >> q; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { a[i][j] = -1; mi[i][j] = -1; } for (int i = 1; i <= q; i++) { int x, y, z; cin >> x >> y >> z; a[x][y] = z; } for (int i = 1; i <= n; i++) { multiset<int> st; for (int j = 1; j <= k; j++) { st.insert(a[i][j]); } for (int j = k + 1; j <= m + 1; j++) { auto kk = st.end(); kk--; if ((*st.begin()) != -1) mi[i][j - k] = (*kk); st.erase(st.find(a[i][j - k])); st.insert(a[i][j]); } } int ans = 2e9; for (int j = 1; j <= m - k + 1; j++) { multiset<int> st; for (int i = 1; i <= k; i++) { st.insert(mi[i][j]); } for (int i = k + 1; i <= n + 1; i++) { auto kk = st.end(); kk--; if ((*st.begin()) != -1) ans = min(ans, (*kk)); st.erase(st.find(mi[i - k][j])); st.insert(mi[i][j]); } } if (ans == 2e9) cout << -1; else cout << ans; }
|
#include <bits/stdc++.h> using namespace std; struct node { node* child[26]; priority_queue<int> pq; bool leaf; node() { memset(child, 0, sizeof child); leaf = false; } }; void insert(node* curr, const string& x) { for (auto c : x) { if (!curr->child[c - a ]) curr->child[c - a ] = new node(); curr = curr->child[c - a ]; } curr->leaf = true; } void dfs(node* curr, int lev) { for (int i = 0; i < 26; i++) { if (!curr->child[i]) continue; dfs(curr->child[i], lev + 1); while (!curr->child[i]->pq.empty()) curr->pq.push(curr->child[i]->pq.top()), curr->child[i]->pq.pop(); } if (curr->leaf) curr->pq.push(lev); else { if (!curr->pq.empty() && lev) curr->pq.pop(), curr->pq.push(lev); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; node* root = new node(); while (n--) { string x; cin >> x; insert(root, x); } dfs(root, 0); long long ans = 0; while (!root->pq.empty()) ans += root->pq.top(), root->pq.pop(); cout << ans << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; inline int Get() { int res = 0, q = 1; char ch = getchar(); while ((ch < 0 || ch > 9 ) && ch != - ) ch = getchar(); if (ch == - ) q = -1, ch = getchar(); while (ch >= 0 && ch <= 9 ) res = res * 10 + ch - 0 , ch = getchar(); return res * q; } const double eps = 1e-12, pi = M_PI; const int oo = (int)2e9, mod = (int)1e9 + 7; const long long INF = (long long)1e17; const int N = (int)1e5; typedef int arr[N + 10]; arr a, b, c, lstx, lsty, id, u, v, sc; bool vs[10]; int d[10], ord[10]; int n, m; bool cmp_x(const int &x, const int &y) { return a[x] < a[y]; } bool cmp_y(const int &x, const int &y) { return b[x] < b[y]; } struct node { node *ch[2]; int s; } * Rt[N + 10]; node *New() { node *res = new node; res->ch[0] = res->ch[1] = NULL, res->s = 0; return res; } node *Copy(node *x) { node *res = new node; res->ch[0] = x->ch[0], res->ch[1] = x->ch[1], res->s = x->s; return res; } node *New(node *x, int d) { if (!x->ch[d]) return New(); return Copy(x->ch[d]); } void Update(node *x) { x->s = 0; if (x->ch[0]) x->s += x->ch[0]->s; if (x->ch[1]) x->s += x->ch[1]->s; } void Add(int l, int r, node *&x, int pos) { if (l == r) { ++x->s; return; } int mid = (l + r) >> 1; if (pos <= mid) Add(l, mid, x->ch[0] = New(x, 0), pos); else Add(mid + 1, r, x->ch[1] = New(x, 1), pos); Update(x); } int Query(int l, int r, node *x, int ml, int mr) { if (!x || ml > mr) return 0; if (l >= ml && r <= mr) return x->s; int mid = (l + r) >> 1, res = 0; if (ml <= mid) res += Query(l, mid, x->ch[0], ml, mr); if (mr > mid) res += Query(mid + 1, r, x->ch[1], ml, mr); return res; } void Init() { scanf( %d n , &n); for (int i = (1), end = (n); i <= end; ++i) scanf( %d %d n , a + i, b + i), u[i] = a[i], v[i] = b[i], c[i] = a[i], lstx[i] = i, lsty[i] = i; for (int i = (1), end = (9); i <= end; ++i) scanf( %d , d + i); c[n + 1] = oo; sort(c + 1, c + n + 1); int cnt = 0; for (int i = (1), end = (n); i <= end; ++i) if (c[i] != c[i + 1]) c[++cnt] = c[i]; for (int i = (1), end = (n); i <= end; ++i) a[i] = lower_bound(c + 1, c + cnt + 1, a[i]) - c; for (int i = (1), end = (n); i <= end; ++i) c[i] = b[i]; sort(c + 1, c + n + 1); cnt = 0; for (int i = (1), end = (n); i <= end; ++i) if (c[i] != c[i + 1]) c[++cnt] = c[i]; m = cnt; for (int i = (1), end = (n); i <= end; ++i) b[i] = lower_bound(c + 1, c + cnt + 1, b[i]) - c; sort(lstx + 1, lstx + n + 1, cmp_x); sort(lsty + 1, lsty + n + 1, cmp_y); Rt[0] = New(); int tot = 0; for (int i = (1), end = (n); i <= end; ++i) { int x = lstx[i]; if (a[x] == a[lstx[i - 1]]) Rt[i] = Rt[sc[tot]], id[x] = tot; else Rt[i] = Copy(Rt[sc[tot]]), sc[++tot] = i, id[x] = tot; Add(1, m, Rt[i], b[x]); } } void Check() { int x1 = lstx[ord[1] + ord[4] + ord[7]], x2 = lstx[ord[1] + ord[2] + ord[4] + ord[5] + ord[7] + ord[8] + 1]; int y1 = lsty[ord[7] + ord[8] + ord[9]], y2 = lsty[ord[7] + ord[8] + ord[9] + ord[4] + ord[5] + ord[6] + 1]; int num = Query(1, m, Rt[sc[id[x1]]], b[y2], m); if (num != ord[1]) return; num = Query(1, m, Rt[sc[id[x1]]], b[y1] + 1, b[y2] - 1); if (num != ord[4]) return; num = Query(1, m, Rt[sc[id[x1]]], 1, b[y1]); if (num != ord[7]) return; num = Query(1, m, Rt[sc[id[x2] - 1]], b[y2], m) - ord[1]; if (num != ord[2]) return; num = Query(1, m, Rt[sc[id[x2] - 1]], 1, b[y1]) - ord[7]; if (num != ord[8]) return; num = Query(1, m, Rt[n], b[y2], m) - ord[1] - ord[2]; if (num != ord[3]) return; num = Query(1, m, Rt[n], 1, b[y1]) - ord[7] - ord[8]; if (num != ord[9]) return; num = Query(1, m, Rt[n], b[y1] + 1, b[y2] - 1) - Query(1, m, Rt[sc[id[x2] - 1]], b[y1] + 1, b[y2] - 1); if (num != ord[6]) return; double px1 = (double)u[x1] + 0.5, px2 = (double)u[x2] - 0.5; double py1 = (double)v[y1] + 0.5, py2 = (double)v[y2] - 0.5; printf( %.8lf %.8lf n , px1, px2); printf( %.8lf %.8lf n , py1, py2); exit(0); } void Dfs(int x) { if (x > 9) { Check(); return; } for (int i = (1), end = (9); i <= end; ++i) if (!vs[i]) { vs[i] = 1, ord[x] = d[i]; Dfs(x + 1); vs[i] = 0; } } int main() { Init(); Dfs(1); printf( -1 n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxN = 100010; int n, m, res, l[3][maxN], r[3][maxN], g[16 * maxN], t[16 * maxN], ans[maxN], con[maxN]; bool avail[maxN]; stack<int> s[4 * maxN]; struct ioi { int v, p, t, k; }; ioi c[4 * maxN]; bool cmp(ioi i, ioi j) { if (i.v < j.v) return true; if (i.v > j.v) return false; if (i.t < j.t) return true; if (i.t > j.t) return false; if (i.t == 1) return (i.k > j.k); else return (i.k < j.k); } void update(int k, int l, int r, int i, int x) { if ((i < l) || (i > r)) return; if (l == r) { t[k] += x; if (t[k]) g[k] = l; else g[k] = 0; return; } int mid = (l + r) >> 1; update(k * 2, l, mid, i, x); update(k * 2 + 1, mid + 1, r, i, x); t[k] = t[k * 2] + t[k * 2 + 1]; if (g[k * 2] == 0) g[k] = g[k * 2 + 1]; else if (g[k * 2 + 1] == 0) g[k] = g[k * 2]; else g[k] = min(g[k * 2], g[k * 2 + 1]); } int get(int k, int l, int r, int i) { if (r < i) return 0; if (l >= i) return g[k]; if (l == r) return 0; int mid = (l + r) >> 1; int tmp_1 = get(k * 2, l, mid, i); int tmp_2 = get(k * 2 + 1, mid + 1, r, i); if (!tmp_1) return tmp_2; if (!tmp_2) return tmp_1; return min(tmp_1, tmp_2); } int main() { scanf( %d , &n); int x, y, z, num = 0; for (int i = 1; i <= n; ++i) { scanf( %d%d , &x, &y); l[1][i] = x; r[1][i] = y; c[++num].v = x; c[num].t = 1; c[num].k = 1; c[num].p = i; c[++num].v = y; c[num].t = 2; c[num].k = 1; c[num].p = i; } scanf( %d , &m); for (int i = 1; i <= m; ++i) { scanf( %d%d%d , &x, &y, &z); l[2][i] = x; r[2][i] = y; con[i] = z; c[++num].v = x; c[num].t = 1; c[num].k = 2; c[num].p = i; c[++num].v = y; c[num].t = 2; c[num].k = 2; c[num].p = i; } sort(c + 1, c + num + 1, cmp); int val = c[1].v; c[1].v = 1; for (int i = 2; i <= num; ++i) { int tmp_val = c[i].v; if (c[i].v != val) c[i].v = c[i - 1].v + 1; else c[i].v = c[i - 1].v; if (c[i].t == 1) l[c[i].k][c[i].p] = c[i].v; else r[c[i].k][c[i].p] = c[i].v; val = tmp_val; } for (int i = 1; i <= num; ++i) { if (c[i].k == 1) { if (c[i].t == 1) { int u = get(1, 1, c[num].v, r[1][c[i].p]); if (u == 0) { res = -1; break; } while ((!s[u].empty()) && (!avail[s[u].top()])) s[u].pop(); if (u == 0) { res = -1; break; } u = s[u].top(); ans[c[i].p] = u; con[u]--; if (!con[u]) update(1, 1, c[num].v, r[2][u], -1), avail[u] = 0; } } else if (c[i].t == 1) { update(1, 1, c[num].v, r[2][c[i].p], 1); s[r[2][c[i].p]].push(c[i].p); avail[c[i].p] = 1; } else { update(1, 1, c[num].v, c[i].v, -1); avail[c[i].p] = 0; } } if (res == -1) printf( NO ); else { printf( YES n ); for (int i = 1; i <= n; ++i) printf( %d , ans[i]); } fclose(stdin); fclose(stdout); }
|
#include <bits/stdc++.h> using namespace std; using INT = long long; using pii = pair<INT, INT>; using pi3 = pair<pii, INT>; INT a[5010]; bool dp[5010][5010]; INT val[5010]; vector<pi3> ans; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); INT n, k, v; cin >> n >> k >> v; for (INT i = 1; i <= n; i++) cin >> a[i]; INT sum = 0; for (INT i = 1; i <= n; i++) sum += a[i]; if (sum < v) { cout << NO << endl; return 0; } for (INT i = 0; i < k; i++) val[i] = n + 1; dp[0][0] = 1; for (INT i = 1; i <= n; i++) { for (INT j = 0; j < k; j++) if (dp[i - 1][j]) { INT temp = (j + a[i]) % k; dp[i][temp] = 1; dp[i][j] = 1; val[temp] = min(val[temp], i); } dp[i][a[i] % k] = 1; val[a[i] % k] = min(val[a[i] % k], i); } INT p = v % k; int fff = 0; INT ID, sz; if (p == 0) { cout << YES << endl; if (a[1]) cout << (a[1] + k - 1) / k << 1 2 << endl; a[2] += a[1]; fff = 1; ID = 1; sz = 0; } if (!fff) { INT id = val[p]; if (id == n + 1) { cout << NO << endl; return 0; } cout << YES << endl; ID = id; p -= (a[id] % k); if (p < 0) p += k; sz = a[id]; id = val[p]; while (p != 0) { p -= (a[id] % k); ans.push_back(pi3(pii(a[id] / k + 1, id), ID)); sz += a[id]; a[id] = 0; if (p < 0) p += k; id = val[p]; } } INT sr = 1; if (ID == sr) sr = n; for (INT i = 1; i <= n; i++) { if (a[i] == 0 || i == ID || i == sr) continue; ans.push_back(pi3(pii((a[i] + k - 1) / k, i), sr)); } INT val = (v - sz) / k; if (val > 0) { ans.push_back(pi3(pii(val, sr), ID)); } if (val < 0) { ans.push_back(pi3(pii(-val, ID), sr)); } for (auto p : ans) { INT u = p.first.first; INT sr = p.first.second; INT ta = p.second; cout << u << << sr << << ta << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m, k; vector<vector<int> > g; vector<vector<int> > revg; int active[1000005]; int del; int limx, limy, limz; unordered_set<int> visited; void add(int a, int b) { if (active[a] && active[b]) { g[a].push_back(b); revg[b].push_back(a); } } int get(int a, int b, int c) { return a * m * k + b * k + c; } bool downlimit(int v) { if (v == del) return false; int z(v % k); v /= k; int y(v % m); int x(v / m); return x <= limx && y <= limy && z <= limz; } void dfs(int v) { visited.insert(v); for (int i(0), _l((int)(((int)g[v].size())) - 1); i <= _l; ++i) { int to(g[v][i]); if (downlimit(to)) dfs(to); } } void solve() { memset((active), 0, sizeof(active)); scanf( %d%d%d n , &n, &m, &k); g.resize(n * m * k); revg.resize(n * m * k); for (int i(0), _l((int)(n)-1); i <= _l; ++i) for (int j(0), _l((int)(m)-1); j <= _l; ++j) { string s; getline(cin, s); for (int q(0), _l((int)(k)-1); q <= _l; ++q) active[get(i, j, q)] = (s[q] == 1 ); if (j == m - 1) getline(cin, s); } for (int i(0), _l((int)(n)-1); i <= _l; ++i) for (int j(0), _l((int)(m)-1); j <= _l; ++j) for (int q(0), _l((int)(k)-1); q <= _l; ++q) { if (i < n - 1) add(get(i, j, q), get(i + 1, j, q)); if (j < m - 1) add(get(i, j, q), get(i, j + 1, q)); if (q < k - 1) add(get(i, j, q), get(i, j, q + 1)); } int ans(0); for (int x(0), _l((int)(n)-1); x <= _l; ++x) for (int y(0), _l((int)(m)-1); y <= _l; ++y) for (int z(0), _l((int)(k)-1); z <= _l; ++z) { int v(get(x, y, z)); del = v; limx = x + 1, limy = y + 1, limz = z + 1; bool frogs(false); for (int i(0), _l((int)(((int)revg[v].size())) - 1); i <= _l; ++i) { int from(revg[v][i]); visited.clear(); dfs(from); for (int j(0), _l((int)(((int)g[v].size())) - 1); j <= _l; ++j) { int to(g[v][j]); if (!visited.count(to)) frogs = true; if (frogs) break; } if (frogs) break; } ans += frogs; } printf( %d n , ans); } int main() { solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; inline int d_compare(double x, double y) { return (fabs(x - y) <= 1e-8) ? 0 : (x > y) ? 1 : -1; } inline int mod(long long x, int y) { if (y == 0) return x; if (x < 0) x = -x; if (x >= y) x %= y; return x; } inline int cmod(int x, int y) { if (y == 0) return x; if (x < 0) x += (abs(x) / y + 1) * y; if (x >= y) x = x % y; return x; } int Bcount(int mask) { int n = 0; while (mask) n += ((mask & -mask) != 0), mask &= ~(mask & -mask); return n; } inline bool Bstate(int mask, int idx) { return (mask & (1 << idx)); } inline int B1(int mask, int idx) { return mask | (1 << idx); } inline int B0(int mask, int idx) { return mask & ~(1 << idx); } inline int drift(int mask, int n) { return mask & ((1 << n) - 1); } void debugmask(int mask, int n) { for (int i = 0; i < n; i++) cout << Bstate(mask, i); } class point { public: int x, y; point(int i, int j) { x = i; y = j; } }; bool arr[2002][2002]; int n, m; bool check(point wall) { if (arr[wall.x + 1][wall.y] && arr[wall.x][wall.y + 1] && arr[wall.x + 1][wall.y + 1]) return 1; if (arr[wall.x - 1][wall.y] && arr[wall.x][wall.y - 1] && arr[wall.x - 1][wall.y - 1]) return 1; if (arr[wall.x + 1][wall.y] && arr[wall.x][wall.y - 1] && arr[wall.x + 1][wall.y - 1]) return 1; if (arr[wall.x - 1][wall.y] && arr[wall.x][wall.y + 1] && arr[wall.x - 1][wall.y + 1]) return 1; return 0; } void dfs(point wall) { if (wall.x > n || wall.y > m) return; arr[wall.x][wall.y] = 1; if (!arr[wall.x - 1][wall.y] && check(point(wall.x - 1, wall.y))) dfs(point(wall.x - 1, wall.y)); if (!arr[wall.x + 1][wall.y] && check(point(wall.x + 1, wall.y))) dfs(point(wall.x + 1, wall.y)); if (!arr[wall.x][wall.y - 1] && check(point(wall.x, wall.y - 1))) dfs(point(wall.x, wall.y - 1)); if (!arr[wall.x][wall.y + 1] && check(point(wall.x, wall.y + 1))) dfs(point(wall.x, wall.y + 1)); if (!arr[wall.x - 1][wall.y + 1] && check(point(wall.x - 1, wall.y + 1))) dfs(point(wall.x - 1, wall.y + 1)); if (!arr[wall.x + 1][wall.y - 1] && check(point(wall.x + 1, wall.y - 1))) dfs(point(wall.x + 1, wall.y - 1)); if (!arr[wall.x - 1][wall.y - 1] && check(point(wall.x - 1, wall.y - 1))) dfs(point(wall.x - 1, wall.y - 1)); if (!arr[wall.x + 1][wall.y + 1] && check(point(wall.x + 1, wall.y + 1))) dfs(point(wall.x + 1, wall.y + 1)); } void input() { memset(arr, 0, sizeof arr); char c; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> c; arr[i][j] = (c == . ); } } int main() { std::ios_base::sync_with_stdio(false); input(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (!arr[i][j] && check(point(i, j))) dfs(point(i, j)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) if (arr[i][j]) cout << . ; else cout << * ; cout << endl; } cout << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; void solve() {} const int maxn = (int)1e6 + 420; const int MOD = (int)1e9 + 7; int n; int dp[maxn][4][4]; void contr() { cout << 0 << n ; exit(0); } int madd(int a, int b) { long long sum = a + b; sum %= MOD; return sum; } signed main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; n = (int)s.size(); s = . + s; dp[0][0][0] = 1; for (int i = 1; i <= n; i++) { for (int l = 0; l <= 3; l++) { for (int m = 0; m <= 3; m++) { for (int r = 0; r <= 3; r++) { if ((i == 1) and !(l == 0 and m == 0)) continue; if ((i == 2) and !(l == 0)) continue; bool ok = true; if (i >= 2) { if (m == 0 and (l == 3 or r == 3)) { ok = false; continue; } if (m == 1 and !(l == 3 ^ r == 3)) { ok = false; continue; } if (m == 2 and (l != 3 or r != 3)) { ok = false; continue; } } if (r == 0 and m == 3) { ok = false; continue; } if (r == 2 and m != 3) { ok = false; continue; } if (r == 1 and i == n and m != 3) { ok = false; continue; } if (r == 2 and i == n) { ok = false; continue; } vector<int> v = {r, m, l}; for (int j = 0; j < 3; j++) { if (s[i - j] == ? ) continue; if (i - j < 1) continue; if ((v[j] == 3)) { if (s[i - j] != * ) { ok = false; continue; } } else if (s[i - j] - 0 != v[j]) { ok = false; continue; } } if (ok) { dp[i][m][r] = madd(dp[i][m][r], dp[i - 1][l][m]); } } } } } int ans = 0; for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { ans = madd(ans, dp[n][i][j]); } } cout << ans << n ; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n], e = 0, s = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 1) e++; else s++; } int ans = 0; for (int b = 0; b < k; b++) { int c = b, ee = e, second = s; while (c < n) { if (a[c] == 1) ee--; else second--; c += k; } ans = max(ans, abs(ee - second)); } cout << ans; }
|
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; else return gcd(b % a, a); } long long int power(long long int a, long long int b, long long int m) { long long int ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, k; cin >> n >> k; long long int ans = 6; if (n == 2) { cout << ans << endl; return 0; } if (k == 1 || k == n) { ans = ans + 3 * (n - 2); } else { k = min(k, n - k + 1); for (long long int i = 1; i < k - 1; i++) ans = ans + 3; ans = ans + (k - 1); ans = ans + 3 * (n - k); } cout << ans << endl; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, c, d, s = 0; cin >> n; while (n--) { cin >> a >> b >> c >> d; s = s + (c - a + 1) * (d - b + 1); } printf( %d , s); return 0; }
|
#include <bits/stdc++.h> using namespace std; int T, t[200010], n; int dp[2000][2000]; int main() { scanf( %d , &T); while (T--) { scanf( %d , &n); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n + n; ++j) dp[i][j] = (1 << 30); for (int i = 1; i <= n; ++i) scanf( %d , &t[i]); sort(t + 1, t + n + 1); for (int i = 1; i <= n; ++i) { for (int j = i; j <= n + n; ++j) { if (j == 1) { dp[i][j] = abs(t[i] - 1); continue; } for (int k = 1; k < j; ++k) { dp[i][j] = min(dp[i][j], dp[i - 1][k] + abs(j - t[i])); } } } int ans = (1 << 30); for (int i = 1; i <= n + n; ++i) ans = min(ans, dp[n][i]); cout << ans << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <class T> void input(T &x) { char c = getchar(); x = 0; for (; (c < 48 || c > 57); c = getchar()) ; for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } } inline long long bigmod(long long p, long long e, long long M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } const int N = 10005; int dp[N]; int n; int a[N]; int main() { input(n); for (int i = 0; i < n; i++) input(a[i]); long long ans = 0; for (int round = 0; round < n; round++) { int f = (n - round) % n; vector<pair<int, int> > v; v.push_back(make_pair(0, 0)); for (int j = 1; j < n; j++) { int now = (f + j) % n; int range = a[now]; int iMin = 1e8; for (int k = v.size() - 1; k >= 0; k--) { int pos = v[k].second; if (j - range <= pos) iMin = min(iMin, 1 + v[k].first); else break; } while (v.size() && v.back().first >= iMin) v.pop_back(); v.push_back(make_pair(iMin, j)); } ans += v.back().first; } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int N, M, dp[109][109][26]; vector<pair<int, char> > v[109]; void solve(int i, int j, int k) { if (dp[i][j][k] != -1) return; dp[i][j][k] = 0; for (auto e : v[i]) if (e.second >= k) { solve(j, e.first, e.second); if (dp[j][e.first][e.second] == 0) { dp[i][j][k] = 1; break; } } } int main() { scanf( %d %d , &N, &M); while (M--) { int x, y; char c; scanf( %d %d %c n , &x, &y, &c), c -= a ; v[x].push_back({y, c}); } for (int k = 25; k >= 0; k--) for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) dp[i][j][k] = -1; for (int k = 25; k >= 0; k--) for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) solve(i, j, k); for (int i = 1; i <= N; i++, printf( n )) for (int j = 1; j <= N; j++) { bool win = 0; for (auto e : v[i]) if (dp[j][e.first][e.second] == 0) { win = 1; break; } if (win) printf( A ); else printf( B ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { char str[999]; cin >> str; str[0] = toupper(str[0]); cout << str << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000009; const int MAXN = 128; const int MAXK = 4; struct TrieNode { int next[MAXK], fail; int danger; void init() { fail = -1; danger = 0; for (int i = 0; i < MAXK; ++i) next[i] = -1; } } T[MAXN]; int root, Top; void Init() { root = 0, Top = 1; T[root].init(); } int GetId(char ch) { switch (ch) { case A : return 0; case G : return 1; case C : return 2; case T : return 3; } } void TrieIns(char *s) { int i = 0; int p = root; while (s[i]) { int idx = GetId(s[i]); if (T[p].next[idx] == -1) { T[Top].init(); T[p].next[idx] = Top++; } p = T[p].next[idx]; i++; } T[p].danger = strlen(s); } void Build_AC_Automation() { int cur, tmp; queue<int> q; T[root].fail = root; T[root].danger = 0; q.push(root); while (!q.empty()) { cur = q.front(); q.pop(); for (int i = 0; i < MAXK; ++i) { if (T[cur].next[i] != -1) { tmp = T[cur].next[i]; if (cur == root) T[tmp].fail = root; else { T[tmp].fail = T[T[cur].fail].next[i]; T[tmp].danger = max(T[tmp].danger, T[T[tmp].fail].danger); } q.push(tmp); } else { if (cur == root) T[cur].next[i] = root; else T[cur].next[i] = T[T[cur].fail].next[i]; } } } } long long dp[1010][128][11]; int n, m; int main() { memset(dp, 0, sizeof(dp)); cin >> n >> m; Init(); for (int i = 0; i < m; ++i) { char str[20]; cin >> str; TrieIns(str); } Build_AC_Automation(); dp[0][0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < Top; ++j) { for (int k = 0; k <= 10; ++k) { if (dp[i][j][k] == 0) continue; for (int t = 0; t < MAXK; ++t) { int aa = T[j].next[t]; if (T[aa].danger > k) { dp[i + 1][aa][0] += dp[i][j][k]; dp[i + 1][aa][0] %= MOD; } else if (k + 1 <= 10) { dp[i + 1][aa][k + 1] += dp[i][j][k]; dp[i + 1][aa][k + 1] %= MOD; } } } } } long long ans = 0; for (int i = 0; i < Top; ++i) { ans = ans + dp[n][i][0]; ans %= MOD; } cout << ans % MOD << endl; }
|
#include <bits/stdc++.h> using namespace std; int digit(int n) { int counter = 0; while (n != 0) { counter++; n /= 10; } return counter; } int main() { int T; cin >> T; for (int i = 0; i < T; i++) { int n; cin >> n; if (n == 1) { cout << -1 << n ; } else { cout << 5; for (int j = 0; j < n - 1; j++) { cout << 7; } cout << n ; } } }
|
#include <bits/stdc++.h> using namespace std; void base() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int32_t main() { base(); int n, num; cin >> n >> num; int turn = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (turn == j) { cout << num << ; } else { cout << 0 << ; } } cout << endl; turn++; } return 0; }
|
#include <bits/stdc++.h> using namespace std; struct edge { int v, id; edge *next; } * h[233333], pool[555555]; int top; inline void addedge(int u, int v, int id) { edge *tmp = &pool[top++]; tmp->v = v; tmp->id = id; tmp->next = h[u]; h[u] = tmp; edge *pmt = &pool[top++]; pmt->v = u; pmt->id = -id; pmt->next = h[v]; h[v] = pmt; } int n, m; int s[233333], f[233333], vis[233333], pa[233333]; void dfs(int u) { vis[u] = 1; for (edge *tmp = h[u]; tmp; tmp = tmp->next) { if (tmp->v != pa[u] && !vis[tmp->v]) { pa[tmp->v] = u; dfs(tmp->v); if (s[tmp->v]) { if (tmp->id > 0) f[tmp->id] += s[tmp->v]; else f[-tmp->id] -= s[tmp->v]; s[u] += s[tmp->v]; } } } } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &s[i]); scanf( %d , &m); int u, v; for (int i = 1; i <= m; i++) { scanf( %d%d , &u, &v); addedge(u, v, i); } for (int i = 1; i <= n; i++) { if (!vis[i]) { dfs(i); if (s[i] != 0) { printf( Impossible n ); return 0; } } } printf( Possible n ); for (int i = 1; i <= m; i++) printf( %d n , f[i]); return 0; }
|
#include <bits/stdc++.h> using namespace std; void solve() { long long n, m, count = 0; cin >> n >> m; long long a[m]; for (long long i = 0; i < m; i++) { cin >> a[i]; for (long long j = 0; j < i; j++) { if (a[j] < a[i]) count++; } } cout << count << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { solve(); } return 0; }
|
#include <bits/stdc++.h> const int N = 100001; using namespace std; long long a[N], sum[N]; int main() { int n; while (scanf( %d , &n) == 1) { for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); for (int i = 1, j; i < n; i++) { for (j = 0; (1 << j) + i <= n; j++) ; j--; a[i + (1 << j)] += a[i]; } long long ans = 0; for (int i = 1; i < n; i++) { ans += a[i]; printf( %I64d n , ans); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m; int main() { scanf( %d%d , &n, &m); if (n == 1 && m == 2) { printf( 0 n1 1 n1 2 n1 1 n ); return 0; } if (n == 2 && m == 1) { printf( 0 n1 1 n2 1 n1 1 n ); return 0; } if (n * m % 2 == 1 || n == 1 || m == 1) { printf( %d n%d %d %d %d n , 1, n, m, 1, 1); for (int i = 1; i <= n; ++i) { if (i % 2 == 1) { for (int j = 1; j <= m; ++j) printf( %d %d n , i, j); } else { for (int j = m; j > 0; --j) printf( %d %d n , i, j); } } printf( %d %d n , 1, 1); } else if (n % 2 == 0) { printf( 0 n1 1 n ); for (int i = 1; i <= n; ++i) { if (i % 2 == 1) { for (int j = 2; j <= m; ++j) printf( %d %d n , i, j); } else { for (int j = m; j > 1; --j) printf( %d %d n , i, j); } } for (int i = n; i > 0; --i) printf( %d %d n , i, 1); } else { printf( 0 n1 1 n ); for (int j = 1; j <= m; ++j) { if (j % 2 == 1) { for (int i = 2; i <= n; ++i) printf( %d %d n , i, j); } else { for (int i = n; i > 1; --i) printf( %d %d n , i, j); } } for (int j = m; j > 0; --j) printf( %d %d n , 1, j); } return 0; }
|
#include <bits/stdc++.h> using namespace std; bool b[123][123]; int arr[123][123]; int k[123]; int z[123]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); bool valid = 0, flag = 0; pair<int, int> ans1, ans2; int n; cin >> n; string s, t; cin >> s >> t; for (int i = 0; i < s.size(); i++) { if (s[i] != t[i]) { if (b[t[i] - a ][s[i] - a ]) { ans2 = make_pair(arr[t[i] - a ][s[i] - a ] + 1, i + 1); flag = 1; break; } if (!valid) { if (z[t[i] - a ] == 1) { ans1 = make_pair(k[t[i] - a ] + 1, i + 1); valid = 1; } else if (z[s[i] - a ] == 2) { ans1 = make_pair(k[s[i] - a ] + 1, i + 1); valid = 1; } } z[s[i] - a ] = 1; z[t[i] - a ] = 2; k[s[i] - a ] = i; k[t[i] - a ] = i; b[s[i] - a ][t[i] - a ] = 1; arr[s[i] - a ][t[i] - a ] = i; } } int h = 0; for (int i = 0; i < s.size(); i++) { if (s[i] != t[i]) h++; } if (flag) { cout << h - 2 << endl; cout << ans2.first << << ans2.second << endl; } else if (valid) { cout << h - 1 << endl; cout << ans1.first << << ans1.second << endl; } else { cout << h << endl; cout << -1 << << -1 << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long n, m; long long a[100]; long long ans = 1e18; long long po[100]; map<long long, long long> ma; long long fun(long long left1) { long long ans1 = 1e18; if (left1 <= 0) { return 0; } for (long long i = 0; i < n; i++) { long long left = left1; long long temp = 0; if (po[i] >= left) left = 0, temp = a[i]; else if (left % po[i] == 0) { temp = left / po[i]; temp *= a[i]; left = 0; } else { temp = left / po[i]; temp *= a[i]; left = left % po[i]; } if (ma[left] != 0) ans1 = min(ans1, temp + ma[left]); else { ma[left] = fun(left); ans1 = min(ans1, temp + ma[left]); } } return ans1; } int main() { cin >> n >> m; for (long long i = 0; i < n; i++) cin >> a[i]; po[0] = 1; for (long long i = 1; i <= 30; i++) { po[i] = po[i - 1] * 2; } for (long long i = 0; i < n; i++) { long long left = m; long long temp; if (po[i] >= left) left = 0, temp = a[i]; else if (left % po[i] == 0) { temp = left / po[i]; temp *= a[i]; left = 0; } else { temp = left / po[i]; temp *= a[i]; left = left % po[i]; } ans = min(ans, temp + fun(left)); } cout << ans; return 0; }
|
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long l, r; cin >> l >> r; long long temp = r; long long num = 1, ans = 1; while (temp) { temp /= 10; num *= 10; } num--; long long left_lim = (num) / 10; long long mid = (num + 1) / 2; if (r <= mid) { ans = (num - r) * r; } else { if (l <= mid) { ans = (num - mid) * (mid); } else { ans = (num - l) * l; } } cout << ans << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j; cin >> n; cin >> m; int A[n], B[m]; for (i = 0; i < n; i++) cin >> A[i]; for (j = 0; j < m; j++) cin >> B[j]; for (j = 0; j < n; j++) { for (i = 0; i < m; i++) { if (A[j] == B[i]) cout << A[j] << ; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long n, m, k, x, s, a[200005], b[200005], c[200005], d[200005], ans; int bfind(int t) { int lo = 0, hi = k - 1, mid; while (lo < hi) { mid = lo + (hi - lo + 1) / 2; if (d[mid] > t) hi = mid - 1; else lo = mid; } return lo; } int main() { cin >> n >> m >> k >> x >> s; for (int i = 0; i < m; i++) scanf( %I64d , &a[i]); for (int i = 0; i < m; i++) scanf( %I64d , &b[i]); for (int i = 0; i < k; i++) scanf( %I64d , &c[i]); for (int i = 0; i < k; i++) scanf( %I64d , &d[i]); ans = x * n; for (int i = 0; i < m; i++) { if (b[i] <= s) ans = min(ans, a[i] * n); } for (int i = 0; i < k; i++) { if (d[i] <= s) ans = min(ans, x * (n - c[i])); } for (int i = 0; i < m; i++) { if (b[i] + d[0] <= s) { int idx = bfind(s - b[i]); ans = min(ans, a[i] * (n - c[idx])); } } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; const int maxn = 100 + 10; int n, m; int a[maxn], b[maxn]; bitset<65> tt1[50000], tt2[50000]; vector<int> v; map<int, int> mp; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; bitset<65> *t1 = tt1 + 25000, *t2 = tt2 + 25000; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { t1[a[i] + b[j]][i] = 1; t2[a[i] + b[j]][j] = 1; if (!mp[a[i] + b[j]]) { mp[a[i] + b[j]] = 1; v.push_back(a[i] + b[j]); } } } int mine = -1; for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v.size(); j++) { int sum = (t1[v[i]] | t1[v[j]]).count() + (t2[v[i]] | t2[v[j]]).count(); if (sum > mine) mine = sum; } } printf( %d n , mine); return 0; }
|
#include <bits/stdc++.h> using namespace std; int a[2001000]; int Partition(int l, int r) { int temp = l + rand() % (r - l); swap(a[l], a[temp]); int x = a[l], i = l - 1, j = r + 1; while (1) { do i++; while (a[i] < x); do j--; while (a[j] > x); if (i < j) swap(a[i], a[j]); else return j; } } void QuickSort(int l, int r) { if (l < r) { int q = Partition(l, r); QuickSort(l, q); QuickSort(q + 1, r); } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; QuickSort(0, n - 1); for (int i = 0, j = n - 1; i < j; i++, j--) swap(a[i], a[j]); long long k = 0; for (int i = 0; pow(4, i) <= n + 1; i++) for (int j = 0; j < pow(4, i); j++) k += a[j]; cout << k << endl; }
|
#include <bits/stdc++.h> using namespace std; template <typename T> inline T sqr(const T &a) { return a * a; } template <typename T> inline int nread(vector<T> &a) { int n; cin >> n; a.clear(); a.resize(n); for (int i = 0; i < n; i++) cin >> a[i]; return n; } template <typename T> inline void nwread(int n, vector<T> &a) { a.clear(); a.resize(n); for (int i = 0; i < n; i++) cin >> a[i]; } const int N = 3001; int gr[N][N], cg[N]; int res[N * N]; pair<int, int> que[N * N]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) cg[i] = 0; for (int i = 0; i < m; i++) { int x, y; scanf( %d%d , &x, &y); x--; y--; gr[x][cg[x]++] = y; gr[y][cg[y]++] = x; } set<pair<pair<int, int>, int> > zapr; for (int i = 0; i < k; i++) { int x, y, z; scanf( %d%d%d , &x, &y, &z); x--; y--; z--; zapr.insert(make_pair(make_pair(x, y), z)); } map<pair<int, int>, int> path; int qi = 0, qj = 1; que[0] = make_pair(-1, 0); while (qi < qj) { pair<int, int> tek = que[qi++]; int pv = tek.first, tv = tek.second; vector<int> nreb; for (int i = 0; i < cg[tv];) { int fv = gr[tv][i]; if (zapr.count(make_pair(tek, fv))) i++; else { que[qj++] = make_pair(tv, fv); path[make_pair(tv, fv)] = pv; if (fv == n - 1) { int lr = 2; res[0] = fv; res[1] = tv; while (tv != 0) { pv = path[make_pair(tv, fv)]; res[lr++] = pv; fv = tv; tv = pv; } reverse(res, res + lr); cout << lr - 1 << endl; for (int i = 0; i < lr; i++) printf( %d , res[i] + 1); return 0; } if (cg[tv]) gr[tv][i] = gr[tv][cg[tv] - 1]; cg[tv]--; } } } cout << -1; }
|
#include <bits/stdc++.h> using namespace std; int a[210000]; long long dp[210000][2]; const long long INF = 1LL << 60; int N; void init() { for (int i = 0; i < 210000; i++) for (int j = 0; j < 2; j++) dp[i][j] = INF; } long long solve(int x, bool t) { if (x <= 0 || x > N) return 0; if (x == 1) return -1; if (dp[x][t] == -INF) return dp[x][t] = -1; if (dp[x][t] != INF) return dp[x][t]; dp[x][t] = -INF; int i2 = x; if (t) i2 += a[x]; else i2 -= a[x]; long long r = solve(i2, !t); long long res; if (r == -1) res = -1; else res = a[x] + r; return dp[x][t] = res; } int main() { cin >> N; for (int i = 2; i <= N; i++) cin >> a[i]; init(); for (int i = 1; i < N; i++) { long long ret = solve(1 + i, false); if (~ret) ret += i; cout << ret << endl; } return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC target( sse4 ) using namespace std; using ii = pair<int, int>; using ll = long long; const int N = 1e5 + 5; const int mod = 1e9 + 7; char str1[50005], str2[50005]; int n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> str1; n = strlen(str1); for (int sz = 1; sz <= n / 2; sz++) { m = sz; int cur = 0; for (int i = sz; i < n; i++) { if (str1[i] == str1[i - sz]) cur++; else cur = 0; str2[m++] = str1[i]; if (cur == sz) cur = 0, m -= sz; } for (int i = sz; i < m; i++) str1[i] = str2[i]; n = m; } for (int i = 0; i < n; i++) cout << str1[i]; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf( %d%d , &n, &m); if (n == 1) printf( 1 n ); else { if (m * 2 <= n) printf( %d n , m + 1); else printf( %d n , m - 1); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const double pi = 3.1415926535897; int main() { double d, h, v, e; cin >> d >> h >> v >> e; double s = (-1 * h) / (e - ((4 * v) / (pi * d * d))); if (s >= 0) cout << YES << endl << s; else cout << NO ; }
|
#include <bits/stdc++.h> using namespace std; void per() { cerr << endl; } template <typename Head, typename... Tail> void per(Head H, Tail... T) { cerr << H << ; per(T...); } template <class T> bool uin(T& a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T& a, T b) { return a < b ? (a = b, true) : false; } template <class U, class V> ostream& operator<<(ostream& out, const pair<U, V>& a) { return out << ( << a.first << , << a.second << ) ; } template <class U, class V> istream& operator>>(istream& in, pair<U, V>& a) { return in >> a.first >> a.second; } template <typename W, typename T = typename enable_if<!is_same<W, string>::value, typename W::value_type>::type> ostream& operator<<(ostream& out, const W& v) { out << { ; for (const auto& first : v) out << first << , ; return out << } ; } template <class T> void readArr(T from, T to) { for (auto i = from; i != to; ++i) cin >> *i; } mt19937 mrand(1337); unsigned int myRand32() { return mrand() & (unsigned int)(-1); } unsigned long long myRand64() { return ((unsigned long long)myRand32() << 32) ^ myRand32(); } const int mod = 1000000007; void add(int& a, int b) { a += b; if (a >= mod) a -= mod; } void dec(int& a, int b) { a -= b; if (a < 0) a += mod; } int mult(int a, int b) { return a * (long long)b % mod; } int bp(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = mult(res, a); a = mult(a, a); b >>= 1; } return res; } int main() { int vl = 1, vr = 1000000000; cout << ? 1 1 << endl; int base1; cin >> base1; while (vl + 1 < vr) { int vm = (vl + vr) >> 1; cout << ? 1 << vm << endl; int first; cin >> first; if (first == base1 - (vm - 1)) vl = vm; else vr = vm; } int y1 = vl; int x1 = base1 - (y1 - 1) + 1; cout << ? 1000000000 1 << endl; int base2; cin >> base2; int x2 = 1000000000 - (base2 - (y1 - 1)); cout << ? 1 1000000000 << endl; int base3; cin >> base3; int y2 = 1000000000 - (base3 - (x1 - 1)); cout << ! << x1 << << y1 << << x2 << << y2 << endl; }
|
#include <bits/stdc++.h> int main() { int i, j, k, n, t, c1, c2, c3, c4, c5; scanf( %d%d%d%d%d , &c1, &c2, &c3, &c4, &c5); t = c1 + c2 + c3 + c4 + c5; if (t == 0) printf( -1 n ); else if (t % 5 == 0) printf( %d n , t / 5); else printf( -1 n ); }
|
#include <bits/stdc++.h> using namespace std; const int BIG = 1000200; char buf[BIG]; int skip0[BIG], skip1[BIG]; int vpre[BIG]; struct E { int to; char sign; int pw; }; vector<E> vg[BIG]; int main() { while (scanf( %s , buf) != EOF) { string s(buf); s = 00 + s; int N = (int)((s).size()); int prev0 = 0, prev1 = 0; memset((skip0), (-1), sizeof(skip0)); memset((skip1), (-1), sizeof(skip1)); for (int i = 0; i < (int)(N); ++i) { skip0[i] = prev0; skip1[i] = prev1; if (s[i] == 0 ) { prev0 = i; } if (s[i] == 1 ) { prev1 = i; } } for (int i = 0; i < (int)(BIG); ++i) vg[i].clear(); for (int i = 0; i < (int)(N); ++i) { if (i == 0) continue; vg[i].push_back((E){skip1[i], + , N - 1 - i}); vg[i].push_back((E){skip0[i], - , N - 1 - i}); } int start = -1; for (int i = 0; i < (int)(N); ++i) if (s[N - 1 - i] == 1 ) { start = N - 1 - i; break; } assert(start >= 0); queue<pair<int, int> > q; memset((vpre), (-1), sizeof(vpre)); q.push(make_pair(start, -2)); while (!q.empty()) { int curr = q.front().first; int prev = q.front().second; q.pop(); if (vpre[curr] != -1) continue; vpre[curr] = prev; if (curr == 0) break; for (__typeof((vg[curr]).begin()) e = (vg[curr]).begin(); e != (vg[curr]).end(); ++e) { q.push(make_pair(e->to, curr)); } } assert(vpre[0] >= 0); vector<E> outp; for (int curr = 0; curr >= 0; curr = vpre[curr]) { int prev = vpre[curr]; for (__typeof((vg[prev]).begin()) e = (vg[prev]).begin(); e != (vg[prev]).end(); ++e) { if (e->to == curr) { outp.push_back(*e); break; } } } printf( %d n , (int)((outp).size())); for (int i = 0; i < (int)((int)((outp).size())); ++i) printf( %c2^%d n , outp[i].sign, outp[i].pw); } }
|
#include <bits/stdc++.h> using namespace std; const int NMAX = 5e3; const double eps = 1e-14; double dp[NMAX + 5][NMAX + 5]; int n, k; double p[NMAX + 5]; double np[NMAX + 5]; int t[NMAX + 5]; int main() { scanf( %d %d , &n, &k); for (int i = 1; i <= n; i++) { int x; scanf( %d %d , &x, &t[i]); p[i] = x / 100.0; np[i] = (100 - x) / 100.0; } dp[0][0] = 1; for (int i = 0; i <= n; i++) { double prob = 1; for (int j = 1; j <= t[i + 1]; j++) { prob *= np[i + 1]; } for (int j = i; j <= k; j++) { if (dp[i][j] < eps) { continue; } if (j < k) { dp[i][j + 1] += dp[i][j] * np[i]; dp[i + 1][j + 1] += dp[i][j] * p[i + 1]; if (j <= k - t[i + 1]) { dp[i + 1][j + t[i + 1]] += dp[i][j] * prob; if (j <= k - t[i + 1] - 1) { dp[i + 1][j + t[i + 1] + 1] -= dp[i][j] * prob; } } } } } double ans = 0; for (int i = 1; i < n; i++) { double prob = 1; for (int j = k; j > k - t[i + 1]; j--) { ans += i * dp[i][j] * prob; prob *= (1 - p[i + 1]); } } for (int j = 1; j <= k; j++) { ans += n * dp[n][j]; } printf( %.9f n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; bool cmp(long long a, long long b) { if (a > b) return true; return false; } long long n, dp[100010], res; pair<long long, long long> a[100010]; map<long long, long long> m; map<long long, long long>::iterator it; int main() { int t = 1; while (t--) { scanf( %lld , &n); for (int i = 0; i < n; i++) { scanf( %lld%lld , &a[i].first, &a[i].second); } sort(a, a + n); for (int i = 0; i < n; i++) { m[a[i].first] = i; it = m.lower_bound(a[i].first - a[i].second); if (it == m.begin()) dp[i] = 1; else { --it; dp[i] = 1 + dp[it->second]; } res = max(dp[i], res); } cout << n - res << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, i, sum1 = 0, sum2 = 0, a[200]; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] < 0) { sum2 += a[i]; } else sum1 += a[i]; } cout << sum1 - sum2; return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } int ans[200200], vst[200200], pos[200200], neg[200200]; set<int> s[200200]; priority_queue<pair<int, int> > q; int main() { int n, m, k, first; cin >> n >> m; for (int i = 1; i <= n; i++) { scanf( %d , &k); q.push(pair<int, int>(-k, i)); while (k--) { scanf( %d , &first); s[i].insert(first); if (first > 0) pos[first] = i; else neg[-first] = i; } } for (int i = 1; i <= m; i++) { if (!pos[i] || !neg[i]) { int u = pos[i] ? pos[i] : neg[i]; vst[u] = 1; ans[i] = (pos[i] == u); } } while (!q.empty()) { int u = q.top().second; q.pop(); if (vst[u]) continue; vst[u] = 1; if (s[u].empty()) { puts( NO ); return 0; } first = *s[u].begin(); if (first > 0) { ans[first] = 1; if (neg[first]) { s[neg[first]].erase(-first); q.push(pair<int, int>(-s[neg[first]].size(), neg[first])); } } else { first = -first; ans[first] = 0; if (pos[first]) { s[pos[first]].erase(first); q.push(pair<int, int>(-s[pos[first]].size(), pos[first])); } } } puts( YES ); for (int i = 1; i <= m; i++) putchar(ans[i] + 0 ); return 0; }
|
#include <bits/stdc++.h> using namespace std; char s[200005]; bool b[200005]; int n, ans = 0; void solve(int u) { if (u == 1) { if (u == n) { ans = 1; return; } if (s[u] == < ) { b[u] = true; ans++; } solve(u + 1); if (s[u] == > ) if (b[u + 1]) ans++; return; } if (u > n) return; if (u == n) { if (s[u] == > | b[u - 1]) { b[u] = true; ans++; } return; } if (s[u] == < ) { if (b[u - 1]) { b[u] = true; ans++; } solve(u + 1); } else { solve(u + 1); if (b[u + 1]) { b[u] = true; ans++; } } } int main() { scanf( %d , &n); scanf( %s , s + 1); solve(1); printf( %d n , ans); }
|
#include <bits/stdc++.h> using namespace std; int x[10][10]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; x[a][b] = x[b][a] = 1; } if (n < 7) cout << m << n ; else { int sum = 30; for (int i = 1; i <= 7; i++) { for (int j = i + 1; j <= 7; j++) { int ans = 0; for (int o = 1; o <= 7; o++) { if (x[i][o] == 1 && x[j][o] == 1) ans++; } if (ans < sum) sum = ans; } } cout << m - sum << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; for (long long i = 0; i < n; ++i) { if (i % 4 < 2) putchar( a ); else putchar( b ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int l = s.length(); int fc = 0; int sc = 0; for (int i = 0; i < l; i++) { if (s[i] == 4 ) fc++; if (s[i] == 7 ) sc++; } if (!fc && !sc) cout << -1 << endl; else if (sc > fc) cout << 7 << endl; else cout << 4 << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long int T; vector<long int> sweet(3); cin >> T; while (T--) { long int sum = 0; cin >> sweet[0] >> sweet[1] >> sweet[2]; sort(sweet.begin(), sweet.end()); sum = min((sweet[2] + sweet[1] + sweet[0]) / 2, (sweet[2] + sweet[1] + sweet[0]) - sweet[2]); cout << sum << endl; } }
|
#include <bits/stdc++.h> using namespace std; inline bool is_palindrome(const string& s) { return std::equal(s.begin(), s.end(), s.rbegin()); } const long long MOD = 1000000007; const long long INF = 1e9 + 5; const double eps = 1e-7; const double PI = acos(-1.0); inline void debug_vi(vector<int> a) { for (long long i = (long long)(0); i < (long long)(a.size()); i++) cout << a[i] << ; } inline void debug_vll(vector<long long> a) { for (long long i = (long long)(0); i < (long long)(a.size()); i++) cout << a[i] << ; } const int N = 1e5 + 5; int in[N]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, s, a, b; cin >> n >> s; memset(in, 0, sizeof(in)); for (long long i = (long long)(0); i < (long long)(n - 1); i++) { cin >> a >> b; a--; b--; in[a]++; in[b]++; } int l = 0; for (long long i = (long long)(0); i < (long long)(n); i++) { if (in[i] == 1) { l++; } } double ans = 2.0 * s / l; cout << fixed << showpoint << setprecision(15) << ans; ; return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) using namespace std; struct node { int a; int b; int sum; }; int n, q; int nblock; node nums[300315]; int acum = 0; int fib[300315]; long long auxi; void add(int &a, int b) { a += b; if (a > 1000000009) a -= 1000000009; } inline int calcf(int x) { if (x <= 1 || fib[x] != 0) return fib[x]; fib[x] = 0; add(fib[x], calcf(x - 2)); add(fib[x], calcf(x - 1)); return fib[x]; } inline int sumfibo(int a, int b, int posx, int posy) { int x = 0; auxi = (1LL * calcf(posx - 1) * a) % (1LL * 1000000009); add(x, int(auxi)); auxi = (1LL * calcf(posx) * b) % (1LL * 1000000009); add(x, int(auxi)); int y = 0; auxi = (1LL * calcf(posy) * a) % (1LL * 1000000009); add(y, int(auxi)); auxi = (1LL * calcf(posy + 1) * b) % (1LL * 1000000009); add(y, int(auxi)); add(y, 1000000009 - x); return y; } inline void update(int ini, int fin) { int inc = 1; int acumblock = 0; if ((ini - 1) / nblock == (fin - 1) / nblock) { for (int i = 0; i < nblock; i++) { int j = i + ini; if (j <= fin) { add(acumblock, calcf(inc)); inc++; } if (((j - 1) / nblock) == (fin - 1) / nblock) { add(nums[j].sum, acumblock); } } } else { int iniblock; iniblock = ini - ((ini - 1) % nblock); int l = iniblock; if (ini % nblock != 1) { for (int i = ini; i < iniblock + nblock; i++) { add(acumblock, calcf(inc)); inc++; add(nums[i].sum, acumblock); } l = iniblock + nblock; } while (l + nblock <= fin + 1) { add(nums[l].a, calcf(inc)); add(nums[l].b, calcf(inc + 1)); inc += nblock; l += nblock; } acumblock = 0; for (int i = 0; i < nblock; i++) { int j = i + l; if (j <= fin) { add(acumblock, calcf(inc)); inc++; } if (((j - 1) / nblock) == (fin - 1) / nblock) add(nums[j].sum, acumblock); } } } inline int get(int ini, int fin) { int iniblock; int ans = 0; if ((ini - 1) / nblock == (fin - 1) / nblock) { iniblock = ini - ((ini - 1) % nblock); if (ini % nblock) { if (fin % nblock) add(ans, sumfibo(nums[iniblock].a, nums[iniblock].b, ini % nblock, fin % nblock)); else add(ans, sumfibo(nums[iniblock].a, nums[iniblock].b, ini % nblock, nblock)); } else add(ans, sumfibo(nums[iniblock].a, nums[iniblock].b, nblock, nblock)); add(ans, nums[fin].sum); if (ini % nblock != 1) { add(ans, 1000000009 - nums[ini - 1].sum); } return ans; } else { iniblock = ini - ((ini - 1) % nblock); if (ini % nblock) add(ans, sumfibo(nums[iniblock].a, nums[iniblock].b, ini % nblock, nblock)); else add(ans, sumfibo(nums[iniblock].a, nums[iniblock].b, nblock, nblock)); add(ans, nums[iniblock + nblock - 1].sum); if (ini % nblock != 1) { add(ans, 1000000009 - nums[ini - 1].sum); } int l = iniblock + nblock; while (l + nblock <= fin + 1) { add(ans, sumfibo(nums[l].a, nums[l].b, 1, nblock)); add(ans, nums[l + nblock - 1].sum); l += nblock; } if (l <= fin) { add(ans, nums[fin].sum); add(ans, sumfibo(nums[l].a, nums[l].b, 1, fin % nblock)); } return ans; } } int main() { fib[0] = 0; fib[1] = 1; fib[2] = 1; scanf( %d %d , &n, &q); nblock = int(sqrt(n * 1.0)); if (nblock * nblock != n) nblock++; for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); add(acum, x); nums[i].sum = acum; if (i % nblock == 0) acum = 0; } for (int i = 0; i < q; i++) { int x, y, z; scanf( %d %d %d , &x, &y, &z); if (x == 1) { update(y, z); } else if (x == 2) { printf( %d n , get(y, z)); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; int mod = MOD; struct ModInt { unsigned val; ModInt() : val(0) {} ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {} ModInt pow(long long exponent) { ModInt tmp = *this, res = 1; while (exponent > 0) { if (exponent & 1) res *= tmp; tmp *= tmp; exponent >>= 1; } return res; } ModInt &operator+=(const ModInt &rhs) { if ((val += rhs.val) >= mod) val -= mod; return *this; } ModInt &operator-=(const ModInt &rhs) { if ((val += mod - rhs.val) >= mod) val -= mod; return *this; } ModInt &operator*=(const ModInt &rhs) { val = (unsigned long long)val * rhs.val % mod; return *this; } ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); } bool operator==(const ModInt &rhs) const { return val == rhs.val; } bool operator!=(const ModInt &rhs) const { return val != rhs.val; } bool operator<(const ModInt &rhs) const { return val < rhs.val; } bool operator<=(const ModInt &rhs) const { return val <= rhs.val; } bool operator>(const ModInt &rhs) const { return val > rhs.val; } bool operator>=(const ModInt &rhs) const { return val >= rhs.val; } ModInt operator-() const { return ModInt(-val); } ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; } ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; } ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; } ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; } friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.val; } friend istream &operator>>(istream &is, ModInt &rhs) { long long x; is >> x; rhs = ModInt(x); return is; } private: ModInt inv() const { unsigned a = val, b = mod; int x = 1, y = 0; while (b) { unsigned tmp = a / b; a -= tmp * b; swap(a, b); x -= tmp * y; swap(x, y); } return ModInt(x); } }; ModInt abs(const ModInt &x) { return x.val; } struct Combinatorics { Combinatorics(int MAX = 5000000) { MAX <<= 1; fact.resize(MAX + 1); fact_inv.resize(MAX + 1); fact[0] = 1; for (int i = (1); i < (MAX + 1); ++i) fact[i] = fact[i - 1] * i; fact_inv[MAX] = ModInt(1) / fact[MAX]; for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i; } ModInt nCk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[k] * fact_inv[n - k]; } ModInt nPk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[n - k]; } ModInt nHk(int n, int k) { if (n < 0 || k < 0) return ModInt(0); return (k == 0 ? ModInt(1) : nCk(n + k - 1, k)); } private: vector<ModInt> fact, fact_inv; }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; vector<int> w(n); for (int i = (0); i < (n); ++i) cin >> w[i]; int dislike = 0, like = 0; for (int i = (0); i < (n); ++i) { if (a[i] == 0) dislike += w[i]; else like += w[i]; } for (int i = (0); i < (n); ++i) { vector<vector<vector<ModInt> > > dp( m + 1, vector<vector<ModInt> >(m + 1, vector<ModInt>(m + 1, 0))); dp[0][0][0] = 1; int dl = dislike, l = like; if (a[i] == 0) dl -= w[i]; else l -= w[i]; for (int x = (0); x < (m); ++x) { for (int y = (0); y < (m + 1); ++y) for (int z = (0); z < (m + 1); ++z) { int sum = like + dislike - z + (x - y - z) + (a[i] == 0 ? -y : y); if (a[i] == 0 && y > w[i]) continue; if (z > dl || y + z > x || sum <= 0) continue; dp[x + 1][y + 1][z] += dp[x][y][z] * (w[i] + (a[i] == 0 ? -y : y)) / sum; dp[x + 1][y][z + 1] += dp[x][y][z] * (dl - z) / sum; dp[x + 1][y][z] += dp[x][y][z] * (l + (x - y - z)) / sum; } } ModInt ans = 0; for (int y = (0); y < (m + 1); ++y) for (int z = (0); z < (m + 1); ++z) { if (a[i] == 0 && y > w[i]) continue; if (z > dl || y + z > m) continue; ans += dp[m][y][z] * (w[i] + (a[i] == 0 ? -y : y)); } cout << ans << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int s, v1, v2, t1, t2; cin >> s >> v1 >> v2 >> t1 >> t2; int res1 = (s * v1) + (t1 * 2); int res2 = (s * v2) + (t2 * 2); if (res1 < res2) { cout << First ; } if (res1 > res2) { cout << Second ; } if (res1 == res2) { cout << Friendship ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int M = 1200000; const int inf = 0x3f3f3f3f; int n, m, k; vector<int> g[M]; int ans; int dfs(int u, int f) { vector<int> v = {0, 0}; for (int to : g[u]) { if (to == f) continue; v.push_back(dfs(to, u)); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int sz = g[u].size(); ans = max(ans, v[0] + v[1] + sz + 1); return v[0] + sz - 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int _ = (1); _ <= (t); ++_) { cin >> n; int u, v; for (int i = (1); i <= (n); ++i) g[i].clear(); for (int i = (2); i <= (n); ++i) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } ans = 0; dfs(1, 1); cout << ans << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; class BIT { public: int size; vector<long long> arr; BIT(int n) { size = n + 5; arr.resize(size); } void add(int i, long long v) { for (int x = i + 1; x < size; x += (x & (-x))) arr[x] += v; } long long sum(int i) { i = min(i, size - 2); long long res = 0; for (int x = i + 1; x > 0; x -= (x & (-x))) res += arr[x]; return res; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, w; cin >> n >> w; vector<pair<long long, long long>> p(n); for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second; sort(p.begin(), p.end(), [&](pair<long long, long long> p0, pair<long long, long long> p1) { if (abs(p0.first) * abs(p1.second - w) == abs(p1.first) * abs(p0.second - w)) { return abs(p0.first) * abs(p1.second - w + 1) > abs(p1.first) * abs(p0.second - w + 1); } else { return abs(p0.first) * abs(p1.second - w) < abs(p1.first) * abs(p0.second - w); } }); vector<int> in(n); for (int i = 0; i < n; i++) in[i] = i; sort(in.begin(), in.end(), [&](int i0, int i1) { pair<long long, long long> p0 = p[i0], p1 = p[i1]; if (abs(p0.first) * abs(p1.second + w) == abs(p1.first) * abs(p0.second + w)) { return abs(p0.first) * abs(p1.second + w - 1) > abs(p1.first) * abs(p0.second + w - 1); } else { return abs(p0.first) * abs(p1.second + w) < abs(p1.first) * abs(p0.second + w); } }); BIT bit(n); long long res = 0; for (int i = n - 1; i >= 0; i--) { res += bit.sum(in[i]); bit.add(in[i], 1); } cout << res << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 50; ; const int M = 255; const int mod = 998244353; const int mo = 123; const double pi = acos(-1.0); int n, m; int ans = 0; int vis[N]; vector<int> vec; int main() { scanf( %d%d , &n, &m); for (int i = 0; i < n; i++) { int res = 0; for (int j = 0, x; j < m; j++) { scanf( %d , &x); if (x) res |= (1 << j); } if (!vis[res]) { vec.push_back(res); vis[res]++; } } bool ok = false; for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < vec.size(); j++) { int x = vec[i] + vec[j]; int y = vec[i] ^ vec[j]; if (x == y) ok = true; } } if (ok) puts( YES ); else puts( NO ); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int n, br; int p[MAXN], niz[MAXN], num[MAXN], bio[MAXN]; inline void ne() { printf( NO n ); exit(0); } void dfs1(int x) { if (bio[x]) return; bio[x] = br; num[br]++; dfs1(niz[x]); } void dfs2(int x) { if (bio[x]) return; bio[x] = 1; if (bio[niz[x]]) return; printf( %d %d n , x, niz[x]); dfs2(niz[x]); } void dfs3(int x, int y) { if (bio[x]) return; bio[x] = 1; printf( %d %d n , x, y); dfs3(niz[x], niz[y]); } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &p[i]); for (int i = 1; i <= n; i++) niz[p[i]] = p[p[i]]; br = 1; for (int i = 1; i <= n; i++) if (!bio[i]) { dfs1(i); br++; } int poc = 0, val = MAXN, grupa = 0; for (int i = 1; i <= n; i++) if (num[bio[i]] < val) { val = num[bio[i]]; poc = i; grupa = bio[i]; } if (num[grupa] > 2) ne(); for (int i = 1; i < br; i++) if (num[i] % num[grupa] != 0) ne(); printf( YES n ); memset(bio, 0, sizeof(bio)); dfs2(poc); for (int i = 1; i <= n; i++) if (!bio[i]) dfs3(i, poc); return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<int> primes; void sieve() { vector<bool> isComposite(100000000, false); for (int i = 2; i * i <= 100000000; i++) { if (isComposite[i] == false) for (int j = i * 2; j <= 100000000; j += i) isComposite[j] = true; } for (int i = 2; i <= 100000000; i++) if (isComposite[i] == false) primes.push_back(i); for (auto it : primes) cout << it << ; } long long LCM(int n) { long long lcm = 1; for (int i = 0; i < primes.size() && primes[i] <= n; i++) { int pp = primes[i]; while (pp * primes[i] <= n) pp = pp * primes[i]; lcm *= pp; lcm %= 1000000007; } return lcm; } int main() { int t; cin >> t; while (t--) { long long int n; string keyboard; string s; cin >> keyboard; cin >> s; map<char, int> mp; for (int i = 0; i < keyboard.size(); i++) { mp[keyboard[i]] = i + 1; } long long int time = 0; for (int i = 1; i < s.length(); i++) { time += abs((mp[s[i]] - mp[s[i - 1]])); } cout << time << n ; } }
|
#include<bits/stdc++.h> using namespace std; #define sfd(x) scanf( %d ,&x) #define sfd2(x,y) scanf( %d%d ,&x,&y) #define sfd3(x,y,z) scanf( %d%d%d ,&x,&y,&z) #define sfs(x) scanf( %s ,x) #define sfll(x) scanf( %lld ,&x) #define sfll2(x,y) scanf( %lld%lld ,&x,&y) #define pfd(x) printf( %d ,x) #define pfs(x) printf( %s ,x) #define pfll(x) printf( %lld ,x) #define nl printf( n ) #define sp printf( ) #define pfyes printf( YES ) #define pfno printf( NO ) #define pf0 printf( 0 ) #define FOR(i,a,b,in) for(int i=a;i<=b;i+=in) #define RFOR(i,a,b,de) for(int i=a;i>=b;i-=de) #define rep(i,n) for(int i=0;i<n;i++) #define pb push_back #define mkp make_pair #define mod 998244353 typedef long long int ll; typedef vector<int> vi; typedef pair<int,int> pii; // ------------------------// ll power(long long x, int y, int p) { ll res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } int main() { int t; sfd(t); while(t--) { int n; sfd(n); char s[n+1]; sfs(s); int c0 = 0; int c1 = 0; int cx = 0; rep(i,n) { if(s[i]== 1 ) { cx+=1; } else { c1+=(cx/2); cx=0; c0+=1; } } c1+=(cx/2); ll v1=1,v2=1,v3=1; for(int i=1;i<=c0;i++) { v1=(v1*i)%mod; } for(int i=1;i<=c1;i++) { v2=(v2*i)%mod; } for(int i=1;i<=c0+c1;i++) { v3=(v3*i)%mod; } v1=(v1*v2)%mod; ll ans = (v3*power(v1,mod-2,mod))%mod; pfll(ans); nl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int fen1[200002]; void upd1(int pos, int val) { for (; pos <= 200000; pos |= (pos + 1)) fen1[pos] += val; } int fnd1(int pos) { int ret = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) ret += fen1[pos]; return ret; } int fen2[200002]; void upd2(int pos, int val) { for (; pos >= 0; pos = (pos & (pos + 1)) - 1) fen2[pos] += val; } int fnd2(int pos) { int ret = 0; for (; pos <= 200000; pos |= (pos + 1)) ret += fen2[pos]; return ret; } int now[200002]; int main() { int n, k, a, b, q; scanf( %d%d%d%d%d , &n, &k, &a, &b, &q); while (q--) { int t; scanf( %d , &t); if (t == 1) { int d, aa; scanf( %d%d , &d, &aa); --d; int add = max(0, min(aa, b - now[d])); upd1(d, add); add = max(0, min(aa, a - now[d])); upd2(d, add); now[d] += aa; } else { int p; scanf( %d , &p); --p; int ans = fnd1(p - 1) + fnd2(p + k); printf( %d n , ans); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, m, k; cin >> n >> m >> k; k = n * m / 2 - k; if (n % 2 == 1) { swap(n, m); k = n * m / 2 - k; } if (m % 2 == 1) { if ((k >= n / 2 && (k - n / 2) % (2) == 0)) { cout << YES << n ; continue; } else { cout << NO << n ; continue; } } else { if (k % 2 == 0) cout << YES << n ; else cout << NO << n ; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; struct node { long long num; int p; } nd[maxn]; long long sum[maxn], f[maxn]; bool cp1(node a, node b) { return a.num < b.num; } int main() { int n, k; ios::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> nd[i].num; nd[i].p = i; } sort(nd + 1, nd + 1 + n, cp1); cin >> k; sum[0] = 0; for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nd[i].num; f[1] = 0; for (int i = 2; i <= k; i++) f[i] = nd[i].num * (i - 1) - sum[i - 1] + f[i - 1]; long long mm = f[k]; int p = 1; for (int i = k + 1; i <= n; i++) { f[i] = nd[i].num * (k - 1) + f[i - 1] + nd[i - k].num * (k - 1) - 2 * (sum[i - 1] - sum[i - k]); if (f[i] < mm) { mm = f[i]; p = i - k + 1; } } for (int i = 0; i < k; i++) printf( %d%c , nd[i + p].p, i == k - 1 ? n : ); }
|
#include<bits/stdc++.h> #define ll int #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum; string s1, s2, s; int main() { FAST_IO; cin >> n >> m >> k; ll h[n + 1][m], l[n][m + 1]; vector<vector<ll> >re ( n + 1, vector<ll> ( m + 1, 0 ) ); vector<vector<ll> >temp ( n + 1, vector<ll> ( m + 1, 0 ) ); for ( i = 1; i <= n; i++ ) for ( j = 1; j < m; j++ ) cin >> h[i][j]; for ( i = 1; i < n; i++ ) for ( j = 1; j <= m; j ++ ) cin >> l[i][j]; for ( t = 1; t <= k / 2; t++ ) { for ( i = 1; i <= n; i++ ) { for ( j = 1; j <= m; j++ ) { temp[i][j] = ( int ) 1e9;; if ( i != n ) temp[i][j] = min ( temp[i][j], re[i + 1][j] + l[i][j] * 2 ); if ( i != 1 ) temp[i][j] = min ( temp[i][j], re[i - 1][j] + l[i - 1][j] * 2 ); if ( j != m ) temp[i][j] = min ( temp[i][j], re[i][j + 1] + h[i][j] * 2 ); if ( j != 1 ) temp[i][j] = min ( temp[i][j], re[i][j - 1] + h[i][j - 1] * 2 ); } } swap ( re, temp ); } for ( i = 1; i <= n; cout << endl, i++ ) for ( j = 1; j <= m; j++ ) { if ( j > 1 ) cout << ; cout << ( k % 2 ? -1 : re[i][j] ); } }
|
#include <bits/stdc++.h> using namespace std; // #include <atcoder/all> // using namespace atcoder; #define rep(i,n) for (int i = 0; i < (n); ++i) #define STRING(str) #str #define ll long long #define ld long double template <typename T> void print(T x, string name) { cout << name+ : << x << endl; } template <typename T > void print_vec_1d(vector<T> x, string name) { cout << name << endl; cout << [ ; for (int i=0;i<x.size();i++) { cout << x[i]; if (i != x.size()-1) cout << , ; else cout << ] << endl; } } template <typename T > void print_vec_2d(vector<vector<T>> x, string name) { cout << name << endl; cout << [ ; for (int i=0;i<x.size();i++) { cout << [ ; for (int j=0;j<x[i].size();j++) { cout << x[i][j]; if (j != x[i].size()-1) cout << , ; else { cout << ] ; if (!((i == x.size()-1) && (j == x[i].size()-1))) { cout << , << endl; } } } } cout << ] << endl; } template <typename T > void print_vec_3d(vector<vector<vector<T>>> x, string name) { cout << name << endl; cout << [ ; for (int i=0;i<x.size();i++) { cout << [ ; for (int j=0;j<x[i].size();j++) { cout << [ ; for (int k=0;k<x[i][j].size();k++) { cout << x[i][j][k]; if (k != x[i][j].size()-1) cout << , ; else { cout << ] ; if (!((i == x.size()-1) && (j == x[i].size()-1) && (k == x[i][j].size()-1))) { cout << , << endl; } } } if (j != x[i].size()-1) cout << , ; else { cout << ] ; if (!((i == x.size()-1) && (j == x[i].size()-1))) { cout << , << endl; } } } } cout << ] << endl; } int main() { // ios::sync_with_stdio(0); // cin.tie(0); int n; cin >> n; int ind_2; cout << ? 1 << n << n ; cin >> ind_2; ind_2--; int a1, a2; if (ind_2 != 0 && ind_2 != n-1) { cout << ? 1 << ind_2+1 << n ; cin >> a1; a1--; int a2; cout << ? << ind_2+1 << << n << n ; cin >> a2; a2--; } bool left_flag; if (ind_2 == n-1) { left_flag = true; } else if (ind_2 == 0) { left_flag = false; } else if (ind_2 == a1) { left_flag = true; } else { // ind_2 == a2 left_flag = false; } if (left_flag) { // ? ? ? 1 ? ? ? 2 ? ? ? ? int ok = 0; int ng = ind_2; while (abs(ok-ng) > 1) { int mid = (ok+ng)/2; int b; cout << ? << mid+1 << << ind_2+1 << n ; cin >> b; b--; if (b == ind_2) { ok = mid; } else { ng = mid; } } cout << ! << ok+1 << n ; } else { // ? ? ? ? ? ? ? 2 ? 1 ? ? int ok = n-1; int ng = ind_2; while (abs(ok-ng) > 1) { int mid = (ok+ng)/2; int b; cout << ? << ind_2+1 << << mid+1 << n ; cin >> b; b--; if (b == ind_2) { ok = mid; } else { ng = mid; } } cout << ! << ok+1 << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int INFint = 2147483647; const long long INF = 9223372036854775807ll; const long long MOD = 1000000007ll; long long cnt[100000]; int main() { ios_base::sync_with_stdio(0); long long a; cin >> a; string s; cin >> s; for (int i = 0; i < s.size(); i++) { long long sum = 0; for (int j = i; j < s.size(); j++) { sum += s[j] - 0 ; cnt[sum]++; } } long long ans = 0; if (a == 0) { ans = cnt[0] * cnt[0]; for (int i = 1; i < 100000; i++) { ans += cnt[0] * cnt[i] * 2; } } else { for (int i = 1; i < 100000; i++) { if (i <= a) { if (a % i == 0 && a / i < 100000) ans += cnt[i] * cnt[a / i]; } else break; } } cout << ans << endl; fprintf(stderr, nTIME = %lf n , 1.0 * clock() / CLOCKS_PER_SEC); return 0; }
|
#include <bits/stdc++.h> using namespace std; template <class T> void debug(T a, T b) { ; } template <class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } namespace std { template <class S, class T> ostream& operator<<(ostream& out, const pair<S, T>& a) { out << ( << a.first << , << a.second << ) ; return out; } } // namespace std long long int readL() { long long int res; scanf( %I64d , &res); return res; } void printL(long long int res) { printf( %I64d , res); } const int INF = 5e8; int n, m; vector<int> g[100005]; pair<int, int> mst2[100005]; pair<int, int> es[100005]; int idx[100005], rev[100005]; vector<int> g2[100005]; int res[100005]; bool cut[100005]; int size[100005], whole; int gsize(int v, int p) { size[v] = 1; for (auto to : g2[v]) { if (cut[to] || to == p) continue; size[v] += gsize(to, v); } return size[v]; } int root, cost; void groot(int v, int p) { int C = whole - size[v]; for (auto to : g2[v]) { if (cut[to] || to == p) continue; groot(to, v); chmax(C, size[to]); } if (cost > C) { cost = C; root = v; } } void dfs(int v, int rank) { whole = gsize(v, -1); cost = INF; groot(v, -1); v = root; res[v] = rank; if (whole == 1) return; cut[v] = 1; for (auto to : g2[v]) { if (cut[to]) continue; dfs(to, rank + 1); } } int main() { cin >> n >> m; for (int i = 0; i < (m); ++i) { int a, b; scanf( %d%d , &a, &b); --a; --b; if (a > b) swap(a, b); es[i] = {a, b}; } es[m++] = {0, n - 1}; sort(es, es + m, [&](pair<int, int> a, pair<int, int> b) { return a.second - a.first < b.second - b.first; }); set<pair<pair<int, int>, int> > range; int cnt = 0; for (int i = 0; i < (m); ++i) { int a = es[i].first, b = es[i].second; auto it = range.lower_bound({{a, -1}, -1}); pair<int, int> last = {-1, -1}; while (it != range.end() && it->first.second <= b) { last = it->first; g[cnt].push_back(it->second); g[it->second].push_back(cnt); range.erase(it++); } if (last.second == b) mst2[cnt] = {last.first, b}; else mst2[cnt] = {b - 1, b}; range.insert({{a, b}, cnt}); ++cnt; } for (int i = 0; i < (cnt); ++i) if (mst2[i].second > mst2[i].first) swap(mst2[i].first, mst2[i].second); for (int i = 0; i < (cnt); ++i) idx[i] = i; sort(idx, idx + cnt, [&](int a, int b) { return mst2[a] < mst2[b]; }); for (int i = 0; i < (cnt); ++i) rev[idx[i]] = i; for (int i = 0; i < (cnt); ++i) for (auto to : g[i]) g2[rev[i]].push_back(rev[to]); dfs(0, 0); for (int i = 0; i < (cnt); ++i) printf( %d n , res[i] + 1); return 0; }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.