solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; inline int nxt() { int x; scanf("%d", &x); return x; } struct State { int len; int link; array<int, 2> to; State() : len(0), link(-1) { to.fill(-1); } State(int _len, int _link) : len(_len), link(_link) { to.fill(-1); } bool has(int c) const { return to[c] > -1; } }; struct SA { vector<State> a; int cur; SA() : a(1), cur(0) {} void add(int c) { int nw = a.size(); a.emplace_back(a[cur].len + 1, -1); while (cur >= 0) { if (!a[cur].has(c)) { a[cur].to[c] = nw; cur = a[cur].link; } else { break; } } if (cur == -1) { a[nw].link = 0; cur = nw; return; } int by_c = a[cur].to[c]; if (a[by_c].len == a[cur].len + 1) { a[nw].link = by_c; } else { int clone = a.size(); auto st = a[by_c]; a.push_back(st); a[clone].len = a[cur].len + 1; a[by_c].link = clone; a[nw].link = clone; while (cur > -1 && a[cur].to[c] == by_c) { a[cur].to[c] = clone; cur = a[cur].link; } } cur = nw; } void add(const vector<int>& v) { for (int c : v) { add(c); } } void show() { for (int i = 0; i < (int)a.size(); ++i) { cerr << "State #" << i << ": len = " << a[i].len << ", link = " << a[i].link << ";"; for (int c : {0, 1}) { if (a[i].to[c] > -1) { cerr << " transition to " << a[i].to[c] << " by " << c << ","; } } cerr << "\n"; } } }; int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n = nxt(); string s; cin >> s; vector<int> zeroes; for (int i = 0; i < n; ++i) { if (s[i] == '0') { zeroes.push_back(i); } } vector<int> normal, flipped; for (int i : zeroes) { normal.push_back(i % 2); flipped.push_back((i + 1) % 2); } vector<int> a = normal; a.insert(a.end(), (flipped).begin(), (flipped).end()); SA sa; sa.add(a); n = (int)a.size(); int sz = sa.a.size(); vector<vector<int>> sons(sz); vector<vector<int>> par(sz); for (int i = 1; i < sz; ++i) { sons[sa.a[i].link].push_back(i); par[i].push_back(sa.a[i].link); } for (int i = 0;; ++i) { bool changed = false; for (int v = 0; v < sz; ++v) { if ((int)par[v].size() <= i) { continue; } if ((int)par[par[v][i]].size() <= i) { continue; } changed = true; int nw = par[par[v][i]][i]; par[v].push_back(nw); } if (!changed) { break; } } vector<int> tin(sz), tout(sz); int timer = 0; function<void(int)> dfs = [&](int v) { tin[v] = timer++; for (int x : sons[v]) { dfs(x); } tout[v] = timer; }; dfs(0); auto isPar = [&](int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }; vector<int> nth_suf(n + 1); for (int i = 0; i < n; ++i) { nth_suf[i + 1] = sa.a[nth_suf[i]].to[a[i]]; } auto lca = [&](int u, int v) { if (isPar(v, u)) { return v; } for (int i = (int)par[v].size() - 1; i >= 0; --i) { if (i >= (int)par[v].size()) { continue; } if (!isPar(par[v][i], u)) { v = par[v][i]; } } return par[v][0]; }; int q = nxt(); while (q--) { int l1 = nxt() - 1, l2 = nxt() - 1, len = nxt(); int real_l1 = lower_bound((zeroes).begin(), (zeroes).end(), l1) - zeroes.begin(); int real_r1 = lower_bound((zeroes).begin(), (zeroes).end(), l1 + len) - zeroes.begin(); int real_l2 = lower_bound((zeroes).begin(), (zeroes).end(), l2) - zeroes.begin(); int real_r2 = lower_bound((zeroes).begin(), (zeroes).end(), l2 + len) - zeroes.begin(); len = real_r1 - real_l1; if (len != real_r2 - real_l2) { puts("No"); continue; } if (len == 0) { puts("Yes"); continue; } real_l1 += l1 % 2 * (int)zeroes.size(); real_l2 += l2 % 2 * (int)zeroes.size(); int u = nth_suf[real_l1 + len]; int v = nth_suf[real_l2 + len]; int l = lca(u, v); puts(sa.a[l].len >= len ? "Yes" : "No"); } return 0; }
4
#include <iostream> #include <string> #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm> #include <numeric> #include <functional> #include <set> #include <sstream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cctype> #include <climits> #include <fstream> #include <bitset> #include <time.h> #include <assert.h> #define LL long long #define VI vector<int> #define VL vector<long long> #define FOR(i,a,b) for(int i= (a); i<((int)b); ++i) #define RFOR(i,a) for(int i=(a); i >= 0; --i) #define FOE(i,a) for(auto i : a) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define DUMP(x) cerr << #x << " = " << (x) << endl; #define SUM(x) std::accumulate(ALL(x), 0LL) #define MIN(v) *std::min_element(v.begin(), v.end()) #define MAX(v) *std::max_element(v.begin(), v.end()) #define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end()) #define BIT(n) (1LL<<(n)) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define EPS 1e-14 using namespace std; vector<bitset<32>> make(int k) { bitset<32> b(k); vector<bitset<32>> ans = {b}; FOR(i, 0, 32) { if (b[i] == 1) { bitset<32> tmp(k); FOR(j, 0, i) { tmp[j] = true; } tmp[i] = false; ans.emplace_back(tmp); } } return ans; } int main(int argc, char *argv[]) { int N, K, A, B; cin >> N >> K; vector<pair<int, int>> v; FOR(_, 0, N) { cin >> A >> B; v.emplace_back(make_pair(A, B)); } LL ans = 0; FOE(t, make(K)) { LL tmp_ans = 0; FOE(p, v) { bool ok = true; bitset<32> b(p.first); FOR(i, 0, 32) { if (t[i] == 0 and b[i] == 1) { ok = false; break; } } if (ok) { tmp_ans += p.second; } } ans = max(ans, tmp_ans); } std::cout << ans << std::endl; }
0
#include <bits/stdc++.h> int main() { int n, k, p1, p2, count; scanf("%d", &n); scanf("%d", &k); p1 = 1; p2 = 1 + k; count = 0; while (p2 - p1 > 0 && count < n) { printf("%d %d ", p1++, p2--); count += 2; } if (count < n) { if (p1 == p2) { printf("%d ", p1); count++; p1 = 2 + k; } else { p1 = 2 + k; } while (count < n) { printf("%d ", p1++); count++; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; string tostr(long long a) { stringstream rr; rr << a; return rr.str(); } long long pow(long long c, long long d) { return d == 0 ?: c * pow(c, d - 1); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t, z = 0; cin >> t; while (t--) { long long n, m, k = 0, i, j, x, y, p = 0, s = 0, h, f = 0; string a; cin >> a; vector<long long> v; if (a[0] == '1') { k = 1; } for (i = 1; i < a.size(); i++) { if (a[i] == '1') k++; else { if (k > 0) { v.push_back(k); } k = 0; } } if (k > 0) { v.push_back(k); } if (v.size() >= 1) { sort(v.begin(), v.end()); for (i = v.size() - 1; i >= 0; i -= 2) { s += v[i]; } } cout << s << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int a[205]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); swap(a[0], a[n - 1]); for (int i = 0; i < n; i++) cout << a[i] << ' '; cout << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; vector<pair<bool, int>> ab(n); vector<int> b(m); long long res = 0; for (int i = 0; i < n; ++i) { cin >> ab[i].second; } for (int i = 0; i < m; ++i) { cin >> b[i]; b[i]--; } for (int i = 0; i < m; ++i) { ab[b[i]].first = true; } sort(ab.begin(), ab.end()); int cnt = 0; while (!ab[cnt].first) { res += ab[cnt].second; cnt++; } vector<int> auction(n - cnt); for (int i = 0; i < n - cnt; ++i) { auction[i] = ab[i + cnt].second; } sort(auction.begin(), auction.end()); reverse(auction.begin(), auction.end()); for (int i = 0; i < auction.size(); ++i) { res += max((long long)auction[i], res); } cout << res << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } template <typename T> string to_str(T str) { stringstream stream; stream << str; return stream.str(); } template <typename T> int to_int(T num) { int val; stringstream stream; stream << num; stream >> val; return val; } vector<string> split(const string& s, char delim) { vector<string> elems; stringstream ss(s); string item; while (getline(ss, item, delim)) elems.push_back(item); return elems; } const int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int T2 = 105; const int T3 = 1005; const int T4 = 10005; const int T5 = 100005; int p[T5], t[T5], s1[T5], s2[T5]; int main() { ios_base::sync_with_stdio(0); int n, c; cin >> n >> c; long long int ans1 = 0, ans2 = 0; for (int i = 1; i <= n; i++) cin >> p[i]; for (int i = 1; i <= n; i++) cin >> t[i]; for (int i = 1; i <= n; i++) { s1[i] = t[i] + s1[i - 1]; ans1 += max(0, p[i] - c * s1[i]); } for (int i = n; i >= 1; i--) { s2[i] = t[i] + s2[i + 1]; ans2 += max(0, p[i] - c * s2[i]); } if (ans1 == ans2) cout << "Tie\n"; else if (ans1 > ans2) cout << "Limak\n"; else cout << "Radewoosh\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; string s[100005][2]; long long in[100005]; long long dp[100005][2]; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> in[i]; } for (int i = 0; i < n; ++i) { cin >> s[i][0]; s[i][1] = s[i][0]; reverse(s[i][1].begin(), s[i][1].end()); } for (int i = 0; i < n; ++i) { dp[i][0] = 9999999999999999; dp[i][1] = 9999999999999999; } dp[0][0] = 0; dp[0][1] = in[0]; for (int i = 1; i < n; ++i) { if (dp[i - 1][0] != 9999999999999999) { if (s[i][0] >= s[i - 1][0]) { dp[i][0] = dp[i - 1][0]; } if (s[i][1] >= s[i - 1][0]) { dp[i][1] = dp[i - 1][0] + in[i]; } } if (dp[i - 1][1] != 9999999999999999) { if (s[i][0] >= s[i - 1][1]) { dp[i][0] = min(dp[i][0], dp[i - 1][1]); } if (s[i][1] >= s[i - 1][1]) { dp[i][1] = min(dp[i][1], dp[i - 1][1] + in[i]); } } if (dp[i][0] == 9999999999999999 && dp[i][1] == 9999999999999999) { cout << -1; return 0; } } cout << min(dp[n - 1][0], dp[n - 1][1]); return 0; }
3
#include <iostream> #include <string> using namespace std; #define BASE1 124248592 #define BASE2 163282852 #define MOD 197436252 long long hash_temp[1000][1000], hash_field[1000][1000], hash_pattern[1000][1000]; void gen_hash(string str[], int H, int W, int R, int C, long long hash[][1000]) { for (int i = 0; i < H; i++) { long long temp = 0, multi = 1; for (int j = 0; j < W; j++) { temp = (temp * BASE1 + str[i][j]) % MOD; if (j < C) { multi = multi * BASE1 % MOD; } else { temp = ((temp - str[i][j - C] * multi) % MOD + MOD) % MOD; } hash_temp[i][j] = temp; } } for (int j = 0; j < W; j++) { long long temp = 0, multi = 1; for (int i = 0; i < H; i++) { temp = (temp * BASE2 + hash_temp[i][j]) % MOD; if (i < R) { multi = multi * BASE2 % MOD; } else { temp = ((temp -hash_temp[i - R][j] * multi) % MOD + MOD) % MOD; } hash[i][j] = temp; } } } int main() { int H, W, R, C; string field[1000], pattern[1000]; cin >> H >> W; for (int i = 0; i < H; i++) { cin >> field[i]; } cin >> R >> C; for (int i = 0; i < R; i++) { cin >> pattern[i]; } gen_hash(field, H, W, R, C, hash_field); gen_hash(pattern, R, C, R, C, hash_pattern); for (int i = R - 1; i < H; i++) { for (int j = C - 1; j < W; j++) { if (hash_field[i][j] == hash_pattern[R - 1][C - 1]) { cout << i - R + 1 << " " << j - C + 1 << "\n"; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { short negative = 1; x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') negative = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); x *= negative; } inline void write(long long x) { static long long sta[35]; long long top = 0; do { sta[top++] = x % 10, x /= 10; } while (x); while (top) putchar(sta[--top] + 48); puts(""); } bool prime(int n) { if (n == 0 || n == 1) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } const long long power(int n, int k) { long long t = 1; for (int i = 0; i < (int)(k); i++) t *= n; return t; } const string turn2(int n) { string s = ""; while (n != 0) { s += (char)(n % 2 + '0'); n /= 2; } reverse(s.begin(), s.end()); return s; } const string turn16(int n) { string s = ""; while (n != 0) { if (n % 16 > 9) s += (char)('A' + n % 16 - 10); else s += (char)('0' + n % 16); n /= 16; } reverse(s.begin(), s.end()); return s; } const long long quickpower(int n, int k) { if (k == 1) return n; if (k % 2 == 1) return n * quickpower(n, k / 2) * quickpower(n, k / 2); else return quickpower(n, k / 2) * (quickpower(n, k / 2)); } const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; struct team { int score; int in, out, game; string name; } t[5], g[5]; map<string, int> mp; vector<pair<int, int> > ans; bool less1(const team a, const team b) { if (a.score != b.score) { return a.score >= b.score; } else { if ((a.in - a.out) != (b.in - b.out)) { return (a.in - a.out) >= (b.in - b.out); } else { if (a.in != b.in) { return a.in >= b.in; } else { return a.name <= b.name; } } } } bool less2(pair<int, int> a, pair<int, int> b) { if ((a.first - a.second) != (b.first - b.second)) { return (a.first - a.second) <= (b.first - b.second); } else { if (a.second != b.second) { return a.second <= b.second; } } } bool good(int c, int d, int a, int b) { for (int i = 1; i < 5; i++) { g[i] = t[i]; } string l = t[a].name; g[a].score += 3; g[a].in += c; g[a].out += d; g[b].in += d; g[b].out += c; sort(g + 1, g + 5, less1); for (int i = 1; i <= 2; i++) { if (g[i].name == l) { return 1; } } return 0; } int main() { int l = 1; for (int i = 0; i < (int)(5); i++) { string a, b; cin >> a >> b; int c, d; scanf("%d:%d", &c, &d); if (c > d) { if (!mp[a]) { mp[a] = l; t[l].score += 3; t[l].in += c; t[l].out += d; t[l].name = a; t[l].game++; l++; } else { t[mp[a]].score += 3; t[mp[a]].in += c; t[mp[a]].out += d; t[mp[a]].game++; } if (!mp[b]) { mp[b] = l; t[l].in += d; t[l].out += c; t[l].name = b; t[l].game++; l++; } else { t[mp[b]].score += 0; t[mp[b]].in += d; t[mp[b]].out += c; t[mp[b]].game++; } } if (c == d) { if (!mp[a]) { mp[a] = l; t[l].score += 1; t[l].in += c; t[l].out += d; t[l].name = a; t[l].game++; l++; } else { t[mp[a]].score += 1; t[mp[a]].in += c; t[mp[a]].out += d; t[mp[a]].game++; } if (!mp[b]) { mp[b] = l; t[l].score += 1; t[l].in += d; t[l].out += c; t[l].name = b; t[l].game++; l++; } else { t[mp[b]].score += 1; t[mp[b]].in += d; t[mp[b]].out += c; t[mp[b]].game++; } } if (d > c) { if (!mp[a]) { mp[a] = l; t[l].score += 0; t[l].in += c; t[l].out += d; t[l].name = a; t[l].game++; l++; } else { t[mp[a]].score += 0; t[mp[a]].in += c; t[mp[a]].out += d; t[mp[a]].game++; } if (!mp[b]) { mp[b] = l; t[l].score += 3; t[l].in += d; t[l].out += c; t[l].name = b; t[l].game++; l++; } else { t[mp[b]].score += 3; t[mp[b]].in += d; t[mp[b]].out += c; t[mp[b]].game++; } } } int a = mp["BERLAND"], b = 0; for (int i = 1; i <= 4; i++) { if (i != a && t[i].game == 2) { b = i; break; } } for (int i = 1; i <= 30; i++) for (int j = 0; j < (int)(i); j++) { if (good(i, j, a, b)) { ans.push_back(pair<int, int>(i, j)); } } if (ans.size() == 0) { cout << "IMPOSSIBLE" << endl; return 0; } sort(ans.begin(), ans.end(), less2); cout << ans[0].first << ":" << ans[0].second << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<int> ans; int main() { int x; cin >> x; int cnt = 0; while ((x & (x + 1)) != 0) { int n = 0; while (!(x >> n & 1)) n++; ans.push_back(n); x ^= ((1 << n) - 1); cnt++; if ((x & (x + 1)) != 0) { x++; cnt++; } } cout << cnt << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << ' '; } return 0; }
2
#include<iostream> #include<cstring> using namespace std; int main() { string a,b,c; cin>>a>>b>>c; cout<<a[0]<<b[0]<<c[0]; }
0
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 14; int sp[MAX]; char sir[MAX]; int numara(int i, int j) { return sp[j] - sp[i - 1]; } int main() { int n, k; cin >> n >> k; cin >> (sir + 1); for (register int i = 1; i <= n; ++i) { if (sir[i] == 'a') { sp[i] = sp[i - 1] + 1; } else { sp[i] = sp[i - 1]; } } int best = 0; for (register int i = 1; i <= n; ++i) { int st = 1; int dr = i; int sol = i; while (st <= dr) { int mij = (st + dr) >> 1; if (numara(mij, i) <= k) { sol = mij; dr = mij - 1; } else { st = mij + 1; } } best = max(best, i - sol + 1); } for (register int i = 1; i <= n; ++i) { if (sir[i] == 'a') { sp[i] = sp[i - 1]; } else { sp[i] = sp[i - 1] + 1; } } for (register int i = 1; i <= n; ++i) { int st = 1; int dr = i; int sol = i; while (st <= dr) { int mij = (st + dr) >> 1; if (numara(mij, i) <= k) { sol = mij; dr = mij - 1; } else { st = mij + 1; } } best = max(best, i - sol + 1); } cout << best << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 20; const double pi = acos(-1); const int mod = 1e9 + 7; inline long long read() { 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; } int T; long long sum[N]; int cnt[N]; int n; long long a[N][5005]; map<long long, int> _mp; int id[N * 5005][2]; int tot; void add(long long &x, long long y) { x += y; } void dec(long long &x, long long y) { x -= y; } int nxt[N * 5005]; int vis[N * 5005]; int dp[(1 << N) + 1]; int pp[N]; vector<pair<long long, long long> > vec[(1 << N) + 1]; vector<int> used; int ok[(1 << N) + 1]; void dfs(int x) { for (int i = 1; i <= n; i++) pp[i] = 0; used.clear(); int now = x; int sk = 0; while (now) { if (pp[id[now][0]]) { for (auto tt : used) vis[tt] = 0; return; } pp[id[now][0]] = 1; sk |= (1 << (id[now][0] - 1)); vis[now] = 1; used.push_back(now); now = nxt[now]; if (vis[now]) { if (now == x) { if (!ok[sk]) { ok[sk] = 1; for (int i = 1; i < used.size(); i++) { int u = used[i - 1], v = used[i]; vec[sk].push_back(make_pair(u, v)); } vec[sk].push_back(make_pair(used[used.size() - 1], x)); } } for (auto tt : used) { vis[tt] = 0; } return; } } } int pre[(1 << N) + 1]; long long ans[N]; struct node {}; void Dp() { dp[0] = 1; for (int i = 1; i < (1 << n); i++) { for (int t = i;; t = (t - 1) & i) { int v = i ^ t; if (ok[v] && dp[t]) { dp[i] = 1; pre[i] = t; } if (t == 0) break; } } if (!dp[(1 << n) - 1]) { cout << "No\n"; return; } cout << "Yes\n"; int now = (1 << n) - 1; while (now) { int v = pre[now]; int kk = v ^ now; for (auto ww : vec[kk]) { ans[id[ww.first][0]] = a[id[ww.second][0]][id[ww.second][1]]; } now = pre[now]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int tt = _mp[ans[j]]; if (id[tt][0] == i) { cout << ans[j] << " " << j << "\n"; break; } } } } void solve() { n = read(); long long res = 0; for (int i = 1; i <= n; i++) { cnt[i] = read(); for (int j = 1; j <= cnt[i]; j++) { a[i][j] = read(); _mp[a[i][j]] = ++tot; id[tot][0] = i; id[tot][1] = j; add(sum[i], a[i][j]); } add(res, sum[i]); } if (res % n != 0) { cout << "No\n"; return; } res /= n; for (int i = 1; i <= tot; i++) { long long tmp = res - sum[id[i][0]] + a[id[i][0]][id[i][1]]; if (_mp.find(tmp) != _mp.end()) { if (_mp[tmp] == i && tmp != a[id[i][0]][id[i][1]]) continue; int wq = _mp[tmp]; nxt[i] = wq; } } for (int i = 1; i <= tot; i++) dfs(i); Dp(); } int main() { T = 1; for (register int i = 1; i <= T; i++) solve(); }
3
#include<bits/stdc++.h> using namespace std; struct edge { int v,nxt; }e[10010]; int n,t,u,v,s[5010],h[5010],g[5010],a[5010],f[5010][5010]; void add(int u,int v) { t++; e[t].v=v; e[t].nxt=h[u]; h[u]=t; } inline void dfs(int u,int fa) { s[u]=f[u][1]=1; for(int i=h[u];i>0;i=e[i].nxt) { int v=e[i].v; if(v==fa) continue; dfs(v,u); for(int j=1;j<=s[u]+s[v];j++) { a[j]=0; } for(int j=1;j<=s[u];j++) { for(int k=0;k<=s[v];k++) { a[j+k]=(a[j+k]+1ll*f[u][j]*f[v][k])%1000000007; } } s[u]+=s[v]; for(int j=1;j<=s[u];j++) f[u][j]=a[j]; } for(int i=2;i<=s[u];i+=2) { f[u][0]=(f[u][0]-1ll*f[u][i]*g[i]%1000000007+1000000007)%1000000007; } } int main() { cin>>n; for(int i=1;i<n;i++) { scanf("%d%d",&u,&v); add(u,v); add(v,u); } g[0]=1; for(int i=2;i<=n;i+=2) { g[i]=1ll*g[i-2]*(i-1)%1000000007; } dfs(1,0); cout<<1000000007-f[1][0]; return 0; }
0
#include <bits/stdc++.h> char nums[100005][10]; int seq[100005]; char temp[10]; int N; int min_p, max_p; int last; int len; int e[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; int check(int mid, int flag) { char tmp[20]; int cnt = 0; for (int i = 0; i < flag; ++i) { tmp[++cnt] = mid % 10 + '0'; mid = mid / 10; } int ans = 0; for (int i = 0; i < len; ++i) { if (temp[i] != '?') ans = ans * 10 + temp[i] - '0'; else ans = ans * 10 + tmp[cnt--] - '0'; } return ans; } int get_num() { int l, r, mid; len = 0; int flag = 0; int num = 0; while (temp[len]) { if (temp[len] == '?') { flag++; } else { num = num * 10 + temp[len] - '0'; } ++len; } if (!flag) return num; l = 0; if (temp[0] == '?') l = e[flag - 1]; r = e[flag] - 1; while (l <= r) { mid = (l + r) / 2; if (check(mid, flag) > last) { r = mid - 1; } else l = mid + 1; } return check(l, flag); } void num2str(int row, int num) { int len = 0; char temp_num[10]; while (num) { temp_num[len++] = num % 10; num /= 10; } for (int i = 0; i < len; ++i) { nums[row][i] = temp_num[len - 1 - i] + '0'; } nums[row][len] = '\0'; } int main() { scanf("%d", &N); last = 0; for (int i = 0; i < N; ++i) { scanf("%s", temp); int num = get_num(); if (num <= last) { printf("NO\n"); return 0; } else { seq[i] = num; last = num; } } printf("YES\n"); for (int i = 0; i < N; ++i) { printf("%d\n", seq[i]); } return 0; }
5
#include <bits/stdc++.h> using namespace std; struct node { int l, r; int va; } f[100]; int k, x, n, m; bool gao(int a1, int a2, int a3, int a4, int vax, int vay) { f[1].l = a1; f[1].r = a2; f[1].va = vax; f[2].l = a3; f[2].r = a4; f[2].va = vay; int i; for (i = 3; i <= k; i++) { f[i].l = f[i - 2].l; f[i].r = f[i - 1].r; f[i].va = f[i - 2].va + f[i - 1].va; if (f[i - 2].r == 0 && f[i - 1].l == 1) f[i].va++; if (f[i].va > x) return false; } if (f[k].va == x) { int now = 0; if (a1 == 1) { now++; printf("C"); } else if (a1 == 2) { now++; printf("Z"); } for (i = 0; i < vax; i++) { now += 2; printf("AC"); } for (i = now; i < n - 1; i++) { now++; if (a1 == 0) printf("A"); else printf("C"); } if (now < n) { if (a2 == 1) printf("C"); else if (a2 == 0) printf("A"); else printf("Z"); } puts(""); now = 0; if (a3 == 1) { now++; printf("C"); } else if (a3 == 2) { now++; printf("Z"); } for (i = 0; i < vay; i++) { now += 2; printf("AC"); } for (i = now; i < m - 1; i++) { now++; if (a3 == 0) printf("A"); else printf("C"); } if (now < m) { if (a4 == 1) printf("C"); else if (a4 == 0) printf("A"); else printf("Z"); } puts(""); return true; } else return false; } int main() { int i, j, a1, a2, a3, a4; scanf("%d%d%d%d", &k, &x, &n, &m); int flag = 0; for (a1 = 0; a1 < 3; a1++) { if (flag) break; for (a2 = 0; a2 < 3; a2++) { if (flag) break; for (a3 = 0; a3 < 3; a3++) { if (flag) break; for (a4 = 0; a4 < 3; a4++) { if (flag) break; for (i = 0; i <= n / 2; i++) { if (flag) break; if (a1 == 2 && i * 2 + 1 > n) continue; if (a2 == 2 && i * 2 + 1 > n) continue; if (a1 == 2 && a2 == 2 && i != 0 && i * 2 + 2 > n) continue; if (a1 == 2 && a2 == 0 && i * 2 + 2 > n) continue; if (a1 == 1 && a2 == 2 && i * 2 + 2 > n) continue; if (a1 != a2 && n == 1) continue; if (a1 == 0 && a2 == 1 && i == 0) continue; if (a2 == 0 && i * 2 + 1 > n) continue; if (a1 == 1 && i * 2 + 1 > n) continue; if (a1 == 1 && a2 == 0 && i * 2 + 2 > n) continue; for (j = 0; j <= m / 2; j++) { if (flag) break; if (a3 == 2 && j * 2 + 1 > m) continue; if (a4 == 2 && j * 2 + 1 > m) continue; if (a3 == 2 && a4 == 2 && j != 0 && j * 2 + 2 > m) continue; if (a3 == 2 && a4 == 0 && j * 2 + 2 > m) continue; if (a3 == 1 && a4 == 2 && j * 2 + 2 > m) continue; if (a3 != a4 && m == 1) continue; if (a4 == 0 && j * 2 + 1 > m) continue; if (a3 == 0 && a4 == 1 && j == 0) continue; if (a3 == 1 && j * 2 + 1 > m) continue; if (a3 == 1 && a4 == 0 && j * 2 + 2 > m) continue; if (gao(a1, a2, a3, a4, i, j)) flag = 1; } } } } } } if (flag == 0) puts("Happy new year!"); }
4
#include <bits/stdc++.h> using namespace std; struct pt { int x; int y; pt() {} pt(int x, int y) : x(x), y(y) {} }; vector<pt> v; bool comp(const pt &a, const pt &b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } int ccw(pt &a, pt &b, pt &c) { return 1LL * a.x * (b.y - c.y) + 1LL * b.x * (c.y - a.y) + 1LL * c.x * (a.y - b.y) > 0; } int cw(pt &a, pt &b, pt &c) { return 1LL * a.x * (b.y - c.y) + 1LL * b.x * (c.y - a.y) + 1LL * c.x * (a.y - b.y) < 0; } vector<pt> convex_hull(vector<pt> &v) { vector<pt> up; vector<pt> down; sort(v.begin(), v.end(), comp); up.push_back(v[0]); down.push_back(v[0]); for (int i = 1; i < (int)v.size(); i++) { while ((int)up.size() >= 2 && !cw(up[(int)up.size() - 2], up[(int)up.size() - 1], v[i])) { up.pop_back(); } while ((int)down.size() >= 2 && !ccw(down[(int)down.size() - 2], down[(int)down.size() - 1], v[i])) { down.pop_back(); } up.push_back(v[i]); down.push_back(v[i]); } for (int i = (int)down.size() - 2; i > 0; i--) { up.push_back(down[i]); } return up; } long long int area2(pt &a, pt &b, pt &c) { return abs(1LL * a.x * (b.y - c.y) + 1LL * b.x * (c.y - a.y) + 1LL * c.x * (a.y - b.y)); } vector<pt> max_tri_area(vector<pt> &v) { int a = 0; int b = 1; int c = 2; int ba = 0; int bb = 1; int bc = 2; int n = (int)v.size(); vector<pt> tri; while (1) { while (1) { while (area2(v[a], v[b], v[c]) < area2(v[a], v[b], v[(c + 1) % n])) { c = (c + 1) % n; } if (area2(v[a], v[b], v[c]) < area2(v[a], v[(b + 1) % n], v[c])) { b = (b + 1) % n; continue; } else { break; } } if (area2(v[a], v[b], v[c]) > area2(v[ba], v[bb], v[bc])) { ba = a; bb = b; bc = c; } a = (a + 1) % n; if (a == b) { b = (b + 1) % n; } if (b == c) { c = (c + 1) % n; } if (a == 0) { break; } } tri.push_back(v[ba]); tri.push_back(v[bb]); tri.push_back(v[bc]); return tri; } int main(void) { int n; int x, y; scanf(" %d %*d", &n); for (int i = 0; i < n; i++) { scanf(" %d %d", &x, &y); v.push_back(pt(x, y)); } v = convex_hull(v); v = max_tri_area(v); int ax = v[0].x; int ay = v[0].y; int bx = v[1].x; int by = v[1].y; int cx = v[2].x; int cy = v[2].y; printf("%d %d\n", ax + bx - cx, ay + by - cy); printf("%d %d\n", bx + cx - ax, by + cy - ay); printf("%d %d\n", cx + ax - bx, cy + ay - by); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n; long long res; long long arr[100010]; unordered_map<long long, long long> cnt; int main() { ios_base::sync_with_stdio(false); int T; cin >> T; while (T--) { cin >> n; res = 0; for (int i = 0; i < n; i++) { char c; cin >> c; arr[i] = (c - '0'); arr[i]--; } long long currsum = 0; for (int i = 0; i < n; i++) { currsum += arr[i]; if (currsum == 0) res++; res += cnt[currsum]; cnt[currsum]++; } cout << res << endl; cnt.clear(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long k, d, t; cin >> k >> d >> t; t <<= 1; double ans = 0; long long x = k / d, tx = 0; if (k % d) x++; tx = x * d + k; ans += t / tx * x * d; t %= tx; if (t <= 2 * k) ans += t / 2.0; else ans += t - k; printf("%.15lf", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int x, y, n; long long M = 1000000007; cin >> x >> y >> n; long long int a[6] = {x, y, y - x, -x, -y, x - y}; long long int c = (a[(n - 1) % 6] % M + M) % M; cout << c; }
2
#include <bits/stdc++.h> using namespace std; long long int mod = 1e9 + 7; int I_INF = 2e9; long long int L_INF = 1e18; void solve() { long long int n, i, j, l, r, index, ctr, num; cin >> n >> l >> r; vector<long long int> vect(n, 0); vect[0] = 2 * (n - 1); for (i = 1; i < n; i++) { vect[i] = 2 * (n - 1 - i); vect[i] += vect[i - 1]; } vect[n - 1]++; index = (long long int)(lower_bound(vect.begin(), vect.end(), l) - vect.begin()); ctr = (index > 0) ? vect[index - 1] : 0; while (ctr < r) { if (index == (n - 1)) { ctr++; if (ctr >= l && ctr <= r) cout << 1 << " "; } else { for (i = index + 1; i < n; i++) { ctr++; num = index + 1; if (ctr >= l && ctr <= r) cout << num << " "; ctr++; num = i + 1; if (ctr >= l && ctr <= r) cout << num << " "; } } index++; } cout << endl; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; const double EPS = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; bool cmp(string left, string right) { if (left.length() != right.length()) { return left.size() < right.size(); } int n = left.size(); for (int i = 0; i < n; i++) { if (left[i] != right[i]) { return left[i] < right[i]; } } return false; } int main() { string str; cin >> str; int cp = 0; int ans = 1; for (int i = str.length() - 1; i >= 0; i--) { int c = cp + 1; while (0 <= i && str[i] == '0') { i--; c++; } string ss = str.substr(i, c); str = str.substr(0, i); if (ss.length() == 0 || str.length() == 0) break; if (cmp(str, ss)) { str += ss; cp = c; } else { ans++; } } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 200002; int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<pair<int, int> > b(n); unsigned long long ans = 0; for (int i = 0; i < n; i++) cin >> b[i].first; for (int i = 0; i < n; i++) cin >> b[i].second; sort(b.begin(), b.end()); int x = b[0].first; multiset<int> s; unsigned long long su = 0; for (int i = 0; i < n; i++) { if (b[i].first != x) { for (int j = x; j < b[i].first; j++) { if (s.empty()) break; int r = (*s.rbegin()); s.erase(s.lower_bound(r)); su -= r; ans += su; } x = b[i].first; } s.insert(b[i].second), su += b[i].second; } while (!s.empty()) { int r = (*s.rbegin()); s.erase(s.lower_bound(r)); su -= r; ans += su; } cout << ans << endl; }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000000") using namespace std; const long long int INF = 2e9; int ma[3000][3000]; inline bool in(int x, int y, int d, int j, int len) { if ((x == d || x == d + len) && y >= j && y <= j + len) return true; if ((y == j || y == j + len) && x >= d && x <= d + len) return true; return false; } int main() { vector<pair<int, int> > pos; int n, m; cin >> n >> m; memset(ma, 0, sizeof ma); int l = INF, r = -INF, u = -INF, d = INF; for (int i = 0; i < (n); i++) for (int j = 0; j < (m); j++) { if (!j) scanf("\n"); char c; scanf("%c", &c); if (c == 'w') { pos.push_back(make_pair(i, j)); ma[i][j]++; l = min(l, j); r = max(r, j); u = max(u, i); d = min(d, i); } } int len = max(r - l, u - d); int fx = -1, fy; for (int j = 0; j + len < m; j++) { bool can = false; if (d + len >= n || j + len >= m) continue; for (int k = 0; k < (int)pos.size(); k++) { int x = pos[k].first; int y = pos[k].second; if (!in(x, y, d, j, len)) { can = true; break; } } if (!can) { fx = d; fy = j; break; } } if (fx == -1) { for (int i = 0; i + len < n; i++) { if (i + len >= n || l + len >= m) continue; bool can = false; for (int k = 0; k < (int)pos.size(); k++) { int x = pos[k].first; int y = pos[k].second; if (!in(x, y, i, l, len)) { can = true; break; } } if (!can) { fx = i; fy = l; break; } } } if (fx == -1) { cout << -1; return 0; } for (int i = 0; i < (n); i++) { for (int j = 0; j < (m); j++) { if (ma[i][j]) printf("w"); else if (in(i, j, fx, fy, len)) printf("+"); else printf("."); } printf("\n"); } }
4
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll N=1e5+4; vector<ll> adj[N]; vector<vector<ll>> dp(N,vector<ll>(2)); const ll mod=1e9+7; void dfs(ll u,ll p){ dp[u][0]=1; dp[u][1]=1; for(ll v:adj[u]){ if(v==p) continue; dfs(v,u); dp[u][0]=dp[u][0]*((dp[v][0]+dp[v][1])%mod)%mod; dp[u][1]=dp[u][1]*dp[v][0]%mod; } } int main(){ ll n; cin>>n; for(ll i=1;i<n;i++){ ll u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1,-1); cout<<(dp[1][0]+dp[1][1])%mod<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long mod = (long long)(1e9 + 7); long long modpow(long long a, long long b) { if (!b) return 1; a %= mod; return modpow(a * a % mod, b / 2) * (b & 1 ? a : 1) % mod; } long long getbase(long long a, int base) { long long b = 1LL << base; return 1LL * ((a + 1) / (2 * b) * b) + max(0LL, ((a + 1) % (2 * b) - b)); } vector<long long> decompose(long long a, long long b) { vector<long long> basis; for (int base = 0; base <= 54; base++) basis.push_back(getbase(b, base) - getbase(a, base)); return basis; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; scanf("%d", &t); stringstream ss; vector<long long> ans(55, 0); for (int i = 0; i < t; i++) { long long x, m; scanf("%lld%lld", &x, &m); vector<long long> newvec = decompose(x - 1, x + m - 1); for (int base = 0; base < 55; base++) ans[base] = (ans[base] + newvec[base]) % 2; } for (int i = 0; i < 55; i++) if (ans[i] % 2) { cout << "tolik" << endl; exit(0); } cout << "bolik" << endl; return 0; }
3
#include <iostream> #include <set> using namespace std; int main() { int n; multiset<int> s; cin >> n; for(int i=0;i<n;i++){ int a; cin >> a; a = -a; auto itr = s.upper_bound(a); if(itr != s.end()){ s.erase(itr); } s.insert(a); } cout << s.size() << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; bool cmp(const pair<int, int> &a, const pair<int, int> &b) { if (a.second != b.second) { return a.second > b.second; } return a.first > b.first; } pair<int, int> ar[1010]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d", &ar[i].first, &ar[i].second); } sort(ar, ar + n, cmp); int counter = 1, ret = 0; for (int i = 0; i < n; i++) { if (counter == 0) break; counter--; counter += ar[i].second; ret += ar[i].first; } printf("%d\n", ret); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m; const int N = 1e6 + 2; struct Line { int y, xl, xr, v; Line() {} Line(int y, int xl, int xr, int v) : y(y), xl(xl), xr(xr), v(v) {} bool operator<(const Line &rhs) const { return y < rhs.y; } }; vector<Line> lines; int x[N], y[N]; struct Tree { int data[N << 4]; int add[N << 4]; int qL, qR, v; void init() { memset(data, 0, sizeof(data)); memset(add, 0, sizeof(add)); } void update(int o, int L, int R) { if (qL <= L && R <= qR) { data[o] += v; add[o] += v; return; } push_down(o, L, R); int M = (L + R) / 2, lc = o << 1, rc = lc | 1; if (qL <= M) update(lc, L, M); if (qR > M) update(rc, M + 1, R); data[o] = max(data[lc], data[rc]); } int query(int o, int L, int R) { if (qL <= L && R <= qR) return data[o]; push_down(o, L, R); int M = (L + R) / 2, lc = o << 1, rc = lc | 1; int val = -1; if (qL <= M) val = max(val, query(lc, L, M)); if (M < qR) val = max(val, query(rc, M + 1, R)); return val; } void push_down(int o, int L, int R) { int M = (L + R) / 2, lc = o << 1, rc = lc | 1; if (add[o]) { data[lc] += add[o], data[rc] += add[o]; add[lc] += add[o], add[rc] += add[o]; add[o] = 0; } } } tree; int main() { ios::sync_with_stdio(0); cin >> n >> m; int Min = 1e9; for (int i = 1; i <= n; i++) { int u, v; cin >> u >> v; x[i] = u + v, y[i] = u - v; Min = min(Min, x[i]); } int add = max(-Min, 0) + 1; int len = -1; for (int i = 1; i <= n; i++) { x[i] += add; len = max(len, x[i]); } for (int i = 1; i <= n; i++) { lines.emplace_back(y[i] - m, max(1, x[i] - m), min(len, x[i] + m), 1); lines.emplace_back(y[i] + m + 1, max(1, x[i] - m), min(len, x[i] + m), -1); } sort(lines.begin(), lines.end()); int ans = 0; for (int i = 0, j; i < lines.size(); i = j) { j = i; while (j < lines.size() && lines[i].y == lines[j].y) { tree.qL = lines[j].xl, tree.qR = lines[j].xr, tree.v = lines[j].v; tree.update(1, 1, len); j++; } ans = max(ans, tree.data[1]); } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; double ans, res, n, m, i, j, all, now; double DP[1002][1002]; int main() { cin >> n >> m; DP[0][0] = 1; for (i = 1; i <= n; i++) for (j = 0; j <= min((int)i, int(m)); j++) { if (j >= 1) DP[(int)i][(int)j] = DP[(int)i - 1][(int)j - 1] * (m - j + 1) / (n * m - i + 1) + DP[(int)i - 1][(int)j] * (n * m - i + 1 - (m - j)) / (n * m - i + 1); else DP[(int)i][(int)j] = DP[(int)i - 1][(int)j] * (n * m - i + 1 - (m - j)) / (n * m - i + 1); } for (i = 1; i <= min(n, m); i++) { now = DP[(int)n][(int)i] * i * i / n; ans += now; } printf("%.10f\n", ans); }
3
#include <bits/stdc++.h> using namespace std; const int N = 200010; typedef long long LL; int fa[N], p[N], c0[N], c1[N]; struct node { int u, c0, c1; bool operator < (const node &b) const { LL t1 = (LL)c1 * b.c0, t2 = (LL)c0 * b.c1; if (t1 != t2) return t1 < t2; return u < b.u; } }; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } set<node> s; int main() { int n; scanf("%d", &n); for (int i = 2; i <= n; i++) scanf("%d", &p[i]); for (int i = 1, a; i <= n; i++) { scanf("%d", &a), c0[i] = a == 0, c1[i] = a == 1, fa[i] = i; if (i != 1) s.insert((node){i, c0[i], c1[i]}); } LL res = 0; while (!s.empty()) { int u = s.begin()->u; s.erase(s.begin()); int t = find(p[u]); if (t != 1) s.erase((node){t, c0[t], c1[t]}); res += (LL)c1[t] * c0[u], c1[t] += c1[u], c0[t] += c0[u]; fa[u] = t; if (t != 1) s.insert((node){t, c0[t], c1[t]}); } printf("%lld\n", res); }
0
#include<iostream> #include<cstdio> #include<algorithm> #include<map> #include<string> using namespace std; struct Edge{ int a,b,d; bool operator<(const Edge a)const{ return d<a.d; } }; int par[100],rank_[100]; void init(int n){ for(int i=0;i<n;i++) par[i] = i, rank_[i] = 1; } int find(int x){ if(par[x]==x) return x; return par[x] = find(par[x]); } bool unite(int _a,int _b){ int a = find(_a), b = find(_b); if(a==b) return false; if(rank_[a]>rank_[b]) par[b] = a; else{ par[a] = b; if(rank_[a]==rank_[b]) rank_[b]++; } return true; } int main(){ int N,M; while(cin>>N,N){ init(N); cin>>M; Edge e[10000]; for(int i=0;i<M;i++) scanf("%d,%d,%d",&e[i].a,&e[i].b,&e[i].d); sort(e,e+M); int ans = 0; for(int i=0;i<M;i++){ if(unite(e[i].a,e[i].b)){ ans += e[i].d/100-1; } } printf("%d\n",ans); } }
0
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const int kMaxn = 55; int n, a[kMaxn], b[kMaxn]; pair<int, int> c[kMaxn]; int main() { static int i, j, x, y, ans; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d", a + i); c[i].first = a[i]; c[i].second = i; } sort(c + 1, c + n + 1); for (i = 2; i <= n; ++i) b[c[i - 1].second] = a[c[i].second]; b[c[n].second] = a[c[1].second]; for (i = 1; i <= n; ++i) printf("%d ", b[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, col[1005]; vector<int> G[1005]; bool ok = 1; bool vis[1005]; int dist[1005], cno[1005], idx = 0, ans[1005]; void dfs(int u, int co) { cno[u] = idx; col[u] = co; for (int i = 0; i < G[u].size(); i++) { if (!col[G[u][i]]) dfs(G[u][i], -co); else if (col[G[u][i]] == col[u]) { ok = 0; return; } if (!ok) return; } } queue<int> dl; void bfs(int s, int ccno) { memset(vis, 0, sizeof(vis)); memset(dist, 0, sizeof(dist)); vis[s] = 1; dl.push(s); dist[s] = 0; while (!dl.empty()) { int u = dl.front(); dl.pop(); for (int i = 0; i < G[u].size(); i++) if (!vis[G[u][i]]) { ans[ccno] = max(ans[ccno], dist[G[u][i]] = dist[u] + 1); vis[G[u][i]] = 1; dl.push(G[u][i]); } } } int main() { scanf("%d%d", &n, &m); while (m--) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } memset(col, 0, sizeof(col)); for (int i = 1; i <= n; i++) if (!col[i]) { idx++; dfs(i, 1); } if (!ok) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) bfs(i, cno[i]); int tot = 0; for (int i = 1; i <= idx; i++) tot += ans[i]; printf("%d\n", tot); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, a[100100], dp[100100], st, dr, mid; int bs(int val, int idx) { st = 1; dr = idx - 1; if (a[dr] < val) return -1; if (a[1] >= val) return 0; while (st <= dr) { mid = st + dr >> 1; if (a[mid] >= val) dr = mid - 1; else st = mid + 1; } return st - 1; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; dp[1] = 20; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + 20; int idx = bs(a[i] - 90 + 1, i); if (idx != -1) dp[i] = min(dp[i], dp[idx] + 50); idx = bs(a[i] - 1440 + 1, i); if (idx != -1) dp[i] = min(dp[i], dp[idx] + 120); } for (int i = 1; i <= n; i++) cout << dp[i] - dp[i - 1] << '\n'; return 0; }
4
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> mp; #define inf 1e9 int main(){ bool f = true; while(1){ int a,b; cin>>a>>b; if(a==0)break; if(f){ f = false; }else{ cout<<endl; } vector<int> ans; for(int i=a;i<=b;i++){ if(i%400==0){ ans.push_back(i); }else if(i%100==0){ }else if(i%4==0){ ans.push_back(i); } } if(ans.size()==0){ cout<<"NA"<<endl; }else{ for(int i=0;i<ans.size();i++)cout<<ans[i]<<endl; } } return 0; }
0
#include <bits/stdc++.h> int n, i, j, tot, d, k, tt, tot1; int a[100010], p[100010], f[100010], s[100010], a1[1000010], b1[1000010]; int pp(int); int swap(int, int); int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); p[a[i]] = i; } f[1] = 0; for (i = 2; i <= n; i++) { if (f[i] == 0) for (j = 2; j <= n / i; j++) f[i * j] = 1; } for (i = 2; i <= n; i++) { if (f[i] == 0) { tot++; s[tot] = i; } } for (i = 1; i <= n; i++) { while (p[i] != i) { tt++; d = p[i] - i + 1; k = pp(d); tot1++; a1[tot1] = p[i] - k + 1; b1[tot1] = p[i]; swap(p[i], p[i] - k + 1); } } printf("%d\n", tot1); for (i = 1; i <= tot1; i++) printf("%d %d\n", a1[i], b1[i]); } int pp(int x) { int i1; for (i1 = tot; i1 >= 1; i1--) { if (s[i1] <= x) { return s[i1]; } } } int swap(int a1, int b1) { int temp, x, y; x = a[a1]; y = a[b1]; temp = a[a1]; a[a1] = a[b1]; a[b1] = temp; temp = p[x]; p[x] = p[y]; p[y] = temp; return 0; }
3
#include<stdio.h> #include<string.h> #include<math.h> int main(void) { int a,s,d,f,g,h,i,j,k; char z[10]; while(scanf("%s %d %d",z,&a,&s)!=EOF){ d=a*200+s*300; f=a+s; printf("%s %d %d\n",z,f,d); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXM = 100005, MAXN = 5005; const double MAXL = 100000.000; struct edge { int u, v, num; double w, delta; bool choose; bool st_edge() { return u == 1 || v == 1; } }; edge e[MAXM]; int n, m, k; int f[MAXN]; bool cmp(edge e1, edge e2) { return e1.w + e1.delta < e2.w + e2.delta; } int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); } int kruskal(double offset) { int tmp = 0; for (int i = 1; i <= n; i++) f[i] = i; for (int i = 0; i < m; i++) { if (e[i].st_edge()) e[i].delta = offset; e[i].choose = 0; } sort(e, e + m, cmp); for (int i = 0; i < m; i++) { int f_u = Find(e[i].u), f_v = Find(e[i].v); if (f_u != f_v) { if (tmp + e[i].st_edge() <= k) { tmp += e[i].st_edge(); f[f_u] = f_v; e[i].choose = 1; } } } return tmp; } int main() { int tmp = 0; cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> e[i].u >> e[i].v >> e[i].w; e[i].delta = 0.0; e[i].num = i + 1; e[i].choose = 0; tmp += e[i].st_edge(); } if (tmp < k || (n > 1 && k == 0)) { cout << -1; return 0; } kruskal(MAXL); tmp = 0; for (int i = 0; i < m; i++) tmp += e[i].choose; if (tmp != n - 1) { cout << -1; return 0; } double l = -MAXL, r = MAXL, mid; double ans = 0.0; for (int i = 1; i <= 50; i++) { mid = (l + r) / 2; int res = kruskal(mid); if (res < k) r = mid; else l = mid, ans = mid; } kruskal(ans); cout << n - 1 << endl; for (int i = 0; i < m; i++) if (e[i].choose) cout << e[i].num << " "; return 0; }
5
#include<bits/stdc++.h> using namespace std; constexpr int N = 100005; struct Edege{ int u, v, w; bool operator < (const Edege &b) const{ return w < b.w; } }; struct Node{ int id, x; bool operator < (const Node &b) const { return x < b.x; } }; int fa[N], cnt; Node a[N], b[N]; Edege e[N * 3]; void add(int u, int v, int w){ e[cnt].u = u; e[cnt].v = v; e[cnt++].w = w; } int findFa(int u){ if(fa[u] != u){ fa[u] = findFa(fa[u]); } return fa[u]; } int main(){ int n; scanf("%d", &n); for(int i = 1; i <= n; ++i){ fa[i] = i; } cnt = 1; for(int i = 1; i <= n; ++i){ a[i].id = b[i].id = i; scanf("%d%d", &a[i].x, &b[i].x); } sort(a + 1, a + n + 1); for(int i = 2; i <= n; ++i){ add(a[i].id, a[i - 1].id, a[i].x - a[i - 1].x); } sort(b + 1, b + n + 1); for(int i = 2; i <= n; ++i){ add(b[i].id, b[i - 1].id, b[i].x - b[i - 1].x); } sort(e + 1, e + cnt + 1); long long tempAns = 0; for(int i = 1; i < cnt; ++i){ int u = e[i].u, v = e[i].v, w = e[i].w; u = findFa(u); v = findFa(v); if(u != v){ tempAns += w; fa[v] = u; } } cout << tempAns << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int i, n, m, q, p, j, cnt; long long sum[22][2]; int a[(1 << 20) + 1], b[(1 << 20) + 1]; long long ans; void solve(int dep, int l, int r) { int mid, x, y, z, i; if (dep == 0) return; mid = (l + r) / 2; solve(dep - 1, l, mid); solve(dep - 1, mid + 1, r); cnt = 0; for (i = mid + 1; i <= r; ++i) { cnt++; b[cnt] = a[i]; } for (i = l; i <= mid; ++i) sum[dep][0] += (lower_bound(b + 1, b + cnt + 1, a[i]) - (b + 1)); cnt = 0; for (i = l; i <= mid; ++i) { cnt++; b[cnt] = a[i]; } for (i = mid + 1; i <= r; ++i) sum[dep][1] += (lower_bound(b + 1, b + cnt + 1, a[i]) - (b + 1)); x = l; y = mid + 1; z = l; while (x <= mid && y <= r) { if (a[x] < a[y]) { b[z] = a[x]; x++; z++; } else { b[z] = a[y]; y++; z++; } } while (x <= mid) { b[z] = a[x]; x++; z++; } while (y <= r) { b[z] = a[y]; y++; z++; } for (i = l; i <= r; ++i) a[i] = b[i]; } int main() { scanf("%d", &n); m = (1 << n); for (i = 1; i <= m; ++i) scanf("%d", &a[i]); solve(n, 1, m); scanf("%d", &q); for (i = 1; i <= q; ++i) { scanf("%d", &p); while (p > 0) { swap(sum[p][0], sum[p][1]); p--; } ans = 0; for (j = 1; j <= n; ++j) ans = ans + sum[j][0]; cout << ans << endl; } }
3
#include "bits/stdc++.h" using namespace std; #include "string" #define int long long #define pi pair <int, int> #define ff first #define ss second #define boost ios::sync_with_stdio(false);cin.tie(nullptr) #define endl '\n' int32_t main() { boost; int n; cin >> n; vector < int > a(n), b(n); for(int &i : a) cin >> i; for(int &i : b) cin >> i; reverse(b.begin(), b.end()); int l = 0, r = 0; for(int i = 0; i < n; i++) { if(a[i] != b[i]) continue; l = i, r = i; while(r < n && a[r] == b[r]) r++; break; } r--; for(int i = 0; i < n && l <= r; i++) { if(b[i] != a[l] && a[i] != b[l]) swap(b[i], b[l++]); } if(l <= r) { cout << "No" << endl; return 0; } cout << "Yes" << endl; for(int &i : b) cout << i << ' '; cout << endl; }
0
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; const double EPS = 1e-7; long long data[200005]; long long query(int x) { long long sum = 0; for (; x > 0; x -= (x & -x)) sum += data[x]; return sum; } void update(int x, int val) { for (; x < 200005; x += (x & -x)) data[x] += val; } void update(int x, int y, int k) { update(x, k); update(y + 1, -k); } int main() { ios::sync_with_stdio(false); int n; cin >> n; double p = 0; int cant = 1; for (int i = 0; i < n; i++) { int t; cin >> t; if (t == 1) { int a, x; cin >> a >> x; update(1, a, x); p += (1.0 * a * x) / cant; } else if (t == 2) { int k; cin >> k; update(cant + 1, cant + 1, k); p = (p * cant + k) / (cant + 1); cant++; } else { long long val = query(cant); p = (p * cant - val) / (cant - 1); update(cant, cant, -val); cant--; } cout.precision(6); cout << fixed << p << endl; } return 0; }
3
/* _/ _/ _/_/_/ _/ _/_/_/_/ _/_/ _/_/_/_/ _/_/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/ _/_/ _/_/ _/_/ _/_/ _/ */ #include<iostream> #include<algorithm> #include<cmath> #include<iomanip> #include<set> #include<map> #include<queue> #include<vector> using namespace std; using ll=long long; const int MOD=1e9+7; const double pi=3.14159265358979323846; const int inf=2e9; const ll INF=1e18; using P=pair<int,int>; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; int main() { cin.tie(0),cout.tie(0); ios::sync_with_stdio(false); int p; cin >> p; cout << p/500*500 << endl; }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long INF = 8 * 1e18; const int big = 1e6 + 7; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long power(long long x, long long y, long long p = mod) { long long res = 1; x %= p; while (y > 0) { if (y & 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; } long long lcm(long long x, long long y) { return ((x * y) / (gcd(x, y))); } long long modi(long long x, long long p = mod) { return power(x, p - 2, p); } void solve() { int n; long long m; cin >> n >> m; vector<long long> dp(n + 1, 0); dp[1] = 1; long long sum = 1; for (int i = 2; i < n + 1; i++) { long long an = sum; int sq = sqrt(i); for (int j = 2; j < sq + 1; j++) (an += dp[i / j]) %= m; for (int j = 1; j < sq + 1; j++) (an += dp[j] * ((i / j) - (i / (j + 1)))) %= m; if ((i / sq) == sq) an -= dp[sq]; (an += m) %= m; dp[i] = an; sum += an; } cout << dp[n] << endl; } void fastio() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void preprocessing() {} int main() { cout << setprecision(10); bool q = 0; fastio(); int t = 1; if (q) cin >> t; preprocessing(); while (t--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; const int MAX = 3e5 + 10; struct Point { int x, y; } p[MAX]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y); int minx = 1e9, miny = 1e9; int maxx = -1e9, maxy = -1e9; for (int i = 0; i < n; i++) { minx = min(minx, p[i].x); miny = min(miny, p[i].y); maxy = max(maxy, p[i].y); maxx = max(maxx, p[i].x); } int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, p[i].x - minx + p[i].y - miny); ans = max(ans, p[i].x - minx + maxy - p[i].y); ans = max(ans, maxx - p[i].x + maxy - p[i].y); ans = max(ans, maxx - p[i].x + p[i].y - miny); } printf("%d", 2 * ans); for (int i = 4; i <= n; i++) printf(" %d", 2 * (maxx - minx + maxy - miny)); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main(){ int K,T; cin>>K>>T; int a[T]; for(int i=0;i<T;i++){ cin>>a[i]; } sort(a,a+T); int d=a[T-1]; if(d>K-d+1){ cout<<d-(K-d)-1<<endl; return 0; } cout<<0<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int ar[200005]; char ch[200005]; int n, m, ii, k; long long add(long long a, long long b) { a += b; if (a >= 1000000007) a -= 1000000007; return a; } long long mul(long long a, long long b) { a *= b; a %= 1000000007; return a; } void solve() { scanf("%d%s", &n, ch); long long a = 0, ab = 0, abc = 0, gun = 1; for (int i = 0; i < n; i++) { if (ch[i] == 'a') { a = add(a, gun); } else if (ch[i] == 'b') { ab = add(ab, a); } else if (ch[i] == 'c') { abc = add(abc, ab); } else { long long tema = add(a, add(add(a, gun), a)); long long temab = add(ab, add(add(ab, a), ab)); long long temabc = add(abc, add(add(abc, ab), abc)); a = tema; ab = temab; abc = temabc; gun = mul(gun, 3); } } printf("%lld\n", abc); } int main() { int t = 1; while (t--) solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 3e5; vector<pair<long long, long long> > adj[N]; long long n; long long dfs(long long node, long long par) { long long ans = 0; for (int i = 0; i < adj[node].size(); i++) { long long nn = adj[node][i].first; long long ncost = adj[node][i].second; if (nn == par) continue; ans += dfs(nn, node) + ncost; } return ans; } long long wtf(long long node, long long par) { long long besrot = 0; for (int i = 0; i < adj[node].size(); i++) { long long nn = adj[node][i].first; long long ncost = adj[node][i].second; if (nn == par) continue; long long q = wtf(nn, node) + ncost; besrot = max(besrot, q); } return besrot; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n - 1; ++i) { long long y, x, z; cin >> x >> y >> z; adj[x].push_back({y, z}); adj[y].push_back({x, z}); } long long ans = 2 * dfs(1, 0); long long besrot = wtf(1, 0); cout << ans - besrot << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 5010; int main() { int n, m, l[maxn], r[maxn], cnt[maxn] = {0}, tot = 0; cin >> n >> m; for (int i = 0; i < m; i++) { scanf("%d%d", &l[i], &r[i]); for (int j = l[i]; j <= r[i]; j++) { cnt[j]++; } } for (int i = 1; i <= n; i++) if (cnt[i]) tot++; int ans = 0; for (int i = 0; i < m; i++) { int sum[maxn], cnti = 0; memcpy(sum, cnt, sizeof cnt); for (int j = l[i]; j <= r[i]; j++) sum[j]--; for (int j = 1; j <= n; j++) if (sum[j]) cnti++; for (int j = 1; j <= n; j++) sum[j] = sum[j] == 1 ? 1 : 0; for (int j = 1; j <= n; j++) sum[j] += sum[j - 1]; for (int j = i + 1; j < m; j++) { int cntj = sum[r[j]] - sum[l[j] - 1]; ans = max(ans, cnti - cntj); } } cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 1010; namespace Solver { int n; int64_t ans; map<pair<int, int>, int64_t> a; void solve() { cin >> n; for (int i = 1; i <= n; ++i) { string s; cin >> s; vector<int> b; for (int c : s) { if (c == '(') { b.push_back(0); } else { if (!b.empty() && b.back() == 0) { b.pop_back(); } else { b.push_back(1); } } } pair<int, int> x; for (int e : b) { x.first += e; } x.second = b.size() - x.first; a[x] += 1; } for (auto e : a) { pair<int, int> x = e.first; if (x.first == 0 && x.second == 0) { ans += e.second * e.second; } else { if (x.first == 0) { ans += e.second * a[{x.second, 0}]; } } } cout << ans << "\n"; } } // namespace Solver int main() { ios::sync_with_stdio(0); Solver::solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> istream& operator>>(istream& in, vector<T>& v) { for (long long i = 0; i < ((int)(v).size()); i++) cin >> v[i]; return in; } template <typename T, typename second> istream& operator>>(istream& in, pair<T, second>& p) { cin >> p.first >> p.second; return in; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (auto& it : v) { os << it << " "; } return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for (auto& it : v) { os << it << " "; } return os; } template <typename T, typename second> ostream& operator<<(ostream& os, const map<T, second>& v) { for (auto& it : v) { os << it << " "; } return os; } template <typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << "," << p.second << ')'; } void solve() { long long n; cin >> n; cout << std::fixed << std::setprecision(15) << 1 / tan(3.14159265358979323846 / (2 * n)) << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const int MAXN = 1e5 + 5; const long long INF = 1e9; struct maxFlow { vector<pair<int, int> > e[MAXN]; vector<long long> cap[MAXN]; int level[MAXN]; int nxt[MAXN]; int s, t; int N; queue<int> q; void init(int n) { N = n; for (int i = 0; i < N; i++) e[i].clear(), cap[i].clear(); } void addEdge(int x, int y, long long cxy, long long cyx) { e[x].push_back({y, cap[y].size()}); e[y].push_back({x, cap[x].size()}); cap[x].push_back(cxy); cap[y].push_back(cyx); } bool bfs() { while (!q.empty()) q.pop(); for (int i = 0; i < N; i++) level[i] = -1; level[s] = 0; q.push(s); while (!q.empty()) { int p = q.front(); q.pop(); if (p == t) return true; for (int i = 0; i < (int)e[p].size(); i++) { int x = e[p][i].first; if (level[x] == -1 && cap[p][i] != 0) { level[x] = level[p] + 1; q.push(x); } } } return false; } long long augment(int x, long long c) { if (x == t) return c; for (int &i = nxt[x]; i < (int)e[x].size(); i++) { int y = e[x][i].first; if (cap[x][i] > 0 && level[y] == level[x] + 1) { long long tempFlow = augment(y, min(c, cap[x][i])); if (tempFlow > 0) { cap[x][i] -= tempFlow; cap[y][e[x][i].second] += tempFlow; return tempFlow; } } } return 0; } long long solve(int source, int sink) { s = source, t = sink; long long ans = 0; while (bfs()) { long long flow = 0; for (int i = 0; i < N; i++) nxt[i] = 0; while (flow = augment(s, INF)) ans += flow; } return ans; } }; int nn, mm; vector<int> dx = {-3, 0, 2, 2, -2, -2, 1, 1, -1, -1, 3, 0}; vector<int> dy = {0, -3, -1, 1, 1, -1, 2, -2, 2, -2, 0, 3}; int getId(int x, int y) { return (x * mm + y); } bool isValid(int x, int y) { return (x >= 0 && y >= 0 && x < nn && y < mm); } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m; cin >> n >> m; nn = n % 300, mm = m % 300; maxFlow flowGraph; flowGraph.init(nn * mm + 2); int source = nn * mm, sink = source + 1; for (int i = 0; i < nn; i++) { for (int j = 0; j < mm; j++) { if ((i + j) & 1) { flowGraph.addEdge(source, getId(i, j), 2, 0); for (int k = 0; k < 12; k++) { int newX = i + dx[k], newY = j + dy[k]; if (isValid(newX, newY)) { flowGraph.addEdge(getId(i, j), getId(newX, newY), INF, 0); } } } else { flowGraph.addEdge(getId(i, j), sink, 2, 0); } } } cout << n * m - (nn * mm - flowGraph.solve(source, sink)); return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, ans; int l; string k; long long powx(long long x, int y) { if (y == 0) return 1; long long s = powx(x, y / 2); if (y % 2) return s * s * x; else return s * s; } int main() { scanf("%I64d", &n); long long t = n; while (t > 0) { t /= 10LL; l++; } cin >> k; int p = 0, v = 0, last = k.size() - 1; long long sum = 0; for (int i = k.size() - 1; i >= 0; i--) { long long t = sum; sum += powx(10, v) * (k[i] - 48); if (sum < n && v < l) v++; else { int kkk = i; if (k[i + 1] == '0') { int ss = 0; while (k[++i] == '0') { ss++; if (i == last) break; } i--; } ans += powx(n, p) * t; sum = k[i] - 48; v = 1; p++; last = i; } } ans += powx(n, p) * sum; printf("%I64d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, p, k; int main() { cin >> n >> p >> k; if (p - k > 1) cout << "<<" << ' '; for (int i = max(1, p - k); i < p; i++) { cout << i << ' '; } cout << "(" << p << ")" << ' '; for (int i = p + 1; i <= min(p + k, n); i++) { cout << i << ' '; } if (p + k < n) cout << ">>"; return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; int deletes(vector<int> &v, int l, int h) { int me = INT_MAX; int r = 0; int a = l; for (int i = l; i <= h; ++i) { me = min(me, v[i]); } for (int i = l; i <= h; ++i) { v[i] -= me; } for (int i = l; i <= h; ++i) { if (v[i] == 0) { if (a <= i - 1) { r += deletes(v, a, i - 1); } a = i + 1; } } if (a <= h) { r += deletes(v, a, h); } return min(h - l + 1, r + me); } int solve(vector<int> &v) { int a = 0; int r = 0; for (int i = 0; i < v.size(); ++i) { if (v[i] == 0) { if (a <= i - 1) { r += deletes(v, a, i - 1); } a = i + 1; } } if (a < v.size()) { r += deletes(v, a, v.size() - 1); } return r; } int main() { int n; cin >> n; vector<int> v(n, 0); for (int i = 0; i < n; ++i) { cin >> v[i]; } cout << solve(v) << '\n'; return 0; }
5
#include <bits/stdc++.h> #include <cmath> using namespace std; typedef long long ll; //typedef unsigned long long ll; #define rep(i, n) for (int i = 0; i < (n); ++i) //#define rep(i, n) for (ll i = 0; i < (n); ++i) //#define sz(x) ll(x.size()) //typedef pair<ll, int> P; typedef pair<ll, ll> P; //const double INF = 1e10; //const ll INF = LONG_LONG_MAX / 10; //const ll INF = 1e15; const ll MINF = LONG_LONG_MIN; const int INF = INT_MAX / 10; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) //typedef pair<int, int> P; //typedef pair<double, double> P; bool contain(set<P> &s, P a) { return s.find(a) != s.end(); } //ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt"); //ofstream outfile("log.txt"); //outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl; // std::cout << std::bitset<8>(9); void print_line(vector<int> &line) { if (line.size() == 0ll) { cout << endl; return; } for (ll i = 0; i < line.size(); i++) { cout << line[i]; if (i == line.size() - 1) cout << endl; else cout << ' '; } } typedef priority_queue<long long, vector<long long>, greater<long long>> PQ_ASK; const int mod = 1000000007; int main() { string s; cin >> s; vector<int> alphabet(26, 0); for (auto c : s) alphabet[c - 'a']++; PQ_ASK pq; rep(i, 26) { if (alphabet[i] > 0) pq.push(alphabet[i]); } if (pq.size() == 1) { cout << s.size() << endl; return 0; } int ans = 0; while (pq.size() > 1) { int x = pq.top(); pq.pop(); int y = pq.top(); pq.pop(); ans += x + y; pq.push(x + y); } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; const long long inf = 2e9 + 1; int main() { ios_base::sync_with_stdio(false); long long n, k; cin >> n >> k; vector<long long> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } long long lf = 0, rg = inf; while (lf < rg - 1) { long long md = (lf + rg) / 2; long long extra = 0; bool flag = true; for (int i = 0; i < n; ++i) { long long val = b[i] - md * a[i]; if (val < 0) { extra += abs(val); if (extra > k) { break; flag = false; } } } if (extra <= k && flag) { lf = md; } else { rg = md; } } cout << lf << "\n"; }
4
#include <bits/stdc++.h> using namespace std; int main() { int i, a = 0, d = 0; string n; getline(cin, n); string s; getline(cin, s); for (i = 0; i < s.length(); i++) { if (s[i] == 'A') a++; if (s[i] == 'D') d++; } if (a > d) cout << "Anton"; else if (d > a) cout << "Danik"; else cout << "Friendship"; }
1
#include <bits/stdc++.h> using namespace std; int main() { char A[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; string str; cin >> str; int z = A[0]; int k{0}, l{0}, n{0}; for (int i = 0; i < str.length(); i++) { k = z - str[i]; if (abs(k) > 13) { l += 26 - abs(k); } else { l = l + abs(k); } z = str[i]; } cout << l << endl; return 0; }
1
#include <iostream> #include <cstdio> #include <iomanip> #include <vector> #include <map> #include <set> #include <queue> #include <bitset> #include <stack> #include <utility> #include <numeric> #include <algorithm> #include <functional> #include <cctype> #include <complex> #include <string> #include <sstream> #include <fstream> #include <cassert> using namespace std; //common typedef int i32; typedef long long i64,ll; typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll; typedef vector<string> vs; typedef long double ld; #define BR "\n" #define ALL(c) (c).begin(),(c).end() #define REP(i,n) for(int i=0;i<(int)(n);++i) #define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it) #define IN(l,v,r) ((l)<=(v) && (v)<(r)) //config //#undef NDEBUG //#define INF 1<<30 //#define EPS 1e-8 //const ll MOD =100000007; //debug #ifdef NDEBUG #define DUMP(x) #define DUMPLN(x) #define DEBUG(x) #define DEBUGLN(x) #define LINE() #define LINELN() #define CHECK(exp,act) #define STOP(e) #else #define DUMP(x) cerr << #x << " = " << (x) #define DUMPLN(x) DUMP(x) <<endl #define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__ #define DEBUGLN(x) DEBUG(x)<<endl #define LINE() cerr<< " (L" << __LINE__ << ")" #define LINELN() LINE()<<endl #define CHECK(exp,act) if(exp!=act){DUMPLN(exp);DEBUGLN(act);} #define STOP(e) CHECK(e,true);if(!(e)) exit(1); #endif template<class T> inline string toString(const vector<T>& x) { stringstream ss; REP(i,x.size()){ if(i!=0)ss<<" "; ss<< x[i]; } return ss.str(); } template<class T> inline string toString(const vector<vector<T> >& map) { stringstream ss; REP(i,map.size()){ if(i!=0)ss<<BR; ss<< toString(map[i]); } return ss.str(); } template<class K,class V> string toString(map<K,V>& x) { string res;stringstream ss; for(auto& p:x)ss<< p.first<<":" << p.second<<" "; return ss.str(); } string BITtoString(int bit){ stringstream ss; while(bit!=0){ss<<(bit%2);bit/=2;} string res=ss.str();reverse(ALL(res)); return res; } template<typename T,typename V> inline T mod(T v,V MOD){ return (v%MOD+MOD)%MOD; } // checked by http://codeforces.com/contest/237/submission/4710047 // http://codeforces.com/contest/277/submission/4710018 namespace MinCostFlows{ typedef int Flow;typedef ll Cost; const Flow FINF=1<<30; const Cost CINF=1<<30,EPS =0; class MinCostFlow{ private: bool EQ(Cost a,Cost b){return abs(a-b)<EPS;} int V; struct Edge{ int from,to;Flow cap;Cost cost;int rev; Edge(int from,int to,Flow cap,Cost cost,int rev):from(from),to(to),cap(cap),cost(cost),rev(rev){} }; typedef vector<vector<Edge>> Graph; vector<Cost> dist,h;vector<int> prevv,preve; typedef pair<Cost,int> pci; public: Graph G; MinCostFlow(int V):V(V){ G=Graph(V); dist=vector<Cost>(V);h=vector<Cost>(V); prevv=vector<int>(V);preve=vector<int>(V); } void add_edge(int from,int to,Flow cap,Cost cost){ G[from].push_back(Edge(from,to,cap,cost,G[to].size())); G[to].push_back(Edge(to,from,0,-cost,G[from].size()-1)); } // void clearGraph(){ // REP(i,V)G[i].clear(); // } //primal Dual O(V^2*U*C) pair<Cost,Flow> mincostflow(int s,int t,Flow f){ pair<Cost,Flow> res(0,0); fill(ALL(h),0); while(f > 0){ fill(ALL(dist),FINF);dist[s] = 0; priority_queue<pci,vector<pci>,greater<pci> > que; que.push(pci(0,s)); while(!que.empty()){ pci p = que.top();que.pop(); int v = p.second; if(dist[v] < p.first)continue; REP(i,(int)G[v].size()){ Edge &e = G[v][i]; if(e.cap<=0)continue; if(dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){ dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v;preve[e.to] = i; que.push(pci(dist[e.to],e.to)); } } } if(EQ(dist[t],FINF))break; REP(i,V)h[i] += dist[i]; Flow d = f; for(int v = t; v != s; v = prevv[v])d = min(d,G[prevv[v]][preve[v]].cap); f -= d;res.first += d*h[t];res.second+=d; for(int v = t; v != s; v = prevv[v]){ Edge &e = G[prevv[v]][preve[v]]; e.cap -= d;G[v][e.rev].cap += d; } } return res; } }; } using namespace MinCostFlows; class Main{ public: void run(){ int n;cin >> n; MinCostFlow mcf(n+n+2); int S=n+n,T=n+n+1; vector<vector<int>> ws(n,vector<int>(n)); REP(y,n)REP(x,n)cin >> ws[y][x]; vector<vector<int>> es(n,vector<int>(n)); REP(y,n)REP(x,n)cin >> es[y][x]; vector<string> board(n); REP(i,n)cin >> board[i]; REP(y,n)mcf.add_edge(S,y,1,0); REP(x,n)mcf.add_edge(n+x,T,1,0); REP(y,n)REP(x,n){ int c=0; //cost 差分 if(board[y][x]=='.')c+=2*ws[y][x]; REP(y1,n)if(y!=y1)if(board[y1][x]=='o')c+=es[y1][x]; REP(x1,n)if(x!=x1)if(board[y][x1]=='o')c+=es[y][x1]; mcf.add_edge(y,n+x,1,c); } Cost c=mcf.mincostflow(S,T,n).first/2; int num=0; vector<vector<int> > wboard(n,vector<int>(n)); vector<vector<int> > eboard(n,vector<int>(n)); REP(i,n+n+2){ EACH(e,mcf.G[i]){ if(IN(0,e->from,n) && IN(n,e->to,n+n)){ if(e->cap==0 && board[e->from][e->to-n]=='.'){ num++;wboard[e->from][e->to-n]=1; } if(e->cap==1 && board[e->from][e->to-n]=='o'){ num++;eboard[e->from][e->to-n]=1; } } } } cout << c <<endl; cout << num <<endl; REP(y, n)REP(x,n)if(wboard[y][x]){ cout <<y+1 <<" "<< x+1 << " write" << endl; } REP(y, n)REP(x,n)if(eboard[y][x]){ cout <<y+1 <<" "<< x+1 << " erase" << endl; } } }; int main(){ ios::sync_with_stdio(false); Main().run(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1); const double EPS = 1e-10; inline bool isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; string res = ""; for (int i = 0; i < (int)s.size();) { if (isVowel(s[i])) { res += s[i++]; continue; } int j = i, numCons = 0, dup = 0; while (j < (int)s.size()) { if (isVowel(s[j])) { break; } ++numCons; if (numCons == 1) { dup = 1; } if (numCons > 1) { if (s[j] == s[j - 1]) { ++dup; } else { dup = 1; } } if (numCons >= 3 && dup != numCons) { res += ' '; break; } res += s[j++]; } i = j; } cout << res; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T& x) { x = 0; char ch = getchar(); long long f = 1; while (!isdigit(ch)) { if (ch == '-') f *= -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } x *= f; } template <typename T, typename... Args> void read(T& first, Args&... args) { read(first); read(args...); } template <typename T> void write(T arg) { T x = arg; if (x < 0) { putchar('-'); x = -x; } if (x > 9) { write(x / 10); } putchar(x % 10 + '0'); } template <typename T, typename... Ts> void write(T arg, Ts... args) { write(arg); if (sizeof...(args) != 0) { putchar(' '); write(args...); } } using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } const int N = 1000005; const long long MOD = 998244353; int n, m; int head[N], cnt; struct Edge { int next, w, t; } edge[N << 1]; void add(int f, int t, int w) { edge[cnt].t = t; edge[cnt].w = w; edge[cnt].next = head[f]; head[f] = cnt++; } struct node { int v; int value; node(int a, int b) : v(a), value(b){}; bool operator<(const node& no) const { return value > no.value; } }; int dis[N][2]; int len[N][2]; void dijkstra(int s) { memset(dis, 0x3f3f3f3f, sizeof(dis)); memset(len, 0x3f3f3f3f, sizeof(len)); priority_queue<node> q; dis[s][0] = len[s][0] = 0; q.push(node(s, 0)); while (!q.empty()) { node no = q.top(); q.pop(); int u = no.v; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].t, w = edge[i].w; if (w == 0) { if (dis[v][0] > min(dis[u][1] + 1, dis[u][0])) { dis[v][0] = min(dis[u][1] + 1, dis[u][0]); if (dis[u][1] + 1 < dis[u][0]) len[v][0] = len[u][1] + 1; else if (dis[u][1] + 1 == dis[u][0]) len[v][0] = min(len[u][1], len[u][0]) + 1; else len[v][0] = len[u][0] + 1; q.push(node(v, dis[v][0])); } else if (dis[v][0] == min(dis[u][0], dis[u][1] + 1)) { if (dis[v][0] == dis[u][1] + 1) len[v][0] = min(len[v][0], len[u][1] + 1); if (dis[v][0] == dis[u][0]) len[v][0] = min(len[v][0], len[u][0] + 1); } } else { if (dis[v][1] > min(dis[u][1], dis[u][0] + 1)) { dis[v][1] = min(dis[u][1], dis[u][0] + 1); if (dis[u][1] < dis[u][0] + 1) len[v][1] = len[u][1] + 1; else if (dis[u][1] == dis[u][0] + 1) len[v][1] = min(len[u][1], len[u][0]) + 1; else len[v][1] = len[u][0] + 1; q.push(node(v, dis[v][1])); } else if (dis[v][1] == min(dis[u][1], dis[u][0] + 1)) { if (dis[v][1] == dis[u][0] + 1) len[v][1] = min(len[v][1], len[u][0] + 1); if (dis[v][1] == dis[u][1]) len[v][1] = min(len[v][1], len[u][1] + 1); } } } } } struct node2 { int v; int p; int k; int value; node2(int a, int b, int c, int d) : v(a), value(b), k(c), p(d){}; bool operator<(const node2& no) const { return value > no.value; } }; int dis2[N][50]; long long q_pow(long long a, long long p) { long long sum = 1; while (p) { if (p & 1) { sum = sum * a % MOD; } a = a * a % MOD; p >>= 1; } return sum; } int fac[25]; void init() { fac[0] = 1; for (int i = 1; i <= 20; i++) fac[i] = fac[i - 1] * 2; } void dijkstra2(int s) { memset(dis2, 0x3f3f3f3f, sizeof(dis2)); priority_queue<node2> q; dis2[s][0] = 0; q.push(node2(s, 0, 0, 0)); while (!q.empty()) { node2 no = q.top(); q.pop(); int now = no.p; int k = no.k; int u = no.v; if (k > 17) continue; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].t, w = edge[i].w; if (w == now) { if (k - 1 >= 0 && dis2[v][k] > min(dis2[u][k] + 1, dis2[u][k - 1] + 1 + fac[k - 1])) { dis2[v][k] = min(dis2[u][k] + 1, dis2[u][k - 1] + 1 + fac[k - 1]); q.push(node2(v, dis2[v][k], k, now)); } if (k - 1 < 0 && dis2[v][k] > dis2[u][k] + 1) { dis2[v][k] = dis2[u][k] + 1; q.push(node2(v, dis2[v][k], k, now)); } } else { if (dis2[v][k + 1] > dis2[u][k] + 1 + fac[k]) { dis2[v][k + 1] = dis2[u][k] + 1 + fac[k]; q.push(node2(v, dis2[v][k + 1], k + 1, now ^ 1)); } } } } } int main() { init(); memset(head, -1, sizeof(head)); read(n, m); for (int i = 1; i <= m; i++) { int u, v; read(u, v); add(u, v, 0); add(v, u, 1); } dijkstra(1); int k, l; if (dis[n][0] < dis[n][1]) k = dis[n][0], l = len[n][0]; else if (dis[n][0] > dis[n][1]) k = dis[n][1], l = len[n][1]; else k = dis[n][1], l = min(len[n][1], len[n][0]); if (k > 17) { long long ans = (l + q_pow(2, k) - 1 + MOD) % MOD; write(ans), putchar('\n'); } else { dijkstra2(1); int ans = 0x3f3f3f3f; for (int i = 0; i <= 18; i++) ans = min(ans, dis2[n][i]); cout << ans << endl; } return 0; }
3
#include<cstdio> #include<cstdlib> #include<string> #include<iostream> using namespace std; struct Node{ Node *r,*l,*p; int key; }; Node *root, *NIL; void insert(int k){ Node *y = NIL; Node *x = root; Node *z; z =(Node *)malloc(sizeof(Node)); z->key = k; z->l = NIL; z->r = NIL; while(x!=NIL){ y=x; if(z->key < x->key){ x=x->l; } else{ x=x->r; } } z->p=y; if(y==NIL){ root=z; } else{ if(z->key < y->key){ y->l=z; } else{ y->r=z; } } } void inorder(Node *u){ if(u==NIL) return; inorder(u->l); printf(" %d",u->key); inorder(u->r); } void preorder(Node *u){ if(u==NIL) return; printf(" %d",u->key); preorder(u->l); preorder(u->r); } int main(){ int n, i, x; string com; scanf("%d", &n); for ( i = 0; i < n; i++ ){ cin>>com; if ( com == "insert" ){ scanf("%d", &x); insert(x); } else if ( com== "print" ){ inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long base1 = 403007, base2 = 403963, p1 = 1e9 + 7, p2 = 1e9 + 9, po1[400005], po2[400005]; int n; vector<int> adj[100005]; map<pair<int, int>, int> mp; int ty[100005], cnt = 0; pair<int, int> hsh[100005]; void dfs(int x, int p) { long long hsh1 = 0, hsh2 = 0; for (auto nxt : adj[x]) { if (nxt == p) continue; dfs(nxt, x); hsh1 += po1[ty[nxt]]; hsh1 %= p1; hsh2 += po2[ty[nxt]]; hsh2 %= p2; } if (!mp.count({hsh1, hsh2})) mp[{hsh1, hsh2}] = cnt++; ty[x] = mp[{hsh1, hsh2}]; hsh[x] = {hsh1, hsh2}; } int id = 1, mx = 0, now, num[400005]; void dfs1(int x, int p) { if (p) { long long hsh1 = hsh[p].first, hsh2 = hsh[p].second; hsh1 = (hsh1 - po1[ty[x]] + p1) % p1; hsh2 = (hsh2 - po2[ty[x]] + p2) % p2; if (num[ty[p]] == 1) --now; --num[ty[p]]; hsh[p] = {hsh1, hsh2}; if (!mp.count(hsh[p])) mp[hsh[p]] = cnt++; ty[p] = mp[hsh[p]]; ++num[ty[p]]; if (num[ty[p]] == 1) ++now; hsh1 = hsh[x].first, hsh2 = hsh[x].second; hsh1 = (hsh1 + po1[ty[p]]) % p1; hsh2 = (hsh2 + po2[ty[p]]) % p2; if (num[ty[x]] == 1) --now; --num[ty[x]]; hsh[x] = {hsh1, hsh2}; if (!mp.count(hsh[x])) mp[hsh[x]] = cnt++; ty[x] = mp[hsh[x]]; ++num[ty[x]]; if (num[ty[x]] == 1) ++now; } if (mx < now) { mx = now; id = x; } for (auto nxt : adj[x]) { if (nxt != p) dfs1(nxt, x); } long long hsh1 = hsh[x].first, hsh2 = hsh[x].second; hsh1 = (hsh1 - po1[ty[p]] + p1) % p1; hsh2 = (hsh2 - po2[ty[p]] + p2) % p2; if (num[ty[x]] == 1) --now; --num[ty[x]]; hsh[x] = {hsh1, hsh2}; ty[x] = mp[hsh[x]]; ++num[ty[x]]; if (num[ty[x]] == 1) ++now; hsh1 = hsh[p].first, hsh2 = hsh[p].second; hsh1 = (hsh1 + po1[ty[x]]) % p1; hsh2 = (hsh2 + po2[ty[x]]) % p2; if (num[ty[p]] == 1) --now; --num[ty[p]]; hsh[p] = {hsh1, hsh2}; ty[p] = mp[hsh[p]]; ++num[ty[p]]; if (num[ty[p]] == 1) ++now; } int main() { po1[0] = po2[0] = 1; for (int i = 1; i < 400005; ++i) { po1[i] = (po1[i - 1] * base1) % p1; po2[i] = (po2[i - 1] * base2) % p2; } cin >> n; for (int i = 1; i < n; ++i) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } dfs(1, 0); for (int i = 1; i <= n; ++i) ++num[ty[i]]; for (int i = 0; i < cnt; ++i) if (num[i] >= 1) ++mx; now = mx; dfs1(1, 0); cout << id << endl; }
4
#include <bits/stdc++.h> using namespace std; char s[300005]; int main() { cin >> s; long long ans = 0; int len = strlen(s); int r = len; for (int i = len - 1; i >= 0; i--) { r = min(r, i + 9); for (int j = 1; i + 2 * j < r; j++) { if (s[i] == s[i + j] && s[i] == s[i + 2 * j]) { r = min(r, i + 2 * j); break; } } ans += (len - 1) - r + 1; } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; void _fill_int(int* p, int val, int rep) { int i; for (i = 0; i < rep; i++) p[i] = val; } signed long long GETi() { signed long long i; scanf("%lld", &i); return i; } int N, M; string S[2]; const int sp = 1000; signed long long dist[2][300000][2][2]; signed long long distsp[300000 / sp][2][2]; void dfs(int type, int sx, int sy) { int i; if (S[sy][sx] == 'X') return; dist[type][sx][sy][sy] = 0; queue<int> Q; Q.push(sx * 2 + sy); while (!Q.empty()) { int cx = Q.front() / 2; int cy = Q.front() % 2; Q.pop(); int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1}; for (i = 0; i < 4; i++) { int tx = cx + dx[i]; int ty = cy + dy[i]; if (tx < 0 || tx >= N || ty < 0 || ty >= 2) continue; if (type == 0 && (tx >= sx + sp || tx < sx)) continue; if (type == 1 && (tx > sx || tx <= sx - sp)) continue; if (S[ty][tx] == 'X') continue; if (dist[type][tx][sy][ty] <= dist[type][cx][sy][cy] + 1) continue; dist[type][tx][sy][ty] = dist[type][cx][sy][cy] + 1; Q.push(tx * 2 + ty); } } } signed long long dfs2(int sx, int sy, int gx, int gy) { int x, y, i; int dis[sp][2]; if (sx == gx && sy == gy) return 0; for (x = 0; x < 2; x++) for (y = 0; y < sp; y++) dis[y][x] = sp * 5; dis[0][sy] = 0; queue<int> Q; Q.push(sx * 2 + sy); while (!Q.empty()) { int cx = Q.front() / 2; int cy = Q.front() % 2; Q.pop(); int dx[3] = {1, 0, 0}, dy[3] = {0, 1, -1}; for (i = 0; i < 3; i++) { int tx = cx + dx[i]; int ty = cy + dy[i]; if (tx > gx || ty < 0 || ty >= 2) continue; if (S[ty][tx] == 'X') continue; if (dis[tx - sx][ty] <= dis[cx - sx][cy] + 1) continue; dis[tx - sx][ty] = dis[cx - sx][cy] + 1; if (tx == gx && ty == gy) return dis[cx - sx][cy] + 1; Q.push(tx * 2 + ty); } } return 1LL << 40; } void solve() { int f, i, j, k, l, x, y; cin >> N >> M; cin >> S[0] >> S[1]; for (i = 0; i < 2; i++) for (x = 0; x < 2; x++) for (y = 0; y < 2; y++) for (k = 0; k < 300000; k++) dist[i][k][x][y] = 1LL << 40; for (i = 0; i < N; i += sp) dfs(0, i, 0), dfs(0, i, 1); for (i = 0; i < N; i += sp) dfs(1, i, 0), dfs(1, i, 1); for (i = 0; i < N / sp + 1; i++) { if (i == 0 || i * sp >= N) continue; distsp[i][0][0] = distsp[i][0][1] = distsp[i][1][0] = distsp[i][1][1] = 1LL << 40; if (S[0][i * sp] == '.') { distsp[i][0][0] = min(dist[0][i * sp - 1][0][0] + dist[1][i * sp - 1][0][0], dist[0][i * sp - 1][0][1] + dist[1][i * sp - 1][0][1]); distsp[i][1][0] = min(dist[0][i * sp - 1][1][0] + dist[1][i * sp - 1][0][0], dist[0][i * sp - 1][1][1] + dist[1][i * sp - 1][0][1]); } if (S[1][i * sp] == '.') { distsp[i][0][1] = min(dist[0][i * sp - 1][0][0] + dist[1][i * sp - 1][1][0], dist[0][i * sp - 1][0][1] + dist[1][i * sp - 1][1][1]); distsp[i][1][1] = min(dist[0][i * sp - 1][1][0] + dist[1][i * sp - 1][1][0], dist[0][i * sp - 1][1][1] + dist[1][i * sp - 1][1][1]); } } for (i = 0; i < M; i++) { cin >> x >> y; int sx = (x - 1) % N; int sy = (x - 1) / N; int gx = (y - 1) % N; int gy = (y - 1) / N; if (gx < sx) swap(sx, gx), swap(sy, gy); signed long long ret = 0; if (sx / sp == gx / sp) { ret = dfs2(sx, sy, gx, gy); } else { int tx = (sx + (sp - 1)) / sp * sp; signed long long hoge[2]; hoge[0] = dist[1][sx][0][sy]; hoge[1] = dist[1][sx][1][sy]; while (tx + sp <= gx) { signed long long nhoge[2]; nhoge[0] = min(hoge[0] + distsp[tx / sp + 1][0][0], hoge[1] + distsp[tx / sp + 1][1][0]); nhoge[1] = min(hoge[0] + distsp[tx / sp + 1][0][1], hoge[1] + distsp[tx / sp + 1][1][1]); hoge[0] = nhoge[0]; hoge[1] = nhoge[1]; tx += sp; } ret = min(hoge[0] + dist[0][gx][0][gy], hoge[1] + dist[0][gx][1][gy]); } if (ret >= 1LL << 40) (void)printf("-1\n"); else (void)printf("%lld\n", ret); } } int main(int argc, char** argv) { string s; if (argc == 1) ios::sync_with_stdio(false); for (int i = 1; i < argc; i++) s += argv[i], s += '\n'; for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long pd[100][100][100]; int t, B, G; int v[100]; long long solve(int pos, int b, int g) { if (pd[pos][b][g] != -1) return pd[pos][b][g]; if (b + g == t) { if (g >= 1 && b >= 4) return 1; return 0; } if (pos == G + B) return 0; long long ans = 0; if (v[pos] == 1) ans += solve(pos + 1, b, g + 1); else ans += solve(pos + 1, b + 1, g); ans += solve(pos + 1, b, g); return pd[pos][b][g] = ans; } int main() { memset(v, 0, sizeof(v)); memset(pd, -1, sizeof(pd)); scanf("%d %d %d", &B, &G, &t); for (int i = 0; i < G; i++) v[i] = 1; cout << solve(0, 0, 0) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 1000100; int dp[N][2], n, a[N]; string s; int f(int idx, bool goingOn) { if (idx == n) { return 0; } if (dp[idx][goingOn] != -1) { return dp[idx][goingOn]; } int ans = 100000000; if (goingOn) { if (a[idx] == 1) { ans = min(ans, f(idx + 1, 1)); } else { ans = min(ans, f(idx + 1, 1) + 1); ans = min(ans, f(idx + 1, 0)); } } else { if (a[idx] == 1) { ans = min(ans, f(idx + 1, 1) + 2); ans = min(ans, f(idx + 1, 0) + 1); } else { ans = min(ans, f(idx + 1, 0)); } } return dp[idx][goingOn] = ans; } int main() { ios::sync_with_stdio(false); cin >> s; n = s.size(); for (int i = 0; i < n; i++) { a[i] = (s[i] == '1'); } memset(dp, -1, sizeof dp); cout << f(0, 0) << "\n"; }
5
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int add(int a, int b) { long long x = a + b; if (x >= 998244353) x -= 998244353; if (x < 0) x += 998244353; return x; } long long mul(long long a, long long b) { return (a * b) % 998244353; } long long pw(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % 998244353; a = (a * a) % 998244353; b >>= 1; } return ans; } int n; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; int x = 1; while (x <= n) { if (n - x <= 1) { cout << 1; return 0; } if (x % 2 == 0) x = (x * 2 + 1); else x = (x * 2 + 2); } cout << 0; return 0; }
5
#include <bits/stdc++.h> using namespace std; inline long long read() { char c = getchar(); long long x = 0; bool f = 0; for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); if (f) x = -x; return x; } struct P { long long x, y; P(double x = 0, double y = 0) : x(x), y(y) {} P& operator+=(P o) { return x += o.x, y += o.y, *this; } P& operator-=(P o) { return x -= o.x, y -= o.y, *this; } friend P operator+(P a, P b) { return a += b; } friend P operator-(P a, P b) { return a -= b; } friend long long operator*(P a, P b) { return a.x * b.x + a.y * b.y; } friend long long operator%(P a, P b) { return a.x * b.y - a.y * b.x; } }; long long n, m; long long x[1000005], y[1000005]; P v[1000005 << 1]; inline long long C(long long n, long long m) { long long res = 1; for (register long long i = (0); i <= (m - 1); ++i) res *= n - i, res /= i + 1; return res; } bool in(P a) { return a.y < 0 || (a.x > 0 && !a.y); } bool cmp(P a, P b) { if (in(a) != in(b)) return in(a) < in(b); return a % b > 0; } signed main() { n = read(); for (register long long i = (1); i <= (n); ++i) x[i] = read(), y[i] = read(); long long all = C(n, 5), res = 0; for (register long long i = (1); i <= (n); ++i) { m = 0; for (register long long j = (1); j <= (n); ++j) if (i != j) v[++m] = P(x[j] - x[i], y[j] - y[i]); sort(v + 1, v + m + 1, cmp); for (register long long j = (1); j <= (m); ++j) v[m + j] = v[j]; long long p = 1; for (register long long j = (m + 1); j <= (m + m); ++j) { while (p <= j - m || v[j] % v[p] > 0) p++; res += C(j - p, 3); } } cout << all * 5 - res; return 0; }
5
#include <bits/stdc++.h> using namespace std; bool deb_mode = 0; class debugger { vector<string> vars; public: template <typename T> debugger &operator,(const T v) { stringstream ss; ss << v, vars.push_back(ss.str()); return *this; } void printall() { int j = 0, l = vars.size(); for (int i = 0; i < (l - 1); i++) { if (i) cout << ", "; for (j; j < vars[l - 1].size(); j++) { if (vars[l - 1][j] == ',') { j++; break; } cout << vars[l - 1][j]; } cout << " = " << vars[i]; } vars.clear(); } } Dbug; template <typename T1, typename T2> inline ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << "(" << p.first << "," << p.second << ")"; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { bool first = true; os << "{"; for (unsigned int i = 0; i < v.size(); i++) { if (!first) os << ","; os << v[i]; first = false; } return os << "}"; } template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) { bool first = true; os << "{"; for (typename set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if (!first) os << ","; os << *ii; first = false; } return os << "}"; } template <typename T1, typename T2> inline ostream &operator<<(ostream &os, const map<T1, T2> &v) { bool first = true; os << "{"; for (typename map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if (!first) os << ","; os << *ii; first = false; } return os << "}"; } template <typename T> void getarray(T a[], int b, int e) { for (int i = b; i < e + b; i++) cin >> a[i]; } template <typename T> void printarray(T a[], int b, int e) { for (int i = b; i < e - 1 + b; i++) cout << a[i] << " "; if (e - 1 + b >= 0) cout << a[e - 1 + b] << '\n'; } template <typename T> void printV(vector<T> v) { int sz = v.size(); if (sz) cout << v[0]; for (int i = 1; i < sz; i++) cout << " " << v[i]; printf("\n"); } template <typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <typename T> T BigMod(T b, T p, T m) { if (p == 0) return 1; if (p % 2 == 0) { T s = BigMod(b, p / 2, m); return ((s % m) * (s % m)) % m; } return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; } template <typename T> T ModInv(T b, T m) { return BigMod(b, m - 2, m); } template <class T> inline void read(T &x) { bool Minus = 0; char c; for (c = getchar(); c <= 32; c = getchar()) ; if (c == '-') Minus = 1, c = getchar(); for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0'; if (Minus) x = -x; } char getc() { char c; for (c = getchar(); c <= 32; c = getchar()) ; return c; } int CI(int &_x) { return scanf("%d", &_x); } int CI(int &_x, int &_y) { return scanf("%d %d", &_x, &_y); } int CI(int &_x, int &_y, int &_z) { return scanf("%d %d %d", &_x, &_y, &_z); } int CI(int &_a, int &_b, int &_c, int &_d) { return scanf("%d %d %d %d", &_a, &_b, &_c, &_d); } template <class T> string toString(T n) { ostringstream oss; oss << n; return oss.str(); } int toInt(string s) { int r = 0; istringstream sin(s); sin >> r; return r; } long long int toLong(string s) { long long int r = 0; istringstream sin(s); sin >> r; return r; } bool isVowel(char ch) { ch = tolower(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return true; return false; } bool isUpper(char c) { return c >= 'A' && c <= 'Z'; } bool isLower(char c) { return c >= 'a' && c <= 'z'; } int dr8[8] = {+1, -1, +0, +0, +1, -1, -1, +1}; int dc8[8] = {+0, +0, -1, +1, +1, +1, -1, -1}; int dr4[4] = {+0, +0, +1, -1}; int dc4[4] = {-1, +1, +0, +0}; int kn8r[8] = {+1, +2, +2, +1, -1, -2, -2, -1}; int kn8c[8] = {+2, +1, -1, -2, -2, -1, +1, +2}; const double EPS = 1e-9; const int INF = (1 << 30) - 1; const long long int LINF = (1ll << 62) - 1; const int mod = (int)1e9 + 7; const int MAX = (int)1e5; long long int lim = (long long int)1e9; vector<long long int> lucky; void dfs(long long int x) { if (x >= lim) return; lucky.push_back(x); dfs(x * 10 + 4); dfs(x * 10 + 7); } long long int vl, vr, pl, pr; long long int commonLength(long long int x1, long long int x2, long long int l, long long int r) { return max(0ll, min(x2, r) - max(x1, l) + 1); } long long int Calc(long long int x1, long long int x2, long long int y1, long long int y2) { long long int ret = commonLength(x1, x2, pl, pr) * commonLength(y1, y2, vl, vr); ret += commonLength(x1, x2, vl, vr) * commonLength(y1, y2, pl, pr); if (x2 == y1) ret -= commonLength(x2, x2, pl, pr) * commonLength(y1, y1, vl, vr); return ret; } int main() { if (deb_mode) srand(int(time(NULL))); int i, j, k, l, n, m, q, a, b, c; dfs(0ll); lucky.push_back(lim); sort(lucky.begin(), lucky.end()); cin >> pl >> pr >> vl >> vr >> k; long long int allSpace = (pr - pl + 1ll) * (vr - vl + 1ll); long long int posSpace = 0ll; for (i = 1; i + k < lucky.size(); i++) { posSpace += Calc(lucky[i - 1] + 1, lucky[i], lucky[i + k - 1], lucky[i + k] - 1); } if (deb_mode) { cout << "Line= " << 257 << " -> "; Dbug, posSpace, allSpace, "posSpace, allSpace"; Dbug.printall(); cout << '\n'; }; cout << fixed << setprecision(12) << (long double)posSpace / allSpace << "\n"; if (deb_mode) cerr << "TIME = " << clock() << " ms" << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; long int mod = 1000000007; int main() { map<char, int> m; string s; getline(cin, s); for (int i = 0; i < s.length(); i++) { if (s[i] != ' ') m[s[i]]++; } string a; getline(cin, a); bool ans = true; for (int i = 0; i < a.length(); i++) { if (a[i] != ' ') { if (m[a[i]] == 0) m.erase(a[i]); if (m.find(a[i]) == m.end()) { cout << "NO"; ans = false; break; } else { m[a[i]]--; } } } if (ans) cout << "YES"; return 0; }
2
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1e9 + 7; int main() { ll T1, T2; cin >> T1 >> T2; ll A1, A2; cin >> A1 >> A2; ll B1, B2; cin >> B1 >> B2; if (A1 > B1) { swap(A1, B1); swap(A2, B2); } ll diff = A1 * T1 + A2 * T2 - (B1*T1 + B2 * T2); if (diff < 0) { cout << 0 << endl; } else if (diff == 0) { cout << "infinity" << endl; } else { ll tot = (B1 - A1)*T1; ll cnt = 1 + tot / diff * 2; if (tot >= diff && tot%diff == 0) cnt--; cout << cnt << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; char s[N]; int n, w[N]; bool ctrl(int x) { int pk = 0, exp = 0; for (int i = 1; i <= n; i++) { if (s[i] == '*' && i > exp && !pk) pk = i; if (s[i] == 'P') { if (pk == 0) exp = i + x; else { int dis = i - pk; if (dis > x) return 0; else { if (x > dis * 3) exp = i + x - dis * 2; else exp = i + (x - dis) / 2; pk = 0; } if (exp >= n) return 1; } } } if (pk) return 0; return 1; } int main() { scanf("%d\n%s", &n, s + 1); for (int i = 1; i <= n; i++) { w[i] = w[i - 1] + (s[i] == '*'); } int l = 0, r = n << 1, ans = 0; while (l <= r) { int mid = l + r >> 1; if (ctrl(mid)) ans = mid, r = mid - 1; else l = mid + 1; } printf("%d", ans); return 0; }
5
#include <iostream> using namespace std; int main(){ int x; cin >> x; cout << ((x<1200) ? "ABC" : "ARC")<< endl; }
0
#include<bits/stdc++.h> using namespace std; const int N=510; int a[N][N]; int main(){ int n,m,q;scanf("%d%d%d",&n,&m,&q); for(int i=1,x,y;i<=m;++i) scanf("%d%d",&x,&y),a[x][y]++; for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) a[i][j]+=a[i][j-1]; for(int i=1,x,y;i<=q;++i){ int ans=0; scanf("%d%d",&x,&y); for(int i=x;i<=y;++i) ans+=a[i][y]; printf("%d\n",ans); } return 0; }
0
#include <iostream> using namespace std; #define MAX 100000 typedef long long llong; int n,k; llong T[MAX]; int check(long p){ int i =0; for (int j=0;j<k;j++){ llong s=0; while(s+T[i]<=p){ s +=T[i]; i++; if (i==n) return n; } } return i; } int solve(){ llong left =0; llong right = 100000 * 10000; llong mid; while(right - left >1 ){ mid =(left+right)/2; int v=check(mid); if(v>=n)right=mid; else left=mid; } return right; } int main(){ cin >> n >> k; for (int i =0; i<n;i++){ cin >> T[i]; } llong ans=solve(); cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; using cat = long long; struct group { char c; int l, r; group operator+(int x) const { return {c, l + x, r + x}; } group operator-(int x) const { return {c, l - x, r - x}; } }; void apply(vector<group>& gA, vector<group>& gB, int prefA, int prefB, vector<group>& gA_nw, vector<group>& gB_nw) { gA_nw.reserve(prefB + (int)gA.size() - prefA); gB_nw.reserve(prefA + (int)gB.size() - prefB); for (int i = 0; i < prefA; i++) gB_nw.push_back(gA[i]); for (int i = 0; i < prefB; i++) gA_nw.push_back(gB[i]); if (gB_nw.empty() || gB_nw.back().c != gB[prefB].c) { int dl = -gB[prefB].l + (gB_nw.empty() ? 0 : gB_nw.back().r); for (int i = prefB; i < (int)gB.size(); i++) gB_nw.push_back(gB[i] + dl); } else { gB_nw.back().r += gB[prefB].r - gB[prefB].l; if (prefB + 1 < (int)gB.size()) { int dl = -gB[prefB + 1].l + gB_nw.back().r; for (int i = prefB + 1; i < (int)gB.size(); i++) gB_nw.push_back(gB[i] + dl); } } gB_nw[0].l = 0; if (gA_nw.empty() || gA_nw.back().c != gA[prefA].c) { int dl = -gA[prefA].l + (gA_nw.empty() ? 0 : gA_nw.back().r); for (int i = prefA; i < (int)gA.size(); i++) gA_nw.push_back(gA[i] + dl); } else { gA_nw.back().r += gA[prefA].r - gA[prefA].l; if (prefA + 1 < (int)gA.size()) { int dl = -gA[prefA + 1].l + gA_nw.back().r; for (int i = prefA + 1; i < (int)gA.size(); i++) gA_nw.push_back(gA[i] + dl); } } gA_nw[0].l = 0; } void finish_ops(vector<group> gA, vector<group> gB, vector<pair<int, int> >& ans, int sw) { for (int i = 0; i < (int)gA.size(); i++) gA[i].r -= gA[i].l; for (int i = 0; i < (int)gB.size(); i++) gB[i].r -= gB[i].l; for (int i = 1; i < (int)gA.size(); i++) { if (sw == 0) ans.push_back({gA[i - 1].r, gB[i - 1].r}); else ans.push_back({gB[i - 1].r, gA[i - 1].r}); gA[i].r += gB[i - 1].r; gB[i].r += gA[i - 1].r; } } int decode(char c, int l) { if (c >= '0' && c <= '9') return c - '0'; if (c == 'h') return l / 2; if (c == 'H') return l / 2 + 1; return -1; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); string S[2]; cin >> S[0] >> S[1]; bool ans_found = false; vector<pair<int, int> > ans; string ops[] = {"01", "10", "hh", "hH", "Hh", "HH", "h0", "H0", "0h", "0H"}; for (int k = 0; k < 2; k++) { string A = S[k], B = S[1 - k]; bool sw = k; A += 'a'; B += 'b'; vector<group> gA, gB; for (int i = 0; i < (int)A.length(); i++) if (i == 0 || A[i - 1] != A[i]) { int l = i, r = i; while (r < (int)A.length() && A[r] == A[i]) r++; gA.push_back({A[i], l, r}); } for (int i = 0; i < (int)B.length(); i++) if (i == 0 || B[i - 1] != B[i]) { int l = i, r = i; while (r < (int)B.length() && B[r] == B[i]) r++; gB.push_back({B[i], l, r}); } if (gA.size() == gB.size() && (!ans_found || gA.size() - 1 < ans.size())) { ans = vector<pair<int, int> >(); finish_ops(gA, gB, ans, sw); ans_found = true; } int op_id[2]; for (op_id[0] = 0; op_id[0] < 6; op_id[0]++) { int prefA0 = decode(ops[op_id[0]][0], gA.size()), prefB0 = decode(ops[op_id[0]][1], gB.size()); if (prefA0 >= (int)gA.size() || prefB0 >= (int)gB.size()) continue; vector<group> gA_nw0, gB_nw0; apply(gA, gB, prefA0, prefB0, gA_nw0, gB_nw0); if (gA_nw0.size() == gB_nw0.size() && (!ans_found || gA_nw0.size() < ans.size())) { ans = vector<pair<int, int> >(1); if (sw == 0) ans[0] = {prefA0 ? gA[prefA0 - 1].r : 0, prefB0 ? gB[prefB0 - 1].r : 0}; else ans[0] = {prefB0 ? gB[prefB0 - 1].r : 0, prefA0 ? gA[prefA0 - 1].r : 0}; finish_ops(gA_nw0, gB_nw0, ans, sw); ans_found = true; } for (op_id[1] = 0; op_id[1] < 6; op_id[1]++) { int prefA1 = decode(ops[op_id[1]][0], gA_nw0.size()), prefB1 = decode(ops[op_id[1]][1], gB_nw0.size()); if (prefA1 >= (int)gA_nw0.size() || prefB1 >= (int)gB_nw0.size()) continue; if ((gA_nw0.size() + gB_nw0.size()) % 2 != 0 && op_id[1] >= 2) continue; vector<group> gA_nw1, gB_nw1; apply(gA_nw0, gB_nw0, prefA1, prefB1, gA_nw1, gB_nw1); if (gA_nw1.size() == gB_nw1.size() && (!ans_found || gA_nw1.size() + 1 < ans.size())) { ans = vector<pair<int, int> >(2); if (sw == 0) { ans[0] = {prefA0 ? gA[prefA0 - 1].r : 0, prefB0 ? gB[prefB0 - 1].r : 0}; ans[1] = {prefA1 ? gA_nw0[prefA1 - 1].r : 0, prefB1 ? gB_nw0[prefB1 - 1].r : 0}; } else { ans[0] = {prefB0 ? gB[prefB0 - 1].r : 0, prefA0 ? gA[prefA0 - 1].r : 0}; ans[1] = {prefB1 ? gB_nw0[prefB1 - 1].r : 0, prefA1 ? gA_nw0[prefA1 - 1].r : 0}; } finish_ops(gA_nw1, gB_nw1, ans, sw); ans_found = true; } } } } cout << ans.size() << "\n"; for (auto it = ans.begin(); it != ans.end(); it++) cout << it->first << " " << it->second << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<int> elements; map<int, int> max_positions; cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; if (max_positions[a] < i) { max_positions[a] = i; } elements.push_back(a); } int count = 1; int current_max = -1; for (int i = 0; i < n - 1; i++) { current_max = max(current_max, max_positions[elements[i]]); if (current_max == i) { count = (2 * count) % 998244353; } } cout << count << endl; return 0; }
5
#include<iostream> using namespace std; int main() { int R0,W0,C,R; while(true) { cin >> R0 >> W0 >> C >>R ; if(R0 == 0) break; double d = static_cast<double>(C*W0-R0)/R; if(d>0&&(int)d==d)cout << (int)d << endl; else if (d>0)cout << (int)d+1 << endl; else cout << 0 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, ss = 0, su = 0; string s; cin >> n >> s; int m = n / 2 - 1; for (int i = 0; i <= m; i++) { if (s[i] == '4' || s[i] == '7') ss += (s[i] - '0'); else { cout << "NO"; return 0; } } for (int i = m + 1; i < n; i++) { if (s[i] == '4' || s[i] == '7') su += (s[i] - '0'); else { cout << "NO"; return 0; } } if (ss == su) cout << "YES"; else cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; vector<int> pr; bool saw[maxn]; void gherbal(int mx) { for (int i = 2; i <= mx; i++) { if (!saw[i]) { pr.push_back(i); for (int j = 2 * i; j <= mx; j += i) { saw[j] = true; } } } } void printvec(vector<int> v) { int n = v.size(); cout << "# "; for (int i = 0; i < n; i++) cout << v[i] << " "; cout << endl; } void printarr(int a[], int n) { for (int i = 0; i < n; i++) { cout << a[i] << " "; } } vector<int> subset; void gen_subset(vector<int> v, int k) { if (k == v.size() - 1) { printvec(subset); } else { subset.push_back(v[k]); gen_subset(v, k + 1); subset.pop_back(); gen_subset(v, k + 1); } } int visited[maxn]; vector<int> adj[maxn]; void dfs(int u, int t) { visited[u] = t; for (auto g : adj[u]) { if (!visited[g]) { if (t == 1) { dfs(g, 2); } else { dfs(g, 1); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pair<int, int>> f; for (int i = 1; i <= n; i++) { int u, v; cin >> u >> v; f.push_back({u, v}); adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < n; i++) { adj[2 * i].push_back((2 * i) + 1); adj[(2 * i) + 1].push_back(2 * i); } for (int i = 1; i <= 2 * n; i++) { if (!visited[i]) { dfs(i, 1); } } for (auto h : f) { cout << visited[h.first] << " " << visited[h.second] << endl; } }
5
#include <bits/stdc++.h> using namespace std; int min(int a, int b, int c) { if (a <= b && a <= c) return a; else if (b <= a && b <= c) return b; else return c; } int main() { string firstword, secondword; cin >> firstword >> secondword; int x = firstword.size(); int y = secondword.size(); int** arr = new int*[x + 1]; for (int i = 0; i < x + 1; i++) arr[i] = new int[y + 1]; for (int i = 0; i <= x; i++) { arr[i][0] = i; } for (int j = 0; j <= y; j++) { arr[0][j] = j; } for (int i = 1; i <= x; i++) { for (int j = 1; j <= y; j++) { if (firstword[i - 1] == secondword[j - 1]) arr[i][j] = arr[i - 1][j - 1]; else arr[i][j] = min(arr[i - 1][j], arr[i - 1][j - 1], arr[i][j - 1]) + 1; } } cout << arr[x][y] << endl; int x1 = firstword.size(); int y1 = secondword.size(); while (x1 > 0 || y1 > 0) { if (arr[x1][y1] == 1 + arr[x1][y1 - 1]) { cout << "INSERT " << x1 + 1 << " " << secondword[y1 - 1] << endl; y1--; } else if (arr[x1][y1] == 1 + arr[x1 - 1][y1 - 1]) { cout << "REPLACE " << x1 << " " << secondword[y1 - 1] << endl; x1--; y1--; } else if (arr[x1][y1] == 1 + arr[x1 - 1][y1]) { cout << "DELETE " << x1 << endl; x1--; } else { x1--; y1--; } } for (int i = 0; i < x; ++i) delete[] arr[i]; delete[] arr; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int v1, v2, t, d; cin >> v1 >> v2 >> t >> d; int ans = 0; for (int i = 0; i <= t - 2; i++) { int s1, s2; s1 = v1 + (i * d); s2 = v2 + ((t - i - 2) * d); if (abs(s1 - s2) <= d) { int now = v1; for (int j = 0; j < i; j++) { now += d; ans += now; } now = v2; for (int j = 0; j < (t - i - 2); j++) { now += d; ans += now; } break; } } cout << ans + v1 + v2 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<pair<int, char>> ans; int main() { int n, m, i = 1, j = 1, moves = 0; long long k, t = 0; cin >> n >> m >> k; if (k > 4 * m * n - 2 * m - 2 * n) { cout << "NO\n"; return 0; } cout << "YES\n"; if (m == 1) { if (n - 1 >= k) { cout << 1 << "\n" << k << " D\n"; } else { cout << 2 << "\n" << n - 1 << " D\n"; cout << k - n + 1 << " U\n"; } return 0; } while (1) { moves++; if (k <= t + m - 1) { ans.push_back({k - t, 'R'}); t = k; break; } else { ans.push_back({m - 1, 'R'}); t += m - 1; j = m; } if (i == n && j == m && n != 1) break; moves++; if (k <= t + m - 1) { ans.push_back({k - t, 'L'}); t = k; break; } else { ans.push_back({m - 1, 'L'}); t += m - 1; j = 1; } moves++; ans.push_back({1, 'D'}); i++; t++; if (k == t) break; } while (t != k) { moves++; if (k <= t + n - 1) { ans.push_back({k - t, 'U'}); t = k; break; } else { ans.push_back({n - 1, 'U'}); t += n - 1; } moves++; if (k <= t + n - 1) { ans.push_back({k - t, 'D'}); t = k; break; } else { ans.push_back({n - 1, 'D'}); t += n - 1; } moves++; ans.push_back({1, 'L'}); t++; } cout << moves << "\n"; for (int l = 0; l < ans.size(); ++l) { cout << ans[l].first << " " << ans[l].second << "\n"; } }
4
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0), cout.tie(0); ios::sync_with_stdio(false); string a, b; while (cin >> a >> b) { unsigned int len = a.size(); int ans = 0; bool OK = true; map<int, int> dic; for (unsigned int i = 0; i < len; ++i) { if (a.at(i) != b.at(i)) { if (!dic.count(a.at(i)) && !dic.count(b.at(i))) { ++ans; dic[a.at(i)] = b.at(i); dic[b.at(i)] = a.at(i); } else if (dic[a.at(i)] != b.at(i) || dic[b.at(i)] != a.at(i)) { cout << -1 << endl; OK = false; break; } } else { if (!dic.count(a.at(i))) { dic[a.at(i)] = a.at(i); } else if (dic[a.at(i)] != a.at(i)) { cout << -1 << endl; OK = false; break; } } } if (OK) { cout << ans << endl; int vis[1000] = {0}; if (ans != 0) { for (char c = 'a'; c <= 'z'; ++c) { if (!vis[(int)c] && dic[c] != 0 && dic[c] != c) { cout << c << " " << (char)dic[c] << endl; vis[dic[c]] = c; } } } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int dp[2001][10][715]; int tr[22222]; int inv[22222]; int su[2001][2]; int tr2[22222]; int ten[5] = {1, 10, 100, 1000, 10000}; struct fq { int x, y, z; }; queue<fq> q; int pls(int xx, int yy, int zz) { return tr[tr2[xx + 1000 * zz]]; } int can(int xx, int yy) { int rev = 0; bool found = false; for (int i = 0; i < 4; i++) { if ((xx % ten[i + 1]) / ten[i] == yy) { return tr[tr2[xx - yy * ten[i]]]; } } return -1; } int ii[4], jj[4]; int main() { int cnt = 0; for (int i = 0; i < 10; i++) { for (int j = i; j < 10; j++) { for (int k = j; k < 10; k++) { for (int l = k; l < 10; l++) { tr[i * 1000 + j * 100 + k * 10 + l] = cnt; inv[cnt] = i * 1000 + j * 100 + k * 10 + l; cnt++; } } } } for (ii[0] = 0; ii[0] < 10; ii[0]++) { for (ii[1] = 0; ii[1] < 10; ii[1]++) { for (ii[2] = 0; ii[2] < 10; ii[2]++) { for (ii[3] = 0; ii[3] < 10; ii[3]++) { for (int i = 0; i < 4; i++) { jj[i] = ii[i]; } for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) { if (jj[i] > jj[j]) { int tmp = jj[i]; jj[i] = jj[j]; jj[j] = tmp; } } } tr2[ii[0] * 1000 + ii[1] * 100 + ii[2] * 10 + ii[3]] = jj[0] * 1000 + jj[1] * 100 + jj[2] * 10 + jj[3]; } } } } int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &su[i][0], &su[i][1]); } for (int i = 0; i <= n; i++) { for (int j = 1; j <= 9; j++) { for (int k = 0; k < 715; k++) { dp[i][j][k] = 2100000000; } } } dp[0][1][0] = 0; q.push({0, 1, 0}); while (!q.empty()) { fq me = q.front(); q.pop(); int tm = dp[me.x][me.y][me.z]; int val = inv[me.z]; if (me.y < 9) { if (dp[me.x][me.y + 1][me.z] == 2100000000) { dp[me.x][me.y + 1][me.z] = tm + 1; q.push({me.x, me.y + 1, me.z}); } } if (me.y > 1) { if (dp[me.x][me.y - 1][me.z] == 2100000000) { dp[me.x][me.y - 1][me.z] = tm + 1; q.push({me.x, me.y - 1, me.z}); } } if (me.x < n && su[me.x + 1][0] == me.y && val < 1000) { int new_z = pls(val, su[me.x + 1][0], su[me.x + 1][1]); if (dp[me.x + 1][me.y][new_z] == 2100000000) { dp[me.x + 1][me.y][new_z] = tm + 1; q.push({me.x + 1, me.y, new_z}); } } int zz = can(val, me.y); if (zz >= 0) { if (dp[me.x][me.y][zz] == 2100000000) { dp[me.x][me.y][zz] = tm + 1; q.push({me.x, me.y, zz}); } } } int ans = 2100000000; for (int i = 1; i <= 9; i++) { ans = min(ans, dp[n][i][0]); } printf("%d\n", ans); }
3
#include <bits/stdc++.h> using namespace std; map<string, long long> M; map<long long, string> M1; long long snm[2018]; bool flag[2018]; vector<string> ans; long long Time; vector<pair<string, string> > ANS; long long find(long long v) { if (snm[v] != v) return snm[v] = find(snm[v]); else return v; } void unite(long long x, long long y) { x = find(x); y = find(y); snm[y] = x; } void SNm(long long n) { for (int i = 1; i <= 2017; i++) snm[i] = i; } int main() { int n, x, y, i; cin >> n; SNm(n); string s, s1; for (i = 1; i <= n; i++) { cin >> s >> s1; if (M[s] == 0) { Time++; M[s] = Time; M1[Time] = s; ans.push_back(s); } if (M[s1] == 0) { Time++; M[s1] = Time; M1[Time] = s1; ans.push_back(s1); } x = find(M[s]); y = find(M[s1]); unite(x, y); } for (i = ans.size() - 1; i >= 0; i--) { x = M[ans[i]]; y = find(x); if (!flag[y]) { ANS.push_back({M1[y], ans[i]}); flag[y] = true; } } cout << ANS.size() << endl; for (i = 0; i < ANS.size(); i++) cout << ANS[i].first << " " << ANS[i].second << endl; return 0; }
2
#include <bits/stdc++.h> long long num[1000000], num1[1000000], num2[1000000]; long long solve(long long a) { long long i; i = 0; while (a > 0) { i += a & 1; a = a >> 1; } return i; } long long ans1[1000000], ans2[1000000]; int main() { long long i, j, k, a, b, c, n, m, ans; scanf("%lld%lld", &n, &m); for (i = 0; i < n; i++) { scanf("%lld", &num1[i]); } for (i = 0; i < m; i++) { scanf("%lld", &num2[i]); } memset(ans1, 0, sizeof(*ans1) * 300000); memset(ans2, 0, sizeof(*ans2) * 300000); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { a = num1[i] + num2[j]; a += 30000; ans1[a] |= (long long)1 << i; ans2[a] |= (long long)1 << j; } } *num = 0; for (i = 0; i <= 100000; i++) { if ((ans1[i] | ans2[i]) > 0) { num[++(*num)] = i; } } ans = 0; for (i = 1; i <= *num; i++) { for (j = i; j <= *num; j++) { a = ans1[num[i]] | ans1[num[j]]; b = ans2[num[i]] | ans2[num[j]]; c = solve(a) + solve(b); ans = (((ans) > (c)) ? (ans) : (c)); } } printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n), g(n); for (int i = 0; i < n; i++) { cin >> v[i]; g[i] = v[i]; } sort(g.begin(), g.end()); int d = 0; for (int i = 0; i < n; i++) { if (v[i] != g[i]) d++; } if (d <= 2) cout << "YES"; else cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; long long BIT1[1000010], BIT2[1000010], arr[1000010], val[1000010]; map<long long, long long> m; long long ans[1000010]; void pointupdate(long long index, long long val, long long n, long long BIT[]) { for (long long i = index; i <= n; i += (i & (-i))) { BIT[i] += val; } } long long getsum(long long index, long long n, long long BIT[]) { long long sum = 0; for (long long i = index; i > 0; i -= (i & (-i))) { sum += BIT[i]; } return sum; } int main() { long long n; cin >> n; long long k = 0; for (long long i = 0; i < n; i++) { cin >> arr[i]; if (!m[arr[i]]) { val[k++] = arr[i]; m[arr[i]]++; } } sort(val, val + k); for (long long i = 0; i < k; i++) { m[val[i]] = i + 1; } long long k1 = 0; for (long long i = 0; i < n; i++) { long long x = m[arr[i]]; if (x == k) ans[i] = 0; else { long long sum1 = getsum(k, k, BIT1); long long sum2 = getsum(x, k, BIT1); ans[i] = sum1 - sum2; } pointupdate(x, 1, n, BIT1); } long long ans1 = 0; for (long long i = n - 1; i >= 0; i--) { long long x = m[arr[i]]; if (x > 1) { long long sum1 = getsum(x - 1, k, BIT2); if (sum1) ans1 = ans1 + ans[i] * sum1; } pointupdate(x, 1, k, BIT2); } cout << ans1 << endl; }
5
#include <bits/stdc++.h> using namespace std; set<char> s; map<string, int> mp; map<char, int> mp2; vector<int> p, rk; int getp(int v) { if (v == p[v]) return v; return p[v] = getp(p[v]); } void join(int u, int v) { u = getp(u); v = getp(v); if (u == v) return; if (rk[u] < rk[v]) swap(u, v); rk[u] += rk[v]; p[v] = u; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(cin.failbit); int n; cin >> n; string elem[n]; set<string> novo; for (int i = 0; i < (n); ++i) { cin >> elem[i]; for (auto& a : elem[i]) s.insert(a); novo.insert(elem[i]); } int cnt = 0, cnt2 = 0; for (auto& a : s) mp2.insert({a, cnt2++}); p = rk = vector<int>((int)(mp2).size(), 1); iota(p.begin(), p.end(), 0); for (int i = 0; i < (n); ++i) { int j = 0; for (auto& a : elem[i]) { join(mp2[elem[i][0]], mp2[a]); } } set<int> ans; for (auto& a : mp2) ans.insert(getp(a.second)); cout << (int)(ans).size() << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std ; const int MAX = 3e5 + 10 ; int r[MAX] , c[MAX] , freq[MAX] ; vector<int>v[MAX] ; int n , m , q ; int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n>>m>>q ; int Max = 0 ; while(q--) { int i , j ; cin>>i>>j ; r[i]++ , c[j]++ ; Max = max(Max , c[j]) ; v[j].push_back(i) ; } int cnt = 0 ; for(int j = 1 ; j < MAX ; ++j) { if(c[j] != Max) continue ; cnt++ ; for(auto &i : v[j]) freq[i]++ ; } int ans = 0 ; for(int i = 1 ; i < MAX ; ++i) { int x = Max ; if(freq[i] == cnt) x-- ; ans = max(ans , r[i] + x) ; } return cout<<ans<<"\n" , 0 ; }
0
#include <bits/stdc++.h> using namespace std; int m, k, p; vector<int> v[100005]; int N[100005], M; void DFS(int dep, int nw, int pr) { N[dep]++, M = max(M, dep); for (int i = 0; i < v[nw].size(); i++) if (pr != v[nw][i]) DFS(dep + 1, v[nw][i], nw); } void INIT() { int a, b; scanf("%d%d%d", &m, &k, &p); for (int i = 1; i < m; i++) { scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } memset(N, 0, sizeof N); DFS(0, 1, -1); N[0] = 0; } void PROC() { int lw = 1, tot = 0, sp = 0; int ans = 0; for (int i = 1; i <= M; i++) { sp += tot; tot += N[i]; while (tot > k || sp > p) tot -= N[lw], sp -= N[lw] * (i - lw), lw++; int tmp = k - tot; tmp = min(tmp, N[lw - 1]); if (i != lw - 1) tmp = min(tmp, (p - sp) / (i - lw + 1)); ans = max(ans, tot + tmp); } printf("%d\n", ans); } int main() { INIT(); PROC(); return 0; }
4
#include <stdio.h> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <vector> #include <set> #include <string> #include <iostream> typedef long long int ll; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000001 using namespace std; int div_row[8] = {-1,-1,-1,0,0,1,1,1},div_col[8] = {-1,0,1,-1,1,-1,0,1},H,W; int main(){ int work_row,work_col,max_length; char table[10][21]; string ans; set<string> SPELL; while(true){ cin >> H >> W; if(H == 0 && W == 0)break; SPELL.clear(); ans = "0"; for(int i = 0; i < H; i++){ for(int k = 0; k < W; k++)cin >>table[i][k]; } max_length = 2; for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ for(int a = 0; a < 8; a++){ string work; work += table[row][col]; work_row = (row + div_row[a]+H)%H; work_col = (col + div_col[a]+W)%W; while(work_row != row || work_col != col){ work += table[work_row][work_col]; if(SPELL.find(work) != SPELL.end()){ if(work.length() > max_length){ max_length = work.length(); ans = work; }else if(work.length() == max_length){ if(work < ans){ ans = work; } } }else{ SPELL.insert(work); } work_row = (work_row + div_row[a]+H)%H; work_col = (work_col + div_col[a]+W)%W; } } } } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> int a[200000]; int main(void) { int n, m, i, l, r, sum1 = 0, sum2 = 0, t; while (scanf("%d%d", &n, &m) != EOF) { for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] == 1) sum1++; else sum2++; } for (i = 0; i < m; i++) { scanf("%d%d", &l, &r); t = r - l + 1; if (t % 2 == 1) printf("0\n"); else { if (sum1 >= t / 2 && sum2 >= t / 2) printf("1\n"); else printf("0\n"); } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { char a[100]; cin >> a; int i, c = 1; int b = strlen(a); for (i = 0; i < b; i++) { if (a[i] == '1') { if (a[i + 1] == '1') { c++; if (c == 7) break; } else c = 1; } else if (a[i] == '0') { if (a[i + 1] == '0') { c++; if (c == 7) break; } else c = 1; } } if (c == 7) cout << "YES"; else cout << "NO"; return 0; }
1
#include <iostream> #include <bits/stdc++.h> int k,m,n; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int t; scanf("%d",&t); for(int i=0;i<t;i++){ scanf("%d %d %d",&n,&m,&k); if(n*m-1==k){ printf("YES\n"); } else{ printf("NO\n"); } } return 0; }
2