solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int a[600][600]; int main(void) { int num = 1; int x = 1, y = 1, z = 1; a[1][1] = 1; while (num <= 250000) { a[x][++y] = ++num; for (int j = 0; j < z; j++) a[++x][y] = ++num; for (int j = 0; j < z; j++) a[x][--y] = ++num; ++z; a[++x][y] = ++num; for (int j = 0; j < z; j++) a[x][++y] = ++num; for (int j = 0; j < z; j++) a[--x][y] = ++num; ++z; } int n; scanf("%d", &n); if (n == 1 || n == 2) { printf("-1"); return 0; } a[1][1] = 1, a[1][2] = 3, a[1][3] = 9; a[2][1] = 5, a[2][2] = 8, a[2][3] = 7; a[3][1] = 4, a[3][2] = 2, a[3][3] = 6; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { printf("%d ", n * n + 1 - a[i][j]); } printf("\n"); } }
16
#include <bits/stdc++.h> using namespace std; long long a[1000000], k, temp, last, sum, temper, i, j, n; int main() { std::ios_base::sync_with_stdio(false); cin >> n >> k; for (i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } temper = a[1]; sort(a + 1, a + n + 1); last = a[1]; temp = k; for (i = 2; i <= n; i++) { if (k >= (a[i] - last) * (i - 1)) { k -= (a[i] - last) * (i - 1); last = a[i]; } } i = 0; while (i + 1 <= n && a[i + 1] <= last) i++; for (j = 1; j <= i; j++) a[j] = last + (k / i) + (j <= k % i); k = temp; for (i = 1; i <= n; i++) a[i] = -a[i]; sort(a + 1, a + n + 1); last = a[1]; for (i = 2; i <= n; i++) { if (k >= (a[i] - last) * (i - 1)) { k -= (a[i] - last) * (i - 1); last = a[i]; } } i = 0; while (i + 1 <= n && a[i + 1] <= last) i++; for (j = 1; j <= i; j++) a[j] = last + (k / i) + (j <= k % i); for (i = 1; i <= n; i++) a[i] = -a[i]; sort(a + 1, a + n + 1); cout << a[n] - a[1]; return 0; }
12
#include <bits/stdc++.h> #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") using namespace std; const int MAX = 2e5 + 5; const long long MAX2 = 11; const long long MOD = 1100000007; const long long MOD2 = 1000005329; const long long INF = 2e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 1000; int n; double r, v, a, b, x, le, ri, mid, z, tmp, bg, pos; inline double get(double mid) { tmp = mid - floor(mid / z) * z; pos = sin(tmp * bg) * r * 2.0 + mid * v; return pos; } int main() { cout << fixed << setprecision(10); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> r >> v; z = 2.0 * r * pi / v; bg = v / r / 2.0; for (long long i = 1; i <= n; ++i) { cin >> a >> b; x = b - a; le = 0.0, ri = MOD; for (long long lp = 1; lp <= 50; ++lp) { mid = (le + ri) / 2.0; if (get(mid) >= x) ri = mid; else le = mid; } cout << mid << '\n'; } return 0; }
17
#include <bits/stdc++.h> constexpr int MAX = 1 << 22; std::vector<int> find_compatible(const std::vector<int>& vector) { std::vector<int> masks(MAX, -1); for (auto x : vector) { masks[x] = x; } for (int i = 0; i < masks.size(); ++i) { if (masks[i] == -1) { continue; } for (int j = 1; (i | j) < MAX; j <<= 1) { masks[i | j] = masks[i]; } } std::vector<int> result(vector.size()); for (int i = 0; i < vector.size(); ++i) { result[i] = masks[(MAX - 1) ^ vector[i]]; } return result; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int size; std::cin >> size; std::vector<int> vector(size); for (auto& x : vector) { std::cin >> x; } for (auto x : find_compatible(vector)) { std::cout << x << ' '; } std::cout << '\n'; return 0; }
14
#include <bits/stdc++.h> using namespace std; int main() { int s, v1, t1, v2, t2; cin >> s >> v1 >> v2 >> t1 >> t2; int s1 = s * v1 + 2 * t1; int s2 = s * v2 + 2 * t2; if (s1 == s2) cout << "Friendship"; else if (s1 > s2) cout << "Second" << endl; else cout << "First" << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int main() { int n, m, x; long long sum = 0; while (~scanf("%d %d", &n, &m)) { while (n--) { scanf("%d", &x); sum += x; if (n == 0) printf("%lld", sum / m); else printf("%lld ", sum / m); sum %= m; } cout << endl; } }
1
#include <bits/stdc++.h> const int TETA = 26; std::vector<int> fix(std::vector<int> f) { std::vector<int> odd; for (int i = 0; i < TETA; i++) { if (f[i] % 2 == 1) { odd.push_back(i); } } for (int left = 0, right = odd.size() - 1; left <= right; left++, right--) { f[odd[left]] += 1; f[odd[right]] -= 1; } return f; } int main(int argc, char* argv[]) { std::string s; std::cin >> s; std::vector<int> f(TETA, 0); for (int i = 0; i < (int)s.size(); i++) { f[s[i] - 'a'] += 1; } f = fix(f); std::string answer; answer.resize(s.size()); for (int id = 0, left = 0, right = (int)answer.size() - 1; left <= right; left++, right--) { if (left == right) { for (int i = 0; i < TETA; i++) { if (f[i] == 1) { answer[left] = ('a' + i); } } } else { while (id < TETA && f[id] < 2) { id += 1; } answer[left] = answer[right] = ('a' + id); f[id] -= 2; } } std::cout << answer << "\n"; return 0; }
10
#include <bits/stdc++.h> using namespace std; vector<int> ans; map<int, int> check; int main() { int n; cin >> n; int pr = 1; for (int i = 1; i <= 31; i++) { check[pr] = i - 1; pr *= 2; } int q = 0; bool f = true; while (true) { if (((n + 1) & n) == 0) { cout << q << endl; for (auto j : ans) cout << check[j] << ' '; return 0; } q++; if (f) { int st; for (auto i : check) { if (i.first >= n) { st = i.first; break; } } n = (n ^ (st - 1)); ans.push_back(st); f = false; } else { n++; f = true; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<pair<long long, long long>> a(n); for (long long i = 0; i < n; i++) cin >> a[i].first >> a[i].second; sort(a.begin(), a.end()); vector<pair<pair<long long, long long>, long long>> b; pair<long long, long long> cur = a[0]; long long cnt = 1; for (long long i = 1; i < n; i++) { if (a[i] != cur) { b.push_back({cur, cnt}); cur = a[i]; cnt = 0; } cnt++; } b.push_back({cur, cnt}); unordered_map<long long, long long> x, y; long long ans = 0; for (long long i = 0; i < b.size(); i++) { ans += x[b[i].first.first] * b[i].second; ans += y[b[i].first.second] * b[i].second; ans += b[i].second * (b[i].second - 1) / 2; x[b[i].first.first] += b[i].second; y[b[i].first.second] += b[i].second; } cout << ans; return 0; }
6
#include <bits/stdc++.h> using namespace std; template <typename P, typename Q> ostream& operator<<(ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; } char g[2][13]; void show(void) { for (int i = 0; i < 2; ++i) { for (int j = 0; j < 13; ++j) { cout << g[i][j]; } cout << endl; } return; } bool valid(string s) { map<char, set<char>> adj; int di[] = {-1, -1, -1, 0, 0, +1, +1, +1}; int dj[] = {-1, 0, +1, -1, +1, -1, 0, +1}; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 13; ++j) { for (int d = 0; d < 8; ++d) { int ni = i + di[d]; int nj = j + dj[d]; if (!(0 <= ni && ni < 2)) continue; if (!(0 <= nj && nj < 13)) continue; adj[g[i][j]].insert(g[ni][nj]); } } } for (int i = 0; i + 1 < s.size(); ++i) { if (adj[s[i]].count(s[i + 1]) == 0) { return false; } } return true; } bool f(string s) { char x; for (char c = 'A'; c <= 'Z'; ++c) { if (count(s.begin(), s.end(), c) == 2) { x = c; break; } } vector<pair<int, int>> v; for (int j = 0; j < 13; ++j) { v.push_back(make_pair(0, j)); } for (int j = 0; j < 13; ++j) { v.push_back(make_pair(1, 12 - j)); } for (int i = 0; i < v.size(); ++i) { bool flg = false; int k = i; for (int j = 0; j < s.size(); ++j) { if (flg && s[j] == x) continue; if (s[j] == x) flg = true; int idx = k % v.size(); g[v[idx].first][v[idx].second] = s[j]; ++k; } if (valid(s)) { return true; } } return false; } int main(int argc, char* argv[]) { string s; while (cin >> s) { if (f(s)) show(); else cout << "Impossible" << endl; } return 0; }
8
#include <bits/stdc++.h> using namespace std; char a[1000005]; vector<int> D; int parse(char *s, char *e) { int val = 0; while (s != e) val = val * 10 + (*(s++)) - '0'; return val; } void parse() { int year = parse(a, a + 4); int month = parse(a + 5, a + 7); int day = parse(a + 8, a + 10); int hour = parse(a + 11, a + 13); int minute = parse(a + 14, a + 16); int second = parse(a + 17, a + 19); D.push_back(year); D.push_back(month); D.push_back(day); D.push_back(hour); D.push_back(minute); D.push_back(second); } int getTime(int year, int mon, int day, int hour, int minute, int second) { static int ms[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int days = 0; for (int i = 1; i < mon; i++) days += ms[i]; days += day; days *= 86400; days += hour * 3600 + minute * 60 + second; return days; } int compare(int x, int y) { int vx = getTime(D[x], D[x + 1], D[x + 2], D[x + 3], D[x + 4], D[x + 5]); int vy = getTime(D[y], D[y + 1], D[y + 2], D[y + 3], D[y + 4], D[y + 5]); return vy - vx; } int main(void) { int n, m; scanf("%d%d", &n, &m); gets(a); while (gets(a) != NULL) { if (a[0] == '2') parse(); } int fail = 1; for (int i = (m - 1) * 6; i < ((int)(D).size()); i += 6) { if (compare(i - (m - 1) * 6, i) < n) { printf("%04d-%02d-%02d %02d:%02d:%02d\n", D[i], D[i + 1], D[i + 2], D[i + 3], D[i + 4], D[i + 5]); fail = 0; break; } } if (fail) puts("-1"); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 100050; long long t[N], a[N], sol[N]; multiset<long long> snow; int main() { int n, i; scanf("%i", &n); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = 1; i <= n; i++) scanf("%lld", &t[i]), t[i] += t[i - 1]; long long sum = 0; for (i = 1; i <= n; i++) { sum += a[i] + t[i - 1]; snow.insert(a[i] + t[i - 1]); while (snow.size() && *snow.begin() <= t[i]) { sol[i] += *snow.begin() - t[i - 1]; snow.erase(snow.begin()); } sol[i] += (t[i] - t[i - 1]) * snow.size(); printf("%lld ", sol[i]); } printf("\n"); return 0; }
8
#include <bits/stdc++.h> using namespace std; struct node { double x, r; int f, id; } a[100005]; bool cmp1(node a, node b) { return a.x < b.x; } bool cmp2(node a, node b) { return a.id < b.id; } int isin(int i, double x, double y) { double t = (x - a[i].x) * (x - a[i].x) + y * y; if (t <= a[i].r * a[i].r) return 1; return 0; } int main() { int n, i, m; double x, y; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf%lf", &a[i].x, &a[i].r); a[i].f = -1; a[i].id = i; } sort(a, a + n, cmp1); scanf("%d", &m); int ans = 0; for (i = 1; i <= m; i++) { scanf("%lf%lf", &x, &y); int l = 0, r = n - 1, mid; while (l <= r) { mid = (l + r) >> 1; if (isin(mid, x, y)) { if (a[mid].f == -1) { a[mid].f = i; ans++; } } if (x > a[mid].x) l = mid + 1; else r = mid - 1; } if (l + 1 < n && isin(l + 1, x, y)) { if (a[l + 1].f == -1) { a[l + 1].f = i; ans++; } } if (l - 1 >= 0 && isin(l - 1, x, y)) { if (a[l - 1].f == -1) { a[l - 1].f = i; ans++; } } } sort(a, a + n, cmp2); printf("%d\n", ans); for (i = 0; i < n - 1; i++) printf("%d ", a[i].f); printf("%d\n", a[n - 1].f); return 0; }
9
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int power(long long int x, long long int y) { if (y == 0) return 1; else if (y % 2 == 0) return power(x, y / 2) * power(x, y / 2); else return x * power(x, y / 2) * power(x, y / 2); } long long int modpower(long long int x, long long int n, long long int M) { int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long int mmi(long long int n, long long int M) { return modpower(n, M - 2, M); } int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int a[n]; long long int mi = 0; for (long long int i = 0; i < n; i++) { cin >> a[i]; mi = (a[i] > a[mi] ? i : mi); } vector<long long int> b(n); b[0] = a[mi]; a[mi] = 0; long long int x = b[0]; for (long long int i = 1; i <= n - 1; i++) { long long int in = 0, mx = 0; for (long long int j = 0; j < n; j++) if (a[j] && gcd(a[j], x) > mx) { mx = gcd(a[j], x); in = j; } b[i] = a[in]; x = mx; a[in] = 0; } for (long long int i = 0; i < n; i++) cout << b[i] << ' '; cout << '\n'; } }
5
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 9; const long long int MOD = 1e9 + 696969; mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } const long long INF = 1e18; const int maxn = 500100; namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } int bsf(unsigned int n) { return __builtin_ctz(n); } } // namespace internal } // namespace atcoder namespace atcoder { template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S> &v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder using namespace atcoder; int n, d[maxn]; bool odw[maxn]; vector<int> v[maxn]; int init[maxn]; int A[maxn], B[maxn]; int pre[maxn], maxpre[maxn]; int DL; int zap[maxn], q; struct S { int zero, one; }; using F = bool; S op(S l, S r) { return S{max(l.zero, r.zero), max(l.one, r.one)}; } S e() { return S{0, 0}; } S mapping(F l, S r) { if (!l) return r; return S{r.one, r.zero}; } F composition(F l, F r) { return (l ^ r); } F id() { return false; } void initdfs(int x) { odw[x] = 1; for (auto u : v[x]) { if (!odw[u]) { d[u] = d[x] + 1; initdfs(u); } } } void dfs(int x) { odw[x] = 1; pre[x] = ++DL; maxpre[x] = pre[x]; for (auto u : v[x]) { if (!odw[u]) { d[u] = d[x] + 1; dfs(u); maxpre[x] = max(maxpre[x], maxpre[u]); } } } void toggle(int edge, lazy_segtree<S, op, e, F, mapping, composition, id> &seg) { int node = B[edge]; int a = pre[node], b = maxpre[node] + 1; seg.apply(a, b, true); } vector<int> solve(int root) { 0 && cerr; DL = -1; for (int i = (1); i <= (n); ++i) odw[i] = 0, d[i] = 0, maxpre[i] = 0, pre[i] = 0; dfs(root); for (int i = (1); i <= (n - 1); ++i) { if (d[A[i]] > d[B[i]]) swap(A[i], B[i]); } vector<int> res(q + 1, 0); vector<S> vec(n); for (int i = (1); i <= (n); ++i) { int kt = pre[i]; vec[kt] = {d[i], 0}; } lazy_segtree<S, op, e, F, mapping, composition, id> seg(vec); for (int i = (1); i <= (n - 1); ++i) { if (init[i]) toggle(i, seg); } for (int i = (1); i <= (q); ++i) { toggle(zap[i], seg); S wyn = seg.all_prod(); res[i] = wyn.zero; } return res; } int main() { { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; cin >> n; for (int i = (1); i <= (n - 1); ++i) { cin >> A[i] >> B[i] >> init[i]; v[A[i]].push_back(B[i]); v[B[i]].push_back(A[i]); } cin >> q; for (int i = (1); i <= (q); ++i) cin >> zap[i]; for (int i = (1); i <= (n); ++i) odw[i] = 0, d[i] = 0; initdfs(1); int opt = 1; for (int i = (2); i <= (n); ++i) { if (d[i] > d[opt]) opt = i; } int one_end = opt; for (int i = (1); i <= (n); ++i) odw[i] = 0, d[i] = 0; initdfs(opt); opt = 1; for (int i = (2); i <= (n); ++i) { if (d[i] > d[opt]) opt = i; } int second_end = opt; auto a1 = solve(one_end); auto a2 = solve(second_end); for (int i = (1); i <= (q); ++i) cout << max(a1[i], a2[i]) << "\n"; }
20
#include <bits/stdc++.h> using namespace std; int main() { long long j, t, k, l, sum, r, y, z, h, x, mini, maxi, kmax, d, i, g, n, m, q, v, kol, raz, viv, braz; cin >> x; cin >> y; l = 0; r = 1000000; while (l != r - 1) { z = (l + r) / 2; sum = z * (z + 1) / 2; if (sum > (x + y)) { r = z; } else { l = z; } } braz = (x + y) - (l * (l + 1) / 2); kol = l; i = 1; l = 0; r = 1000000; while (l != r - 1) { z = (l + r) / 2; sum = z * (z + 1) / 2; if (sum > x) { r = z; } else { l = z; } } k = l; cout << k << endl; raz = x - ((k + 1) * k) / 2; if (raz > braz) { i = 1; while (i <= k) { if (i + raz > k) { viv = i; for (i = 1; i < viv; i++) { cout << i << " "; } for (i = viv + 1; i <= k; i++) { cout << i << " "; } cout << viv + raz << endl; break; } i++; } cout << kol - k << endl; for (i = k + 1; i < viv + raz; i++) { cout << i << " "; } for (i = viv + raz + 1; i <= kol; i++) { cout << i << " "; } cout << viv << endl; } else { for (i = 1; i <= k; i++) { cout << i << " "; } cout << endl; cout << kol - k << endl; for (i = k + 1; i <= kol; i++) { cout << i << " "; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1]; for (int i = 0; i < n; ++i) cin >> a[i]; int dp[n][n]; for (int i = 0; i < n; ++i) dp[i][i] = 1; for (int i = 0; i < n - 1; ++i) { if (a[i] == a[i + 1]) dp[i][i + 1] = 1; else dp[i][i + 1] = 2; } for (int i = 2; i < n; ++i) { for (int j = 0; j < n - i; ++j) { dp[j][j + i] = 1 + dp[j + 1][j + i]; dp[j][j + i] = min(dp[j][j + i], dp[j][j + 1] + dp[j + 2][j + i]); for (int k = j + 2; k < j + i; ++k) { if (a[j] == a[k]) dp[j][j + i] = min(dp[j][j + i], dp[j + 1][k - 1] + dp[k + 1][j + i]); } if (a[j] == a[j + i]) dp[j][j + i] = min(dp[j][j + i], dp[j + 1][j + i - 1]); } } cout << dp[0][n - 1]; }
11
#include <bits/stdc++.h> using namespace std; map<pair<long long, int>, pair<long long, int> > mp; pair<long long, int> dfs(long long n, int mx) { if (n == 0) return make_pair(0, 0); if (n < 10) { pair<long long, int> ret = make_pair(1, 0); if (mx > n) ret.second = mx - n; return ret; } pair<long long, int> state = make_pair(n, mx); if (mp.count(state)) return mp[state]; long long base = 1; while (base * 10 <= n) base *= 10; pair<long long, int> ret = make_pair(0, 0); for (int i = max(mx, int(n / base));; i = max(mx, int(n / base))) { pair<long long, int> cur = dfs(n % base, i); ret.first += cur.first; n -= n % base; n -= cur.second; if (cur.second == 0 && i) n -= i, ret.first++; if (i == 0) break; if (n <= 0) break; } ret.second = -n; mp.insert(make_pair(state, ret)); return ret; } int main() { long long n; cin >> n; cout << dfs(n, 0).first << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); const double eps = 0.00000000001; int t, n, s[605]; double l, r, m; struct N { double x, y; N(double a = 0, double b = 0) { x = a, y = b; } } A[305]; struct L { N a, b; double f; } a[605], b[305], c[605]; N operator+(N a, N b) { return N(a.x + b.x, a.y + b.y); } N operator-(N a, N b) { return N(a.x - b.x, a.y - b.y); } double operator*(N a, N b) { return a.x * b.y - a.y * b.x; } L go(L s, double m) { N c = N(-sin(s.f) * m, cos(s.f) * m); s.a = s.a + c; s.b = s.b + c; return s; } bool operator<(L a, L b) { return fabs(a.f - b.f) > eps ? a.f < b.f : (a.b - a.a) * (b.b - a.a) < -eps; } N inter(L a, L b) { double k1, k2, t; k1 = (b.b - a.a) * (a.b - a.a); k2 = (a.b - a.a) * (b.a - a.a); t = k1 / (k1 + k2); return N(b.b.x + (b.a.x - b.b.x) * t, b.b.y + (b.a.y - b.b.y) * t); } bool jud(L a, L b, L t) { N p = inter(a, b); return (t.b - t.a) * (p - t.a) < eps; } void hpi() { int l = 1, r = 0; t = n; for (int i = 1; i <= t; ++i) { while (l < r && jud(b[r - 1], b[r], a[i])) --r; while (l < r && jud(b[l + 1], b[l], a[i])) ++l; b[++r] = a[i]; } while (l < r && jud(b[r - 1], b[r], b[l])) --r; while (l < r && jud(b[l + 1], b[l], b[r])) ++l; t = r - l + 1; } bool jud() { hpi(); if (t < 3) return 1; t = n; for (int i = 1; i <= t; ++i) a[i + t] = a[i]; for (int i = 1, j = 1, k = 2; i <= t; ++i) { for (;; ++j) { if (fabs(c[j].f - a[i].f) < eps) continue; N x = inter(a[i], c[j]); if ((x.x - c[j].a.x) * (c[j].b.x - x.x) > -eps && (x.y - c[j].a.y) * (c[j].b.y - x.y) > -eps) break; } A[i].x = 1e6; for (k = i + 1;; ++k) { if (fabs(a[k].f - a[i].f) < eps) continue; if (a[i].f + pi > pi) { if (a[k].f < a[i].f - eps && a[i].f - pi < a[k].f + eps) break; } else if (!(a[i].f < a[k].f - eps && a[k].f < a[i].f + pi - eps)) break; N x = inter(a[i], a[k]); if ((x - c[j].a) * (c[j].b - c[j].a) > eps) break; if (A[i].x == 1e6) A[i] = x; else { if (a[i].b.x >= a[i].a.x && x.x >= A[i].x || a[i].b.x <= a[i].a.x && x.x <= A[i].x) if (a[i].b.y >= a[i].a.y && x.y >= A[i].y || a[i].b.y <= a[i].a.y && x.y <= A[i].y) A[i] = x; } } s[i] = k - i; } for (int i = 1; i <= t; ++i) if (s[i] + s[i + s[i] > t ? i + s[i] - t : i + s[i]] >= t) return 1; return 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lf%lf", &A[i].x, &A[i].y); A[n + 1] = A[1]; for (int i = 1; i <= n; ++i) { c[i].a = A[i]; c[i].b = A[i + 1]; c[i].f = atan2(A[i + 1].y - A[i].y, A[i + 1].x - A[i].x); } sort(c + 1, c + n + 1); for (int i = 1; i <= n; ++i) c[i + n] = c[i]; l = 0; r = 30000; for (int fuck = 1; fuck < 80; ++fuck) { m = (l + r) / 2; for (int i = 1; i <= n; ++i) a[i] = go(c[i], m); t = n; if (jud()) r = m; else l = m; } m = r + 0.0000000001; for (int i = 1; i <= n; ++i) a[i] = go(c[i], m); jud(); printf("%.8lf\n", m); for (int i = 1; i <= t; ++i) { n = i + s[i]; if (n > t) n -= t; if (s[i] + s[n] >= t) { printf("%.8lf %.8lf\n", A[i].x, A[i].y); printf("%.8lf %.8lf\n", A[n].x, A[n].y); return 0; } } return 0; }
25
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 9; const int M = 1e3 + 9; const int INFINT = 1e9 + 9; const long long INFLL = 1e18 + 9; const long double EPS = 1e-16; int deg[N]; int main() { ios::sync_with_stdio(0); int n, v, u, ans = 0; cin >> n; for (int i = 0; i < (n - 1); i++) { cin >> v >> u; v--; u--; deg[v]++; deg[u]++; } for (int i = 0; i < (n); i++) if (deg[i] == 1) ans++; cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 5, M = 104206969, OO = 0x3f3f3f3f; const double Pii = 3.14159265359; long long t, n, m; long long a[N], b[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) { cin >> n >> m; set<pair<int, int>> st; for (int i = 0; i < n; ++i) { cin >> a[i]; st.insert({i, a[i]}); } for (int i = 0; i < m; ++i) cin >> b[i]; set<int> vis; long long ans = 0; for (int i = 0; i < m; ++i) { if (vis.count(b[i])) { vis.erase(b[i]); ans++; } else { ans += (2 * vis.size()); while (!st.empty()) { auto p = *st.begin(); st.erase(st.begin()); if (p.second == b[i]) { ans++; break; } else { ans += 2; vis.insert(p.second); } } } } cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int INF = (1 << 30); const long long INFLL = (1ll << 60); const long long MOD = (long long)(1e9 + 7); void mul_mod(long long& a, long long b) { a *= b; a %= MOD; } void add_mod(long long& a, long long b) { a = (a < MOD) ? a : (a - MOD); b = (b < MOD) ? b : (b - MOD); a += b; a = (a < MOD) ? a : (a - MOD); } pair<int, long long> dp[3030][3030], sp[3030][3030]; long long w[3030], b[3030]; int ch[3030], m; vector<int> g[3030]; void solve(int v, int p = -1) { int i, j, k, u; pair<int, long long> tmp; for (i = (g[v].size() - 1); i >= 0; --i) { if (g[v][i] == p) { g[v].erase(g[v].begin() + i); break; } } for (i = (g[v].size() - 1); i >= 0; --i) { u = g[v][i]; solve(u, v); } sp[g[v].size()][1] = pair<int, long long>(0, w[v] - b[v]); ch[v] = 1; for (i = (g[v].size() - 1); i >= 0; --i) { u = g[v][i]; for (j = 1; j <= ch[v]; ++j) { for (k = 1; k <= ch[u]; ++k) { sp[i][j + k - 1] = pair<int, long long>(0, -INFLL); sp[i][j + k] = pair<int, long long>(0, -INFLL); } } for (j = 1; j <= ch[v]; ++j) { for (k = 1; k <= ch[u]; ++k) { tmp = pair<int, long long>(sp[i + 1][j].first + dp[u][k].first, sp[i + 1][j].second + dp[u][k].second); sp[i][j + k - 1] = max(sp[i][j + k - 1], tmp); tmp = pair<int, long long>(sp[i + 1][j].first + dp[u][k].first + ((dp[u][k].second > 0ll) ? 1 : 0), sp[i + 1][j].second); sp[i][j + k] = max(sp[i][j + k], tmp); } } ch[v] += ch[u]; } for (j = 1; j <= ch[v]; ++j) { dp[v][j] = sp[0][j]; } } int main(void) { int t, n, i, j, u, v, ans; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (i = 0; i < n; ++i) { scanf("%lld", &b[i]); } for (i = 0; i < n; ++i) { scanf("%lld", &w[i]); } for (i = 1; i < n; ++i) { scanf("%d%d", &u, &v); --u; --v; g[u].push_back(v); g[v].push_back(u); } solve(0); ans = (dp[0][m].first + (dp[0][m].second > 0ll ? 1 : 0)); for (i = 0; i < n; ++i) { g[i].clear(); } printf("%d\n", ans); } return 0; }
17
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; string tostr(int n) { stringstream rr; rr << n; return rr.str(); } const int mod = 1e9 + 7; const int mxn = 1e5 + 9; const long double eps = 1e-9; long long qpow(long long n, long long k) { long long ans = 1; while (k) { if (k & 1) ans = (ans * n) % mod; n = (n * n) % mod; k >>= 1; } return ans; } vector<long long> u, v, a, b; const long long mx = (long long)1e18; void pre(vector<long long>& p, long long i, long long x, vector<long long>& ans) { if (i == p.size()) { ans.push_back(x); return; } pre(p, i + 1, x, ans); while (x <= mx / p[i]) pre(p, i + 1, x *= p[i], ans); } long long cnt(long long n) { long long i, j, ans = 0; for (i = 0; i < u.size(); i++) { ans += upper_bound(v.begin(), v.end(), n / u[i]) - v.begin(); } return ans; } int main() { long long i, j, k, m, s, e, n, t; cin >> n; for (i = 0; i < n; i++) { cin >> t; i % 2 == 0 ? a.push_back(t) : b.push_back(t); } cin >> k; pre(a, 0LL, 1LL, u); pre(b, 0LL, 1LL, v); sort(u.begin(), u.end()); sort(v.begin(), v.end()); s = 1LL, e = mx; while (s < e) { m = s + (e - s) / 2; ; if (cnt(m) < k) s = m + 1; else e = m; } cout << e << "\n"; return 0; }
16
#include <bits/stdc++.h> using namespace std; using namespace std; template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << '\n'; } 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...); } long long gcd(long long a, long long b) { if (a < b) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long isPrime(long long n) { long long p = (long long)sqrt(n); for (long long i = 2; 2 < p + 1 ? i < p + 1 : i > p + 1; 2 < p + 1 ? i += 1 : i -= 1) if (n % i == 0) return 0; return 1; } long long pow(long long b, long long e) { if (e == 0) return 1; else if (e % 2 == 0) { long long a = pow(b, e / 2); return a * a; } else { long long a = pow(b, e / 2); return b * a * a; } } long long pow_m(long long x, long long y, long long m = 1000000007) { x = x % m; long long res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } long long modInverse(long long a, long long m) { return pow_m(a, m - 2, m); } long long max(long long a, long long b) { if (a >= b) return a; else return b; } long long min(long long a, long long b) { if (a <= b) return a; else return b; } long long sum(vector<long long> v) { long long sm = 0; for (long long i = 0; 0 < v.size() ? i < v.size() : i > v.size(); 0 < v.size() ? i += 1 : i -= 1) sm += v[i]; return sm; } long long bin_coff(long long n, long long k, long long m) { vector<vector<long long> > ans(n + 1, vector<long long>(k, 0)); for (long long i = 0; 0 < n + 1 ? i < n + 1 : i > n + 1; 0 < n + 1 ? i += 1 : i -= 1) { for (long long j = 0; 0 < min(i, k) + 1 ? j < min(i, k) + 1 : j > min(i, k) + 1; 0 < min(i, k) + 1 ? j += 1 : j -= 1) { if (j == 0 || j == i) ans[i][j] = 1; else ans[i][j] = ans[i - 1][j - 1] % m + ans[i - 1][j] % m; } } return ans[n][k] % m; } long long inverse(long long i) { if (i == 1) return 1; return (1000000007 - ((1000000007 / i) * inverse(1000000007 % i)) % 1000000007 + 1000000007) % 1000000007; } long long maxProduct(vector<long long> arr, long long n, long long k) { sort(arr.begin(), arr.end()); long long prod = 1; if (arr[n - 1] == 0 && (k & 1)) return 0; if (arr[n - 1] <= 0 && (k & 1)) { for (long long i = n - 1; i >= n - k; i--) prod *= arr[i]; return prod; } long long i = 0, j = n - 1; if (k & 1) { prod *= arr[j]; j--; k--; } k >>= 1; for (long long itr = 0; itr < k; itr++) { long long left_product = arr[i] * arr[i + 1]; long long right_product = arr[j] * arr[j - 1]; if (left_product > right_product) { prod *= left_product; i += 2; } else { prod *= right_product; j -= 2; } } return prod; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n; cin >> n; vector<long long> a(n), b(n); for (long long i = 0; 0 < n ? i < n : i > n; 0 < n ? i += 1 : i -= 1) cin >> a[i]; for (long long i = 0; 0 < n ? i < n : i > n; 0 < n ? i += 1 : i -= 1) cin >> b[i]; ; long long pos[n]; map<long long, long long> make_pair; for (long long i = 0; 0 < n ? i < n : i > n; 0 < n ? i += 1 : i -= 1) pos[a[i]] = i; for (long long i = 0; 0 < n ? i < n : i > n; 0 < n ? i += 1 : i -= 1) { long long curr = pos[b[i]] - i; if (curr < 0) curr += n; make_pair[curr]++; } long long ans = 0; for (auto &it : make_pair) ans = max(ans, it.second); cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int getCost(vector<vector<int>> &a) { int n = a.size(), m = a[0].size(); int ans = 0; for (int i = 0; i < m; ++i) { ans += 1LL * (a[n / 2 - 1][i] - a[n / 2][i]) * (a[n / 2 - 1][i] - a[n / 2][i]); } return ans; } void change(vector<vector<int>> &a) { int n = a.size(); swap(a[n / 2 - 1], a[n - 1]); swap(a[n / 2], a[0]); } int main() { int q; scanf("%d", &q); for (int i = 0; i < q; ++i) { int n, m; scanf("%d %d", &n, &m); vector<vector<int>> a(n, vector<int>(m, 0)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { scanf("%d", &a[i][j]); } auto b = a; change(b); if (getCost(a) > getCost(b)) { cout << "YES\n"; } else { cout << "NO\n"; } } }
10
#include <bits/stdc++.h> using namespace std; const int N = 4e6 + 1, M = 3e5 + 1; int Tree[N][2], A[M], B[M], n, cnt, s, H[N][2]; void Add(int x) { int cur = 0; for (int i = 30; i >= 0; i--) { if (1 << i & x) s = 1; else s = 0; H[cur][s]++; if (!Tree[cur][s]) Tree[cur][s] = ++cnt; cur = Tree[cur][s]; } } void D(int x) { int cur = 0; for (int i = 30; i >= 0; i--) { if (1 << i & x) s = 1; else s = 0; H[cur][s]--; cur = Tree[cur][s]; } } int FindBest(int x, int k) { int cur = 0, f = 0; for (int i = 30; i >= 0; i--) { if (1 << i & x) s = 1; else s = 0; if (H[cur][s]) { f += s * (1 << i); A[k] ^= s * (1 << i); cur = Tree[cur][s]; } else { f += (s ^ 1) * (1 << i); A[k] ^= (s ^ 1) * (1 << i); cur = Tree[cur][s ^ 1]; } } return f; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &A[i]); } for (int i = 1; i <= n; i++) { scanf("%d", &B[i]); Add(B[i]); } for (int i = 1; i <= n; i++) { int z = FindBest(A[i], i); D(z); } for (int i = 1; i <= n; i++) { printf("%d ", A[i]); } return 0; }
10
#include <bits/stdc++.h> using namespace std; auto main() -> int { ios_base ::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int m; cin >> m; int ans = min(n, m); (ans % 2 == 1) ? cout << "Akshat\n" : cout << "Malvika\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int abs(int x) { if (x < 0) return -x; return x; } int a[100010]; int main() { ios_base::sync_with_stdio(false); int n, k, x; cin >> n; memset(a, -1, sizeof(a)); bool flag = 1; while (n--) { cin >> x >> k; if (x <= a[k]) { continue; } else if (x == a[k] + 1) { a[k] = x; } else { flag = 0; } } if (flag) { cout << "YES\n"; } else { cout << "NO\n"; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, m; vector<pair<int, int> > a[200020]; int d[200020]; map<pair<int, int>, int> mp; int k, x, y, z; set<pair<int, int> > q; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { scanf("%d%d%d", &x, &y, &z); a[x].push_back(make_pair(y, z)); a[y].push_back(make_pair(x, z)); } for (int i = 1; i <= n; i++) { scanf("%d", &k); for (int j = 1; j <= k; j++) { scanf("%d", &x); mp[make_pair(i, x)] = 1; } } int v, w, to, p, dx; for (int i = 1; i <= n; i++) d[i] = (1 << 30); d[1] = 0; q.insert(make_pair(0, 1)); while (!q.empty()) { v = q.begin()->second; dx = q.begin()->first; if (d[v] != dx) continue; q.erase(q.begin()); while (mp[make_pair(v, dx)] == 1) dx++; for (int j = 0; j < a[v].size(); j++) { to = a[v][j].first; w = a[v][j].second; if (d[to] > dx + w) { q.erase(make_pair(d[to], to)); d[to] = dx + w; q.insert(make_pair(d[to], to)); } } } if (d[n] == (1 << 30)) cout << -1; else cout << d[n]; return 0; }
9
#include <bits/stdc++.h> using namespace std; int root(vector<int> &Arr, int i) { while (Arr[i] != i) { Arr[i] = Arr[Arr[i]]; i = Arr[i]; } return i; } void weighted_union(vector<int> &Arr, vector<int> &size, int A, int B) { int root_A = root(Arr, A); int root_B = root(Arr, B); if (size[root_A] < size[root_B]) { Arr[root_A] = Arr[root_B]; size[root_B] += size[root_A]; } else { Arr[root_B] = Arr[root_A]; size[root_A] += size[root_B]; } } int main() { int n; cin >> n; vector<int> a(n + 1); for (int i = 0; i < n; i++) { cin >> a[i + 1]; } vector<int> size(n + 1, 1); vector<int> b(n + 1); for (int i = 0; i < n; i++) b[i + 1] = i + 1; for (int i = 1; i <= n; i++) { weighted_union(b, size, i, a[i]); } int sum = 0; vector<int> c(n + 1, 0); for (int i = 1; i <= n; i++) { if (c[i] == 1) continue; c[i] = 1; for (int j = i + 1; j <= n; j++) { if (root(b, i) == root(b, j)) c[j] = 1; } sum++; } cout << sum << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long n, level; cin >> n; long long cur = 2; for (level = 1; level < n + 1; level++) { long long y = level * (level + 1) * (level + 1) - (cur / level); cout << y << "\n"; cur = level * (level + 1); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; char a[N]; int main() { int n; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (cin >> n) { cin >> a; sort(a, a + n); puts(a); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } int ls[n], rs[n]; int mn = 1000000; for (int i = 0; i < n; i++) { ls[i] = min(mn, a[i]); mn = ls[i]; } mn = 1000000; for (int i = n - 1; i >= 0; i--) { rs[i] = min(mn, a[i]); mn = rs[i]; } int i1 = -1, j1 = -1, k1 = -1; for (int i = 1; i < n - 1; i++) { if (ls[i] < a[i] and rs[i] < a[i]) { i1 = ls[i]; j1 = rs[i]; k1 = a[i]; } } if (i1 == -1 or j1 == -1 or k1 == -1) cout << "NO" << endl; else { cout << "YES" << endl; for (int i = 0; i < n; i++) { if (a[i] == i1) cout << i + 1 << " "; if (a[i] == k1) cout << i + 1 << " "; if (a[i] == j1) cout << i + 1 << " "; } cout << endl; } } }
1
#include <bits/stdc++.h> long long mpow(long long a, long long n, long long mod) { long long ret = 1; long long b = a; while (n) { if (n & 1) ret = (ret * b) % mod; b = (b * b) % mod; n >>= 1; } return (long long)ret; } using namespace std; double dp[205][205][410]; int prob[205], a[205], off = 205; int get(int val) { val = max(val, -200); val = min(val, 200); return val + off; } void solve() { int n, l, k; scanf("%d", &n); scanf("%d", &l); scanf("%d", &k); for (int i = 1; i <= n; i++) { scanf("%d", &prob[i]); } for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } dp[0][0][get(k)] = 1.0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= i; j++) { for (int w = -200; w <= 200; w++) { if (dp[i - 1][j][get(w)] == 0) { continue; } double val = (1.0 * prob[i]) / (100.0); dp[i][j + 1][get(w + a[i])] += val * dp[i - 1][j][get(w)]; dp[i][j][get(w)] += (1.0 - val) * dp[i - 1][j][get(w)]; } } } double ans = 0; for (int i = l; i <= n; i++) { for (int j = 0; j <= 200; j++) { ans += dp[n][i][get(j)]; } } cout << fixed << setprecision(10) << ans; } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
10
#include <bits/stdc++.h> long long mod = 1e9 + 7; using namespace std; char str[1005][1005]; int black[1005]; int white[1005]; int dp[1005][2]; int main() { int n, m, x, y; cin >> n >> m >> x >> y; int i, j; for (i = 0; i < n; i++) scanf("%s", str[i]); for (i = 0; i < 1006; i++) { black[i] = 0; white[i] = 0; dp[i][0] = 1000000; dp[i][1] = 1000000; } for (i = 0; i < m; i++) { if (i != 0) { black[i] += black[i - 1]; white[i] += white[i - 1]; } for (j = 0; j < n; j++) { if (str[j][i] == '.') white[i]++; else black[i]++; } } for (i = m; i >= 1; i--) { white[i] = white[i - 1]; black[i] = black[i - 1]; } white[0] = 0; black[0] = 0; dp[0][1] = 0; dp[0][0] = 0; for (i = 0; i < m; i++) { for (j = x; j <= y; j++) { if (i + j <= m) { dp[i + j][0] = min(dp[i + j][0], dp[i][1] + white[i + j] - white[i]); dp[i + j][1] = min(dp[i + j][1], dp[i][0] + black[i + j] - black[i]); } } } cout << min(dp[m][0], dp[m][1]); }
9
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double PI = acos((long double)-1.0); const double EPS = 1e-10; const int MOD = 1e9 + 7; template <typename T> void cmin(T &x, T y) { if (y < x) x = y; } template <typename T> void cmax(T &x, T y) { if (y > x) x = y; } long long qpow(long long x, long long n, long long mod = MOD) { if (n < 0) return 0; long long res = 1; while (n) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long sq(long long x) { return x * x; } long long read() { long long res = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) { res = res * 10 + (c - '0'); c = getchar(); } return res; } int n, m, k; int a[200005]; long long prefix[2005]; int y[2005]; long long dp[2005]; namespace Solver { void InitOnce() { int t; } void Read() { int res = scanf("%d%d%d", &n, &m, &k); if (res == -1) exit(0); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); } long long sum(int l, int r) { return prefix[r] - prefix[l - 1]; } void Solve() { for (int i = 1; i <= k; ++i) y[i] = i; while (m--) { int u, v; scanf("%d%d", &u, &v); if (u > k) continue; cmin(y[u], u - v); } for (int i = 1; i <= k; ++i) prefix[i] = prefix[i - 1] + a[i]; memset(dp, LINF, sizeof(dp)); cmin(dp[0], 0LL); for (int i = 1; i <= k; ++i) { for (int j = 1; j <= i; ++j) cmin(dp[i], dp[i - j] + sum(i - y[j] + 1, i)); } printf("%lld\n", dp[k]); } } // namespace Solver int main() { Solver::InitOnce(); while (true) { Solver::Read(); Solver::Solve(); } return 0; }
13
#include <bits/stdc++.h> using namespace std; long long int min(long long int a, long long int b) { if (a < b) return a; return b; } long long int max(long long int a, long long int b) { if (a > b) return a; return b; } int main() { int n, m; cin >> n; if (n % 2 == 0) m = (n + 2) / 2; else m = (n + 1) / 2; cout << m << endl; int x[n], y[n], cnt1 = 0, cnt2 = 1; for (int i = 0; i < n; i++) { if (i % 2 == 0) cnt1++; x[i] = cnt1; if (i % 2 == 1) cnt2++; y[i] = cnt2; } for (int i = 0; i < n; i++) cout << x[i] << " " << y[i] << endl; }
3
#include <bits/stdc++.h> using namespace std; char str[10]; vector<int> V; class op { public: int type; int value; void input() { scanf("%s", str); if (strcmp(str, "add") == 0) { type = 0; } else if (strcmp(str, "del") == 0) { type = 1; } else { type = 2; return; } scanf("%d", &value); V.push_back(value); } } node[100005]; class Node { public: int l, r; int cnt; long long mod[5]; } seg[100005 << 2]; void build(int i, int l, int r) { seg[i].l = l; seg[i].r = r; seg[i].cnt = 0; for (int j = 0; j < 5; j++) seg[i].mod[j] = 0; if (l != r) { int mid = (l + r) >> 1; build(i << 1, l, mid); build(i << 1 | 1, mid + 1, r); } } void updata(int i, int pos, int value) { seg[i].cnt += value; if (seg[i].l == seg[i].r) { if (seg[i].cnt == 0) seg[i].mod[1] = 0; else { seg[i].mod[1] = V[pos - 1]; } return; } int mid = (seg[i].l + seg[i].r) >> 1; if (pos <= mid) { updata(i << 1, pos, value); } else { updata(i << 1 | 1, pos, value); } for (int j = 0; j < 5; j++) { seg[i].mod[j] = seg[i << 1].mod[j]; } for (int j = 0; j < 5; j++) { seg[i].mod[(j + seg[i << 1].cnt) % 5] += seg[i << 1 | 1].mod[j]; } } int main() { int N; while (~scanf("%d", &N)) { int sc = 0; V.clear(); for (int i = 0; i < N; i++) { node[i].input(); } sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end()); if (V.size() == 0) { cout << 0 << endl; continue; } build(1, 1, V.size()); for (int i = 0; i < N; i++) { int pos = lower_bound(V.begin(), V.end(), node[i].value) - V.begin() + 1; if (node[i].type == 0) { updata(1, pos, 1); } else if (node[i].type == 1) { updata(1, pos, -1); } else { cout << seg[1].mod[3] << endl; } } } }
15
#include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const int N = 1000 + 10; const int M = 5000 + 10; const double pi = 3.1415926535; int main(void) { int n; scanf("%d", &n); priority_queue<int, vector<int>, greater<int> > q; for (int i = 1; i <= n; i++) { int t; scanf("%d", &t); q.push(t); } while (n > 2) { int a = q.top(); q.pop(); int b = q.top(); q.pop(); q.push(a + b); n--; } int a = q.top(); q.pop(); int b = q.top(); q.pop(); printf("%d", b - a + 1); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m, node[N], parent[N], sum[N]; struct Edge { int u, v, w; Edge(int u = 0, int v = 0, int w = 0) { this->u = u; this->v = v; this->w = w; } } e[N]; bool cmp(Edge a, Edge b) { return a.w > b.w; } int find(int x) { if (x == parent[x]) return x; return parent[x] = find(parent[x]); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { parent[i] = i; sum[i] = 1; scanf("%d", &node[i]); } int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); e[i] = Edge(u, v, min(node[u], node[v])); } sort(e, e + m, cmp); double ans = 0; for (int i = 0; i < m; i++) { int pa = find(e[i].u); int pb = find(e[i].v); if (pa != pb) { ans += (double)e[i].w * sum[pa] * sum[pb]; parent[pb] = pa; sum[pa] += sum[pb]; } } printf("%.6lf\n", ans * 2 / (n * 1.0 * (n - 1))); return 0; }
11
#include <bits/stdc++.h> using namespace std; struct dat { long long scm, scl, tm, tl; long double pro; bool operator<(dat a) const { return tl * 1e6 * (1 - pro) * a.pro < a.tl * 1e6 * (1 - a.pro) * pro; } } plm[1005]; pair<long double, long double> dp[1005][1561]; int main() { int n, t; cin >> n >> t; for (int i = 0; i < n; i++) { long long x1, x2, t1, t2; long double p; cin >> x1 >> x2 >> t1 >> t2 >> p; plm[i].scm = x1; plm[i].scl = x2; plm[i].tm = t1; plm[i].tl = t2; plm[i].pro = 1 - p; } sort(plm, plm + n); dp[0][0] = make_pair(0, 0); for (int i = 0; i <= t; i++) dp[0][i] = make_pair(0, 0); dp[0][(int)plm[0].tm] = make_pair(plm[0].scm * 1e6, -plm[0].tm * 1e6); dp[0][(int)plm[0].tl + plm[0].tm] = make_pair(plm[0].scm * 1e6 + plm[0].scl * 1e6 * plm[0].pro, -plm[0].tm * 1e6 - plm[0].tl * 1e6 * plm[0].pro); for (int i = 1; i < n; i++) { for (int j = 0; j <= t; j++) { dp[i][j] = dp[i - 1][j]; long double p = plm[i].pro; long long x1 = plm[i].scm, x2 = plm[i].scl, t1 = plm[i].tm, t2 = plm[i].tl; if (t1 <= j) { long double la1 = dp[i - 1][j - t1].first, la2 = -dp[i - 1][j - t1].second; dp[i][j] = max(dp[i][j], make_pair(la1 + x1 * 1e6, -la2 - t1 * 1e6)); } if (t1 + t2 <= j) { long double la1 = dp[i - 1][j - t1 - t2].first, la2 = -dp[i - 1][j - t1 - t2].second; dp[i][j] = max(dp[i][j], make_pair(la1 + x1 * 1e6 + x2 * 1e6 * p, -p * j * 1e6 - (1 - p) * (t1 * 1e6 + la2))); } } } pair<long double, long double> res = make_pair(0, 0); for (int i = 0; i <= t; i++) { res = max(res, dp[n - 1][i]); } cout << fixed << setprecision(15) << res.first / 1e6 << " " << -res.second / 1e6; }
20
#include <bits/stdc++.h> using namespace std; int main() { int n, changes; cin >> n; char input[n + 1]; scanf("%s", input); if (n > ('z' - 'a' + 1)) { cout << "-1" << endl; return 0; } changes = 0; for (int i = 0; i < n; i++) { if (input[i] == '*') continue; for (int j = i + 1; j < n; j++) { if (input[j] == input[i]) { changes++; input[j] = '*'; } } } cout << changes << endl; }
2
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1000005; const long long SQRTN = 1003; const long long LOGN = 22; const double PI = acos(-1); const long long INF = 1e16; const long long MOD = 1000000007; const long long FMOD = 998244353; const double eps = 1e-9; bool primes[100001]; long long spf[100001]; void sieve() { primes[0] = primes[1] = false; for (long long i = 1; i <= 100000; i++) { spf[i] = i; primes[i] = true; } spf[2] = 2; for (long long i = 4; i <= 100000; i += 2) { spf[i] = 2; primes[i] = false; } for (long long i = 3; i * i <= 100000; i += 2) { if (spf[i] == i) { for (long long j = i * i; j <= 100000; j += i) { if (spf[j] == j) spf[j] = i; primes[j] = false; } } } return; } long long power(long long n, long long m, long long p) { long long res = 1; n = n % MOD; while (m > 0) { if (m & 1) { res = ((res) % p * (n) % p) % p; m--; } n = ((n) % p * (n) % p) % p; m >>= 1; } return res; } long long N = 10000; long long factorialNumInverse[100001]; long long naturalNumInverse[100001]; long long fact[100001]; void InverseofNumber(long long p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for (long long i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p; } void InverseofFactorial(long long p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; for (long long i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } void factorial(long long p) { fact[0] = 1; for (long long i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * i) % p; } } long long Binomial(long long N, long long R, long long p) { long long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } long long modInverse(long long n, long long p) { return power(n, p - 2, p); } long long nCrModPFermat(long long n, long long r, long long p) { if (n < r) return 0; if (r == 0) return 1; long long fac[n + 1]; fac[0] = 1; for (long long i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } void solvethetestcase(); signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; for (long long testcase = 1; testcase < t + 1; testcase++) { solvethetestcase(); } } long long n, m, a, b, c, zero, p, t1, t2, t, q, cnt; string getString(string s1, string s2) { string s = ""; long long k = s1.length(); for (long long i = 0; i < k; i++) { if (s1[i] == s2[i]) s.push_back(s1[i]); else s.push_back('E' + 'T' + 'S' - s1[i] - s2[i]); } return s; } void solvethetestcase() { long long k; cin >> n >> k; vector<string> vec(n); map<string, long long> mi; for (long long i = 0; i < n; i++) { string s; cin >> s; vec[i] = s; mi[s]++; } long long ans = 0; for (long long i = 0; i < n; i++) { string s1 = vec[i]; for (long long j = i + 1; j < n; j++) { string s2 = vec[j]; ans += mi[getString(s1, s2)]; } } cout << ans / 3 << "\n"; return; }
7
#include <bits/stdc++.h> using namespace std; long long n; int dx[6] = {-1, -2, -1, 1, 2, 1}, dy[6] = {2, 0, -2, -2, 0, 2}; int main() { cin >> n; if (n == 0) { cout << "0 0\n"; return 0; } long long l = 0, r = 1000000000; while (l < r) { long long mid = (l + r + 1) >> 1; long long pp = mid * (mid + 1) * 3; if (pp >= n) r = mid - 1; else l = mid; } n -= l * (l + 1) * 3; long long sx = l * 2 + 2, sy = 0, qs = l + 1; for (int i = 0; i < 6; i++) { if (n < qs) { sx += dx[i] * n; sy += dy[i] * n; break; } n -= qs; sx += dx[i] * qs; sy += dy[i] * qs; } cout << sx << " " << sy << "\n"; }
13
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, K = 300; struct query { int l, r, ind; }; bool cmp(query a, query b) { if (a.l / K != b.l / K) return (a.l / K < b.l / K); else return (a.r < b.r); } int poj[N], sol[N], arr[N]; query qs[N]; int prijelaz(pair<int, int> a, pair<int, int> b, int t) { if (a.first < b.first) { for (int i = a.first; i < b.first; i++) { if (arr[i] > 100000) continue; if (poj[arr[i]] == arr[i]) t--; poj[arr[i]]--; if (poj[arr[i]] == arr[i]) t++; } } else { for (int i = b.first; i < a.first; i++) { if (arr[i] > 100000) continue; if (poj[arr[i]] == arr[i]) t--; poj[arr[i]]++; if (poj[arr[i]] == arr[i]) t++; } } if (a.second < b.second) { for (int i = a.second + 1; i <= b.second; i++) { if (arr[i] > 100000) continue; if (poj[arr[i]] == arr[i]) t--; poj[arr[i]]++; if (poj[arr[i]] == arr[i]) t++; } } else { for (int i = b.second + 1; i <= a.second; i++) { if (arr[i] > 100000) continue; if (poj[arr[i]] == arr[i]) t--; poj[arr[i]]--; if (poj[arr[i]] == arr[i]) t++; } } return t; } int main() { ios::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> arr[i]; } for (int i = 0; i < m; i++) { cin >> qs[i].l >> qs[i].r; qs[i].ind = i; } sort(qs, qs + m, cmp); int temp = 0; pair<int, int> lst = make_pair(1, 0); temp = 0; for (int i = 0; i < m; i++) { pair<int, int> kju = make_pair(qs[i].l, qs[i].r); temp = prijelaz(lst, kju, temp); sol[qs[i].ind] = temp; lst = kju; } for (int i = 0; i < m; i++) cout << sol[i] << "\n"; return 0; }
10
#include <bits/stdc++.h> using namespace std; void read(int &x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); } const int N = 5e5 + 5; int n, res, a[N], lg[N], ma[N], na[N], b[N]; struct st { int c[20][N]; void work(int *a) { memcpy(c[0], a, (n + 1) << 2); for (int j = 1; j < 20; ++j) for (int i = 0; i + (1 << j) - 1 <= n; ++i) c[j][i] = min(c[j - 1][i], c[j - 1][i + (1 << (j - 1))]); } int ask(int x, int d) { int l = x - d - 1, r = x + d, t = lg[r - l + 1]; return min(c[t][l], c[t][r - (1 << t) + 1]); } } mx, mn; signed main() { read(n); lg[0] = -1; for (int i = 1; i <= n + 1; ++i) lg[i] = lg[i >> 1] + 1; for (int i = 1; i <= n; ++i) read(a[i]); a[0] = a[1], a[n + 1] = a[n]; for (int i = 0; i <= n; ++i) ma[i] = -min(a[i], a[i + 1]), na[i] = max(a[i], a[i + 1]); mn.work(na), mx.work(ma); for (int i = 1; i <= n; ++i) { int l = 0, r = min(i - 1, n - i), mid; int ta, tb, val = 0; while (l <= r) { mid = l + r >> 1; ta = mn.ask(i, mid), tb = -mx.ask(i, mid); val = max(val, min(ta, tb)); ta > tb ? l = mid + 1 : r = mid - 1; } b[i] = val, res = max(res, l); } printf("%d\n", res); for (int i = 1; i <= n; ++i) printf("%d ", b[i]); puts(""); return 0; }
25
#include <bits/stdc++.h> #define ll long long #define endl '\n' #define PI acos(-1) #define sz 10020 #define mod 10000007 #define RUN_FAST ios::sync_with_stdio(false); using namespace std; //int row[]={0, 0, -1, 1};///4 adjacent(Up, down, left, right) //int col[]={1, -1, 0, 0}; //int row[]={-2, -2, 2, 2, -1, -1, 1, 1};///Knight moves on //int col[]={1, -1, 1, -1, 2, -2, 2, -2};///chess board int main() { RUN_FAST; cin.tie(nullptr); int T, n, i, j, r1, c1, r2, c2; vector <string> v; vector <pair <int, int>> pos; string s; cin >> T; while (T--) { cin >> n; for (i=0; i<n; i++) { cin >> s; v.push_back(s); } for (i=0; i<n; i++) { for (j=0; j<n; j++) if (v[i][j]=='*') pos.push_back({i, j}); } r1=pos[0].first, c1=pos[0].second; r2=pos[1].first, c2=pos[1].second; if (r1==r2) { if (r1==0) v[r1+1][c1]=v[r1+1][c2]='*'; else v[r1-1][c1]=v[r1-1][c2]='*'; } else if (c1==c2) { if (c1==0) v[r1][c1+1]=v[r2][c1+1]='*'; else v[r1][c1-1]=v[r2][c1-1]='*'; } else v[r1][c2]=v[r2][c1]='*'; for (string ss: v) cout << ss << endl; v.clear(); pos.clear(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int M = 100000 + 10; int d[M]; long long int pred[M]; long long int a[M]; long long int dp[110][M]; long long int pres[M]; long long int dq[M]; long long int yk(int i, int k) { long long int val = dp[i][k] + pres[k]; return val; } int main() { int n, m, p; cin >> n >> m >> p; for (int i = 2; i <= n; i++) { cin >> d[i]; pred[i] = pred[i - 1] + d[i]; } for (int i = 1; i <= m; i++) { int h, t; cin >> h >> t; int val = t - pred[h]; a[i] = val; } sort(a + 1, a + m + 1); for (int i = 1; i <= m; i++) { pres[i] = pres[i - 1] + a[i]; } for (int i = 1; i <= m; i++) { dp[1][i] = (i - 0) * a[i] - (pres[i] - pres[0]); } for (int i = 2; i <= p; i++) { int s = 0; int t = 0; for (int j = 1; j <= m; j++) { while ((t - s) >= 2 && (yk(i - 1, dq[t - 1]) - yk(i - 1, dq[t - 2])) * (j - dq[t - 1]) > (yk(i - 1, j) - yk(i - 1, dq[t - 1])) * (dq[t - 1] - dq[t - 2])) { t--; } dq[t] = j; t++; while ((t - s) >= 2 && dp[i - 1][dq[s]] - dq[s] * a[j] + pres[dq[s]] > dp[i - 1][dq[s + 1]] - dq[s + 1] * a[j] + pres[dq[s + 1]]) { s++; } dp[i][j] = dp[i - 1][dq[s]] + (j - dq[s]) * a[j] - (pres[j] - pres[dq[s]]); } } cout << dp[p][m] << endl; return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { long long q; cin >> q; while (q--) { long long n, k; cin >> n >> k; string s; cin >> s; long long lz = -1, ocnt = 0; for (long long i = 0; i < s.size(); i++) { if (k == 0) break; if (s[i] == '1') ocnt++; if (s[i] == '0') { if (ocnt == 0) { lz = i; continue; } long long dist = i - (lz + 1); if (dist <= k) { k = k - dist; s[lz + 1] = '0'; s[i] = '1'; lz++; } else { long long x = i - k; if (s[x] == '1') { s[x] = '0'; s[i] = '1'; k = 0; } } } } cout << s << "\n"; } return 0; }
7
#include <bits/stdc++.h> using namespace std; int n, m, last; int nxt[300300 * 6][27], link[300300 * 6], dp[300300 * 6], cnt[300300 * 6], ord[300300 * 6], occur[3][300300 * 6]; char s[300300 * 6]; void extend(int c) { if (nxt[last][c] && dp[nxt[last][c]] == dp[last] + 1) { last = nxt[last][c]; return; } int x = last, y = last = ++m; dp[y] = dp[x] + 1; while (x && !nxt[x][c]) nxt[x][c] = y, x = link[x]; if (!x) link[y] = 1; else { int z = nxt[x][c]; if (dp[z] == dp[x] + 1) link[y] = z; else { int u = ++m; memcpy(nxt[u], nxt[z], sizeof nxt[u]); link[u] = link[z], link[z] = link[y] = u; dp[u] = dp[x] + 1; while (x && nxt[x][c] == z) nxt[x][c] = u, x = link[x]; } } } const int mod = 1e9 + 7; int ans[300300 * 6]; int main() { int mn = 1e9; last = ++m; for (int i = 0; i < 3; i++) { scanf("%s", s); int len = strlen(s); mn = min(mn, len); n = max(n, len); last = 1; for (int j = 0; j < len; j++) extend(s[j] - 'a'), occur[i][last]++; } for (int i = 1; i <= m; i++) cnt[dp[i]]++; for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; for (int i = 1; i <= m; i++) ord[cnt[dp[i]]--] = i; for (int i = m; i >= 1; i--) { int x = ord[i]; for (int j = 0; j < 3; j++) occur[j][link[x]] += occur[j][x]; } for (int i = 1; i <= m; i++) { int x = ord[i], tmp = 1ll * occur[0][x] * occur[1][x] % mod * occur[2][x] % mod; int l = dp[link[x]] + 1, r = dp[x]; ans[l] = (ans[l] + tmp) % mod; ans[r + 1] = (ans[r + 1] - tmp + mod) % mod; } for (int i = 1; i <= mn; i++) { ans[i] = (ans[i] + ans[i - 1]) % mod; printf("%d%c", ans[i], i < mn ? ' ' : '\n'); } return 0; }
16
#include <bits/stdc++.h> using namespace std; struct node { node() { t.resize(3); } node(int a, int b, int c) { t.resize(3); t[0] = a; t[1] = b; t[2] = c; } vector<int> t; }; void actualiser(node &n) { sort(n.t.begin(), n.t.end()); } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; int a, b, c; cin >> a >> b >> c; a--; b--; c--; vector<string> mat(n); for (int c = 0; c < n; c++) cin >> mat[c]; vector<vector<vector<pair<int, int> > > > previous( n, vector<vector<pair<int, int> > >( n, vector<pair<int, int> >(n, make_pair(-1, -1)))); if (b == min(a, min(b, c))) swap(a, b); if (c == min(a, min(b, c))) swap(a, c); if (b == max(a, max(b, c))) swap(b, c); previous[a][b][c] = make_pair(1992, 1992); queue<node> q; q.push(node(a, b, c)); node act; while (q.size()) { act = q.front(); q.pop(); if (*max_element(act.t.begin(), act.t.end()) == 2) { break; } for (int c = 0; c < 3; c++) { for (int c2 = 0; c2 < n; c2++) { if (c2 == act.t[0] || c2 == act.t[1] || c2 == act.t[2]) continue; if (mat[act.t[c]][c2] == mat[act.t[(c + 1) % 3]][act.t[(c + 2) % 3]]) { pair<int, int> change = make_pair(act.t[c], c2); vector<int> new_node(3); new_node[c] = c2; new_node[(c + 1) % 3] = act.t[(c + 1) % 3]; new_node[(c + 2) % 3] = act.t[(c + 2) % 3]; sort(new_node.begin(), new_node.end()); if (previous[new_node[0]][new_node[1]][new_node[2]].first == -1) { previous[new_node[0]][new_node[1]][new_node[2]] = change; q.push(node(new_node[0], new_node[1], new_node[2])); } } } } } if (*max_element(act.t.begin(), act.t.end()) != 2) { cout << -1; return 0; } vector<pair<int, int> > res; while (previous[act.t[0]][act.t[1]][act.t[2]].first != 1992) { pair<int, int> change = previous[act.t[0]][act.t[1]][act.t[2]]; res.push_back(change); if (act.t[0] == change.second) act.t[0] = change.first; else if (act.t[1] == change.second) act.t[1] = change.first; else if (act.t[2] == change.second) act.t[2] = change.first; actualiser(act); } reverse(res.begin(), res.end()); cout << res.size() << endl; for (int c = 0; c < res.size(); c++) { cout << res[c].first + 1 << " " << res[c].second + 1 << endl; } }
13
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int s1 = max((3 * a / 10), (a - (a / 250) * c)); int s2 = max((3 * b / 10), (b - (b / 250) * d)); if (s1 > s2) cout << "Misha"; else if (s1 < s2) cout << "Vasya"; else cout << "Tie"; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct pp { int x; int y; }; int a[100][100], n; vector<pp> v[5]; string s[51]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; s[i] = '0' + s[i]; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { pp p; p.x = i; p.y = j; if (s[i][j] == 'o') v[2].push_back(p); if (s[i][j] == 'x') v[1].push_back(p); if (s[i][j] == '.') v[0].push_back(p); } a[n][n] = 2; for (int i = 0; i < v[0].size(); i++) for (int j = 0; j < v[2].size(); j++) { int xx = v[0][i].x - v[2][j].x; int yy = v[0][i].y - v[2][j].y; a[n + xx][n + yy] = 1; } for (int i = 1; i <= 2 * n - 1; i++) for (int j = 1; j <= 2 * n - 1; j++) if (a[i][j] == 0) { pp p; p.x = i - n; p.y = j - n; v[4].push_back(p); } bool cann = 1; for (int i = 0; i < v[1].size(); i++) { bool sisi = 0; for (int j = 0; j < v[4].size(); j++) { int xx = v[1][i].x - v[4][j].x; int yy = v[1][i].y - v[4][j].y; if (xx >= 1 && yy >= 1 && xx <= n && yy <= n) if (s[xx][yy] == 'o') { sisi = 1; } } if (sisi == 0) cann = 0; } if (cann == 0) cout << "NO" << endl; if (cann == 1) { cout << "YES" << endl; for (int i = 1; i <= 2 * n - 1; i++) { for (int j = 1; j <= 2 * n - 1; j++) { if (a[i][j] == 2) cout << "o"; if (a[i][j] == 1) cout << "."; if (a[i][j] == 0) cout << "x"; } cout << endl; } } }
10
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); } void inout() {} int main() { fast(); inout(); int n, x, y; cin >> n >> x >> y; vector<int> v(n); int a = 0; for (long long int i = 0; i < n; i++) { cin >> v[i]; if (v[i] <= x) a++; } if (x > y) cout << n; else cout << ceil((double)a / 2); cout << "\n"; }
4
#include <bits/stdc++.h> using namespace std; const int N = 10000005; bitset<N> isPrime; vector<int> primes; void sieve() { primes.push_back(0); isPrime.set(); isPrime[0] = isPrime[1] = 0; for (int i = 2; i < N; ++i) { if (isPrime[i]) { primes.push_back(i); for (long long int j = 1LL * i * i; j < N; j += i) { isPrime[j] = 0; } } } } int main() { sieve(); vector<int> rslt; int ones = 0, twos = 0, n, temp1, temp2, m; scanf("%d", &n); while (n--) { scanf("%d", &temp1); temp1 == 1 ? ones++ : twos++; } for (int i = 1; i < primes.size(); i++) { if (!(twos) && !(ones)) break; temp2 = primes[i] - primes[i - 1]; if (ones && (temp2 == 1 || !(twos))) { ones--; rslt.push_back(1); } else if (twos) { m = twos >= temp2 / 2 ? temp2 / 2 : twos; while (m--) { twos--; rslt.push_back(2); } } } for (int y : rslt) printf("%d ", y); return 0; }
4
#include <bits/stdc++.h> char a[4020][2010]; long b[2010]; int main() { int i, j, t, n, m; long k; while (scanf("%d%d%ld", &n, &m, &k) != EOF) { memset(b, 0, sizeof(b)); for (i = 0; i < n; i++) scanf("%s", a[i]); for (t = 0; t < n; t++) { for (j = 0; j < m; j++) { if (a[t + t][j] == 'U' && t + t <= n - 1) b[j]++; if (a[t][j + t] == 'L' && j + t <= m - 1) b[j]++; if (a[t][j - t] == 'R' && j - t >= 0) b[j]++; } } printf("%ld", b[0]); for (j = 1; j < m; j++) printf("% ld", b[j]); printf("\n"); } }
6
#include <bits/stdc++.h> using namespace std; struct S { long long sta; long long end; }; bool cmp(S a, S b) { if (a.end == b.end) { return a.sta < b.sta; } else { return a.end < b.end; } } int main() { struct S s[500100] = {}; long long n, i, j, cnt = 0; while (~scanf("%lld", &n)) { memset(s, 0, sizeof(s)); for (i = 0; i < n; i++) { scanf("%lld%lld", &s[i].sta, &s[i].end); } sort(s, s + n, cmp); for (j = 0, i = 1; i < n; i++) { if (s[j].end < s[i].sta) { cnt++; j = i; } } printf("%lld\n", cnt + 1); cnt = 0; } }
8
#include<bits/stdc++.h> #include <limits> using namespace std; #define ll long long #define ld long double #define pb push_back #define ff first #define ss second #define mp make_pair #define ins insert #define sb substr #define con continue #define ub upper_bound #define lb lower_bound #define pll pair<ll,ll> #define pld pair<ld,ld> ll modexp(ll x, ll n); ll getFractionModulo(ll a, ll b); ll power(ll x, ll y); ll mpower(ll x, ll y, ll p); bool isPrime(ll n); ll const N=(ll)(1e5+1); ll const mod=(ll)(1e9+7); vector<ll>g[N]; ll visited[N]={0}; ll parent[N]={0}; ll a[N]={0}; ll flag=0; vector<ll>no; void dfs(ll v) { if(visited[v]) return; visited[v]=true; no.pb(v); for(auto x:g[v]) { if(!visited[x]) { dfs(x); } } } ll gcd(ll a,ll b) { if(a==0) return b; return gcd(b%a,a); } ll binomialCoeff(ll n, ll r) { // Optimization for the cases when r is large if (r > n - r) r = n - r; // The array C is going to store last row of // pascal triangle at the end. And last entry // of last row is nCr ll C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (ll i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (ll j = min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % mod; } return C[r]; } bool isPerfect(ll N) { if ((sqrt(N) - floor(sqrt(N))) != 0) return false; return true; } ll setBitNumber(ll n) { n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n = n + 1; return (n >> 1); } ll myXOR(ll x, ll y) { ll res = 0; // Initialize result // Assuming 32-bit Integer for (int i = 31; i >= 0; i--) { // Find current bits in x and y bool b1 = x & (1 << i); bool b2 = y & (1 << i); // If both are 1 then 0 else xor is same as OR bool xoredBit = (b1 & b2) ? 0 : (b1 | b2); // Update result res <<= 1; res |= xoredBit; } return res; } long double squareRoot(ll number, ll precision) { ll start = 0, end = number; ll mid; // variable to store the answer long double ans; // for computing integral part // of square root of number while (start <= end) { mid = (start + end) / 2; if (mid * mid == number) { ans = mid; break; } // incrementing start if integral // part lies on right side of the mid if (mid * mid < number) { start = mid + 1; ans = mid; } // decrementing end if integral part // lies on the left side of the mid else { end = mid - 1; } } // For computing the fractional part // of square root upto given precision long double increment = 0.1; for (int i = 0; i < precision; i++) { while (ans * ans <= number) { ans += increment; } // loop terminates when ans * ans > number ans = ans - increment; increment = increment / 10; } return ans; } bool sortbyfirstdesc(const pair<ll,ll> &a, const pair<ll,ll> &b) { return a.first>b.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t,i,l,h,r,x,y,z,d,f,s,k1,k2,c,a,w,b,m,n1,M=(ll)(1e9+7); ll n,K; ll p,q,j,k; // cout<<perfe.size()<<endl; // cout<<gp<<endl; cin>>t; // t=1; while(t--) { cin>>n; string arr[n]; for(i=0;i<n;i++) { cin>>arr[i]; } vector<string>ans; i=0; string temp=""; stack<string>st; while(i<n) { if(arr[i]=="1") { if(temp=="") temp+="1"; else temp+=".1"; ans.pb(temp); st.push(temp); } else { while(st.size()) { string temp1=""; temp=st.top(); st.pop(); j=temp.size()-1; while(j>=0 && temp[j]!='.') { temp1+=temp[j]; j--; } reverse(temp1.begin(),temp1.end()); if(stoi(temp1)+1==stoi(arr[i])) { break; } } if(st.empty()) temp=""; else { while(temp.size() && temp.back()!='.') temp.pop_back(); } if(temp.size() && temp.back()!='.') temp+='.'; temp+=arr[i]; ans.pb(temp); st.push(temp); } i++; } for(i=0;i<ans.size();i++) { cout<<ans[i]<<endl; } } } ll mpower(ll x, ll y, ll p){ ll res = 1; x = x % p; while(y > 0){ if(y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } ll getFractionModulo(ll a, ll b) { ll c = gcd(a, b); a = a / c; b = b / c; // (b ^ m-2) % m ll d = modexp(b, mod - 2); // Final answer ll ans = ((a % mod) * (d % mod)) % mod; return ans; } ll modexp(ll x, ll n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return modexp((x * x) % mod, n / 2); } else { return (x * modexp((x * x) % mod, (n - 1) / 2) % mod); } } ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = res*x; y = y>>1; x = x*x; } return res; } bool isPrime(ll n){ if (n <= 1) return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false; ll p=sqrt(n); for(ll i=5;i<=p;i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } /* 65 183 17 8 10 9*/
8
#include <bits/stdc++.h> using namespace std; std::mt19937 rnd( (int)std::chrono::steady_clock::now().time_since_epoch().count()); long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 200000; int n; int a[MAXN]; int cnt[MAXN], freq[MAXN + 1], mxcnt; int solve() { int ret = 0; int magic = sqrt(n); for (int lim = (1); lim <= (magic); ++lim) { for (int i = (0); i < (n); ++i) cnt[i] = 0; for (int i = (0); i <= (n); ++i) freq[i] = 0; freq[0] = n, mxcnt = 0; int l = 0, r = 0; int cur = 0; while (true) { if (mxcnt > lim) { --freq[cnt[a[l]]]; --cnt[a[l]]; ++freq[cnt[a[l]]]; ++l; if (freq[mxcnt] == 0) --mxcnt; } else { if (freq[mxcnt] >= 2) cur = max(cur, r - l); if (r >= n) break; --freq[cnt[a[r]]]; ++cnt[a[r]]; ++freq[cnt[a[r]]]; ++r; if (mxcnt < n && freq[mxcnt + 1] > 0) ++mxcnt; } } ret = max(ret, cur); } for (int i = (0); i < (n); ++i) cnt[i] = 0; for (int i = (0); i < (n); ++i) ++cnt[a[i]]; int x = 0; for (int i = (0); i < (n); ++i) if (cnt[i] > cnt[x]) x = i; for (int y = (0); y < (n); ++y) if (y != x && cnt[y] > magic) { vector<int> bal(n + 1); bal[0] = 0; for (int i = (0); i < (n); ++i) bal[i + 1] = bal[i] + (a[i] == x ? -1 : a[i] == y ? +1 : 0); int lo = 0; for (int i = (0); i <= (n); ++i) lo = min(lo, bal[i]); int hi = 0; for (int i = (0); i <= (n); ++i) hi = max(hi, bal[i]); vector<int> fst(hi - lo + 1, INT_MAX), lst(hi - lo + 1, INT_MIN); for (int i = (0); i <= (n); ++i) fst[bal[i] - lo] = min(fst[bal[i] - lo], i), lst[bal[i] - lo] = max(lst[bal[i] - lo], i); int cur = 0; for (int i = (0); i < (((int)(fst).size())); ++i) if (fst[i] <= lst[i]) cur = max(cur, lst[i] - fst[i]); ret = max(ret, cur); } return ret; } void run() { scanf("%d", &n); for (int i = (0); i < (n); ++i) scanf("%d", &a[i]), --a[i]; printf("%d\n", solve()); } int solvestupid() { int ret = 0; for (int l = (0); l < (n); ++l) { vector<int> cnt(n, 0); vector<int> freq(n + 1, 0); freq[0] = n; int mxcnt = 0; for (int r = (l); r < (n); ++r) { --freq[cnt[a[r]]]; ++cnt[a[r]]; ++freq[cnt[a[r]]]; if (mxcnt + 1 <= n && freq[mxcnt + 1] > 0) ++mxcnt; if (freq[mxcnt] >= 2) ret = max(ret, r - l + 1); } } return ret; } void stress() { for (int rep = (0); rep < (100000); ++rep) { n = rnd() % 1000 + 1; int mxval = rnd() % min(n, 100); for (int i = (0); i < (n); ++i) a[i] = rnd() % (mxval + 1); int have = solve(); int want = solvestupid(); if (have == want) { printf("."); continue; } printf("err have=%d want=%d\n", have, want); } } int main() { run(); return 0; }
18
#include <bits/stdc++.h> using namespace std; int main() { vector<long long> v, e, o, q; ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); long long t, a, b; cin >> t; while (t--) { long long a, b, c, n; cin >> a >> b >> c >> n; long long d = max(a, max(b, c)); d = d - a + d - b + d - c; if (n < d) cout << "NO" << endl; else { n = n - d; if (n % 3 == 0) cout << "YES" << endl; else cout << "NO" << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 40; long long R() { long long ans = 0, f = 1; char c = getchar(); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1; for (; c >= '0' && c <= '9'; c = getchar()) ans = ans * 10 + c - '0'; return ans * f; } long long dp[N][N], s[N], ans, P, n; void Work() { memset(dp, 0, sizeof(dp)); n = R(); P = R(); for (int i = 0; i <= 29; ++i) { int u = (1 << i), nx = min(n, (1ll << (i + 1)) - 1); if (u > n) s[i] = 0; else s[i] = nx - u + 1; } ans = 0; for (int i = 0; i <= 29; ++i) dp[1][i] = s[i], ans += s[i]; ans %= P; for (int i = 2; i <= 33; ++i) for (int j = 0; j <= 29; ++j) for (int k = j + 1; k <= 29; ++k) { dp[i][k] += dp[i - 1][j] * s[k]; dp[i][k] %= P; ans += dp[i - 1][j] * s[k]; ans %= P; } cout << ans << '\n'; } int main() { int t = R(); while (t--) Work(); return 0; }
9
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; template <class T> void cmin(T &a, T b) { if (b < a) a = b; } template <class T> void cmax(T &a, T b) { if (b > a) a = b; } template <class T> int len(const T &c) { return (int)c.size(); } template <class T> int len(char c[]) { return (int)strlen(c); } long stoi(string s) { long n = 0; for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48); return n; } int main() { ios_base::sync_with_stdio(false); long long n, m, col[101] = {0}, cur, v, tot; vector<long long> res; cin >> n >> m; tot = (1LL << (n - 1)); if (m == tot) { for (int i = n; i >= 1; i--) { cout << i << " "; } return 0; } cur = tot / 2; v = 1; while (1) { if (m > cur) { m -= cur; cur /= 2; v++; } else if (m == cur) { res.push_back(v); col[v] = 1; while (cur != 1) { v++; cur /= 2; } v++; res.push_back(v); col[v] = 1; for (int j = n; j >= 1; j--) { if (col[j] == 0) res.push_back(j); } break; } else { res.push_back(v); col[v] = 1; v++; cur /= 2; } if (len(res) == n) break; } for (int i(0), _n(len(res)); i < _n; ++i) cout << res[i] << " "; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 20; int n, m, ans; char c[MAXN][MAXN]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> c[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { bool k = false; for (int a = -1; a < 2; a++) { if (k) break; for (int b = -1; b < 2; b++) if ((a + b) % 2 && c[i][j] == 'W' && c[i + a][j + b] == 'P') { ans++; k = true; break; } } } cout << ans; return 0; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; vector<int> G[100000 + 1]; deque<int> LAST; deque<int> newLAST; int cnt = 0; int main() { int N, K, zero = 0, lastzero = -1; cin >> N >> K; vector<std::pair<int, int> > D(N); for (long long i = 0; i < (int)N; i++) cin >> D[i].first, D[i].second = i, zero += D[i].first == 0, lastzero = D[i].first == 0 ? i : lastzero; if (zero != 1) return cout << -1, 0; sort((D).begin(), (D).end()); LAST.push_back(lastzero); int i = 1; if (D[i].first != 1) return cout << -1, 0; else { G[lastzero].push_back(D[i].second); G[D[i].second].push_back(lastzero); newLAST.push_back(D[i].second); } i++; for (; i < N; i++) { if (D[i - 1].first + 1 < D[i].first) return cout << -1, 0; if (D[i - 1].first + 1 == D[i].first) { LAST = newLAST; newLAST.clear(); newLAST.push_back(D[i].second); cnt = 0; G[LAST[cnt]].push_back(D[i].second); G[D[i].second].push_back(LAST[cnt]); } if (D[i - 1].first + 0 == D[i].first) { newLAST.push_back(D[i].second); G[LAST[cnt]].push_back(D[i].second); G[D[i].second].push_back(LAST[cnt]); } cnt++; cnt %= LAST.size(); } for (int i = 0; i < 100000; i++) { if (G[i].size() > K) return cout << -1, 0; } set<std::pair<int, int> > V; for (int i = 0; i < 100000; i++) { for (long long j = 0; j < (int)G[i].size(); j++) { V.insert(make_pair(min(i, G[i][j]), max(i, G[i][j]))); } } cout << V.size() << endl; for (auto it = (V).begin(); it != (V).end(); it++) { printf("%d %d\n", it->first + 1, it->second + 1); } return 0; }
10
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long a, long long b) { return b > 0 ? gcd(b, a % b) : a; } inline void YES() { cout << "YES\n"; } inline void NO() { cout << "NO\n"; } const long long mod = 1e9 + 7; const long long maxn = 300; long long rand_int(long long l, long long r) { static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<long long>(l, r)(gen); } long long quick(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = (res * x) % mod; x = x * x % mod; n >>= 1; } return res; } long long inv(long long x) { return quick(x, mod - 2); } long long fac[maxn], invfac[maxn], Inv[maxn]; void init() { Inv[1] = 1; for (long long i = 2; i < maxn; ++i) Inv[i] = (long long)(mod - mod / i) * Inv[mod % i] % mod; fac[0] = 1; for (long long i = 1; i < maxn; ++i) fac[i] = (fac[i - 1] * i) % mod; invfac[maxn - 1] = inv(fac[maxn - 1]); for (long long i = maxn - 2; i >= 0; --i) invfac[i] = (invfac[i + 1] * (i + 1)) % mod; } long long C(long long n, long long m) { if (n < m || m < 0) return 0; return fac[n] * invfac[m] % mod * invfac[n - m] % mod; } long long n, m; long long es[100][100]; const long long lim = 60; unordered_map<long long, unordered_map<long long, long long> > f; bool find(long long a, long long b) { if (f[a].count(b)) return 1; return 0; } bool check(const vector<long long> &a) { bool one = true; bool zero = true; for (long long i = 0; i < a.size(); i++) { for (long long j = 0; j < a.size(); j++) { if (i == j) continue; if (es[a[i]][a[j]]) zero = false; else one = false; } } return (one || zero); } bool check2(const vector<long long> &a) { bool one = true; bool zero = true; for (long long i = 0; i < a.size(); i++) { for (long long j = 0; j < a.size(); j++) { if (i == j) continue; if (find(a[i], a[j])) zero = false; else one = false; } } return (one || zero); } template <typename T> void subset(long long k, long long n, T &&f) { long long t = (1ll << k) - 1; while (t < 1ll << n) { vector<long long> ans; ans.clear(); for (long long i = 0; i < lim; i++) if ((1ll << i) & t) ans.push_back(i + 1); if (check(ans)) { for (auto &v : ans) cout << v << " "; cout << '\n'; exit(0); } long long x = t & -t, y = t + x; t = ((t & ~y) / x >> 1) | y; } } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; cin >> n >> m; if (n <= lim) { for (long long i = 1; i <= m; i++) { long long u, v; cin >> u >> v; es[u][v] = es[v][u] = 1; } subset(5, n, std::cout); cout << -1 << '\n'; return 0; } else { for (long long i = 1; i <= m; i++) { long long u, v; cin >> u >> v; f[u][v] = 1; f[v][u] = 1; } while (1) { vector<long long> ans; ans.clear(); for (long long i = 1; i <= 5; i++) ans.push_back(rand_int(1, n)); sort(ans.begin(), ans.end()); ans.erase(unique(ans.begin(), ans.end()), ans.end()); if (ans.size() != 5) continue; if (check2(ans)) { for (auto &v : ans) cout << v << " "; cout << '\n'; exit(0); } } } return 0; }
15
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; vector<string> y; int n; int main() { cin >> n; y.resize(n); for (int i = 0; i < (int)(n); i++) cin >> y[i]; string tmp, tm; for (int i = 0; i < (int)(n); i++) { tm = "2012"; for (int j = 0; j < (int)(4); j++) for (char x = '0'; x <= '9'; x++) { tmp = y[i]; tmp[j] = x; if ((!i || tmp >= y[i - 1]) && tmp[0] != '0' && tmp <= "2011" && tmp < tm) { tm = tmp; break; } } if (tm == "2012") { cout << "No solution"; exit(0); } else y[i] = tm; } for (int i = 0; i < (int)(n); i++) cout << y[i] << endl; return 0; }
9
#include <bits/stdc++.h> #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3, "Ofast", "inline") using namespace std; const int maxn = 2e6 + 7; const int INF = 1e9; const long long INFF = 1e18; int main() { int num0, num1, num2, num3; bool mark = true; scanf("%d%d%d%d", &num0, &num1, &num2, &num3); if (num2 == num3 - 1 && num0 == 0 && num1 == 0) { printf("YES\n"); printf("3 "); for (int i = 1; i <= num2; i++) printf("2 3 "); printf("\n"); return 0; } if (num1 == num0 - 1 && num2 == 0 && num3 == 0) { printf("YES\n"); printf("0 "); for (int i = 1; i <= num1; i++) printf("1 0 "); printf("\n"); return 0; } if (num1 < num0 || num2 < num3) mark = false; num1 -= num0; num2 -= num3; if (!mark) { printf("NO\n"); return 0; } if (num1 == num2) { printf("YES\n"); for (int i = 1; i <= num0; i++) printf("0 1 "); for (int i = 1; i <= num1; i++) printf("2 1 "); for (int i = 1; i <= num3; i++) printf("2 3 "); printf("\n"); } else if (num1 == num2 + 1) { printf("YES\n"); printf("1 "); for (int i = 1; i <= num0; i++) printf("0 1 "); for (int i = 1; i <= num2; i++) printf("2 1 "); for (int i = 1; i <= num3; i++) printf("2 3 "); printf("\n"); } else if (num2 == num1 + 1) { printf("YES\n"); printf("2 "); for (int i = 1; i <= num3; i++) printf("3 2 "); for (int i = 1; i <= num1; i++) printf("1 2 "); for (int i = 1; i <= num0; i++) printf("1 0 "); printf("\n"); } else { printf("NO\n"); } }
11
#include <bits/stdc++.h> using namespace std; int solve() {} int main() { int n; cin >> n; std::vector<int> v(n); int x; for (int i = 0; i < n; i++) { cin >> x; x--; v[x] = i; } long long s = 0; for (int i = 1; i < n; i++) { s += fabs(v[i] - v[i - 1]); } cout << s; return 0; }
4
#include <bits/stdc++.h> using namespace std; struct point { int x, y, sx, sy; void zip_cords(int a, int b) { if (y - x < 0) sy = (y - x) / (2 * b) - 1; else sy = (y - x) / (2 * b); if (y + x < 0) sx = (y + x) / (2 * a) - 1; else sx = (y + x) / (2 * a); } void init(int a, int b) { scanf("%d%d", &x, &y); zip_cords(a, b); } }; int a, b, n, m; point f, t; int main() { scanf("%d%d", &a, &b); f.init(a, b); t.init(a, b); n = abs(f.sx - t.sx) + 1; m = abs(f.sy - t.sy) + 1; printf("%d", max(n, m) - 1); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int maxm = 1e5 + 10; int x[3000], y[3000]; int sumx[100], sumy[100]; int vis[300][3000]; int main() { int a1, a2, b1, b2, c1, c2; scanf("%d%d%d%d%d%d", &a1, &a2, &b1, &b2, &c1, &c2); printf("3\n"); printf("%d %d\n", b1 + c1 - a1, b2 + c2 - a2); printf("%d %d\n", a1 + b1 - c1, a2 + b2 - c2); printf("%d %d\n", a1 + c1 - b1, a2 + c2 - b2); }
4
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; const long long nax = 2e5 + 5; const long long inf = 1e18; using pii = pair<long long, long long>; bool check(string s1, string s2) { if (s1 == s2) { return true; } if (s1.length() == 1) return false; if (s1.length() % 2 || s2.length() % 2) return false; if (check(s1.substr(0, s1.length() / 2), s2.substr(s2.length() / 2, s2.length() / 2))) { if (check(s1.substr(s1.length() / 2, s1.length() / 2), s2.substr(0, s2.length() / 2))) { return true; } } if (check(s1.substr(0, s1.length() / 2), s2.substr(0, s2.length() / 2))) { if (check(s1.substr(s1.length() / 2, s1.length() / 2), s2.substr(s2.length() / 2, s2.length() / 2))) { return true; } } return false; } void solve() { string s1, s2; cin >> s1 >> s2; if (check(s1, s2)) cout << "YES" << '\n'; else cout << "NO" << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1, t1 = 1; while (t--) { solve(); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int n, *a, d = 1; cin >> n; a = new int[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 0; i < n; i++) { if (a[i] >= d) d++; } cout << d - 1; }
2
#include <bits/stdc++.h> using namespace std; long long bigmod(long long a, long long b) { if (b == 0) { return 1; } if (b % 2 == 0) { long long r = bigmod(a, b / 2); r = r * r; r = r % 1000000009; return r; } else { long long r = bigmod(a, b - 1); r = r * a; r = r % 1000000009; return r; } } int main() { long long n, m, k; scanf("%lld %lld %lld", &n, &m, &k); long long t = min(n - m, m / (k - 1)); long long r = m - t * (k - 1); long long p = r / k; long long ans = t * (k - 1); ans += ((bigmod(2, p + 1) + 1000000009 - 2) * k) % 1000000009; ans %= 1000000009; ans += r % k; ans %= 1000000009; printf("%lld\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; FILE *in, *out; int N, M, K; int ar[5100]; int arx[5100]; int p[5100]; int b[5100]; int q(int x, int y) { int i; cout << "? " << x << " " << y << "\n"; cout.flush(); cin >> i; return i; } int used1[5100]; int used2[5100]; int ANS[5100]; int check() { int i, j, k; for (i = 0; i < N; i++) { if (p[i] >= N || b[i] >= N) return 0; used1[i] = 0; used2[i] = 0; } for (i = 0; i < N; i++) { used1[p[i]]++; if (used1[p[i]] > 1) return 0; used2[b[i]]++; if (used2[b[i]] > 1) return 0; if (i != p[b[i]]) return 0; } for (i = 0; i < N; i++) { ANS[i] = p[i]; } return 1; } int DEBUG() { int i, j, k; for (i = 0; i < N; i++) { cout << p[i] << " "; } cout << "\n"; for (i = 0; i < N; i++) { cout << b[i] << " "; } cout << "\n"; return 0; } int read() { int i, j, k; int x, y; cin >> N; for (i = 0; i < N; i++) ar[i] = q(0, i); for (i = 0; i < N; i++) arx[i] = q(i, 0); int ans = 0; for (i = 0; i < N; i++) { p[0] = i; for (j = 0; j < N; j++) b[j] = (p[0] ^ ar[j]); for (j = 0; j < N; j++) p[j] = (b[0] ^ arx[j]); if (check()) ans++; } cout << "!\n"; cout << ans << "\n"; for (i = 0; i < N; i++) { cout << ANS[i] << " "; } cout << "\n"; cout.flush(); return 0; } int main() { read(); return 0; }
12
#include <bits/stdc++.h> using namespace std; int c[100006]; int main() { int m, n, i, j; while (~scanf("%d", &m)) { int b = 100000000; for (i = 0; i < m; i++) { int a; scanf("%d", &a); b = min(a, b); } scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &c[i]); sort(c, c + n); n--; int ans = 0; while (n >= 0) { i = b; while (i) { ans += c[n--]; i--; if (n == -1) break; } if (n >= 1) n -= 2; else if (n >= 0) n -= 1; } printf("%d\n", ans); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int ans, n; int main() { cin >> n; int t = 0; for (int i = 1; n > 0; i++) { t += i; n -= t; ans++; if (n < 0) { ans--; } } cout << ans; }
0
#include <bits/stdc++.h> using namespace std; map<int, int> h, g; map<int, int>::reverse_iterator rit, rit2; int main() { int n, m, k; cin >> n >> m >> k; for (int(i) = 0; (i) < (int)(n); ++(i)) { int x; cin >> x; h[x]++; } for (int(i) = 0; (i) < (int)(m); ++(i)) { int x; cin >> x; g[x]++; } long long s1 = 0, s2 = 0; rit2 = g.rbegin(); for (rit = h.rbegin(); rit != h.rend(); rit++) { s1 += rit->second; while (rit2 != g.rend() && rit2->first >= rit->first) { s2 += rit2->second; rit2++; } if (s1 > s2) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; char A[2020], B[2020]; int la, lb; int main() { while (scanf("%s%s", A, B) > 0) { la = strlen(A); lb = strlen(B); int m = lb; for (int b = -lb; b < la; ++b) { int c = 0; for (int i = 0, k = b; i < lb; ++i, ++k) c += (k < 0 || k >= la || A[k] != B[i]); if (c < m) m = c; } printf("%d\n", m); } return 0; }
9
#include <bits/stdc++.h> using namespace std; using namespace chrono; template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } long long gcd(long long a, long long b) { if (b > a) { return gcd(b, a); } if (b == 0) { return a; } return gcd(b, a % b); } long long expo(long long a, long long b, long long mod) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1; } return res; } void extendgcd(long long a, long long b, long long *v) { if (b == 0) { v[0] = 1; v[1] = 0; v[2] = a; return; } extendgcd(b, a % b, v); long long x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return; } long long mminv(long long a, long long b) { long long arr[3]; extendgcd(a, b, arr); return arr[0]; } long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); } bool revsort(long long a, long long b) { return a > b; } void swap(long long &x, long long &y) { long long temp = x; x = y; y = temp; } long long combination(long long n, long long r, long long m, long long *fact, long long *ifact) { long long val1 = fact[n]; long long val2 = ifact[n - r]; long long val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m; } void google(long long t) { cout << "Case #" << t << ": "; } vector<long long> sieve(long long n) { bool *arr = new bool[n + 1](); vector<long long> vect; for (long long i = 2; i <= n; i++) if (arr[i] == 0) { vect.push_back(i); for (long long j = 2 * i; j <= n; j += i) arr[j] = 1; } return vect; } long long mod_add(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } long long mod_mul(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } long long mod_sub(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } long long mod_div(long long a, long long b, long long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } long long phin(long long n) { long long number = n; if (n % 2 == 0) { number /= 2; while (n % 2 == 0) n /= 2; } for (long long i = 3; i <= sqrt(n); i += 2) { if (n % i == 0) { while (n % i == 0) n /= i; number = (number / i * (i - 1)); } } if (n > 1) number = (number / n * (n - 1)); return number; } map<long long, vector<long long> > h; bool check() { for (long long i = 0; i < 26; i++) { if (h[i].size() > 0) { return false; } } return true; } bool isCyclicUtil(long long v, bool visited[], bool *recStack) { if (visited[v] == false) { visited[v] = true; recStack[v] = true; vector<long long>::iterator i; for (i = h[v].begin(); i != h[v].end(); ++i) { if (!visited[*i] && h[*i].size() > 0 && isCyclicUtil(*i, visited, recStack)) return true; else if (recStack[*i]) return true; } } recStack[v] = false; return false; } bool isCyclic() { bool *visited = new bool[26]; bool *recStack = new bool[26]; for (long long i = 0; i < 26; i++) { visited[i] = false; recStack[i] = false; } for (long long i = 0; i < 26; i++) if (h[i].size() > 0 && isCyclicUtil(i, visited, recStack)) return true; return false; } vector<long long> ans; void dfs(long long v, vector<bool> &visited) { visited[v] = true; for (long long u : h[v]) { if (!visited[u] && h[u].size() > 0) dfs(u, visited); } ans.push_back(v); } void topological_sort() { vector<bool> visited; visited.assign(26, false); ans.clear(); for (long long i = 0; i < 26; ++i) { if (!visited[i] && h[i].size() > 0) { dfs(i, visited); } } reverse(ans.begin(), ans.end()); vector<long long> a(26, 0); for (auto s1 : ans) { a[s1] = 1; char c1 = 'a' + s1; cout << c1; } for (long long i = 0; i < 26; i++) { if (a[i] == 0) { cout << ((char)('a' + i)); } } cout << "\n"; return; } void solve() { long long t = 1; while (t--) { long long n; cin >> n; string s[n]; for (long long i = 0; i < n; i++) { cin >> s[i]; } for (long long i = 0; i < n - 1; i++) { for (long long j = 0; j < min(s[i].size(), s[i + 1].size()); j++) { if (s[i][j] != s[i + 1][j]) { h[s[i][j] - 'a'].push_back(s[i + 1][j] - 'a'); break; } } } if (isCyclic() || check()) { long long f = 0; for (long long i = 0; i < n - 1; i++) { if (s[i] > s[i + 1]) { f = 1; break; } } if (f) { cout << "Impossible" << "\n"; } else { for (long long i = 0; i < 26; i++) { char c = 'a' + i; cout << c; } cout << "\n"; } } else { topological_sort(); } } return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); auto start1 = high_resolution_clock::now(); solve(); auto stop1 = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop1 - start1); }
8
#include <bits/stdc++.h> using namespace std; const int MAX = 1200; long long row[MAX], col[MAX]; long long sol1[MAX][MAX]; long long soltt[MAX]; long long sol2[MAX]; long long n, m; char mm[MAX][MAX]; int main() { scanf("%lld %lld", &n, &m); long long cnt = 0; for (int i = 0; i < n; i++) scanf("%s", mm[i]); for (int j = 1; j <= m; j++) col[j] = -1; for (int i = 0; i < n; i++) { row[i + 1] = -1; for (int j = 0; j < m; j++) { if (mm[i][j] == 'X') row[i + 1] = j + 1, col[j + 1] = i + 1, cnt++; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { sol1[i][j] = (j * (j - 1)) / 2 + (m - j + 1) * (m - j) / 2; if (row[i] != -1) sol1[i][j] -= std::abs(j - row[i]); } } long long t1, t2, t3, t4, dir; for (int i = 1; i <= n; i++) { t1 = 0; for (int j = 1; j <= m; j++) { if (j != row[i]) t1 += sol1[i][j]; } t2 = 0; if (i != 1) { for (int j = 1; j <= m; j++) { if (j != row[i]) { t2 += soltt[j]; } } } t2 <<= 1; t4 = 0; for (int j = i - 1; j >= 1; j--) { if (row[j] == -1) t4 += (i - j) * m; else t4 += (i - j) * (m - 1); } if (row[i] == -1) t4 = t4 * m; else t4 = t4 * (m - 1); t4 <<= 1; if (i == 1) sol2[i] = t1 + t2 + t4; else sol2[i] = sol2[i - 1] + t1 + t2 + t4; if (i == 1) for (int j = 1; j <= m; j++) soltt[j] = sol1[1][j]; else for (int j = 1; j <= m; j++) soltt[j] += sol1[i][j]; } for (int i = 1; i <= n; i++) { t3 = 0; if (row[i] == -1) dir = 0; else if (i - 1 < 1 || row[i - 1] == -1) dir = 3; else if (row[i - 1] > row[i]) { dir = 2; long long r = i - 2; t3 = 2 * m - row[i] - row[i - 1]; while (r >= 1 && row[r] != -1 && row[r] > row[r + 1]) t3 += m - row[r], r--; } else if (row[i - 1] < row[i]) { dir = 1; long long r = i - 2; t3 = row[i] + row[i - 1] - 2; while (r >= 1 && row[r] != -1 && row[r] < row[r + 1]) t3 += row[r] - 1, r--; } if (dir == 3) t3 = (row[i] - 1) * (m - row[i]) * 4; if (dir == 2) t3 = t3 * (row[i] - 1) * 4; if (dir == 1) t3 = t3 * (m - row[i]) * 4; if (t3) { sol2[n] += t3; } } for (int i = 1; i <= m; i++) { t3 = 0; if (col[i] == -1) dir = 0; else if (i - 1 < 1 || col[i - 1] == -1) dir = 3; else if (col[i - 1] > col[i]) { dir = 2; long long r = i - 2; t3 = 2 * n - col[i] - col[i - 1]; while (r >= 1 && col[r] != -1 && col[r] > col[r + 1]) t3 += n - col[r], r--; } else if (col[i - 1] < col[i]) { dir = 1; long long r = i - 2; t3 = col[i] + col[i - 1] - 2; while (r >= 1 && col[r] != -1 && col[r] < col[r + 1]) t3 += col[r] - 1, r--; } if (dir == 3) t3 = (col[i] - 1) * (n - col[i]) * 4; if (dir == 2) t3 = t3 * (col[i] - 1) * 4; if (dir == 1) t3 = t3 * (n - col[i]) * 4; if (t3) { sol2[n] += t3; } } cnt = n * m - cnt; long long tempcnt = cnt; cnt = cnt * (cnt - 1); printf("%.12lf\n", (double)sol2[n] / (cnt + tempcnt)); return 0; }
17
#include <bits/stdc++.h> using namespace std; int main() { int n, pos, l, r; cin >> n >> pos >> l >> r; int res = 0; if (l == 1 and r == n) { return cout << res, 0; } if (l == 1) { if (pos <= r) { res += (r - pos + 1); } else { res += (pos - r + 1); } return cout << res, 0; } if (r == n) { if (pos >= l) { res += (pos - l + 1); } else { res += (l - pos + 1); } return cout << res, 0; } if (pos >= l and pos <= r) { int d = (l + r) / 2; if (pos > d) { res += (r - pos) + 1; res += (r - l) + 1; } else { res += (pos - l) + 1; res += (r - l) + 1; } } else if (pos < l) { res += (l - pos) + 1; res += (r - l) + 1; } else if (pos > r) { res += (pos - r + 1); res += (r - l) + 1; } cout << res; }
5
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7, INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n) { long long r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } long long inv(long long first) { return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; } inline int rd() { int first = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar(); return first; } const int N = 1e6 + 10; int n, a[N], k; int b[N]; int main() { cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n - 1; ++i) b[i] = a[i] - a[i + 1]; sort(b + 1, b + n); long long ans = 0; for (int i = 1; i <= k - 1; ++i) ans += b[i]; ans += a[n] - a[1]; cout << ans << '\n'; }
6
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k = 1; cin >> n; unordered_map<string, long long> map; map["polycarp"] = 0; vector<long long> v; v.push_back(1); for (long long i = 0; i < n; i++) { string name1, s, name2; cin >> name1 >> s >> name2; transform(name1.begin(), name1.end(), name1.begin(), ::tolower); transform(name2.begin(), name2.end(), name2.begin(), ::tolower); v.push_back(1 + v[map[name2]]); map[name1] = k++; } long long ans = 0; for (long long i = 0; i < v.size(); i++) ans = max(ans, v[i]); cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> int main() { int i, j, k, l, flag, count, flagp, i1, c; char s[10000]; while (scanf("%d", &k) == 1) { scanf("%s", s); flag = 0; int flagc[30] = {0}; l = strlen(s); if (l < k) { printf("IMPOSSIBLE\n"); continue; } for (i = 0, j = l - 1; i <= j; i++, j--) { if (s[i] != s[j] && s[i] != '?' && s[j] != '?') { flag = 1; break; } if (s[i] == '?' && s[j] != '?') { s[i] = s[j]; flagc[s[j] - 'a'] = 1; } else if (s[j] == '?' && s[i] != '?') { s[j] = s[i]; flagc[s[i] - 'a'] = 1; } else if (s[i] == s[j] && s[i] != '?') { flagc[s[i] - 'a'] = 1; } } if (l % 2 && l > 1) { j = l / 2; flagp = 0; if (s[j] == '?') { for (i = k - 1; i >= 0; i--) { if (flagc[i] == 0) { s[j] = i + 'a'; s[l - 1 - j] = i + 'a'; flagp = 1; flagc[i] = 1; break; } } if (flagp == 0) { s[j] = 'a'; flagc[0] = 1; } } flagc[s[j] - 'a'] = 1; } j = l / 2; for (i = j; i >= 0; i--) { if (s[i] == '?') { flagp = 0; for (i1 = k - 1; i1 >= 0; i1--) { if (flagc[i1] == 0) { s[i] = i1 + 'a'; s[l - 1 - i] = i1 + 'a'; flagp = 1; flagc[i1] = 1; break; } } if (flagp == 0) { s[i] = 'a'; s[l - i - 1] = 'a'; flagc[0] = 1; } } } for (i = 0; i < k; i++) { if (flagc[i] != 1) { flag = 1; } } for (i = k; i <= 25; i++) { if (flagc[i] == 1) { flag = 1; } } if (flag == 1) printf("IMPOSSIBLE\n"); else puts(s); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { if (i % 4 == 1) { cout << '.'; } else { cout << '#'; } for (int j = 1; j < m - 1; j++) { if (i % 2) { cout << '.'; } else { cout << '#'; } } if (i % 4 == 3) { cout << '.'; } else { cout << '#'; } cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 300005; int n, q, pa[N], F[N], mx[N], res[N]; vector<int> adj[N]; void dfs(int u) { int num = -1, tmp = -1; F[u] = 1; for (int i = 0; i < (int)adj[u].size(); ++i) { int v = adj[u][i]; dfs(v); F[u] += F[v]; if (F[v] > num) { num = F[v]; tmp = v; } } mx[u] = num; if (tmp == -1) { res[u] = u; return; } res[u] = res[tmp]; while (res[u] != u && mx[pa[res[u]]] <= F[u] / 2) res[u] = pa[res[u]]; } int main() { scanf("%d%d", &n, &q); for (int i = 2; i <= n; ++i) { int x; scanf("%d", &x); pa[i] = x; adj[x].push_back(i); } dfs(1); while (q--) { int x; scanf("%d", &x); printf("%d\n", res[x]); } return 0; }
11
#include <bits/stdc++.h> #pragma GCC optimize "03" using namespace std; const int N = 1e5 + 5; const int mod = 1e9 + 7; const int inf = 1e9 + 9; vector<pair<int, pair<int, int> > > ed; int par[N], we[N]; vector<pair<int, int> > g[N]; int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void merge(int x, int y) { x = root(x); y = root(y); par[x] = y; } void dfs(int u, int p) { for (auto i : g[u]) { if (i.first == p) continue; we[i.first] = i.second; par[i.first] = u; dfs(i.first, u); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) par[i] = i; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; ed.push_back({w, {u, v}}); } sort(ed.begin() + 1, ed.end()); for (int i = 1; i < m; i++) { int u = ed[i].second.first, v = ed[i].second.second; int w = ed[i].first; if (root(u) != root(v)) { merge(u, v); g[u].push_back({v, w}); g[v].push_back({u, w}); } } for (int i = 1; i <= n; i++) par[i] = i; dfs(ed[0].second.first, 0); int x = ed[0].second.second, ans = 0; while (x != ed[0].second.first) { ans = max(ans, we[x]); if (par[x] == x) return cout << (int)1e9, 0; x = par[x]; } cout << ans; return 0; }
11
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 1; int a[MAX_N]; int f[MAX_N]; int l[MAX_N]; int n, m; set<int> num; int main() { cin >> n >> m; for (int x = 1; x <= n; x++) cin >> a[x]; for (int x = 1; x <= m; x++) cin >> l[x]; f[n] = 1; num.insert(a[n]); for (int x = n - 1; x >= 1; x--) { f[x] = f[x + 1]; if (!num.count(a[x])) { num.insert(a[x]); f[x]++; } } for (int x = 1; x <= m; x++) cout << f[l[x]] << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<int> vc; int main() { int n; int num = 1; cin >> n; if (n == 1) { printf("1 0\n"); return 0; } for (int i = 2; i <= n; i++) { int cnt = 0; while (n % i == 0) { cnt++; if (cnt == 1) num *= i; n /= i; } if (cnt) vc.push_back(cnt); } if (n > 1) vc.push_back(1), num *= n; int mx = 0; for (int i = 0; i < vc.size(); i++) { mx = max(vc[i], mx); } sort(vc.begin(), vc.end()); int ans = 0, cnt = 1; while (cnt < mx) { cnt *= 2; ans++; } if ((vc[0] != vc[vc.size() - 1]) || (mx != cnt)) ans++; cout << num << " " << ans; }
7
#include <bits/stdc++.h> using namespace std; namespace Tzh { const long long maxn = 2000010, p = 1e9 + 7; long long inv[maxn], fac[maxn], n, sum, ans, cnt, f[maxn], g[maxn]; long long c(long long n, long long m) { return fac[n] * inv[m] % p * inv[n - m] % p; } long long qpow(long long a, long long b) { long long sum = 1; while (b) { if (b & 1) sum = sum * a % p; a = a * a % p; b >>= 1; } return sum; } void work() { scanf("%lld", &n); fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; inv[n] = qpow(fac[n], p - 2); for (int i = n; i; i--) inv[i - 1] = inv[i] * i % p; for (int i = 1; i <= n; i++) { long long x; scanf("%lld", &x); if (x != -1) g[x] = 1, f[i] = 1; } for (int i = 1; i <= n; i++) { if (!g[i] && !f[i]) cnt++; if (!f[i]) sum++; } for (int i = 0; i <= cnt; i++) { long long tag = i & 1 ? -1 : 1; ans = (ans + p + tag * c(cnt, i) * fac[sum - i] % p) % p; } printf("%lld", ans); return; } } // namespace Tzh int main() { Tzh::work(); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int iinf = 1 << 29; const long long inf = 1ll << 60; const long long mod = 1e9 + 7; void GG() { cout << "-1\n"; exit(0); } long long mpow(long long a, long long n, long long mo = mod) { long long re = 1; while (n > 0) { if (n & 1) re = re * a % mo; a = a * a % mo; n >>= 1; } return re; } long long inv(long long b) { return b == 1 ? 1 : (mod - mod / b) * inv(mod % b) % mod; } const int maxn = 1e5 + 5; bool on[maxn]; bool seen[maxn]; bool pos[maxn]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int non = 0; vector<pair<int, bool> > v; int n, q; cin >> n >> q; fill(pos, pos + maxn, 1); for (int i = (0); i < (q); ++i) { char x; int t; cin >> x >> t; v.push_back({t, x == '+'}); if (x == '-' && !seen[t]) on[t] = 1, non++; seen[t] = 1; }; for (int i = (0); i < (q); ++i) { non += v[i].second ? 1 : -1; if ((v[i].second == 0 && non > 0) || (v[i].second == 1 && non > 1)) { pos[v[i].first] = 0; } if (v[i].second && i && v[i - 1].first != v[i].first) pos[v[i].first] = 0; if (!v[i].second && i != q - 1 && v[i + 1].first != v[i].first) pos[v[i].first] = 0; } vector<int> re; for (int i = (1); i < (n + 1); ++i) { if (pos[i]) re.push_back(i); } cout << (int)re.size() << '\n'; for (int x : re) cout << x << ' '; }
10
#include <bits/stdc++.h> using namespace std; const int N = 1e2 + 10; int n, a[N]; inline void read() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; } inline void solve() { int ans = 0; int sum = 0; sort(a, a + n); for (int i = 0; i < n; ++i) sum += a[i]; if (sum >= 4.5 * n) cout << 0; else { int i = 0; while (sum < 4.5 * n) { sum += 5; sum -= a[i]; ans++; i++; } cout << ans << endl; } } int main() { read(); solve(); }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1000000000") using namespace std; const int maxn = (int)2e5 + 10; struct segm { long long l, r; int num; }; segm v[maxn]; bool operator<(segm a, segm b) { if (a.l != b.l) { return a.l < b.l; } return a.r < b.r; } int ans[maxn]; int main() { long long l, r; int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { long long l1, r1; scanf("%I64d %I64d", &l1, &r1); if (i != 0) { v[i - 1].l = l1 - r; v[i - 1].r = r1 - l; v[i - 1].num = i; } l = l1; r = r1; } n--; sort(v, v + n); vector<pair<long long, int> > a; for (int i = 0; i < m; i++) { long long x; scanf("%I64d", &x); a.push_back(make_pair(x, i + 1)); } sort(a.begin(), a.end()); int pos = 0; set<pair<long long, int> > s; for (int i = 0; i < m; i++) { while (pos < n && v[pos].l <= a[i].first) { s.insert(make_pair(v[pos].r, v[pos].num)); pos++; } while (!s.empty() && (*s.begin()).first < a[i].first) { printf("No"); return 0; } if (s.empty()) { continue; } ans[(*s.begin()).second] = a[i].second; s.erase(s.begin()); } if (pos != n || !s.empty()) { printf("No"); return 0; } printf("Yes\n"); for (int i = 1; i <= n; i++) { printf("%d ", ans[i]); } return 0; }
12
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int lcm(int a, int b) { return a / gcd(a, b) * b; } int main() { int a, b, c, d; cin >> a >> b >> c >> d; int l = lcm(a, c); int a1 = l, b1 = b * (a1 / a), c1 = l, d1 = d * (c1 / c); if (b1 > d1) { int p = b1 - d1, q = b1; int g = gcd(q, p); cout << (p / g) << "/" << (q / g) << endl; } else { int l = lcm(b, d); int b2 = l, d2 = l; int a2 = a * (b2 / b), c2 = c * (d2 / d); if (a2 > c2) { int p = a2 - c2, q = a2; int g = gcd(p, q); cout << (p / g) << "/" << (q / g) << endl; } else if (a2 == c2) { cout << "0/1" << endl; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define ub upper_bound #define lb lower_bound #define sz(s) (int)s.size() #define pii pair<int,int> #define piii pair< int,pair<int,int> > #define ff first #define ss second #define all(a) a.begin(),a.end() #define rall(v) v.rbegin(),v.rend() #define endl '\n' #define matrix vector<vector<int> > #define boost ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define ADITYA freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); #define N 4000001 #define mod 1000000007 #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) #define inf 1e17 bool prime[N+1]; int prime_vec[N+1]; double dp[100][100][100]; bool vis[100][100][100]; void sieve_prime() { memset(prime, true, sizeof(prime)); for (int p=2; p*p<=N; p++) { if (prime[p] == true) { for (int i=p*p; i<=N; i += p) { prime[i] = false; } } } int j=0; for (int p=2; p<=N; p++) { if (prime[p]) { prime_vec[j] = p; j++; } } } ll ncr(ll n, ll r) { if (r > n - r) r = n - r; ll C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (ll i = 1; i <= n; i++) { for (ll j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % mod; } return C[r]; } // ll gcd(ll a, ll b) // { // if (a == 0) // return b; // return gcd(b % a, a); // } // ll lcm(ll a, ll b) // { // return (a / gcd(a, b)) * b; // } int main() { fast_io; boost #ifndef ONLINE_JUDGE ADITYA #endif int t; cin>>t; // sieve_prime(); for(int q=0;q<t;q++) { int n; cin>>n; vector<ll>vec; ll sum = 0; for(int i=0;i<n;i++) { ll x; cin>>x; sum = sum + x; vec.pb(x); } vector<ll>ans(n,0); ans[0] = vec[0]; for(int i=1;i<n;i++) { ll a = vec[i]; ll b = ans[i-1]; if(a==b) { ans[i] = a; } else if(a>b) { ll k = a%b; ll j = a-(a%b); if(k>j) { a = a + j; } else { a = a - k; } ans[i] = a; } else { // ll k = b%a; // ll j = a-(b%a); // if(k>j) // { // a = a + j; // } // else // { // a = a - k; // } // ans[i] = a; // ll mn = INT_MAX; // int flag=1; // for(int j=2;j*j<=b;j++) // { // if(b%j==0) // { // ll r = abs(a-j); // ll rr = j; // ll ss = b/j; // ll s = abs(a-ss); // ll m = min(r,s); // if(mn>m) // { // if(r>s) // { // ans[i] = ss; // } // else // { // ans[i] = rr; // } // } // flag=0; // } // } // if(flag=1) // { // if(abs(a-1)>abs(a-b)) // { // ans[i] = b; // } // else // { // ans[i] = 1; // } // } ans[i] = 1; } } for(int i=0;i<n;i++) { cout<<ans[i]<<" "; } cout<<endl; } }
6
#include <bits/stdc++.h> using namespace std; long long dp[2005][2005]; long long cal(int n, int m) { if (n == 1) return m; if (n == 0) return 1; if (n == m) return 1; if (dp[n][m]) return dp[n][m]; dp[n][m] = (cal(n - 1, m - 1) + cal(n, m - 1)) % 998244353; return dp[n][m]; } long long fpow(long long a, long long p) { long long ret = 1; while (p) { if (p & 1) ret = (ret * a) % 998244353; a = (a * a) % 998244353; p >>= 1; } return ret; } int main() { ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; cout << (((cal(k, n - 1) * fpow(m - 1, k)) % 998244353) * m) % 998244353 << endl; }
7
#include <bits/stdc++.h> using namespace std; using ll = long long; const double pi = 3.1415926535897; void SuperFast() { ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL); } void INOUT() {} void AcCePtEd() { int b, g, n; cin >> b >> g >> n; cout << min(b, n) + min(g, n) - n + 1 << "\n"; } int main() { SuperFast(); INOUT(); int TeStCaSeS = 1; while (TeStCaSeS--) { AcCePtEd(); } return 0; }
3
#include <bits/stdc++.h> int main() { int n, m, c = 0, h = 0, i = 0; scanf(" %d %d", &n, &m); while (c == 0 && h == 0) { int sum = 220; if (i % 2 == 0) { while (sum >= 100 && n > 0) { sum -= 100; n--; } while (sum >= 10 && m > 0) { sum -= 10; m--; } if (sum > 0) { h = 1; } } else { if (m >= 22) { sum = 0; m -= 22; } else if (m >= 12 && n >= 1) { sum = 0; m -= 12; n--; } else if (m >= 2 && n >= 2) { sum = 0; m -= 2; n -= 2; } if (sum > 0) c = 1; } i++; } if (c == 1) printf("Ciel"); else printf("Hanako"); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, min1, min2, min3, max1, max2, max3; cin >> n >> min1 >> max1 >> min2 >> max2 >> min3 >> max3; int a, b, c; a = max(n - min2 - min3 < max1 ? n - min2 - min3 : max1, min1); b = max(n - min3 - a < max2 ? n - min3 - a : max2, min2); c = min(n - a - b, max3 < n - a - b ? n - a - b : max3); cout << a << ' ' << b << ' ' << c; }
3
#include <bits/stdc++.h> using namespace std; long long gcd(long long n1, long long n2) { if (n1 % n2 == 0) return n2; return gcd(n2, n1 % n2); } long long powmod(long long base, long long exponent) { long long ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % 1000000007; base = (base * base) % 1000000007; exponent /= 2; } return ans; } int main() { int n, m; int answer; cin >> n >> m; if (n == 1) answer = m; if (m == 1) answer = n; if (m == 2) { if (n % 4 == 1) answer = n + 1; else answer = 4 * ((n + 3) / 4); } if (n == 2) { if (m % 4 == 1) answer = m + 1; else answer = 4 * ((m + 3) / 4); } if (m > 2 && n > 2) answer = (m * n + 1) / 2; printf("%d", answer); return 0; }
10