func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b) { if (b == 0) return 1; if (b == 1) return a; long long int c = power(a, b / 2); if (b % 2 == 0) return ((c % 1000000007) * (c % 1000000007)) % 1000000007; return ((((c % 1000000007) * (c % 1000000007)) % 1000000007) * (a % 1000000007)) % 1000000007; } int main() { long long int n, i; cin >> n; long long int a[n]; long long int hash[100005] = {0}; long long int mini = 99999999999999; long long int t = 0; for (i = 0; i < n; i++) { cin >> a[i]; hash[a[i]]++; } for (i = 1; i <= 100; i++) { long long int sum = 0; for (long long int j = 1; j <= 100; j++) { if (j < i) sum += (hash[j] * (i - 1 - j)); else if (j > i) sum += (hash[j] * (j - 1 - i)); } if (sum < mini) { mini = sum; t = i; } } cout << t << << mini << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s[n + 1]; if (n % 2 == 0) cout << n * n / 2 << endl; else cout << ((n * n) + 1) / 2 << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i % 2 != 0 && j % 2 != 0) cout << C ; else if (i % 2 != 0 && j % 2 == 0) cout << . ; else if (i % 2 == 0 && j % 2 != 0) cout << . ; else cout << C ; } cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T, size_t N> long long SIZE(const T (&t)[N]) { return N; } template <typename T> long long SIZE(const T &t) { return t.size(); } string to_string(const string s, long long x1 = 0, long long x2 = 1e9) { return + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : ) + ; } string to_string(const char *s) { return to_string((string)s); } string to_string(const bool b) { return (b ? true : false ); } string to_string(const char c) { return string({c}); } template <size_t N> string to_string(const bitset<N> &b, long long x1 = 0, long long x2 = 1e9) { string t = ; for (long long __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + 0 ; } return + t + ; } template <typename A, typename... C> string to_string(const A(&v), long long x1 = 0, long long x2 = 1e9, C... coords); long long l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = ( + to_string(p.first) + , + to_string(p.second) + ) ; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A(&v), long long x1, long long x2, C... coords) { long long rnk = rank<A>::value; string tab(t_a_b_s, ); string res = ; bool first = true; if (l_v_l_v_l == 0) res += n ; res += tab + [ ; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += , ; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n ; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += ] ; if (l_v_l_v_l == 0) res += n ; return res; } void dbgm() { ; } template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << | ; dbgm(T...); } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n, ans = 0, sum = 0, mx1 = 0, mx2 = 0; cin >> n; vector<long long> a(n), od, ev; for (long long i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) ans += a[i]; } vector<long long> p, q; for (long long i = 0; i + 1 < n; i += 2) { p.push_back(a[i + 1] - a[i]); } for (long long i = 1; i + 1 < n; i += 2) { q.push_back(a[i] - a[i + 1]); } if (p.size() > 0) { mx1 = p[0]; sum = 0; for (long long i = 0; i < p.size(); i++) { sum += p[i]; mx1 = max(mx1, sum); sum = max(sum, 0ll); } } if (q.size() > 0) { mx2 = q[0]; sum = 0; for (long long i = 0; i < q.size(); i++) { sum += q[i]; mx2 = max(mx2, sum); sum = max(sum, 0ll); } } ans = max(ans, ans + max(mx1, mx2)); cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = numeric_limits<int>::max(); const long long LLINF = numeric_limits<long long>::max(); const unsigned long long ULLINF = numeric_limits<unsigned long long>::max(); vector<vector<int>> g; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<pair<int, int>> a(n); for (int i = 0; i < n; i++) { int d, b; cin >> d >> b; a[i] = {d, b}; } queue<int> q; for (int i = 0; i < n; i++) { if (a[i].first == 1) q.push(i); } vector<pair<int, int>> ans; while (!q.empty()) { int v = q.front(); q.pop(); if (a[v].first == 0) continue; ans.push_back({v, a[v].second}); a[a[v].second].first--; a[a[v].second].second ^= v; if (a[a[v].second].first == 1) q.push(a[v].second); } cout << ans.size() << n ; for (auto k : ans) cout << k.first << << k.second << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m, an; int f[(1 << 20)], h[(1 << 20)]; int main() { scanf( %d , &n); for (i = 1; i <= n; i++) scanf( %d , &k), f[k]++; m = (1 << 20) - 1; for (i = 0; i <= 19; i++) for (j = 0; j <= m; j++) if (!(j & (1 << i))) f[j] += f[j | (1 << i)]; h[0] = 1; for (i = 1; i <= (1 << 20) - 1; i++) h[i] = h[i - 1] * 2 % 1000000007; for (i = 0; i <= m; i++) { int s = 0; for (j = 0; j <= 19; j++) s += i >> j & 1; if (f[i]) an = ((s & 1 ? -1 : 1) * (h[f[i]] - 1) + an) % 1000000007; } if (an < 0) an += 1000000007; printf( %d n , an); return 0; } |
#include <bits/stdc++.h> using namespace std; int lg[1000005], jump[1000005][20], depth[1000005], head[1000005], cnt = 4; int q, tot, s, t, zj; struct edge { int to, next; } e[2000005]; void add(int x, int y) { e[++tot].to = y; e[tot].next = head[x]; head[x] = tot; } void dfs(int now, int fa) { for (int i = head[now]; i; i = e[i].next) { if (e[i].to != fa) { dfs(e[i].to, now); } } } int LCA(int x, int y) { if (depth[x] < depth[y]) { swap(x, y); } while (depth[x] > depth[y]) { x = jump[x][lg[depth[x] - depth[y]] - 1]; } if (x == y) { return x; } for (int i = lg[depth[x]] - 1; i >= 0; i--) { if (jump[x][i] != jump[y][i]) { x = jump[x][i]; y = jump[y][i]; } } return jump[x][0]; } int dis(int a, int b) { int lca = LCA(a, b); return depth[a] + depth[b] - 2 * depth[lca]; } int main() { ios::sync_with_stdio(false); cin >> q; for (int i = 1; i <= q * 2 + 4; i++) { lg[i] = lg[i - 1] + (1 << lg[i - 1] == i); } add(1, 2), add(1, 3), add(1, 4); jump[2][0] = jump[3][0] = jump[4][0] = 1; depth[1] = 1, depth[2] = depth[3] = depth[4] = 2; s = 2, t = 4, zj = 2; for (int i = 0; i < q; i++) { int v; cin >> v; cnt++; add(v, cnt); add(cnt, v); depth[cnt] = depth[v] + 1; jump[cnt][0] = v; for (int j = 1; j <= lg[depth[cnt]]; j++) { jump[cnt][j] = jump[jump[cnt][j - 1]][j - 1]; } if (dis(cnt, s) > dis(s, t)) { t = cnt; zj = dis(cnt, s); } if (dis(cnt, t) > dis(s, t)) { s = cnt; zj = dis(cnt, t); } cnt++; add(v, cnt); add(cnt, v); depth[cnt] = depth[v] + 1; jump[cnt][0] = v; for (int j = 1; j <= lg[depth[cnt]]; j++) { jump[cnt][j] = jump[jump[cnt][j - 1]][j - 1]; } if (dis(cnt, s) > dis(s, t)) { t = cnt; zj = dis(cnt, s); } if (dis(cnt, t) > dis(s, t)) { s = cnt; zj = dis(cnt, t); } cout << zj << endl; } } |
#include <bits/stdc++.h> using namespace std; long long int INF = 10000000000; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m; cin >> n >> m; long long int chestOdd = 0, chestEven = 0; long long int i; for (i = 0; i < n; i++) { long long int temp; cin >> temp; if (temp % 2 == 0) { chestEven++; } else { chestOdd++; } } long long int keyOdd = 0, keyEven = 0; for (i = 0; i < m; i++) { long long int temp; cin >> temp; if (temp % 2 == 0) { keyEven++; } else { keyOdd++; } } long long int ans = 0; ans = min(keyEven, chestOdd); ans += min(keyOdd, chestEven); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000010; const int mod = 998244353; int n; long long jc[N], inv[N], ans = 0, an = 0; long long poww(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) { ans = ans * x % mod; } x = x * x % mod; y >>= 1; } return ans; } long long C(long long n, long long m) { return jc[n] * inv[m] % mod * inv[n - m] % mod; } int main() { scanf( %d , &n); jc[0] = inv[0] = 1; for (int i = 1; i <= n; i++) { jc[i] = jc[i - 1] * i % mod; inv[i] = poww(jc[i], mod - 2); } for (int i = 1; i <= n; i++) { ans = (ans + poww(3, 1ll * n * (n - i) + i) * C(n, i) % mod * ((i & 1) ? 1 : -1) + mod) % mod; } ans = ans * 2 % mod; for (int i = 0; i < n; i++) { long long t = -poww(3, i); an = (an + C(n, i) * ((i & 1) ? 1 : -1) * (poww(t + 1, n) - poww(t, n) + mod) % mod + mod) % mod; } ans = (ans + 3 * an) % mod; cout << ans; } |
#include <bits/stdc++.h> using namespace std; int main() { int p, q; cin >> p >> q; string s; int **b = new int *[p]; for (long long int i = 0; i < p; i++) b[i] = new int[q]; for (long long int i = 0; i < p; i++) { cin >> s; for (long long int j = 0; j < q; j++) { b[i][j] = int(s[j]); } } for (long long int j = 0; j < q; j++) { int c = b[0][j]; for (long long int i = 0; i < p; i++) { if (b[i][j] > c) { c = b[i][j]; } } for (long long int i = 0; i < p; i++) { if (b[i][j] == c) { b[i][j] = 0; } } } int e = 0; for (long long int i = 0; i < p; i++) { for (long long int j = 0; j < q; j++) { if (b[i][j] == 0) { e += 1; break; } } } cout << e << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; struct pole { int a, b; long long odp; int nr; }; bool porownanie1(pole a, pole b) { if (a.b < b.b) return true; if (a.b > b.b) return false; if (a.nr < b.nr) return true; return false; } bool porownanie2(pole a, pole b) { if (a.nr < b.nr) return true; return false; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> w(n); for (int i = 0; i < n; ++i) cin >> w[i]; int p; cin >> p; vector<pole> a(p); for (int i = 0; i < p; ++i) { cin >> a[i].a >> a[i].b; --a[i].a; a[i].odp = 0; a[i].nr = i; } sort(a.begin(), a.end(), porownanie1); for (int le = 0; le < p;) { int pr = le; while (pr < p && a[pr].b == a[le].b) ++pr; --pr; if (a[le].b <= 540) { vector<long long> s(n, 0); for (int i = n - 1; i >= 0; --i) { s[i] = w[i]; if (i + a[le].b < n) s[i] += s[i + a[le].b]; } for (int i = le; i <= pr; ++i) a[i].odp = s[a[i].a]; } else { for (int i = le; i <= pr; ++i) for (int j = a[i].a; j < n; j += a[i].b) a[i].odp += w[j]; } le = pr + 1; } sort(a.begin(), a.end(), porownanie2); for (int i = 0; i < p; ++i) cout << a[i].odp << endl; return 0; } |
#include <bits/stdc++.h> namespace myland { using namespace std; namespace _abbr { const double EPS(1e-8); const double PI(acos(-1.0)); const int INF(0x3f3f3f3f); const long long INFL(0x3f3f3f3f3f3f3f3fll); const int MOD(1e9 + 7); } // namespace _abbr using namespace _abbr; namespace _solve {} using namespace _solve; namespace _calculate { bool odd(long long x) { return x & 1; } bool even(long long x) { return (x & 1) ^ 1; } bool posi(long long x) { return x > 0; } bool nega(long long x) { return x < 0; } bool zero(long long x) { return x == 0; } bool prime(long long x) { if (x < 2) return 0; for (int i = 2; i * i <= x; i++) if (x % i == 0) return 0; return 1; } long long droot(long long x) { return 1 + (x - 1) % 9; } long long upd(long long a, long long b) { return a % b ? a / b + 1 : a / b; }; long long random(long long a, long long b) { return a + rand() * rand() % (b - a + 1); }; long long bitn(long long x) { long long c = 0; while (x) c++, x >>= 1; return c; } template <class T> T sqr(T x) { return x * x; } long long qpow(long long a, long long n, long long mod = MOD) { long long res(1); while (n) { if (n & 1) (res *= a) %= mod; (a *= a) %= mod; n >>= 1; } return res % mod; } template <class T> void tomin(T& a, T b) { if (b < a) a = b; } template <class T> void tomax(T& a, T b) { if (b > a) a = b; } } // namespace _calculate using namespace _calculate; namespace _simple_algo { long long stol(const string& s) { long long x = 0; for (char c : s) x = x * 10 + c - 48; return x; } string ltos(long long x) { string s = ; if (x == 0) return 0 ; while (x) s = char(x % 10 + 48) + s, x /= 10; return s; } bool pal(const string& s) { int l = s.size(); for (int i = 0, j = l - 1; i < j; i++, j--) if (s[i] != s[j]) return 0; return 1; } } // namespace _simple_algo using namespace _simple_algo; namespace _io { template <class T> void rd(T& x) { cin >> x; } long long rd() { long long x = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } void rd(int& x) { x = rd(); } void rd(long long& x) { x = rd(); } template <class A, class B> void rd(A& a, B& b) { rd(a), rd(b); } template <class A, class B> void rd(pair<A, B>& p) { cin >> p.first >> p.second; } template <class T> void wt(const T& x) { cout << x << endl; } template <class T> void wt(const T& x, char c) { cout << x << c; } template <class T> void wt(const T& x, const string& s) { cout << x << s; } template <class T> void wt(const T& x, int rnd) { cout << fixed << setprecision(rnd) << x << endl; } template <class A, class B> void wt(const pair<A, B>& make_pair) { cout << make_pair.first << << make_pair.second << endl; } template <class T> void wt(const vector<T>& vec, int l, int r) { for (int i = l; i <= r; i++) cout << vec[i] << ; cout << endl; } } // namespace _io using namespace _io; } // namespace myland using namespace myland; const int N(2e5 + 5); long long h, n; long long d[N], s[N]; int main() { rd(h, n); for (int i = 1; i <= n; i++) rd(d[i]), s[i] = s[i - 1] + d[i]; long long t = h; for (int i = 1; i <= n; i++) { t += d[i]; if (t <= 0) wt(i), exit(0); } if (s[n] >= 0) wt(-1), exit(0); else { long long mn(*min_element((s + 1), (s + n + 1))); h += mn; long long l(h / (-s[n])); h -= mn; h += l * s[n]; long long ans(l * n); while (h > 0) { for (int i = 1; i <= n; i++) { h += d[i]; ans++; if (h <= 0) wt(ans), exit(0); } } } } |
#include <bits/stdc++.h> using namespace std; string s; bool check(long long l, long long r) { for (long long i = l; i < (r + l) / 2; i++) { if (s[i] != s[r - i - 1]) { return false; } } return true; } long long sulganlox(long long l, long long r) { if (r - l == 1) { if (s[l] == s[l + 1]) { cout << Impossible << endl; exit(0); } return 2; } if ((r - l) % 2 == 0) { if (check(l, (r + l) / 2)) { return sulganlox(l, (r + l) / 2); } else { return 1; } } else { if (check(l, (r + l) / 2)) { return sulganlox(l, (r + l) / 2); } else { return 2; } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; long long n = s.size(); if (n == 1) { cout << Impossible << endl; return 0; } cout << sulganlox(0, n) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 19391; long long a[maxn]; namespace FAST_IO { template <typename T> void read(T &a) { a = 0; long long f = 1; char c = getchar(); while (!isdigit(c)) { if (c == - ) { f = -1; } c = getchar(); } while (isdigit(c)) { a *= 10; a += c - 0 ; c = getchar(); } a = a * f; } template <typename T> void write(T a) { if (a < 0) { a = -a; putchar( - ); } if (a > 9) { write(a / 10); } putchar(a % 10 + 0 ); } template <typename T> void writeln(T) { write(a); puts( ); } } // namespace FAST_IO long long ans, ans1; priority_queue<long long> big; priority_queue<long long, vector<long long>, greater<long long> > small; signed main() { long long n; FAST_IO::read(n); for (long long i = 1; i <= n; i++) { FAST_IO::read(a[i]); a[i] -= i; } for (long long i = 1; i <= n; i++) { big.push(a[i]); if (a[i] < big.top()) { ans += big.top() - a[i]; big.pop(); big.push(a[i]); } } cout << ans; } |
#include <bits/stdc++.h> using namespace std; const int nmax = 1e5 + 42; int n; long long k; int arr[nmax]; void compress() { set<int> seen = {}; for (int i = 1; i <= n; i++) seen.insert(arr[i]); map<int, int> c = {}; int val = 0; for (auto k : seen) { val++; c[k] = val; } for (int i = 1; i <= n; i++) arr[i] = c[arr[i]]; } int fenwick[nmax][2]; int sum(int pos, int index) { int ret = 0; while (pos) { ret = ret + fenwick[pos][index]; pos = pos - (pos & (-pos)); } return ret; } int query(int l, int r, int index) { return sum(r, index) - sum(l - 1, index); } void update(int pos, int add, int index) { while (pos <= n) { fenwick[pos][index] += add; pos = pos + (pos & (-pos)); } } int main() { scanf( %i%lld , &n, &k); for (int i = 1; i <= n; i++) scanf( %i , &arr[i]); compress(); int r = n + 1; long long now = 0; while (r > 1 && now <= k) { r--; now = now + query(1, arr[r] - 1, 1); if (now > k) { now = now - query(1, arr[r] - 1, 1); r++; break; } update(arr[r], 1, 1); } long long ans = 0; for (int l = 1; l < n; l++) { now = now + query(arr[l] + 1, n, 0) + query(1, arr[l] - 1, 1); update(arr[l], 1, 0); while (l >= r || (now > k && r <= n)) { now = now - query(arr[r] + 1, n, 0) - query(1, arr[r] - 1, 1); update(arr[r], -1, 1); r++; } ans = ans + (n - r + 1); } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int n; std::vector<int> ch[1005]; bool dfs(const int &u) { if (!ch[u].size()) return true; int res = 0; if (ch[u].size()) { for (auto it = ch[u].begin(); it != ch[u].end(); it++) { if (!(ch[*it].size())) res++; } } if (res >= 3) return true; else return false; } int main() { cin >> n; for (int i = 2; i <= n; i++) { int u; cin >> u; ch[u].push_back(i); } for (int i = 1; i <= n; i++) { if (!dfs(i)) { cout << No << endl; return 0; } } cout << Yes << endl; return 0; } |
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; vector<ll> prime_factors(ll num) { vector<ll> ans; while (num % 2) { ans.push_back(2); num /= 2; } for (int i = 3; i * i <= num; i += 2) { while (num % i == 0) { ans.push_back(i); num /= i; } } if (num > 2) ans.push_back(num); return ans; } void tests() { int n; cin >> n; map<int, vector<int>> data; vector<pair<int, int>> ans; ans.reserve(n); for (int i = 1; i < n + 1; i++) { int x; cin >> x; data[x].push_back(i); } for (auto &i : data) { int len = i.second.size(); if (len == 1) ans.push_back(make_pair(i.first, 0)); else if (len == 2) ans.push_back(make_pair(i.first, i.second[1] - i.second[0])); else { int cd = i.second[1] - i.second[0]; for (int pos = 2; pos < len; ++pos) { if (i.second[pos] - i.second[pos - 1] != cd) goto end; } ans.push_back(make_pair(i.first, cd)); } end:; } cout << ans.size() << char(10); for (auto &i : ans) cout << i.first << char(32) << i.second << char(10); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { tests(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[100010]; int b[100010]; int cha[100010]; vector<int> lis; int inc[100010]; int decr[100010]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; cha[x]++; cha[y + 1]--; } for (int i = 1; i <= m; i++) { a[i] = a[i - 1] + cha[i]; } lis.push_back(-100); lis.push_back(a[1]); inc[1] = 1; for (int i = 2; i <= m; i++) { int temp = upper_bound(lis.begin(), lis.end(), a[i]) - lis.begin(); if (temp == lis.size()) lis.push_back(a[i]); inc[i] = temp; lis[temp] = a[i]; } for (int i = m; i >= 1; i--) b[m + 1 - i] = a[i]; for (int i = m; i >= 1; i--) a[i] = b[i]; lis.clear(); lis.push_back(-100); lis.push_back(a[1]); decr[1] = 1; for (int i = 2; i <= m; i++) { int temp = upper_bound(lis.begin(), lis.end(), a[i]) - lis.begin(); if (temp == lis.size()) lis.push_back(a[i]); decr[i] = temp; lis[temp] = a[i]; } for (int i = 1; i <= m; i++) inc[i] += decr[m + 1 - i]; cout << *max_element(inc + 1, inc + m + 1) - 1 << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; pair<char, bool> ar[100][100]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> ar[i][j].first; ar[i][j].second = false; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < m; k++) { if (k == j) continue; if (ar[i][j].first == ar[i][k].first) { ar[i][j].second = true; ar[i][k].second = true; } } for (int k = 0; k < n; k++) { if (k == i) continue; if (ar[i][j].first == ar[k][j].first) { ar[i][j].second = true; ar[k][j].second = true; } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (ar[i][j].second == false) cout << ar[i][j].first; } } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxN = 100007; const int maxM = 300007; const long long INF = (1LL << 60); const long long M = 1000000007; long long C[maxN]; vector<int> G[maxN]; vector<int> H[maxN]; int V[maxN]; stack<int> S; vector<int> SSS[maxN]; void DFSG(int x) { V[x] = 1; for (int g : G[x]) if (V[g] == 0) DFSG(g); S.push(x); } void DFSH(int x, int y) { V[x] = 2; SSS[y].push_back(x); for (int h : H[x]) if (V[h] == 1) DFSH(h, y); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, a, b, k; long long ans = 0, cmb = 1, tans, tcmb; cin >> n; for (int i = 1; i <= n; ++i) { cin >> C[i]; } cin >> m; for (int i = 1; i <= m; ++i) { cin >> a >> b; G[a].push_back(b); H[b].push_back(a); } for (int i = 1; i <= n; ++i) { if (V[i] == 0) DFSG(i); } k = 0; while (!S.empty()) { a = S.top(); S.pop(); if (V[a] == 1) { ++k; DFSH(a, k); } } for (int i = 1; i <= k; ++i) { tans = INF; tcmb = 1; for (int s : SSS[i]) { if (C[s] < tans) { tans = C[s]; tcmb = 1; } else if (C[s] == tans) { ++tcmb; } } ans += tans; cmb *= tcmb; cmb %= M; } cout << ans << << cmb << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int a[N], b[N]; int main() { int n, ans = 0, x; cin >> n >> x; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n); for (int i = 1, j = n; i <= n && j >= 1; i++) { if (a[i] + b[j] >= x) ans++, j--; } cout << 1 << ans; } |
#include <bits/stdc++.h> using namespace std; void check(vector<vector<long long> >& a, vector<vector<long long> >& b) { long long n = a.size(), m = a[0].size(); for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) if (a[i][j] > b[i][j]) swap(a[i][j], b[i][j]); } bool correct(vector<vector<long long> > a) { long long n = a.size(), m = a[0].size(); for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { if ((i + 1 < n && a[i][j] >= a[i + 1][j]) || (j + 1 < m && a[i][j] >= a[i][j + 1]) || (i - 1 >= 0 && a[i][j] <= a[i - 1][j]) || (j - 1 >= 0 && a[i][j] <= a[i][j - 1])) return false; } } return true; } int32_t main() { long long n, m; cin >> n >> m; vector<vector<long long> > a(n, vector<long long>(m, 0)), b(n, vector<long long>(m, 0)); for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) cin >> a[i][j]; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) cin >> b[i][j]; check(a, b); if (correct(a) && correct(b)) cout << Possible n ; else cout << Impossible n ; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) using namespace std; long long n, m, a, b, c, k, temp, x, y; const int MAXN = 100000 + 11; inline long long max(long long a, long long b) { return ((a > b) ? a : b); } inline long long min(long long a, long long b) { return ((a > b) ? b : a); } inline long long gcd(long long a, long long b) { if (b == 0) return a; a %= b; return gcd(b, a); } inline vector<long long> read(int n) { vector<long long> v(n); for (int i = 0; i < v.size(); i++) cin >> v[i]; return v; } vector<int> tree[200000]; void bfst() { for (int i = n - 1; i > 0; --i) merge(tree[i << 1].begin(), tree[i << 1].end(), tree[i << 1 | 1].begin(), tree[i << 1 | 1].end(), back_inserter(tree[i])); } long long qfstgreater(int l, int r, int val) { long long res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) res += tree[l].end() - upper_bound(tree[l].begin(), tree[l].end(), val), l++; if (r & 1) res += tree[r - 1].end() - upper_bound(tree[r - 1].begin(), tree[r - 1].end(), val), r--; } return res; } long long qfstlesser(int l, int r, int val) { long long res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) res += lower_bound(tree[l].begin(), tree[l].end(), val) - tree[l].begin(), l++; if (r & 1) res += lower_bound(tree[r - 1].begin(), tree[r - 1].end(), val) - tree[r - 1].begin(), r--; } return res; } void solveforthiscase(const int& test) { cin >> n >> k; vector<long long> v = read(n); for (int i = 0; i < n; i++) tree[i + n].push_back(v[i]); bfst(); long long inv = 0, ans = 0, r = n - 1; for (; r > 0; r--) { long long ti = 0; if (v[r] < v[0]) ti++; ti += qfstlesser(r + 1, n, v[r]); if (inv + ti > k) break; inv += ti; } r++; if (r == 1) { cout << (n * (n - 1)) / 2; return; } ans += (n - r); for (int l = 1; l < n; l++) { inv += qfstgreater(0, l, v[l]) + qfstlesser(r, n, v[l]); while (r < n && inv > k) { inv -= qfstlesser(r + 1, n, v[r]); inv -= qfstgreater(0, l + 1, v[r++]); } if (r == n) break; ans += n - r; } cout << ans; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); clock_t start, end; start = clock(); int test = 1; for (int i = 1; i <= test; i++) solveforthiscase(i); end = clock(); long double t_t = (long double)(end - start) / (long double)(CLOCKS_PER_SEC); if (false) cerr << (long double)t_t * 1000 << ms ; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int h, l, i; cin >> h >> l; long double x; x = (long double)(l * l - h * h) / (long double)(2 * h); std::cout << std::setprecision(9) << x << n ; } |
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == - ) f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 0 ; return x * f; } const int MAXN = 200010; inline void solve() { int x = read(); for (int i = 2; i <= 30; i++) if (x % ((1 << i) - 1) == 0) { printf( %d n , x / ((1 << i) - 1)); return; } } int main() { for (int Cas = read(); Cas; Cas--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n; vector<int> sz[1001]; vector<int> s[1001]; vector<int> l; bool v[1001]; int dist[1001][1001]; vector<pair<int, pair<int, int> > > t; void bfs(int a, int f) { for (int i = 0; i < sz[a].size(); i++) { int x = sz[a][i]; if (dist[x][f] == 0) { dist[x][f] = dist[a][f] + 1; bfs(x, f); } } } void solve(int a, int b, int y) { if (y == 0) { return; } if (sz[a].size() == 1) { swap(a, b); } if (sz[a].size() == 1) { t.push_back({a, {b, y}}); return; } int esz, msz, besz = 0, bmsz = 0; for (int i = 0; i < sz[a].size(); i++) { int x = sz[a][i]; if (x != b) { esz = x; swap(esz, msz); } } int c, d, e, f; if (sz[b].size() == 1) { e = b; f = b; } else { for (int i = 0; i < sz[b].size(); i++) { int x = sz[b][i]; if (x != a) { besz = x; swap(besz, bmsz); } } } for (int i = 0; i < l.size(); i++) { int x = l[i]; if (dist[a][x] < dist[b][x]) { if (dist[esz][x] < dist[msz][x]) { c = x; } else { d = x; } } else { if (besz != 0) { if (dist[besz][x] < dist[bmsz][x]) { e = x; } else { f = x; } } else { e = x; swap(e, f); } } } y /= 2; t.push_back({c, {e, y}}); t.push_back({d, {f, y}}); t.push_back({c, {d, -y}}); if (e != f) { t.push_back({e, {f, -y}}); } } int main() { cin >> n; for (int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; sz[a].push_back(b); sz[b].push_back(a); s[a].push_back(c); s[b].push_back(c); } for (int i = 1; i <= n; i++) { if (sz[i].size() == 2) { cout << NO ; return 0; } if (sz[i].size() == 1) { l.push_back(i); } } for (int i = 1; i <= n; i++) { dist[i][i] = 1; bfs(i, i); } cout << YES << n ; for (int i = 1; i <= n; i++) { for (int j = 0; j < sz[i].size(); j++) { int x = sz[i][j]; int y = s[i][j]; if (i < x) { solve(i, x, y); } } } cout << t.size() << n ; for (int i = 0; i < t.size(); i++) { cout << t[i].first << << t[i].second.first << << t[i].second.second << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, a[44], b[44], PP[44], PPP[44], PHI[44]; long long dp[1 << 22], DP[1 << 22]; pair<int, int> P[44]; void is_ok() { dp[0] = 0; DP[0] = 0; int st = 0; for (int i = 0; i < n; ++i) { int dr = st + (1 << i); while (st < dr) { ++st; dp[st] = dp[st - (1 << i)] + a[i + 1]; DP[st] = DP[st - (1 << i)] + b[i + 1]; if (dp[st] == DP[st] && st != (1 << n) - 1) return; } } for (int i = 1; i <= n; ++i) cout << b[i] << ; exit(0); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; P[i] = make_pair(a[i], i); } sort(P + 1, P + n + 1); P[n + 1] = P[1]; for (int i = 2; i <= n + 1; ++i) PHI[P[i].second] = PP[P[i].second] = i - 1; for (int t = 1; t <= n; ++t) { for (int i = 1; i <= n; ++i) b[i] = P[PP[i]].first; is_ok(); for (int i = 1; i <= n; ++i) PPP[i] = PP[PHI[i]]; for (int i = 1; i <= n; ++i) PP[i] = PPP[i]; } cout << -1; return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, q; string s; int dp[251][251][251]; int nxt[26][112345]; int len[3]; int chs[3][250]; template <class T, class U> inline void smax(T &a, U b) { a = a > b ? a : b; } template <class T, class U> inline void smin(T &a, U b) { a = a < b ? a : b; } int main() { std::ios::sync_with_stdio(false), std::cin.tie(0); cin >> n >> q; cin >> s; for (int i = 0; i < 26; i++) { nxt[i][n] = nxt[i][n + 1] = n; for (int j = n - 1; j >= 0; j--) if (s[j] == a + i) nxt[i][j] = j; else nxt[i][j] = nxt[i][j + 1]; } dp[0][0][0] = -1; for (int t = 0; t < q; t++) { char c; int group; cin >> c >> group; group -= 1; if (c == + ) { char ch; cin >> ch; chs[group][len[group]] = ch - a ; len[group]++; int i[3]; for (i[0] = group == 0 ? len[0] : 0; i[0] <= len[0]; i[0]++) for (i[1] = group == 1 ? len[1] : 0; i[1] <= len[1]; i[1]++) for (i[2] = group == 2 ? len[2] : 0; i[2] <= len[2]; i[2]++) { if (!i[0] && !i[1] && !i[2]) continue; dp[i[0]][i[1]][i[2]] = n; for (int k = 0; k < 3; k++) { if (i[k] > 0) { smin(dp[i[0]][i[1]][i[2]], nxt[chs[k][i[k] - 1]] [dp[i[0] - (0 == k)][i[1] - (1 == k)][i[2] - (2 == k)] + 1]); } } } } else { len[group]--; } cout << ((dp[len[0]][len[1]][len[2]] < n) ? Yes : No ) << n ; } cout << flush; return 0; } |
#include <bits/stdc++.h> using namespace std; const int mxn = 1e6 + 3; struct node { int v, i, j, id; } a[mxn]; int n, m, r[mxn], c[mxn], f[mxn], v[mxn], o[mxn]; int cmp(node a, node b) { return a.v < b.v; } int fffa(int x) { return f[x] == x ? x : f[x] = fffa(f[x]); } int main() { scanf( %d%d , &n, &m); for (int(i) = (int)(1); (i) <= (int)(n); ++(i)) for (int(j) = (int)(1); (j) <= (int)(m); ++(j)) { int id = (i - 1) * m + j; scanf( %d , &o[id]); a[id].v = o[id]; a[id].i = i; a[id].j = j; a[id].id = f[id] = id; } sort(a + 1, a + 1 + n * m, cmp); for (int(i) = (int)(1); (i) <= (int)(n * m); ++(i)) { int fi = fffa(r[a[i].i]); int fj = fffa(c[a[i].j]); v[a[i].id] = max(v[fi] + (a[i].v > o[fi]), v[fj] + (a[i].v > o[fj])); int p = fffa(a[i].id); if (a[i].v == o[fi]) f[fi] = p; if (a[i].v == o[fj]) f[fj] = p; r[a[i].i] = c[a[i].j] = p; } for (int(i) = (int)(1); (i) <= (int)(n); ++(i)) for (int(j) = (int)(1); (j) <= (int)(m); ++(j)) printf( %d%c , v[fffa((i - 1) * m + j)], n [j == m]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 2000000; const long long int MOD = 1000000009; const long long int P = 31; int N; long long int t[4 * MAXN] = {0}; long long int tp[4 * MAXN] = {0}; void make_push(int v, int l, int r, long long int tmp) { t[v] += (r - l + 1) * tmp; tp[v] += tmp; } void push(int v, int l, int r) { if (l != r) { make_push(2 * v + 1, l, (l + r) / 2, tp[v]); make_push(2 * v + 2, (l + r) / 2 + 1, r, tp[v]); } tp[v] = 0; } void update(int ql, int qr, long long int qv, int v = 0, int l = 0, int r = N - 1) { if (qr < l || r < ql) return; push(v, l, r); if (ql <= l && r <= qr) { make_push(v, l, r, qv); } else { update(ql, qr, qv, 2 * v + 1, l, (l + r) / 2); update(ql, qr, qv, 2 * v + 2, (l + r) / 2 + 1, r); t[v] = t[2 * v + 1] + t[2 * v + 2]; } } long long int get_ans(int qp, int v = 0, int l = 0, int r = N - 1) { if (qp < l || r < qp) return 0; push(v, l, r); if (qp <= l && r <= qp) return t[v]; else return get_ans(qp, 2 * v + 1, l, (l + r) / 2) + get_ans(qp, 2 * v + 2, (l + r) / 2 + 1, r); } int A[MAXN], T[MAXN], X[MAXN]; int id[MAXN]; long long int ans[MAXN]; vector<int> ts; bool cmp(int idl, int idr) { if (X[idl] != X[idr]) return X[idl] < X[idr]; return idl < idr; } int main() { scanf( %d , &N); for (int i = 0; i < N; i++) { scanf( %d %d %d , &A[i], &T[i], &X[i]); id[i] = i; } sort(id, id + N, cmp); ts.reserve(N); int lb, rb = 0; int offs = 0, sz; int tid; while (rb < N) { lb = rb; while (rb < N && X[id[rb]] == X[id[lb]]) rb++; sz = rb - lb; for (int i = lb; i < rb; i++) { ts.push_back(T[id[i]]); } sort(ts.begin(), ts.end()); for (int i = lb; i <= rb; i++) { tid = lower_bound(ts.begin(), ts.end(), T[id[i]]) - ts.begin(); if (A[id[i]] == 1) { update(offs + tid, offs + sz - 1, 1); } else if (A[id[i]] == 2) { update(offs + tid, offs + sz - 1, -1); } else { ans[id[i]] = get_ans(offs + tid); } } offs += sz; ts.clear(); } for (int i = 0; i < N; i++) { if (A[i] == 3) printf( %I64d n , ans[i]); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int h = 0, v = 0, size; string s; cin >> s; size = s.size(); for (int i = 0; i < size; i++) { if (s[i] == 0 ) { if (v == 0) { cout << 1 3 n ; v++; } else { cout << 3 3 n ; v--; } } else { if (h == 0) { cout << 3 1 n ; h++; } else { cout << 3 3 n ; h--; } } } } |
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1e9 + 7, N = 1e5 + 10; void test() { string s; cin >> s; vector<ll> cnt(26); for (auto c : s) { cnt[c - a ]++; } ll m; cin >> m; vector<ll> b(m); for (ll i = 0; i < m; i++) { cin >> b[i]; } ll n = s.length(); vector<ll> done(m); string res(m, . ); ll up = 25; for (ll i = 0; i < m; i++) { ll mx = 1e9; for (ll j = 0; j < m; j++) { if (done[j]) continue; mx = min(mx, b[j]); } if (mx == 1e9) break; vector<ll> pos; for (ll j = 0; j < m; j++) { if (done[j]) continue; if (mx == b[j]) { pos.push_back(j); } } ll v; for (ll j = up; j >= 0; --j) { if (cnt[j] >= pos.size()) { v = j; break; } } up = v - 1; for (auto &e : pos) { res[e] = char(v + a ); } for (auto &e : pos) { for (ll j = 0; j < m; j++) { if (!done[j]) { b[j] -= abs(j - e); } } } for (auto &e : pos) done[e] = 1; } cout << res << n ; return; } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); ll tt = 1; cin >> tt; for (ll i = 0; i < tt; i++) test(); return 0; } |
#include <iostream> #include <bits/stdc++.h> using namespace std; typedef long long int l; #define fastIO ios::sync_with_stdio(false), cin.tie(0) #define pf push_front #define pb push_back #define popb pop_back() #define popf pop_front() #define loop(i,n) for(int i=0;i<n;i++) #define loope(i,n) for(int i=0;i<=n;i++) #define loop1(i,n) for(int i=1;i<n;i++) #define loops(i,x,n) for(int i=x;i<n;i++) #define loopr(i,n) for(int i=n-1;i>=0;i--) #define loop1e(i,n) for(int i=1;i<=n;i++) #define F first #define S second #define LB lower_bound #define UB upper_bound #define vc vector #define vll vector<long long> #define pll pair<long long,long long> #define eb emplace_back #define pb push_back #define mp make_pair #define INT_BITS 32 #define st l t; cin>>t; #define sn l n; cin>>n; #define all(x) x.begin(),x.end() //#define unordered_map<l,l> u; #define MOD1 1000000007 int main() { st; while(t--) { l n; cin>>n; l a[n]; l k = n; for(int i=0;i<n;i++){ a[i]=k; k--; } if(n&1){ int p=n/2; swap(a[p],a[p+1]); } for(int i=0;i<n;i++){ cout<<a[i]<< ; } cout<<endl; // cout<<endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int ans[210000], k; vector<pair<int, int>> item, b; struct R { int l, r, cc; long long value; R(int L, int R, int CC = 0, long long VALUE = 0) { l = L, r = R, cc = CC, value = VALUE; } }; vector<R> range; void update() { long long value; int l, r, c; for (int i = 0; i < range.size(); i++) { l = range[i].l, r = range[i].r; c = range[i].cc, value = range[i].value; for (int j = l; j <= r; j++) b[j].first -= value, ans[b[j].second] += c; } sort(b.begin(), b.end()); range.clear(); range.push_back(R(0, k - 1)); } int main() { int n, c, q, l, r; scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d %d , &c, &q); item.push_back(make_pair(-q, c)); } sort(item.begin(), item.end()); scanf( %d , &k); for (int i = 0; i < k; i++) scanf( %d , &c), b.push_back(make_pair(c, i)); sort(b.begin(), b.end()); int value = 0; update(); for (int i = 0; i < n; i++) { c = item[i].second; q = range.size(); for (int j = 0; j < q; j++) { l = range[j].l, r = range[j].r; value = range[j].value; if (b[l].first >= value + c) range[j].value += c, ++range[j].cc; else if (b[r].first >= c + value) { int index = lower_bound(b.begin() + l, b.begin() + r + 1, make_pair(c + value, 0)) - b.begin(); range.push_back(R(index, r, range[j].cc + 1, value + c)); range[j].r = index - 1; } } if (range.size() > 500) update(); } update(); for (int i = 0; i < k; i++) printf( %d , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long t, i, j, k, l, m, n, p, q, r, s = 0, a, b, c, d, ans = 0, x, y; cin >> n >> k; string s1; cin >> s1; for (i = 0; i < n; i++) { if (s1[i] == G ) { p = i; } else if (s1[i] == T ) { q = i; } } if (abs(p - q) % k != 0) { cout << NO n ; return 0; } a = min(p, q); b = max(p, q); for (i = a; i <= b; i += k) { if (s1[i] == # ) { cout << NO n ; return 0; } } cout << YES n ; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int x, y, a, b; double dis = 0; cin >> x >> y; a = x; b = y; n--; while (n--) { cin >> x >> y; dis += (double)sqrt(pow(y - b, 2) + pow(x - a, 2)); a = x; b = y; } dis = (dis / 50) * k; printf( %.6f , dis); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, sum = 0; cin >> n >> m; sum = n + (n - 1) / (m - 1); cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 200010; const long long MOD = 998244353; int main() { int n; scanf( %d , &n); if (n == 1) printf( 1 2 n ); else printf( %d 2 n , (n - 1) * 2); printf( 1 2 n ); } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; const int N = 3e5 + 2; int v[N], n, q; void read() { cin >> n >> q; for (int i = 1; i <= n; i++) cin >> v[i]; v[0] = 0; v[n + 1] = 0; } int maxima_con[N], minima_con[N]; long long maxima_sum = 0, minima_sum = 0; bool is_maxima(int indx) { return v[indx] > max(v[indx - 1], v[indx + 1]); } bool is_minima(int indx) { return v[indx] < min(v[indx - 1], v[indx + 1]); } void see(int indx) { int &i = indx; maxima_sum -= maxima_con[i]; minima_sum -= minima_con[i]; maxima_con[i] = 0; minima_con[i] = 0; if (is_maxima(i)) { maxima_con[i] = v[i]; maxima_sum += maxima_con[i]; } if (is_minima(i)) { minima_con[i] = v[i]; minima_sum += minima_con[i]; } } void solve(int test_case) { read(); for (int i = 1; i <= n; i++) { see(i); } cout << maxima_sum - minima_sum; cout << n ; ; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; swap(v[l], v[r]); for (int del = -1; del <= 1; del++) { int indx = l + del; if (indx > 0 && indx <= n) see(indx); indx = r + del; if (indx > 0 && indx <= n) see(indx); } cout << maxima_sum - minima_sum; cout << n ; ; } } void clear_data() { for (int i = 1; i <= n; i++) maxima_con[i] = 0, minima_con[i] = 0; maxima_sum = 0; minima_sum = 0; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); int t = 1; cin >> t; for (int i = 1; i <= t; i++) { solve(i); clear_data(); } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 10; const long long mod = 1e9 + 7; long long read() { long long x = 0, f = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) f = -1; c = getchar(); } while (c >= 0 && c <= 9 ) x = (x << 1) + (x << 3) + c - 0 , c = getchar(); return x * f; } double ans; struct BIT { long long tr[N]; long long Bmax; long long lb(long long x) { return (-x) & x; } void init(long long n) { Bmax = n; for (long long i = 0; i <= n; i++) tr[i] = 0; } long long query(long long x) { if (x <= 0) return 0; long long res = 0; while (x) { res += tr[x]; x -= lb(x); } return res; } void update(long long x, long long w) { if (x <= 0) return; while (x <= Bmax) { tr[x] += w; x += lb(x); } } } t1, t2; int a[N]; int main() { long long n = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (long long i = 1; i <= n; i++) { ans += (double)1.00 * i * (i - 1) / 4.00 * (n - i + 1); } t1.init(n); t2.init(n); for (long long i = 1; i <= n; i++) { long long o1 = t1.query(n) - t1.query(a[i]); long long o2 = t2.query(n) - t2.query(a[i]); t1.update(a[i], 1); t2.update(a[i], i); ans += (double)(1.00 * n * (n + 1) * o1 * 0.5 - 1.00 * (n - i + 1) * o2); } printf( %.10lf , ans / (1.00 * n * (n + 1) / 2)); } |
#include <bits/stdc++.h> using namespace std; const int N = 1000005; const int inf = 1e9 + 7; long long fac[N], M, inv[N]; inline long long po(long long x, long long y, long long z) { long long res = 1; while (y) { if (y & 1) res = res * x % z; x = x * x % z; y >>= 1; } return res; } long long ans[N], M2; inline long long C(int x, int y) { return fac[x] * inv[y] % M2 * inv[x - y] % M2; } int main() { int n, m, a; scanf( %d%d%d%lld , &n, &m, &a, &M); long long res = 1; for (int i = (1); i <= (2000000); i++) { res = res * (long long)a % M; if (res == 1) { M2 = i; break; } } fac[0] = fac[1] = inv[0] = inv[1] = 1; for (int i = (2); i <= (M2); i++) { fac[i] = fac[i - 1] * (long long)i % M2; inv[i] = po(fac[i], M2 - 2, M2); } ans[0] = 1; for (int i = (1); i <= (N - 1); i++) { ans[i] = ans[i - 1]; ans[i] += C(m, i); } for (int i = (n - 1); i >= (0); i--) printf( %lld , po(a, ans[i], M)); puts( ); return 0; } |
#include <bits/stdc++.h> using namespace std; int II() { int n; scanf( %d , &n); return n; } int table[100][100]; char message[100][200]; string names[100]; void recprint(int m, int s) { if (m < 0) return; recprint(m - 1, table[m][s]); cout << names[s]; printf( :%s n , message[m]); } int main() { int T = II(); while (T--) { set<std::string> usernames; int N = II(); for (int x = 0; x < N; x++) { string s; cin >> s; usernames.insert(s); } for (auto it = usernames.begin(); it != usernames.end(); it++) names[distance(usernames.begin(), it)] = *it; int M = II(); int sa = N, sb = N; for (int m = 0; m < M; m++) { for (int x = 0; x < N; x++) table[m][x] = N; string user; user.clear(); char c = getchar(); while (c < 32) c = getchar(); while (c != : ) { user += c; c = getchar(); } int it = distance(usernames.begin(), usernames.find(user)); if (it < N) for (int x = 0; x < N; x++) if (x != it) table[m][x] = -1; user.clear(); char *msg = message[m]; c = getchar(); while (true) { bool ended = false; if (c == ! || c == ? || c == . || c == , || c == || c == 10) { int it = distance(usernames.begin(), usernames.find(user)); if (it < N) table[m][it] = -1; ended = true; } if (c == 10) break; *msg = c; msg++; user += c; if (ended) user.clear(); c = getchar(); } *msg = 0; for (int x = 0; x < N; x++) if (table[m][x] != -1) table[m][x] = (x == sa ? sb : sa); sa = sb = -1; for (int x = 0; x < N; x++) if (table[m][x] != -1) sa = x; for (int x = 0; x < N; x++) if (table[m][x] != -1) if (x != sa) sb = x; } if (sa == -1) printf( Impossible n ); else recprint(M - 1, sa); } } |
#include <bits/stdc++.h> using namespace std; vector<int> ans; struct item { int key, prior; item *left, *right; item() {} item(int key, int prior) : key(key), prior(prior), left(NULL), right(NULL) {} }; typedef item* pitem; void split(pitem temp, int key, pitem& left, pitem& right) { if (!temp) left = right = NULL; else if (key < temp->key) split(temp->left, key, left, temp->left), right = temp; else split(temp->right, key, temp->right, right), left = temp; } void insert(pitem& temp, pitem it) { if (!temp) temp = it; else if (it->prior > temp->prior) split(temp, it->key, it->left, it->right), temp = it; else insert(it->key < temp->key ? temp->left : temp->right, it); } void merge(pitem& temp, pitem left, pitem right) { if (!left || !right) temp = left ? left : right; else if (left->prior > right->prior) merge(left->right, left->right, right), temp = left; else merge(right->left, left, right->left), temp = right; } void erase(pitem& temp, int key) { if (temp->key == key) merge(temp, temp->left, temp->right); else erase(key < temp->key ? temp->left : temp->right, key); } pitem unite(pitem left, pitem right) { if (!left || !right) return left ? left : right; if (left->prior < right->prior) swap(left, right); pitem lt, rt; split(right, left->key, lt, rt); left->left = unite(left->left, lt); left->right = unite(left->right, rt); return left; } int getMin(pitem temp) { while (temp->left) temp = temp->left; return temp->key; } pitem eraseMin(pitem temp) { int minn = getMin(temp); erase(temp, minn); return temp; } void dfs(pitem temp) { ans.push_back(temp->key); if (temp->left) dfs(temp->left); if (temp->right) dfs(temp->right); } long long mass[200000]; vector<int> mass1[200000]; unordered_map<int, bool> prefs; int n; bool f(long long m, bool fin) { if (fin) { for (int i = 0; i < n; i++) { int now = 0; int last = -1; for (int u = mass1[i].size() - 1; u > -1; u--) { now *= 2; now += mass1[i][u]; if (prefs[now] == 0 && now <= m) last = now; } cout << last << ; prefs[last] = 1; } return 0; } else for (int i = 0; i < n; i++) { int now = 0; int last = -1; for (int u = mass1[i].size() - 1; u > -1; u--) { now *= 2; now += mass1[i][u]; if (now > m) break; if (prefs[now] == 0) last = now; } if (last == -1) return 0; else prefs[last] = 1; } return 1; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> mass[i]; sort(mass, mass + n); reverse(mass, mass + n); for (int i = 0; i < n; i++) { long long a = mass[i]; while (a) { mass1[i].push_back(a % 2); a /= 2; } } long long l = 1, r = 1e9; for (int b = 0; b < 30; b++) { prefs.clear(); long long mid = (l + r) / 2; if (f(mid, 0)) r = mid; else l = mid; } prefs.clear(); f(r, 1); return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( -O2 ) using namespace std; void err(istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << = << a << t ; err(++it, args...); } template <typename T1, typename T2> ostream& operator<<(ostream& c, pair<T1, T2>& v) { c << ( << v.first << , << v.second << ) ; return c; } template <template <class...> class TT, class... T> ostream& operator<<(ostream& out, TT<T...>& c) { out << { ; for (auto& x : c) out << x << ; out << } ; return out; } const int LIM = 1e5 + 5, MOD = 998244353; int t, n, m, k, x, y, w; inline long long powm(long long x, long long pw, long long Mod = MOD) { x %= Mod; long long res = 1; while (pw) { if (pw & 1LL) res = ((res * x)) % Mod; pw >>= 1; x = ((x * x)) % Mod; } return res; } inline long long inv(long long x, long long Mod = MOD) { return powm(x, Mod - 2, Mod); } template <int mod> struct FWHT { vector<int> fwht(vector<int> P, bool inverse, bool nomod = 0) { int sz = P.size(); for (int len = 1; 2 * len <= sz; len <<= 1) { for (int i = 0; i < sz; i += 2 * len) { for (int j = 0; j < len; ++j) { int u = P[i + j]; int v = P[i + len + j]; if (nomod) { P[i + j] = (u + v); P[i + len + j] = (u - v); } else { P[i + j] = (u + v) % mod; P[i + len + j] = (u - v + mod) % mod; } } } } if (inverse) { long long iv = inv(sz, mod); for (int i = 0; i < sz; ++i) P[i] = P[i] * iv % mod; } return P; } vector<int> mult(vector<int> A, vector<int> B) { A = fwht(A, 0); B = fwht(B, 0); for (int i = 0; i < A.size(); ++i) A[i] = A[i] * 1LL * B[i] % mod; return fwht(A, 1); } vector<int> pow(vector<int> A, long long k) { A = fwht(A, 0); for (int i = 0; i < A.size(); ++i) A[i] = powm(A[i], k); return fwht(A, 1); } }; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; int sz = (1 << k); vector<int> A(sz, 0), B(sz, 0), C(sz, 0), D(sz, 0); long long o1, o2, o3; cin >> o1 >> o2 >> o3; int totxor = 0; for (int i = 0; i < n; ++i) { cin >> x >> y >> w; totxor ^= x; y ^= x, w ^= x; A[y]++; B[w]++; C[y ^ w]++; } FWHT<MOD> f; A = f.fwht(A, 0, 1), B = f.fwht(B, 0, 1), C = f.fwht(C, 0, 1); for (int i = 0; i < sz; ++i) { int x1 = (n + A[i]) / 2, x2 = (n + B[i]) / 2, x3 = (n + C[i]) / 2; int a1 = (x1 + x2 + x3 - n) / 2, b1 = x2 - a1, c1 = x1 - a1, d1 = x3 - a1; D[i] = ((powm(o1 + o2 + o3, a1) * powm(o1 - o2 + o3 + MOD, b1) % MOD) * powm(o1 + o2 - o3 + MOD, c1) % MOD) * powm(o1 - o2 - o3 + MOD + MOD, d1) % MOD; } D = f.fwht(D, 1); for (int i = 0; i < sz; ++i) cout << D[i ^ totxor] << ; cout << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int *a = new int[m]; if (m % 2 == 0) { for (int i = 0; i < m; i++) { if (i % 2 == 0) a[i] = m / 2 - i / 2; if (i % 2 == 1) a[i] = m / 2 + 1 + (i - 1) / 2; } } else { for (int i = 0; i < m; i++) { if (i % 2 == 1) a[i] = (m - 1) / 2 - (i - 1) / 2; if (i % 2 == 0) a[i] = (m + 1) / 2 + i / 2; } } for (int i = 0; i < n; i++) cout << a[i % m] << endl; return 0; } |
#include bits/stdc++.h using namespace std; #define pb push_back #define ll long long #define mp make_pair #define pii pair<int,int> #define vi vector<int> #define vii vector<pii> #define mi map<int,int> #define mii map<pii,int> #define all(a) (a).begin(),(a).end() #define x first #define y second #define sz(x) (int)x.size() #define endl n #define MOD 1000000007 #define hell 1000000007 #define debug(x) cout << > << #x << : << x << endl; #define rep(i,a,b) for(int i=a;i<b;i++) #define rev(i,a,b) for(int i=a;i>=b;i--) #define ll long long #define bg(i) i.begin() #define fset(it,s) for(auto it = s.begin(); it != s.end(); it++) #define track(x) cerr<<#x<< : <<x<<endl #define track2(x, y) cerr<<#x<< : <<x<< | <<#y<< : <<y<<endl #define track3(x, y, z) cerr<<#x<< : <<x<< | <<#y<< : <<y<< | <<#z<< : <<z<<endl #define track4(a, b, c, d) cerr<<#a<< : <<a<< | <<#b<< : <<b<< | <<#c<< : <<c<< | <<#d<< : <<d<<endl #define ed(i) i.end() #define mem(a) memset(a, 0, sizeof(a)) ll mult(ll a,ll b, ll p=MOD){return ((a%p)*(b%p))%p;} ll add(ll a, ll b, ll p=MOD){return (a%p + b%p)%p;} ll fpow(ll n, ll k, ll p = MOD) {ll r = 1; for (; k; k >>= 1LL) {if (k & 1LL) r = mult(r,n,p); n = mult(n,n,p);} return r;} ll inv(ll a, ll p = MOD) {return fpow(a, p - 2, p);} void self_max(int &a, int &b) {a = max(a, b); } void self_min(int &a, int &b) {a = min(a, b); } const ll sizes = 1e7+1; const ll mod = 1000000007; ll n, m , k , o , p; // ll a[sizes]; void solve(){ cin >> n >> m >> k; ll sum1 = 0 , sum2 = 0 , sum3 = 0 , sum = 0; vector<ll>a , b , c, d; for(int i = 0 ; i < n ; i++){ cin >> o; a.pb(o); sum += o; sum1+=o; } for(int i = 0 ; i < m ; i++){ cin >> o; b.pb(o); sum2 += o; sum += o; } for(int i = 0 ; i < k ; i++){ cin >> o; c.pb(o); sum3 += o; sum += o; } sort(all(a));sort(all(b));sort(all(c)); d.push_back(a[0]); d.pb(b[0]); d.pb(c[0]); sort(all(d)); ll mn = min(min(sum1 , sum2) , sum3); mn = min(mn , d[0] + d[1]); cout << sum - (2 * mn) << endl;; } int main() { ios::sync_with_stdio(false); cout<<fixed<<setprecision(2); cin.tie(0); cout.tie(0); // fun(); int t = 1; // cin >> t; int p = t; while(t--){ solve(); } } |
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 115 + 5; char s[maxn][maxn]; int main() { int n, m; while (~scanf( %d%d , &n, &m)) { int mini = inf, maxi = 0, minj = inf, maxj = 0; getchar(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf( %c , &s[i][j]); if (s[i][j] == B ) { mini = min(mini, i); minj = min(minj, j); maxi = max(maxi, i); maxj = max(maxj, j); } } getchar(); } printf( %d %d n , (mini + maxi) / 2, (minj + maxj) / 2); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, two, one; cin >> n >> m; if (m < n - 1 || m > 2 * n + 2) cout << -1 ; else { if (m < n) { cout << 0 ; n--; } while (m > 0 || n > 0) { two = max(m - n - 1, 0); one = min(2 * n + 2, m); if (two) { cout << 11 ; m -= 2; two--; } else if (one) { cout << 1 ; m--; one--; } if (n) cout << 0 ; n--; } } return 0; } |
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int maxn = 100000 + 1; LL h[maxn], p[maxn], up[maxn], sz[maxn]; int par[maxn]; vector<int> G[maxn], order; void DFS(int u) { sz[u] = p[u]; for (int v : G[u]) if (v != par[u]) { par[v] = u; DFS(v); sz[u] += sz[v]; } h[u] = sz[u] - h[u]; order.push_back(u); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; for (cin >> t; t; t -= 1) { int n, m; cin >> n >> m; for (int i = 1; i <= n; i += 1) cin >> p[i]; for (int i = 1; i <= n; i += 1) cin >> h[i]; for (int i = 1; i <= n; i += 1) G[i].clear(); for (int i = 1; i <= n; i += 1) up[i] = 0; for (int i = 1, x, y; i < n; i += 1) { cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } order.clear(); DFS(1); int ans = 1; for (int u : order) if (h[u] < 0 or (h[u] & 1)) ans = 0; else if (h[u] / 2 > up[u] + p[u]) ans = 0; else up[par[u]] += h[u] / 2; cout << (ans ? YES n : NO n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long n; bool check(int x, long long y) { if (n * 2 / x < x + y) return true; if (n * 2 / x > x + y) return false; return n * 2 <= x * (x + y); } long long calc(long long y) { int l = 1, r = 2e9, mid; while (l < r) { mid = (l >> 1) + (r >> 1) + (l & r & 1); if (check(mid, y - 3)) r = mid; else l = mid + 1; } if ((r + y - 3) * r != 2 * n || 0 == (r & 1)) return -1; return r * y / 2; } int main() { bool ok = false; cin >> n; for (int i = 1; i <= 62; ++i) { long long res = calc(1LL << i); if (~res) { ok = true; printf( %I64d n , res); } } if (!ok) puts( -1 ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; pair<int, int> p[maxn], q[maxn]; int t[maxn]; int tot; int n, k; int dp[maxn][2]; void solve() { memset(dp, 0x3f3f3f3f, sizeof(dp)); int e = 0; for (int i = 1; i <= tot; i++) { dp[i][e] = min(dp[i - 1][e], q[i].second - q[i].first); } e ^= 1; for (int ki = 2; ki <= k; ki++, e ^= 1) { dp[1][e] = 0x3f3f3f3f; for (int i = 2; i <= tot; i++) { bool last = q[i - 1].second == q[i].first; dp[i][e] = min(dp[i - 1 - last][e ^ 1] + q[i].second - q[i].first, dp[i - 1][e]); } } cout << dp[tot][e ^ 1] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> k >> n; for (int i = 0; i < n; i++) cin >> t[i]; sort(t, t + n); for (int i = 1; i < n; i++) { p[i] = make_pair(t[i - 1], t[i]); } sort(p + 1, p + n, [](const pair<int, int> &x, const pair<int, int> &y) { return (x.second - x.first) < (y.second - y.first); }); tot = min(3 * k, n - 1); for (int i = 1; i <= tot; i++) { q[i] = p[i]; } sort(q + 1, q + tot + 1, [](const pair<int, int> &x, const pair<int, int> &y) { return x.first < y.first; }); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100; const int V = 100 * 50; bool edge[V][V], nf[V]; int ans, cnt_v, n; int from[V], start_v[N], value[V]; void ri() { int a, i, j, m, x1, x2; memset(from, 0xff, sizeof(from)); memset(edge, false, sizeof(edge)); cin >> n >> m; for (i = 0; i < n; i++) { start_v[i] = cnt_v; cin >> a; j = 2; while (a > 1) { for (; j * j <= a && a % j != 0; j++) ; if (a % j != 0) j = a; value[cnt_v++] = j; a /= j; } } start_v[n] = cnt_v; while (m--) { cin >> x1 >> x2; x1--; x2--; for (i = start_v[x1]; i < start_v[x1 + 1]; i++) for (j = start_v[x2]; j < start_v[x2 + 1]; j++) { edge[i][j] = value[i] == value[j]; edge[j][i] = value[i] == value[j]; } } } bool ok(int v1) { int temp, v2; nf[v1] = false; for (v2 = 0; v2 < cnt_v; v2++) if (edge[v1][v2] && nf[v2]) { nf[v2] = false; temp = from[v2]; from[v2] = v1; if (temp == -1 || ok(temp)) return true; from[v2] = temp; } return false; } void solve() { int i, j; for (i = 0; i < n; i++) { if (i & 1) continue; for (j = start_v[i]; j < start_v[i + 1]; j++) { memset(nf, true, sizeof(nf)); if (ok(j)) ans++; } } } void print() { cout << ans << endl; } int main() { ri(); solve(); print(); return 0; } |
#include <bits/stdc++.h> const int MAXN = 4e5 + 10; const double eps = 1e-8; const long long inf = 1e18 + 7; using namespace std; struct edge { int t, v; edge *next; } e[MAXN << 1], *h[MAXN], *o = e; void add(int x, int y, int w) { o->t = y; o->v = w; o->next = h[x]; h[x] = o++; } long long read() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == - ) f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - 0 , ch = getchar(); return x * f; } int n, a[MAXN]; long long dp[MAXN]; int main() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(); if (n == 1) return 0 * printf( %d n , a[1]); else if (n == 3) { int minn = 1e9 + 7; long long sum = 0; for (int i = 1; i <= n; i++) minn = min(minn, a[i]), sum += a[i]; return 0 * printf( %lld n , sum - minn); } for (int i = n + 1; i <= 2 * n; i++) a[i] = a[i - n]; for (int i = 2 * n; i >= 1; i--) dp[i] = a[i] + dp[i + 2]; long long maxx = 0; for (int i = 1; i <= n; i++) { maxx = max(maxx, a[i] + a[i + 1] + dp[i + 3] - dp[i + n]); } printf( %lld n , maxx); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, k = 0, dem = 0, s; char c[1000001]; int e[100001]; deque<int> pq; int main() { cin >> n; ; for (int i = 1; i <= n; ++i) { cin >> c[i]; if (c[i] == 8 ) { pq.push_back(i); dem++; } } s = n - 11; s /= 2; for (int i = 1; i <= n; i++) { if (s == 0 || dem == 0) break; if (c[i] == 8 ) { pq.pop_front(); dem--; s--; } } if (dem == 0 || pq.front() > n - 10) cout << NO ; else cout << YES ; } |
#include <bits/stdc++.h> using namespace std; vector<int> p, rk; int getp(int v) { if (v == p[v]) return v; return p[v] = getp(p[v]); } long long res; long long get(int cnt) { return cnt * 1ll * (cnt - 1) / 2; } void merge(int u, int v) { u = getp(u); v = getp(v); if (rk[u] < rk[v]) swap(u, v); res -= get(rk[u]); res -= get(rk[v]); rk[u] += rk[v]; res += get(rk[u]); p[v] = u; } int main() { int n, m; cin >> n >> m; res = 0; p = rk = vector<int>(n, 1); iota(p.begin(), p.end(), 0); vector<pair<int, pair<int, int>>> e(n - 1); for (int i = 0; i < n - 1; ++i) { cin >> e[i].second.first >> e[i].second.second >> e[i].first; --e[i].second.first; --e[i].second.second; } vector<pair<int, int>> q(m); vector<long long> ans(m); for (int i = 0; i < m; ++i) { cin >> q[i].first; q[i].second = i; } sort(e.begin(), e.end()); sort(q.begin(), q.end()); int pos = 0; for (int i = 0; i < m; ++i) { while (pos < n - 1 && e[pos].first <= q[i].first) { int u = e[pos].second.first; int v = e[pos].second.second; merge(u, v); ++pos; } ans[q[i].second] = res; } for (int i = 0; i < m; ++i) { cout << ans[i] << ; } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; s[0] = toupper(s[0]); cout << s; } |
#include <bits/stdc++.h> using namespace std; long long i, a[200005], n, x = 0; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; a[i] += x; x = max(x, a[i]); } for (i = 0; i < n; i++) cout << a[i] << ; cout << endl; } |
#include bits/stdc++.h #define FI first.first #define SE first.second #define TH second #define fi first #define se second #define th second using namespace std; string to_string(const string& str) { return str; } template<typename T> string to_string(const set<T>& mys) { if(mys.empty()) return {} ; string ret = { ; for(auto el: mys) ret += to_string(el) + , ; ret.pop_back(), ret.pop_back(); // Remove last space and comma ret += } ; return ret; } template<typename T> string to_string(const pair<T, T>& pr) { return ( + to_string(pr.fi) + , + to_string(pr.se) + ) ; } template<typename T> string to_string(const vector<T>& vc, int w) { if(vc.empty()) return ; if(w + 1 == vc.size()) return to_string(vc[w]); return to_string(vc[w]) + , + to_string(vc, w + 1); } template<typename T> string to_string(const vector<T>& vc) { return { + to_string(vc, 0) + } ; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << << to_string(H); debug_out(T...); } #ifdef DEBUG #define debug(...) cerr << [ << #__VA_ARGS__ << ]: , debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif class DebugStream {}LOG; template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; } #ifdef DEBUG #define LOG clog #endif typedef long long ll; typedef pair<ll, ll> ii; typedef long double ld; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 43; int n, k, dp[maxN][bs], tmp[bs]; vector<int> adj[maxN]; void apply(int* toad, int* used) { for(int i = 0; i < k; i++) toad[(i + 1) % k] ^= used[i]; } void botUp(int nd, int an) { for(auto sn: adj[nd]) { if(sn == an) continue; botUp(sn, nd); apply(dp[nd], dp[sn]); } } void topDown(int nd, int an) { for(auto sn: adj[nd]) { if(sn == an) continue; memcpy(tmp, dp[sn], sizeof tmp); apply(dp[nd], tmp); apply(dp[sn], dp[nd]); apply(dp[nd], tmp); topDown(sn, nd); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef DEBUG freopen( input.txt , r , stdin); #endif cin >> n >> k; k *= 2; for(int i = 1, u, v; i < n; i++) { cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } for(int i = 0; i < n; i++) cin >> dp[i][0]; botUp(0, -1); topDown(0, -1); for(int i = 0; i < n; i++) { int rsp = 0; for(int j = k / 2; j < k; j++) rsp ^= dp[i][j]; cout << (rsp? 1: 0) << ; } cout << n ; } |
#include <bits/stdc++.h> using namespace std; int luck_number[105], tot; int dp[10005][7]; bool is_luck(int x) { while (x) { int y = x % 10; x = x / 10; if (y > 0 && y < 4) return false; if (y > 4 && y < 7) return false; if (y > 7) return false; } return true; } void init() { tot = 0; for (int i = 0; i <= 10000; i++) { if (is_luck(i)) luck_number[tot++] = i; } } void pre10000() { for (int i = 0; i <= 10000; i++) for (int j = 0; j <= 6; j++) dp[i][j] = -1; for (int i = 0; i <= 6; i++) { dp[0][i] = 0; } for (int i = 1; i <= 6; i++) { for (int j = 0; j < tot; j++) { int x = luck_number[j]; for (int k = x; k <= 10000; k++) { if (dp[k - x][i - 1] != -1) { dp[k][i] = x; } } } } } int main() { init(); pre10000(); int cas; scanf( %d , &cas); while (cas--) { long long N; cin >> N; if (N < 10000LL) { int n = (int)(N); if (dp[n][6] != -1) { for (int i = 6; i > 0; i--) { printf( %d , dp[n][i]); n -= dp[n][i]; } puts( ); } else printf( -1 n ); } else { int n1 = (int)(N % 4000LL); if (dp[n1][6] == -1) n1 += 4000, N -= 4000; long long res[7]; for (int i = 6; i > 0; i--) { res[i] = (long long)(dp[n1][i]); n1 -= dp[n1][i]; } long long n2 = N - n1; long long p = n2 / 4000LL, base = 1000LL; while (p) { int d = (int)(p % 10LL); if (d <= 6) { for (int i = 1; i <= d; i++) { res[i] += base * 4; } } else { for (int i = 1; i <= d - 7; i++) { res[i] += base * 4; } for (int i = 6; i >= 3; i--) { res[i] += base * 7; } } base *= 10LL; p /= 10LL; } for (int i = 1; i <= 6; i++) cout << res[i] << ; puts( ); } } return 0; } |
#include <bits/stdc++.h> const long long md = 998244353; const int Inf = 1e9; const long long Inf64 = 1e18; const long long MaxN = 2e5 + 10; const long long MaxM = 11; const long double eps = 1e-15; const long long dx[4] = {0, 1, 0, -1}; const long long dy[4] = {1, 0, -1, 0}; const long long ddx[4] = {1, 1, -1, -1}; const long long ddy[4] = {1, -1, 1, -1}; const long double Pi = acos(-1); using namespace std; long long gcd(long long a, long long b) { while (a) { b %= a; swap(a, b); } return b; } long long gcdex(long long a, long long mod = md) { long long g = mod, r = a, x = 0, y = 1; while (r != 0) { long long q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + mod : x; } long long binpow(long long a, long long n) { long long res = 1; while (n) { if (n & 1) { res *= a; res %= md; } a *= a; a %= md; n >>= 1; } return res % md; } bitset<MaxN> A; bitset<MaxN> B; long long dst(pair<long long, long long> a, pair<long long, long long> b) { return abs(a.first - b.first) + abs(a.second - b.second); } long long Mt[MaxN]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0); cout << fixed, cout.precision(10); long long N; cin >> N; map<long long, vector<pair<long long, long long> > > Mp; for (int i = 0; i < N; i++) { long long a, b; cin >> a >> b; Mp[max(a, b)].push_back({a, b}); } Mp[0].push_back({0, -0}); for (auto& a : Mp) { sort(a.second.begin(), a.second.end(), [=](pair<long long, long long> a, pair<long long, long long> b) { return a.first < b.first || (a.first == b.first && a.second > b.second); }); } vector<vector<long long> > dp(Mp.size() + 1, vector<long long>(2, Inf64)); dp[0][1] = dp[0][0] = 0; long long lvl = 0; long long v = 0; for (auto& it : Mp) { lvl++; pair<long long, long long> l = it.second[0]; pair<long long, long long> r = it.second.back(); pair<long long, long long> lstl = Mp[v][0]; pair<long long, long long> lstr = Mp[v].back(); dp[lvl][0] = min(dp[lvl][0], dp[lvl - 1][0] + dst(lstl, r) + dst(l, r)); dp[lvl][0] = min(dp[lvl][0], dp[lvl - 1][1] + dst(lstr, r) + dst(l, r)); dp[lvl][1] = min(dp[lvl][1], dp[lvl - 1][0] + dst(lstl, l) + dst(l, r)); dp[lvl][1] = min(dp[lvl][1], dp[lvl - 1][1] + dst(lstr, l) + dst(l, r)); v = it.first; } cout << min(dp[lvl][0], dp[lvl][1]) << n ; cerr << n << Time execute: << clock() / (double)CLOCKS_PER_SEC << sec << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(2001); int match[2001 + 1], dis[2001 + 1]; bool visited[2001 + 1], minimumVertexCover[2001], maximumIndependentSet[2001]; const int inf = 1E9; void ResetVisited() { for (int i = 0; i <= 2001; ++i) { visited[i] = 0; } } bool BFS(vector<int> &U) { queue<int> Q; for (auto u : U) { if (match[u] == 2001) { dis[u] = 0; Q.push(u); } else { dis[u] = inf; } } dis[2001] = inf; while (!Q.empty()) { int u = Q.front(); Q.pop(); visited[u] = 1; if (dis[u] < dis[2001]) { for (auto v : adj[u]) { if (dis[match[v]] == inf) { visited[v] = 1; dis[match[v]] = 1 + dis[u]; Q.push(match[v]); } } } } return (dis[2001] != inf); } bool DFS(int u) { if (u == 2001) { return true; } for (auto v : adj[u]) { if (dis[match[v]] == 1 + dis[u]) { if (DFS(match[v])) { match[v] = u; match[u] = v; return true; } } } dis[u] = inf; return false; } int HopcroftKarpMaximumMatching(vector<int> &U, vector<int> &V) { for (auto u : U) { match[u] = 2001; } for (auto v : V) { match[v] = 2001; } int matching = 0; for (ResetVisited(); BFS(U); ResetVisited()) { for (auto u : U) { if (match[u] == 2001) { matching += DFS(u); } } } return matching; } void FillMinimumVertexCover(vector<int> &U, vector<int> &V) { for (auto u : U) { minimumVertexCover[u] = (match[u] != 2001 && visited[u] == 0); } for (auto v : V) { minimumVertexCover[v] = (match[v] != 2001 && visited[v] == 1); } } void FillMaximumIndependentSet(vector<int> &U, vector<int> &V) { for (auto u : U) { maximumIndependentSet[u] = (match[u] == 2001 || visited[u] == 1); } for (auto v : V) { maximumIndependentSet[v] = (match[v] == 2001 || visited[v] == 0); } } vector<tuple<int, int, int> > segments; vector<int> AddSegments(vector<pair<int, int> > v) { vector<int> V; sort(v.begin(), v.end()); for (int i = 0; i < v.size() - 1; ++i) { if (v[i + 1].first == v[i].first) { V.push_back(segments.size()); segments.push_back(make_tuple(v[i].first, v[i].second, v[i + 1].second)); } } return V; } bool DoSegmentsIntersect(int i, int j) { int x, y1, y2, y, x1, x2; tie(x, y1, y2) = segments[i]; tie(y, x1, x2) = segments[j]; return (x1 < x && x < x2 && y1 < y && y < y2); } vector<tuple<int, int, int, int> > MakeAnswerSegments( vector<int> &V, vector<pair<int, int> > &P, bool isHorizontal) { vector<tuple<int, int, int> > v; for (auto i : V) { if (maximumIndependentSet[i]) { v.push_back(segments[i]); } } for (auto p : P) { v.push_back(make_tuple(p.first, p.second, p.second)); } sort(v.begin(), v.end()); vector<tuple<int, int, int, int> > ans; for (int i = 0, j; i < v.size(); i = j) { int x, y1, y2; tie(x, y1, y2) = v[i]; for (j = i + 1; j < v.size(); ++j) { int nx, ny1, ny2; tie(nx, ny1, ny2) = v[j]; if (nx == x && ny1 == y2) { y2 = ny2; } else { break; } } if (isHorizontal) { ans.push_back(make_tuple(y1, x, y2, x)); } else { ans.push_back(make_tuple(x, y1, x, y2)); } } return ans; } void PrintAnswerSegments(vector<tuple<int, int, int, int> > &ans) { printf( %lu n , ans.size()); for (auto t : ans) { int x1, y1, x2, y2; tie(x1, y1, x2, y2) = t; printf( %d %d %d %d n , x1, y1, x2, y2); } } int main() { int n; vector<pair<int, int> > xyPoints, yxPoints; cin >> n; while (n--) { int x, y; cin >> x >> y; xyPoints.push_back(make_pair(x, y)); yxPoints.push_back(make_pair(y, x)); } vector<int> U = AddSegments(xyPoints); vector<int> V = AddSegments(yxPoints); for (auto u : U) { for (auto v : V) { if (DoSegmentsIntersect(u, v)) { adj[u].push_back(v); } } } HopcroftKarpMaximumMatching(U, V); FillMaximumIndependentSet(U, V); vector<tuple<int, int, int, int> > horizontalSegments = MakeAnswerSegments(V, yxPoints, 1); vector<tuple<int, int, int, int> > verticalSegments = MakeAnswerSegments(U, xyPoints, 0); PrintAnswerSegments(horizontalSegments); PrintAnswerSegments(verticalSegments); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long linf = 1e18 + 9; const int inf = 1e9 + 9; const int N = 2e5 + 9; int n, q, l[N], r[N], a[N], wh[N]; long long bit[N], ans[N]; vector<int> h[N]; vector<pair<int, int> > Q[N]; void update(int t) { t++; while (t <= n + 1) { bit[t]++; t += (t & -t); } } long long query(int t) { long long sum = 0; t++; while (t > 0) { sum += bit[t]; t -= (t & -t); } return sum; } int main() { scanf( %d %d , &n, &q); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); wh[a[i]] = i; } for (int i = 1; i <= q; i++) { scanf( %d %d , &l[i], &r[i]); Q[r[i]].push_back(make_pair(l[i], i)); } for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += i) { h[j].push_back(i); } } for (int i = 1; i <= n; i++) { for (int j = 0; j < h[a[i]].size(); j++) { if (wh[h[a[i]][j]] <= i) { update(wh[h[a[i]][j]]); } } for (int j = a[i] * 2; j <= n; j += a[i]) { if (wh[j] < i) { update(wh[j]); } } for (int j = 0; j < Q[i].size(); j++) { ans[Q[i][j].second] += query(i) - query(Q[i][j].first - 1); } } for (int i = 1; i <= q; i++) { printf( %lld n , ans[i]); } return 0; } |
#include <bits/stdc++.h> using namespace std; signed main() { cin.tie(nullptr)->sync_with_stdio(false); long long t; cin >> t; while (t--) { long long n, a, b, da, db; cin >> n >> a >> b >> da >> db; vector<vector<long long>> adj(n + 1); for (long long i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } if (true) { vector<bool> vis(n + 1); vector<long long> dis(n + 1); queue<long long> q; q.push(a); vis[a] = true; while (!q.empty()) { long long cur = q.front(); q.pop(); for (long long nxt : adj[cur]) { if (!vis[nxt]) { vis[nxt] = true; dis[nxt] = dis[cur] + 1; q.push(nxt); } } } if (dis[b] <= da) { cout << Alice n ; continue; } } if (true) { long long start = 0; if (true) { vector<bool> vis(n + 1); vector<long long> dis(n + 1); queue<long long> q; q.push(a); vis[a] = true; while (!q.empty()) { long long cur = q.front(); q.pop(); for (long long nxt : adj[cur]) { if (!vis[nxt]) { vis[nxt] = true; dis[nxt] = dis[cur] + 1; q.push(nxt); } } } long long mx = 0; for (long long i = 1; i <= n; i++) { if (dis[i] > mx) { mx = dis[i]; start = i; } } } if (true) { vector<bool> vis(n + 1); vector<long long> dis(n + 1); queue<long long> q; q.push(start); vis[start] = true; while (!q.empty()) { long long cur = q.front(); q.pop(); for (long long nxt : adj[cur]) { if (!vis[nxt]) { vis[nxt] = true; dis[nxt] = dis[cur] + 1; q.push(nxt); } } } long long dia = 0; for (long long i = 1; i <= n; i++) { dia = max(dia, dis[i]); } if (da * 2 >= dia) { cout << Alice n ; continue; } } } if (da * 2 < db) { cout << Bob n ; } else { cout << Alice n ; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long p, d; while (scanf( %I64d%I64d , &p, &d) != EOF) { long long ans = ++p; for (long long i = 10;; i *= 10) { if (p % i > d) break; ans = p - p % i; } printf( %I64d n , ans - 1); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; if (s.find( 0 ) != string::npos) { s.erase(s.find( 0 ), 1); } else { s.erase(s.size() - 1, 1); } cout << s; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int d = 0, tot = 0, c; bool flag = false; for (int i = 0; i < n; i++) { cin >> c; tot = tot + c; if (tot >= 8 && flag == false) { k = k - 8; tot = tot - 8; d++; if (k <= 0) flag = true; } else if (tot < 8 && flag == false) { k = k - tot; tot = 0; d++; if (k <= 0) flag = true; } } if (flag == true) { cout << d << endl; } else { cout << -1 << endl; } return 0; } |
#include <bits/stdc++.h> int main() { int n, sum = 0, minodd = 101, i; scanf( %d , &n); for (; n > 0; --n) { scanf( %d , &i); if (minodd > i && i % 2) { minodd = i; } sum += i; } if (minodd == 101) printf( 0 ); else if (sum % 2) printf( %d , sum); else printf( %d , sum - minodd); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, t, m; cin >> t; while (t--) { cin >> n; int i; int a[n], b[n]; for (i = 0; i < n; i++) { cin >> a[i]; } int f = 0; for (i = 0; i < n; i++) { cin >> b[i]; } int p = 0, v = 0; for (i = 0; i < n; i++) { if (a[i] < b[i]) { p = i; v = b[i] - a[i]; break; } if (a[i] > b[i]) { f = 1; cout << NO << endl; break; } } if (f == 1) { continue; } for (i = p; i < n; i++) { if (a[i] < b[i]) { if ((b[i] - a[i]) != v) { f = 1; cout << NO << endl; break; } } else if (a[i] > b[i]) { f = 1; cout << NO << endl; break; } else { p = i; break; } } if (f == 1) continue; if (f == 0 && i == n) { cout << YES << endl; continue; } for (i = p; i < n; i++) { if (a[i] != b[i]) { f = 1; cout << NO << endl; break; } } if (f == 0) cout << YES << endl; } } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 2000; int cubeta[MAXN + 2]; int color; int arr[MAXN + 2]; void mapea(int n) { set<int> nums; for (int i = 1; i <= n; i++) nums.insert(arr[i]); map<int, int> val; int cnt = 1; set<int>::iterator it = nums.begin(); while (it != nums.end()) { val[*it] = cnt; cnt++; it++; } for (int i = 1; i <= n; i++) arr[i] = val[arr[i]]; } int busca(int l, int n) { int r = n; while (r > l && cubeta[arr[r]] != color) { cubeta[arr[r]] = color; r--; } return r + 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; mapea(n); int resp = n; for (int l = 0; l <= n; l++) { color++; bool continuar = true; for (int i = 1; i <= l; i++) { if (cubeta[arr[i]] == color) continuar = false; cubeta[arr[i]] = color; } if (!continuar) break; int r = busca(l, n); resp = min(resp, r - l - 1); } cout << resp << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int res = -1; for (int i = 1; i < s.size(); i++) { for (int j = 1; i + j < s.size(); j++) { int p1; string P1 = s.substr(0, i); if (P1.size() > 1 && P1[0] == 0 ) p1 = -1; else if (P1.size() >= 7 && P1 != 1000000 ) p1 = -1; else istringstream(P1) >> p1; int p2; string P2 = s.substr(i, j); if (P2.size() > 1 && P2[0] == 0 ) p2 = -1; else if (P2.size() >= 7 && P2 != 1000000 ) p2 = -1; else istringstream(P2) >> p2; int p3; string P3 = s.substr(i + j, s.size()); if (P3.size() > 1 && P3[0] == 0 ) p3 = -1; else if (P3.size() >= 7 && P3 != 1000000 ) p3 = -1; else istringstream(P3) >> p3; if (p1 == -1 || p2 == -1 || p3 == -1) continue; res = max(res, p1 + p2 + p3); } } cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; using LL = long long; using PII = pair<int, int>; int main() { auto get_factors = [](int n) { vector<int> ret; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); return ret; }; vector<vector<int>> factors(1e5 + 1); for (int i = (1); i <= (1e5); ++i) factors[i] = get_factors(i); ios::sync_with_stdio(false); int tc; cin >> tc; while (tc--) { int x[3], cnt[8] = {0}; cin >> x[0] >> x[1] >> x[2]; vector<int> v; for (int i = 0; i < 3; i++) { for (auto val : factors[x[i]]) v.push_back(val); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (auto val : v) { int mask = 0; for (int i = 0; i < 3; i++) { if (x[i] % val == 0) mask |= 1 << i; } cnt[mask]++; } int ans = 0; for (int a = 1; a < 8; a++) { for (int b = a; b < 8; b++) { int t1 = a | b; if (t1 == (t1 & -t1)) continue; for (int c = b; c < 8; c++) { int t2 = a | c, t3 = b | c; if (t2 == (t2 & -t2)) continue; if (t3 == (t3 & -t3)) continue; if ((a | b | c) != 7) continue; if (a != b && a != c && b != c) { ans += cnt[a] * cnt[b] * cnt[c]; } else if (a == b && b == c) { int n = cnt[a] + 2; ans += n * (n - 1) * (n - 2) / 6; } else if (a == b) { ans += cnt[a] * (cnt[a] + 1) / 2 * cnt[c]; } else if (a == c) { ans += cnt[a] * (cnt[a] + 1) / 2 * cnt[b]; } else if (b == c) { ans += cnt[b] * (cnt[b] + 1) / 2 * cnt[a]; } } } } cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i, name[100], cnt; string str; char ch; cin >> n >> k; cnt = 0; for (i = 1; i <= k - 1; i++) { name[i] = ++cnt; } for (i = k; i <= n; i++) { cin >> str; if (str[0] == N ) { name[i] = name[i - k + 1]; } else { name[i] = ++cnt; } } for (i = 1; i <= n; i++) { if (name[i] <= 26) { ch = ( A - 1) + name[i]; str = ch; } else { ch = ( a ) + name[i] - 26; str = A ; str = str + ch; } cout << str << ; } } |
#include <bits/stdc++.h> using namespace std; char c[100]; int main() { int n; cin >> n; int i; cin.getline(c, 100); int open = 0; string tt = ; for (i = 0; i < n; i++) { cin.getline(c, 100); string s = ; int k = strlen(c); int j = 0, z; while (j < k && (c[j] == || c[j] == t )) j++; z = k - 1; while (z >= j && (c[z] == || c[z] == t )) z--; for (; j <= z; j++) { s += c[j]; } if (s == try ) { open++; } if (s.substr(0, 5) == throw ) { open = 0; k = s.length(); j = 5; while (j < k && (s[j] == ( || s[j] == || s[j] == t )) j++; z = k - 1; while (z >= j && (s[z] == ) || s[z] == || s[z] == t )) z--; for (; j <= z; j++) { tt += s[j]; } } if (s.substr(0, 5) == catch ) { if (open == 0 && tt != ) { j = 5; k = s.length(); while (j < k && (s[j] == ( || s[j] == )) j++; string ss = ; string out = ; for (;; j++) { if (s[j] == || s[j] == , ) break; ss += s[j]; } while (j < k && s[j] != ) j++; for (j++; s[j] != ; j++) { out += s[j]; } if (ss == tt) { printf( %s n , out.c_str()); tt = ; } } open--; if (open < 0) open = 0; } } if (tt != ) { printf( Unhandled Exception n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; void debugv(std::vector<long long> v); void printv(std::vector<long long> v); void inputv(std::vector<long long> &v); void solve() { long long n; cin >> n; string s; cin >> s; unordered_map<string, long long> mp; for (long long i = 0; i < n - 1; i++) { string k = s.substr(i, 2); mp[k]++; } long long maxi = INT_MIN; string ans = ; for (auto i : mp) { if (i.second > maxi) { ans = i.first; maxi = i.second; } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; t = 1; while (t--) { solve(); cout << n ; } return 0; } void debugv(std::vector<long long> v) { long long size = v.size(); for (long long i = 0; i < size; i++) { cerr << v[i] << ; } cout << n ; } void printv(std::vector<long long> v) { long long size = v.size(); for (long long i = 0; i < size; i++) { cout << v[i] << ; } cout << n ; } void inputv(std::vector<long long> &v) { for (auto &i : v) { cin >> i; } } |
#include <bits/stdc++.h> using namespace std; long long n, k, s[30][105], cnt[105]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; string t; cin >> t; s[t[0] - a ][1] = 1; for (int i = 1; i < t.size(); i++) { int u = t[i] - a ; for (int j = i + 1; j >= 2; j--) { s[u][j] = 0; for (int k = 0; k < 26; k++) { s[u][j] += s[k][j - 1]; } } s[u][1] = 1; } for (int i = 0; i < 26; i++) { for (int j = 1; j <= t.size(); j++) { cnt[j] += s[i][j]; } } cnt[0] = 1; long long res = 0, id = t.size(); while (k > 0) { long long u = min(k, cnt[id]); k -= u; res += (n - id) * u; id--; if (id < 0) break; } if (k > 0) return cout << -1, 0; cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; int power(int p, int k) { int a = 1; while (k) { if (k & 1) a = (a * p) % mod; p = (p * p) % mod; k >>= 1; } return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tc = 1; int i, j; cin >> tc; while (tc--) { int n; cin >> n; if (n == 1 || n == 2) cout << 1; else if (n == 3) cout << 2; else if (n % 2 == 0) cout << n / 2; else if (n % 2 == 1) cout << n / 2 + 1; cout << n ; } } |
#include <bits/stdc++.h> using namespace std; struct time { int s; int e; }; int main() { int n; cin >> n; int s, e; int minc = INT_MAX, maxc = -1; for (int i = 0; i < n; i++) { cin >> s >> e; if (minc > e) { minc = e; } if (maxc < s) { maxc = s; } } int m; cin >> m; int maxp = -1, minp = INT_MAX; for (int i = 0; i < m; i++) { cin >> s >> e; if (minp > e) { minp = e; } if (maxp < s) { maxp = s; } } int p = maxp - minc; int q = maxc - minp; if (p < 0 && q < 0) { cout << 0; } else { cout << max(p, q); } } |
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long double PI = 3.141592653589793; const long long int INF = 9223372036854775807ll; void solve() { string p, h; cin >> p >> h; if (h.size() < p.size()) { cout << NO n ; return; } vector<int> x(26, 0); for (int i = 0; i < p.size(); i++) x[p[i] - a ]++; for (int i = 0; i < h.size() - p.size() + 1; i++) { vector<int> v(26, 0); for (int j = i; j < i + p.size(); j++) v[h[j] - a ]++; int cnt = 0; for (int i = 0; i < 26; i++) if (x[i] == v[i]) cnt++; if (cnt == 26) { cout << YES n ; return; } } cout << NO n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; cin >> t; while (t--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; namespace IO { const int maxn(1 << 21 | 1); char *iS, *iT, ibuf[maxn], obuf[maxn], *oS = obuf, *oT = obuf + maxn - 1, c, st[20]; int f, tp, len; inline char getc() { return iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), iS == iT ? EOF : *iS++) : *iS++; } template <class T> inline void read(T &x) { for (c = getc(), f = 1; c < 0 || c > 9 ; c = getc()) f = c == - ? -1 : 1; for (x = 0; c >= 0 && c <= 9 ; c = getc()) x = x * 10 + (c ^ 48); x *= f; } template <class T, class... Other> inline void read(T &x, Other &...y) { read(x), read(y...); } inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; } inline void putc(char ch) { *oS++ = ch; if (oS == oT) flush(); } template <class T> inline void print(T x, char ch = n ) { if (!x) putc( 0 ); if (x < 0) putc( - ), x = -x; while (x) st[++tp] = x % 10 + 0 , x /= 10; while (tp) putc(st[tp--]); putc(ch); } template <class T, class... Other> inline void print(T x, Other... y) { print(x), print(y...); } inline void print_str(char *s) { for (len = strlen(s), f = 0; f < len; ++f) putc(s[f]); } inline void read_str(char *s, int &l) { for (l = 0, c = getc(); (c < a || c > z ) && (c < A || c > Z ); c = getc()) ; for (; (c >= a && c <= z ) || (c >= A && c <= Z ); c = getc()) s[l++] = c; s[l] = 0 ; } } // namespace IO using IO ::flush; using IO ::print; using IO ::print_str; using IO ::read; using IO ::read_str; template <class T> inline void cmax(T &x, T y) { x = x >= y ? x : y; } template <class T, class... Other> inline void cmax(T &x, T y, Other... z) { Cmax(x, y), Cmax(x, z...); } template <class T> inline void cmin(T &x, T y) { x = x <= y ? x : y; } template <class T, class... Other> inline void cmin(T &x, T y, Other... z) { Cmin(x, y), Cmin(x, z...); } inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } const int mod(998244353); inline void inc(int &x, const int y) { x = x + y >= mod ? x + y - mod : x + y; } inline void dec(int &x, const int y) { x = x - y < 0 ? x - y + mod : x - y; } inline int add(const int x, const int y) { return x + y >= mod ? x + y - mod : x + y; } inline int sub(const int x, const int y) { return x - y < 0 ? x - y + mod : x - y; } inline int fpow(long long x, int y) { long long ret = 1; for (; y; y >>= 1, x = x * x % mod) if (y & 1) ret = ret * x % mod; return ret; } const int maxn(1e5 + 5); int n, seq[maxn], st[18][maxn], lg[maxn], mx; long long cnt[maxn], sum[maxn], sumv[maxn]; inline int query_gcd(int l, int r) { int len = lg[r - l + 1]; return gcd(st[len][l], st[len][r - (1 << len) + 1]); } inline long long calc(long long m, long long a, long long b, long long c) { if (c < 0) c = -c, a = -a, b = -b; if (m <= 0) return 0; if (m == 1) return (a + b) / c; if (!a) return b / c * m; long long ma, mb, ra, rb, nm; if (b < 0 || b >= c || a < 0 || a >= c) { ma = (a % c + c) % c, mb = (b % c + c) % c; ra = (a - ma) / c, rb = (b - mb) / c; return calc(m, ma, mb, c) + (ra * m * (m + 1) >> 1) + rb * m; } nm = (a * m + b) / c; return nm * m - calc(nm, c, -b - 1, a); } inline long long solve(long long x, long long cx, long long y, long long cy, long long s) { if (!cx || !cy) return 0; if (cx * x + cy * y <= s) return cx * cy; long long s1 = s - cy * y, s2 = s - cx * x, ss = s - cx * x - cy * y; return calc(s / x, -x, s, y) - calc(s1 / x, -x, s1, y) - calc(s2 / y, -y, s2, x) + calc(ss / x, -x, ss, y); } inline long long check(long long mid) { int i, j, p; long long ret = 0, s, k; for (i = j = 1; i <= mx; ++i) { if (!cnt[i]) continue; k = min(mid / i, cnt[i]); if (k) ret += cnt[i] * k - (k * (k - 1) >> 1); while (j < mx && sumv[j + 1] - sumv[i - 1] <= mid) ++j; if (j >= i) ret += cnt[i] * (sum[j] - sum[i]); if (j == mx) continue; for (p = max(i, j); p < mx; ++p) { s = mid - sumv[p] + sumv[i]; if (s < 0) break; ret += solve(i, cnt[i], p + 1, cnt[p + 1], s); } } return ret; } int main() { int i, j, k, p, g; long long ql = 1, qr = 0, mid, ans, lim; read(n); for (i = 1; i <= n; ++i) read(seq[i]), st[0][i] = seq[i], cmax(mx, seq[i]); for (i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; for (j = 1; j <= lg[n]; ++j) for (i = 1; i + (1 << j) - 1 <= n; ++i) st[j][i] = gcd(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]); for (i = 1; i <= n; ++i) for (j = i; j <= n; ++j) { g = query_gcd(i, j), p = j; for (k = lg[n - j]; ~k; --k) if (p + (1 << k) - 1 <= n && st[k][p] % g == 0) p += (1 << k) - 1; cnt[g] += p - j + 1, j = p; } for (i = 1; i <= mx; ++i) qr += cnt[i] * i, sum[i] = sum[i - 1] + cnt[i], sumv[i] = sumv[i - 1] + cnt[i] * i; lim = (long long)n * (n + 1) >> 1, lim = lim * (lim + 1) >> 1, lim = (lim + 1) >> 1; while (ql <= qr) { mid = (ql + qr) >> 1; if (check(mid) >= lim) qr = mid - 1, ans = mid; else ql = mid + 1; } print(ans); return flush(), 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int flag1; flag1 = 0; int flag2 = 0; for (int i = 0; i < n; i++) { if (s[i] == I ) { flag1++; } if (s[i] == A ) { flag2++; } } if (flag1 == 0) { cout << flag2 << endl; } else if (flag1 == 1) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long INF = mod * mod; const long double eps = 1e-5; const long double pi = acos(-1.0); int n, m; long long w[1 << 18]; vector<int> G[1 << 18]; bool canr[1 << 18]; long long score[1 << 18]; int c[1 << 18]; bool iseda[1 << 18]; long long dfs(int id, int fr) { long long ret = 0; for (int j = 0; j < G[id].size(); j++) { int to = G[id][j]; if (to == fr) continue; ret = max(ret, dfs(to, id)); } return ret + w[id]; } long long ad = 0; bool sear(int id, int fr) { for (int j = 0; j < G[id].size(); j++) { int to = G[id][j]; if (to == fr) continue; if (!iseda[to]) { ad += w[id]; w[id] = 0; return true; } if (sear(to, id)) { ad += w[id]; w[id] = 0; return true; } } return false; } void solve() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> w[i]; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } int s; cin >> s; s--; queue<int> q; q.push(s); canr[s] = true; while (!q.empty()) { int id = q.front(); q.pop(); for (int j = 0; j < G[id].size(); j++) { int to = G[id][j]; if (canr[to]) continue; canr[to] = true; q.push(to); } } int cnt = 0; int cnte = 0; for (int i = 0; i < n; i++) { if (canr[i]) { cnt++; cnte += G[i].size(); } } cnte /= 2; if (cnt == cnte + 1) { long long ans = 0; for (int j = 0; j < G[s].size(); j++) { ans = max(ans, dfs(G[s][j], s)); } cout << ans + w[s] << endl; } else { queue<int> q; for (int i = 0; i < n; i++) { if (canr[i]) { c[i] = G[i].size(); } } for (int i = 0; i < n; i++) { if (canr[i]) { if (c[i] == 1) { q.push(i); iseda[i] = true; score[i] = 0; } } } while (!q.empty()) { int id = q.front(); q.pop(); for (int j = 0; j < G[id].size(); j++) { int to = G[id][j]; if (iseda[to]) continue; score[to] = max(score[to], score[id] + w[id]); c[to]--; if (c[to] == 1) { q.push(to); iseda[to] = true; } } } if (iseda[s]) { long long ans = 0; long long ma = 0; sear(s, -1); for (int i = 0; i < n; i++) { if (canr[i] && !iseda[i]) { ans += w[i]; for (int j = 0; j < G[i].size(); j++) { int to = G[i][j]; if (iseda[to]) { ma = max(ma, dfs(to, i)); } } } } cout << ans + ad + ma << endl; } else { long long ans = 0; long long ma = 0; for (int i = 0; i < n; i++) { if (canr[i] && !iseda[i]) { ans += w[i]; for (int j = 0; j < G[i].size(); j++) { int to = G[i][j]; if (iseda[to]) { ma = max(ma, dfs(to, i)); } } } } cout << ans + ma << endl; } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; const int K = 20; int go[N][K]; int tin[N], tout[N]; vector<int> g[N]; int timer = 0; int perm[N]; int where[N]; pair<int, int> t[4 * N]; int n; void dfs(int u, int p = 0) { go[u][0] = p; tin[u] = timer++; for (int i = 1; i < K; ++i) { go[u][i] = go[go[u][i - 1]][i - 1]; } for (auto v : g[u]) { if (v != p) { dfs(v, u); } } tout[u] = timer++; } bool anc(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; } int lca(int a, int b) { if (anc(a, b)) return a; for (int k = K - 1; k >= 0; --k) { if (!anc(go[a][k], b)) { a = go[a][k]; } } return go[a][0]; } pair<int, int> null = {-1, -1}; pair<int, int> empt = {-2, -2}; int d; bool on_vert(int top, int bot, int v) { return anc(top, v) && anc(v, bot); } bool check(pair<int, int> p, pair<int, int> a) { int lc = lca(p.first, p.second); if (!on_vert(lc, p.first, a.first) && !on_vert(lc, p.second, a.first)) return false; if (!on_vert(lc, p.first, a.second) && !on_vert(lc, p.second, a.second)) return false; return true; } pair<int, int> merge(pair<int, int> a, pair<int, int> b) { if (a.first == -1 || b.first == -1) return {-1, -1}; if (check({a.first, b.first}, a) && check({a.first, b.first}, b)) return {a.first, b.first}; if (check({a.first, b.second}, a) && check({a.first, b.second}, b)) return {a.first, b.second}; if (check({a.second, b.first}, a) && check({a.second, b.first}, b)) return {a.second, b.first}; if (check({a.second, b.second}, a) && check({a.second, b.second}, b)) return {a.second, b.second}; if (check({a.first, a.second}, a) && check({a.first, a.second}, b)) return {a.first, a.second}; if (check({b.first, b.second}, a) && check({b.first, b.second}, b)) return {b.first, b.second}; return {-1, -1}; } void build(int l, int r, int v) { if (l == r) { t[v] = {where[l], where[l]}; } else { int m = (l + r) >> 1; build(l, m, 2 * v + 1); build(m + 1, r, 2 * v + 2); t[v] = merge(t[2 * v + 1], t[2 * v + 2]); } } void modify(int ind, int l, int r, int v) { if (l == r) { t[v] = {where[ind], where[ind]}; } else { int m = (l + r) >> 1; if (ind <= m) modify(ind, l, m, 2 * v + 1); else modify(ind, m + 1, r, 2 * v + 2); t[v] = merge(t[2 * v + 1], t[2 * v + 2]); } } void update(int a, int b) { int pa = perm[a]; int pb = perm[b]; swap(where[perm[a]], where[perm[b]]); swap(perm[a], perm[b]); modify(pa, 0, n - 1, 0); modify(pb, 0, n - 1, 0); } int get(int v, int l, int r, pair<int, int> cur) { if (l == r) { return l; } int m = (l + r) >> 1; auto mem = merge(t[2 * v + 1], cur); if (mem.first != -1) return get(2 * v + 2, m + 1, r, mem); return get(2 * v + 1, l, m, cur); } int mex() { if (t[0].first != -1) return n; return get(0, 0, n - 1, {where[0], where[0]}); } void init() { cin >> n; for (int i = 0; i < n; ++i) { cin >> perm[i]; where[perm[i]] = i; } for (int i = 1; i < n; ++i) { int p; cin >> p; --p; g[p].push_back(i); } dfs(0); build(0, n - 1, 0); } void solve() { int q; cin >> q; while (q--) { int t; cin >> t; if (t == 1) { int a, b; cin >> a >> b; --a, --b; update(a, b); } else { cout << mex() << n ; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20); init(); solve(); } |
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0)->sync_with_stdio(0); string s; cin >> s; string t = s; int n = s.size(); auto check = [&]() { for (int i = 0; i <= 0; i++) { int mx0 = 0, mx1 = 0, mmx0 = 0, mmx1 = 0; for (int j = i; j < n; j++) { if (s[j] == 1 ) { mx1 = max(mx1 + 1, mx0 + 1); } else { mx0++; } if (t[j] == 1 ) { mmx1 = max(mmx1 + 1, mmx0 + 1); } else { mmx0++; } if (max(mx0, mx1) != max(mmx0, mmx1)) { return 0; } } } return 1; }; for (int i = 0; i < n; i++) { if (s[i] == 0 ) continue; s[i] = 0 ; if (!check()) { s[i] = 1 ; } } cout << s << n ; } |
#include <bits/stdc++.h> using namespace std; const long long MAX1 = 1e6 + 10; long long T, a, b, c, d, e, f, x, y, z, l, r, mid, num[MAX1], cnt, flag, p1[55][55], p2[55][55]; string s, t; stack<char> st; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> s; a = s.size(); for (int i = 0; i < a; ++i) { if (st.empty()) st.push(s[i]); else if (st.top() == s[i]) { st.pop(); } else st.push(s[i]); } if (st.empty()) cout << Yes ; else cout << No ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int l; cin >> l; string s; cin >> s; if (s.length() % l != 0) { string pattern(l, 0 ); pattern[0] = 1 ; int times = s.length() / l + 1; while (times-- > 0) { cout << pattern; } } else { string pattern = s.substr(0, l); int times = s.length() / l; string presumablyAnswer; for (int i = 0; i < times; ++i) { presumablyAnswer += pattern; } if (presumablyAnswer > s) { cout << presumablyAnswer << endl; } else { if (count(pattern.begin(), pattern.end(), 9 ) < l) { int i = (int)pattern.size() - 1; while (pattern[i] == 9 ) { pattern[i] = 0 ; --i; } ++pattern[i]; for (int i = 0; i < times; ++i) { cout << pattern; } } else { fill(pattern.begin(), pattern.end(), 0 ); pattern[0] = 1 ; for (int i = 0; i <= times; ++i) { cout << pattern; } } } } return 0; } |
#include <bits/stdc++.h> using namespace std; int cnt[5005]; int nearest[5005]; int main(void) { int n, m; cin >> n >> m; memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; b = b > a ? b : b + n; if (!cnt[a] || b < nearest[a]) nearest[a] = b; cnt[a]++; } for (int i = 1; i <= n; ++i) { int time = 0; for (int j = 1; j <= n; ++j) { int x = (cnt[j] - 1) * n + (nearest[j] + (i > j ? n : 0)); time = max(x, time); } cout << time - i << ; } } |
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f, mod = 1e9 + 7; const int maxn = 2e5 + 100, mxn = 5e5 + 100; const long double PI = acos(-1.0); long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } void exgcd(long long a, long long b, long long& d, long long& x, long long& y) { if (!b) { d = a; x = 1; y = 0; } else { exgcd(b, a % b, d, y, x); y -= x * (a / b); } } long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long power(long long a, long long n, long long mod) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } int n, m; double a[maxn], b[maxn], p, q; double f(double x) { double minn = 100000.0; for (int i = 1; i <= n; i++) minn = min(minn, (1 - b[i] * x) / a[i]); return p * minn + q * x; } int main() { while (cin >> n >> p >> q) { double maxx = 0; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; maxx = max(b[i], maxx); } double l = 0, r = 1 / maxx; for (int i = 1; i <= 400; i++) { double mid = (l + r) / 2.0; double midmid = (mid + r) / 2.0; if (f(mid) >= f(midmid)) r = midmid; else l = mid; } printf( %.10f n , f((l + r) / 2.0)); } return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> final; void seive(int n) { vector<bool> prime(n + 1, true); prime[0] = false; prime[1] = false; for (long long int i = 2; i <= (long long int)sqrt(n); ++i) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } for (long long int i = 2; i <= n; ++i) { if (prime[i]) final.push_back(i); } } const int mod = 998244353; const int mx = 2e5 + 1; vector<int> fact(mx); vector<int> inv_fact(mx); long long int expo_pow(long long int a, long long int b) { a %= mod; long long int res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } void precompute() { fact[0] = 1; for (long long int i = 1; i <= mx; ++i) fact[i] = (fact[i - 1] * i) % mod; for (long long int i = 0; i <= mx; ++i) inv_fact[i] = expo_pow(fact[i], mod - 2) % mod; } long long int compute(long long int n, long long int r) { return fact[n] * 1ll * inv_fact[r] % mod * inv_fact[n - r] % mod; } struct cmp { bool operator()(long long int const& a, long long int const& b) { return a > b; } }; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; while (t--) { long long int n, k, gain, sum = 0; cin >> n >> k; vector<long long int> cost(n), a(n); for (long long int i = 0; i < n; ++i) cin >> a[i]; cin >> gain; for (long long int i = 0; i < n; ++i) cin >> cost[i]; priority_queue<long long int, vector<long long int>, cmp> pq; bool cond = true; for (long long int i = 0; i < n; ++i) { pq.push(cost[i]); if (k < a[i]) { while (!pq.empty() && k < a[i]) { k += gain; sum += pq.top(); pq.pop(); } if (k >= a[i]) continue; else { cond = false; break; } cout << n ; } } if (cond) cout << sum << n ; else cout << -1 << n ; } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, w, l; cin >> n >> l >> w; long double p[n + 1]; long long int b[n + 1]; for (long long int i = 1; i <= n; i++) { cin >> p[i]; p[i] /= 100; } for (long long int i = 1; i <= n; i++) { cin >> b[i]; b[i]++; } long double a[n + 1][n + 1][n + 1]; for (long long int i = 0; i <= n; i++) { for (long long int j = 0; j <= n; j++) { for (long long int k = 0; k <= n; k++) { a[i][j][k] = 0; } } } long double x, y; long long int z = 0; if (w > n) w = n; a[0][0][w] = 1; for (long long int i = 1; i <= n; i++) { for (long long int j = 0; j <= i; j++) { for (long long int k = 0; k <= n; k++) { z = b[i]; z += k; if (z > n) z = n; if (j > 0) a[j][i - j][z] += ((a[j - 1][i - j][k]) * (p[i])); if ((i - j) > 0) a[j][i - j][k] += ((a[j][i - j - 1][k]) * (1.0 - p[i])); } } } long double ans = 0.0; for (long long int i = 0; i <= n; i++) { for (long long int j = 0; j <= n; j++) { for (long long int k = 0; k <= n; k++) { if ((k >= i) && (i >= l) && ((i + j) == n)) { ans += a[i][j][k]; } } } } cout << fixed << setprecision(6) << ans; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; multiset<long long> A, B; for (long long i = 0; i < n; i++) { long long num; cin >> num; A.insert(-num); } for (long long i = 0; i < n; i++) { long long num; cin >> num; B.insert(-num); } long long ansa = 0, ansb = 0; while (!A.empty() || !B.empty()) { long long v1 = 0, v2 = 0; if (A.size() > 0) { v1 = -(*A.begin()); if (B.size() > 0) { v2 = -(*B.begin()); if (v2 >= v1) { auto it = B.begin(); B.erase(it); } else { auto it = A.begin(); A.erase(it); ansa += v1; } } else { auto it = A.begin(); A.erase(it); ansa += v1; } } else { if (B.size() > 0) { auto it = B.begin(); B.erase(it); } } v1 = 0, v2 = 0; if (B.size() > 0) { long long v1 = -(*B.begin()); if (A.size() > 0) { v2 = -(*A.begin()); if (v2 >= v1) { auto it = A.begin(); A.erase(it); } else { auto it = B.begin(); B.erase(it); ansb += v1; } } else { auto it = B.begin(); B.erase(it); ansb += v1; } } else { if (A.size() > 0) { auto it = A.begin(); A.erase(it); } } } cout << ansa - ansb; } |
#include <bits/stdc++.h> using namespace std; int n, m; int adj[5000005], can[5000005], fuck[5000005]; inline int read() { int x = 0, f = 1; char ch = getchar(); for (; ch < 0 || ch > 9 ; ch = getchar()) if (ch == - ) f = -1; for (; ch >= 0 && ch <= 9 ; ch = getchar()) x = x * 10 + ch - 0 ; return x * f; } int main() { n = read(), m = read(); if (n == 1) { puts( 0 ); return 0; } for (int i = 0; i < n; i++) adj[i] = 1 << i; while (m--) { int x = read() - 1, y = read() - 1; adj[x] |= 1 << y, adj[y] |= 1 << x; } int all = (1 << n) - 1; bool flag = 1; for (int i = 0; i < n; i++) if (adj[i] != all) flag = 0; if (flag) { puts( 0 ); return 0; } for (int i = 0; i < n; i++) can[1 << i] = 1, fuck[1 << i] = adj[i]; for (int i = 1; i < (1 << n); i++) if (can[i]) { for (int j = 0; j < n; j++) if ((~i >> j & 1) && (fuck[i] >> j & 1)) can[1 << j | i] = 1, fuck[1 << j | i] = fuck[i] | adj[j]; } int ans = n, mask = 0; for (int i = 1; i < (1 << n); i++) if (can[i] && (fuck[i] == all)) { int res = __builtin_popcount(i); if (ans > res) ans = res, mask = i; } printf( %d n , ans); for (int i = 0; i < n; i++) if (mask >> i & 1) printf( %d , i + 1); puts( ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n; int dig[N]; vector<int> ans[N]; bool vis[N]; int main() { cin >> n; vector<int> x; for (int i = (0); i < (n); i++) scanf( %d , &dig[i]), x.push_back(dig[i]); sort((x).begin(), (x).end()); for (int i = (0); i < (n); i++) dig[i] = lower_bound((x).begin(), (x).end(), dig[i]) - x.begin(); memset(vis, false, sizeof(vis)); int ned = 0; for (int i = (0); i < (n); i++) if (!vis[i]) { for (int u = i; !vis[u]; u = dig[u]) vis[u] = true, ans[ned].push_back(u); ned++; } printf( %d n , ned); for (int i = (0); i < (ned); i++) { printf( %d , ((int)(ans[i]).size())); for (int j = (0); j < (((int)(ans[i]).size())); j++) printf( %d , ans[i][j] + 1); printf( n ); } } |
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; for (int i = 0; i < s1.size(); i++) { if (s1[i] <= 90) s1[i] += 32; if (s2[i] <= 90) s2[i] += 32; if (s1[i] > s2[i]) { cout << 1 << endl; return 0; } else if (s1[i] < s2[i]) { cout << -1 << endl; return 0; } } cout << 0 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; char a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } int ans = 0; for (int j = 0; j < m - 1; j++) { if (a[n - 1][j] == D ) { ans++; } } for (int i = 0; i < n - 1; i++) { if (a[i][m - 1] == R ) { ans++; } } cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1000 + 20, MAX_K = 5 + 2; int a[MAX_K][MAX_N]; int dp[MAX_N]; map<int, int> D, ke; int ans; int n, k; int main() { ios::sync_with_stdio(false); cin >> n >> k; for (int i(0); i < int(k); ++i) for (int j(0); j < int(n); ++j) { cin >> a[i][j]; --a[i][j]; } for (int i(0); i < int(n); ++i) D[a[0][i]] = i; for (int i(0); i < int(k); ++i) for (int j(0); j < int(n); ++j) a[i][j] = D[a[i][j]]; for (int i(0); i < int(n); ++i) { ke.clear(); dp[i] = 1; for (int j(0); j < int(k); ++j) for (int l = 0; l < n && a[j][l] != i; ++l) ke[a[j][l]]++; for (int j(0); j < int(i); ++j) if (ke[j] == k) dp[i] = max(dp[i], dp[j] + 1); } for (int i(0); i < int(n); ++i) ans = max(ans, dp[i]); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const char nl = n ; using vi = vector<int>; using vll = vector<ll>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename... Args, template <typename...> typename T> string to_string(T<Args...> const &); string to_string(string const &s) { return + s + ; } string to_string(char const &c) { return to_string(string(1, c)); } string to_string(char const *c) { return to_string(string(c)); } string to_string(bool const &b) { return (b ? T : F ); } template <typename... Args> string to_string(pair<Args...> const &p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename... Args, template <typename...> typename T> string to_string(T<Args...> const &S) { string s = { ; for (auto const &e : S) s += + to_string(e); s += } ; return s; } template <typename Tail> void debug_out(Tail t) { cerr << [ << to_string(t) << ] << endl; } template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) { cerr << << to_string(h) << , ; debug_out(t...); } template <typename T> void dbr(T lb, T ub) { cerr << { ; for (auto it = lb; it != ub; it++) cerr << << to_string(*it); cerr << } << endl; } template <typename T, typename Comp = less<T>> bool smin(T &mem, T const &v, Comp const &cmp = Comp()) { return cmp(v, mem) ? mem = v, true : false; } template <typename T, typename Comp = less<T>> bool smax(T &mem, T const &v, Comp const &cmp = Comp()) { return cmp(mem, v) ? mem = v, true : false; } const string getEnvVar(string const &key) { char *val = getenv(key.c_str()); return val == NULL ? string( ) : string(val); } vector<string> splitString(const string &str, const string &delim = ) { vector<string> tokens; size_t prev = 0, pos = 0; do { pos = str.find(delim, prev); if (pos == string::npos) pos = str.length(); string token = str.substr(prev, pos - prev); if (!token.empty()) tokens.push_back(token); prev = pos + delim.length(); } while (pos < str.length() && prev < str.length()); return tokens; } void header() { ios::sync_with_stdio(false); cin.tie(nullptr); } int main() { header(); string enc, dec = ; cin >> enc; for (int i = 0; i < enc.length(); i++) { if (enc[i] == . ) { dec += 0 ; } else { if (enc[i + 1] == . ) dec += 1 ; else dec += 2 ; i++; } } cout << dec << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, need4demon, puppets; cin >> n >> x >> y; need4demon = ceil((y / 100.0) * n); if (x >= need4demon) puppets = 0; else puppets = need4demon - x; cout << puppets << endl; } |
#include <bits/stdc++.h> using namespace std; long long n, a[1231231]; long long mark[1231231]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; mark[1] = 1; for (int i = 2; i <= n; i++) { mark[i] = mark[i - 1]; mark[i] = max(mark[i], a[i] + 1); } for (int i = n - 1; i >= 1; i--) { mark[i] = max(mark[i], mark[i + 1] - 1); } long long ans = 0; for (int i = 1; i <= n; i++) { ans += max(0ll, (mark[i] - 1 - a[i])); } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int a[1000000], n; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); sort(a + 1, a + n + 1); int i = 1, j = n / 2 + 1; int ans = n; while (true) { if (i > n / 2) break; if (j > n) break; if (2 * a[i] <= a[j]) { i++; j++; ans--; } else j++; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; inline int read() { int sum = 0; char c = getchar(); bool f = 0; while (c < 0 || c > 9 ) { if (c == - ) f = 1; c = getchar(); } while (c >= 0 && c <= 9 ) { sum = sum * 10 + c - 0 ; c = getchar(); } if (f) return -sum; return sum; } const int N = 200005; const int orz = 2000000; const int otz = 1000000005; int n, K, d, a[N], b[N], c[N]; namespace Seg { int lazy[N * 4], t[N * 4], v, rch; inline void push(int p) { if (!lazy[p]) return; int x = lazy[p], l = p << 1, r = (l | 1); t[l] += x; t[r] += x; lazy[l] += x; lazy[r] += x; lazy[p] = 0; } inline void ch(int p, int l, int r, int L, int R) { if (l == L && r == R) { lazy[p] += v; t[p] += v; return; } int mid = (l + r) >> 1; push(p); if (R <= mid) ch(p << 1, l, mid, L, R); else if (L > mid) ch(p << 1 | 1, mid + 1, r, L, R); else ch(p << 1, l, mid, L, mid), ch(p << 1 | 1, mid + 1, r, mid + 1, R); t[p] = min(t[p << 1], t[p << 1 | 1]); } inline void ask(int p, int l, int r) { if (l == r) { rch = l; return; } int mid = (l + r) >> 1; push(p); if (t[p << 1] < K) ask(p << 1, l, mid); else ask(p << 1 | 1, mid + 1, r); } } // namespace Seg using namespace Seg; int s1[N], s2[N], t1, t2; map<int, int> H; int main() { int i, r, st = 1, ed = 1, ans = 1, ino = 0; n = read(); K = read(); d = read(); for (i = 1; i <= n; i++) a[i] = read() + otz; if (!d) { for (i = 1; i <= n; i = r) { r = i; while (r <= n && a[r] == a[i]) r++; if (r - i > ans) ans = r - i, st = i, ed = r - 1; } printf( %d %d , st, ed); return 0; } for (i = 1; i <= n; i++) b[i] = a[i] % d, c[i] = a[i] / d; a[0] = a[1]; b[0] = b[1]; v = orz; ch(1, 1, n, 1, n); for (i = 1; i <= n; i++) { if (b[i] != b[i - 1]) v = orz, ch(1, 1, n, ino + 1, i - 1), ino = i - 1; if (H[a[i]] > ino) v = orz, ch(1, 1, n, ino + 1, H[a[i]]), ino = H[a[i]]; while (t1 && a[i] > a[s1[t1]]) v = c[i] - c[s1[t1]], ch(1, 1, n, s1[t1 - 1] + 1, s1[t1]), t1--; while (t2 && a[i] < a[s2[t2]]) v = c[s2[t2]] - c[i], ch(1, 1, n, s2[t2 - 1] + 1, s2[t2]), t2--; H[a[i]] = i; s1[++t1] = i; s2[++t2] = i; v = -1; ch(1, 1, n, 1, i); v = -orz; ch(1, 1, n, i, i); ask(1, 1, n); if (i - rch + 1 > ans) ans = i - rch + 1, st = rch, ed = i; } printf( %d %d , st, ed); return 0; } |
#include <bits/stdc++.h> using namespace std; inline int lowbit(int x) { return x & (-x); } int n, K, a[16], sum[1 << 16], mp[1 << 16], b[16], fac[2001], vl[2001]; bitset<2001> f[1 << 16]; vector<int> v[16]; void construct(int cur, int sum) { if (!cur) return; if (sum * K <= 2000 && f[cur][sum * K]) { for (int j = 0; j < n; j++) if ((cur >> j) & 1) b[j]++; construct(cur, sum * K); } else { for (int j = 0; j < n; j++) { if (((cur >> j) & 1) && sum >= a[j] && f[cur ^ (1 << j)][sum - a[j]]) { construct(cur ^ (1 << j), sum - a[j]); return; } } } } int main() { scanf( %d%d , &n, &K); for (int i = 0; i < n; i++) scanf( %d , &a[i]); for (int i = 1; i * K <= 2000; i++) fac[i * K] = fac[i] + 1; for (int i = 1; i <= 2000; i++) { vl[i] = i; for (int j = 0; j < fac[i]; j++) vl[i] /= K; } for (int i = 0; i < n; i++) mp[1 << i] = i; for (int i = 1; i < (1 << n); i++) { int t = lowbit(i); sum[i] = sum[i ^ t] + a[mp[t]]; } f[0][0] = 1; for (int i = 0; i < (1 << n); i++) { for (int j = sum[i] / K * K; j >= 0; j -= K) f[i][j / K] = f[i][j / K] | f[i][j]; for (int j = 0; j < n; j++) if (!((i >> j) & 1)) f[i | (1 << j)] |= f[i] << a[j]; } if (f[(1 << n) - 1][1]) { puts( YES ); construct((1 << n) - 1, 1); int bpos = 0; for (int i = 0; i < n; i++) bpos = max(bpos, b[i]), v[b[i]].push_back(a[i]); for (int i = 1; i < n; i++) { while (v[bpos].empty()) bpos--; int x = v[bpos].size(); printf( %d %d n , v[bpos][x - 1], v[bpos][x - 2]); int cur = v[bpos][x - 1] + v[bpos][x - 2]; v[bpos].pop_back(); v[bpos].pop_back(); v[bpos - fac[cur]].push_back(vl[cur]); } } else puts( NO ); return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.