solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; set<int> x; ; for (long long int i = 0; i < n; i++) { int y; cin >> y; x.insert(y); } int count = 0; int temp; while (!x.empty()) { temp = *x.rbegin(); if (temp % 2 == 0) { x.erase(temp); temp /= 2; x.insert(temp); count++; } else { x.erase(temp); } } cout << count << endl; } }
2
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int n; cout << fixed << setprecision(8); while (cin >> n, n) { double sum = 0, Sum = 0, s; for (int i = n; i--;) { cin >> s; sum += s; Sum += s * s; } cout << sqrt(Sum / n - pow(sum / n, 2)) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long n, l, t, al, mn, kmp[1000005] = {0}; char s[1000005], ans[1000005]; template <typename T> inline void read(T &x) { T c = getchar(), f = 0; for (; c < 48 || 57 < c; c = getchar()) if (c == '-') f = 1; for (x = 0; 48 <= c && c <= 57; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); if (f) x = -x; } template <typename T> inline void print(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) print(x / 10); putchar(x % 10 + 48); } signed main() { scanf("%lld", &n); for (register long long i = 1; i <= n; i++) { scanf("%s", s + 1), t = l = strlen(s + 1), mn = min(al, l), kmp[0] = kmp[1] = 0, s[++t] = '?'; for (register long long j = 1; j <= mn; j++) s[++t] = ans[al - mn + j]; for (register long long j = 1, k; j < t; j++) { k = kmp[j]; while (k && s[k + 1] != s[j + 1]) k = kmp[k]; if (s[k + 1] == s[j + 1]) k++; kmp[j + 1] = k; } for (register long long j = kmp[t] + 1; j <= l; j++) ans[++al] = s[j]; } for (register long long i = 1; i <= al; i++) printf("%c", ans[i]); return 0; }
5
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define MOD 1000000007 using namespace std; using ll = long long; int main() { ll n, k, ans = -10000000000; cin >> n >> k; vector<ll> p(n), c(n); rep(i,n) { cin >> p[i]; --p[i]; } rep(i,n) cin >> c[i]; vector<ll> dp(n); for (ll i = 0; i < n; i++) { ll now = i, start = i, count = 0; rep(j,k) { now = p[now]; count++; dp[i] += c[now]; ans = max(ans,dp[i]); if (now == start) { j = k; ll loop = (k - 1) / count; dp[i] = dp[i] * loop; ll kp = k - (count * loop); rep(r,kp) { now = p[now]; dp[i] += c[now]; ans = max(ans,dp[i]); } } } } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main() { ios_base::sync_with_stdio(false), cin.tie(0); cout.precision(10); cout << fixed; int n, d; cin >> n >> d; vector<pair<int, int>> good, bad; for (int i = 1; i <= n; i++) { int s, p; cin >> s >> p; if (s < d) continue; if (s >= p) good.push_back({s, p}); else bad.push_back({s, p}); } sort(good.begin(), good.end()); sort(bad.begin(), bad.end(), [](pair<int, int> a, pair<int, int> b) { return a.second < b.second; }); int neat = 0; int taken = 0; int i = 0; for (int j = 0; j < bad.size(); j++) { if (bad[j].first < neat) continue; while (i < good.size() && good[i].first < bad[j].second) { neat = max(neat, good[i].second); i++; } if (bad[j].first >= neat) { neat = max(neat, bad[j].second); taken++; } } cout << good.size() + taken << "\n"; return 0; }
4
#include <bits/stdc++.h> int main() { int n, i, j, k, l, count = 0; scanf("%d", &n); l = (n / 2) + 1; for (i = 2; i < l; i++) { j = n - i; if (j % i == 0) ++count; } printf("%d\n", count + 1); return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; inline int read() { int x = 0, w = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); } while (c <= '9' && c >= '0') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return w == 1 ? x : -x; } struct node { int a, b; }; inline bool operator<(node a, node b) { if (a.a == b.a) return a.b < b.b; return a.a < b.a; } map<node, int> p; vector<int> mp[200005], ans1, ans2; int a[200005], b[200005], c[200005], ind[200005]; bool vis[200005], vis2[200005]; set<int> s[200005]; queue<int> que; set<int>::iterator it; inline void dfs1(int u) { vis[u] = 1; ans1.push_back(u); for (int i = 0; i < mp[u].size(); i++) { int v = mp[u][i]; if (vis[v]) continue; dfs1(v); } } inline void bfs() { while (!que.empty()) { int u = que.front(); que.pop(); vis2[u] = 1; it = s[u].begin(); int id = *it; ans2.push_back(id); ind[a[id]] -= 2; ind[b[id]] -= 2; ind[c[id]] -= 2; s[a[id]].erase(id); s[b[id]].erase(id); s[c[id]].erase(id); if (ind[a[id]] == 2) que.push(a[id]); if (ind[b[id]] == 2) que.push(b[id]); if (ind[c[id]] == 2) que.push(c[id]); } } int main() { int T = read(); while (T--) { int n = read(); for (int i = 1; i <= n - 2; i++) { a[i] = read(), b[i] = read(), c[i] = read(); if (a[i] > b[i]) swap(a[i], b[i]); if (a[i] > c[i]) swap(a[i], c[i]); if (b[i] > c[i]) swap(b[i], c[i]); p[(node){a[i], b[i]}]++; p[(node){b[i], c[i]}]++; p[(node){a[i], c[i]}]++; } for (int i = 1; i <= n - 2; i++) { if (p[(node){a[i], b[i]}] == 1) mp[a[i]].push_back(b[i]), mp[b[i]].push_back(a[i]); if (p[(node){b[i], c[i]}] == 1) mp[b[i]].push_back(c[i]), mp[c[i]].push_back(b[i]); if (p[(node){a[i], c[i]}] == 1) mp[a[i]].push_back(c[i]), mp[c[i]].push_back(a[i]); } for (int i = 1; i <= n; i++) vis[i] = 0; dfs1(1); for (int i = 0; i < ans1.size(); i++) printf("%d ", ans1[i]); puts(""); for (int i = 1; i <= n - 2; i++) { ind[a[i]] += 2; ind[b[i]] += 2; ind[c[i]] += 2; s[a[i]].insert(i); s[b[i]].insert(i); s[c[i]].insert(i); } for (int i = 1; i <= n; i++) if (ind[i] == 2) que.push(i), vis2[i] = 1; bfs(); for (int i = 0; i < n - 2; i++) printf("%d ", ans2[i]); puts(""); for (int i = 1; i <= n; i++) mp[i].clear(); p.clear(); ans1.clear(); ans2.clear(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, long long int b, long long int mod) { a %= mod; long long int ret = 1; while (b) { if (b % 2 == 1) { ret = (ret * a) % mod; } a = (a * a) % mod; b /= 2; } } long long int mulmod(long long int a, long long int b, long long int mod) { a %= mod; long long int ret = 0; while (b) { if (b % 2 == 1) { ret = (ret + a) % mod; } a = (a + a) % mod; b /= 2; } } long long int col[100005], k[100005]; map<long long int, long long int> cnt; long long int comp(long long int m) { for (int i = 1; i <= m; i++) { if (k[i] != cnt[i]) { return 0; } } return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m, tot = 0; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> col[i]; } for (int i = 1; i <= m; i++) { cin >> k[i]; tot += k[i]; } for (int i = 1; i <= tot; i++) { cnt[col[i]]++; } long long int lef = 1, rig = tot, f = 0; while (rig <= n) { if (comp(m)) { f = 1; break; } cnt[col[lef]]--; lef++; rig++; cnt[col[rig]]++; } if (f) cout << "YES\n"; else cout << "NO\n"; return 0; }
6
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define MAX 15 int jin[MAX][MAX]; int main() { int n; while(cin>>n) { if(n==0)break; memset(jin,0,sizeof(jin)); int x = n/2, y = n/2 +1; int nextx,nexty; jin[x][y] = 1; for(int i=2;i<=n*n;++i) { nextx = (x==(n-1) )? 0:x+1; nexty =(y==n-1)? 0 : y+1; if(jin[nextx][nexty] == 0) { jin[nextx][nexty] = i; x = nextx; y = nexty; } else { nextx = (nextx==(0))? n-1:nextx-1; nexty = (nexty==n-1)? 0:nexty+1; jin[x][nexty] = i; x = nextx; y = nexty; } } for(int j=0;j<n;j++) { for(int i=0;i<n;i++) { printf("%4d",jin[i][j]); } cout << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, reached = 0, visited[57][57]; string grid[57]; int dx[] = {+0, +0, -1, +1}; int dy[] = {-1, +1, +0, +0}; bool is_valid(int x, int y) { if (x >= 0 && x < n && y >= 0 && y < m) return true; return false; } void dfs(int x, int y, int dir, int turn) { if (!visited[x][y]) reached++; visited[x][y] = true; if (turn > 1) { if (dir == 1 && is_valid(x, y - 1) && grid[x][y - 1] == 'B') dfs(x, y - 1, dir, turn); if (dir == 2 && is_valid(x, y + 1) && grid[x][y + 1] == 'B') dfs(x, y + 1, dir, turn); if (dir == 3 && is_valid(x - 1, y) && grid[x - 1][y] == 'B') dfs(x - 1, y, dir, turn); if (dir == 4 && is_valid(x + 1, y) && grid[x + 1][y] == 'B') dfs(x + 1, y, dir, turn); } else { for (int i = 0; i < 4; ++i) { int _x = x + dx[i], _y = y + dy[i], change = (dir != i + 1); if (is_valid(_x, _y) && grid[_x][_y] == 'B') dfs(_x, _y, i + 1, turn + change); } } } int main() { ios_base ::sync_with_stdio(false); int black_cell = 0; cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> grid[i]; for (int j = 0; j < m; ++j) if (grid[i][j] == 'B') black_cell++; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j] == 'B') { memset(visited, 0, sizeof visited); reached = 0; dfs(i, j, 0, 0); if (reached != black_cell) { cout << "NO" << endl; return 0; } } } } cout << "YES" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; bool IsPalindrome(string s) { int n = s.size(); for (int i = 0; i < n - i - 1; i++) { if (s[i] != s[n - i - 1]) { return false; } } return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { string s; cin >> s; int n = s.size(); string l = "", m = "", r = ""; for (int i = 0; i <= n - i - 1 && s[i] == s[n - i - 1]; i++) { if (i == n - i - 1) { l += s[i]; } else { l += s[i]; r += s[i]; } } reverse(r.begin(), r.end()); string t = ""; int st = l.size(); int en = n - (int)r.size(); for (int i = st; i < en; i++) { t += s[i]; if (IsPalindrome(t) && t.size() > m.size()) { m = t; } } t = ""; for (int i = en - 1; i >= st; i--) { t += s[i]; if (IsPalindrome(t) && t.size() > m.size()) { m = t; } } cout << l << m << r << '\n'; } }
4
#include <bits/stdc++.h> using namespace std; int a[100010], f[100010]; int cmp(const int a, const int b) { return a > b; } int main() { int n, s = 0; while (scanf("%d", &n) != -1) { s = 0; memset(f, 0, sizeof(f)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); s += a[i]; } sort(a, a + n, cmp); if (a[n - 1] != 0) printf("-1\n"); else { if (s % 3 == 0) { int first = 1; for (int i = 0; i < n; i++) { if (first && a[i]) first = 0; if (!first) printf("%d", a[i]); } if (first) printf("%d", a[n - 1]); printf("\n"); } else { int r = s % 3, flag = 0; for (int i = n - 1; i >= 0; i--) { if (a[i] % 3 == r) { f[i] = 1; flag = 1; int first = 1; for (int i = 0; i < n; i++) if (!f[i]) { if (first && a[i]) first = 0; if (!first) printf("%d", a[i]); } if (first) printf("0"); printf("\n"); break; } } if (flag == 0) { int cnt = 0; if (r == 1) { for (int i = n - 1; i >= 0; i--) { if (a[i] % 3 == 2) { cnt++; f[i] = 1; } if (cnt == 2) break; } if (cnt == 2) { int first = 1; for (int i = 0; i < n; i++) if (!f[i]) { if (first && a[i]) first = 0; if (!first) printf("%d", a[i]); } if (first) printf("0"); } else printf("0"); printf("\n"); } else { for (int i = n - 1; i >= 0; i--) { if (a[i] % 3 == 1) { cnt++; f[i] = 1; } if (cnt == 2) break; } if (cnt == 2) { int first = 1; for (int i = 0; i < n; i++) if (!f[i]) { if (first && a[i]) first = 0; if (!first) printf("%d", a[i]); } if (first) printf("0"); } else printf("0"); printf("\n"); } } } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007, N = 500005; long long a, b, c, d, e, f, g = 1, h[N], arr[N]; string s; vector<pair<long long, long long>> v; void solve() { cin >> a >> b >> s; v.clear(); long long l = 0, ok = 0, ans = 0; ; for (long long i = 0; i < a; i++) { if (s[i] == 'W') { ans++; if (i > 0 and s[i - 1] == 'W') ans++; } if (s[i] == 'L') l++; else if (ok == 0) { if (l) v.push_back({2, l}); l = 0, ok = 1; } else { if (l) v.push_back({0, l}); l = 0; } } if (ok == 0) { cout << max(0ll, b * 2 - 1) << "\n"; return; } if (l) v.push_back({1, l}); sort(v.begin(), v.end()); for (long long i = 0; i < v.size(); i++) { if (v[i].first == 0) { if (b >= v[i].second) { ans += 2 * v[i].second + 1; b -= v[i].second; } else { ans += 2 * b; break; } } else { ans += 2 * min(b, v[i].second); b -= min(b, v[i].second); } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> g; while (g--) solve(); }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, x, ans = 0; cin >> n >> m >> k; int st[n], id[n]; vector<int> sc[m]; for (int(q) = 0; (q) < (n); (q)++) { cin >> st[q]; } for (int(q) = 0; (q) < (n); (q)++) { cin >> x; id[q] = x - 1; sc[x - 1].push_back(st[q]); } for (int(q) = 0; (q) < (k); (q)++) { cin >> x; if (st[x - 1] != *max_element(sc[id[x - 1]].begin(), sc[id[x - 1]].end())) ans++; } cout << ans << endl; }
1
#include<iostream> #include<vector> #include<bitset> #include<string> #include<cmath> #include<algorithm> #include<stack> #include<list> #include<queue> #include<map> #include<numeric> #include<set> #include<iterator> using namespace std; #define rep(i,x) for(int i=0;i<x;i++) #define re(i,x,y) for(int i=x;i<y;i++) int INF=1e9; const long long mod=1e9+7; typedef long long ll; int main(){ int n; set<int>a,b; cin>>n; while(n--){ int x; cin>>x; a.insert(x); } cin>>n; while(n--){ int x; cin>>x; b.insert(x); } vector<int>c; set_difference(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c)); for(int i=0;i<c.size();i++)cout<<c[i]<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1000050; const long long Mod = 1e9 + 7; const int inf = 0x3f3f3f3f; int n, a[20], l, r, x; int main(int argc, const char* argv[]) { scanf("%d%d%d%d", &n, &l, &r, &x); for (int i = 0; i < ((n - 1) + 1); i++) scanf("%d", &a[i]); int ans = 0; for (int i = 0; i < (((1 << n) - 1) + 1); i++) { int maxi = -0x3f3f3f3f, mini = 0x3f3f3f3f, tot = 0; for (int j = 0; j < ((n - 1) + 1); j++) if (i & (1 << j)) { tot += a[j]; mini = min(mini, a[j]); maxi = max(maxi, a[j]); } if (tot >= l && tot <= r && (maxi - mini) >= x) ans++; } printf("%d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; if (n % 2 == k % 2 && n >= k) { cout << "YES" << endl; for (int i = 1; i < k; i++) { cout << "1 "; } cout << (n - k + 1) << endl; } else if (n % 2 == 0 && k % 2 == 1 && n >= 2 * k) { cout << "YES" << endl; for (int i = 1; i < k; i++) { cout << "2 "; } cout << (n - 2 * (k - 1)) << endl; } else { cout << "NO" << endl; } } }
2
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void solve() { long long m; vector<long long> arr(3); cin >> arr[0] >> arr[1] >> arr[2] >> m; sort(arr.begin(), arr.end()); long long total_pairs = arr[0] + arr[1] + arr[2] - 3; long long min_pairs = arr[2] - arr[1] - arr[0] - 1; if (m >= min_pairs && m <= total_pairs) { cout << "YES" << "\n"; } else cout << "NO" << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long t; cin >> t; while (t--) solve(); }
2
#include <bits/stdc++.h> using namespace std; const int N = 52; pair<long long, long long> dp[N][N][4 * N]; bool vis[N][N][4 * N]; int n, k, a, b; long long c[N][N]; void initFact() { for (int i = (0); i <= (int)(N - 1); ++i) { c[i][0] = c[i][i] = 1; for (int j = (1); j <= (int)(i - 1); ++j) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % 1000000007; } } pair<long long, long long> solve(int first, int h, int turn) { if ((turn & 1) && first == a && h == b) return {turn, 1}; if (turn >= 4 * N - 5) return {1000000000ll, 0}; pair<long long, long long> &ret = dp[first][h][turn]; if (vis[first][h][turn]) return ret; vis[first][h][turn] = true; ret = {1000000000ll, 0}; for (int i = (0); i <= (int)(first); ++i) for (int j = (0); j <= (int)(h); ++j) if ((i || j) && (i + 2 * j <= k)) { pair<long long, long long> res = solve(a - first + i, b - h + j, turn + 1); res.second = res.second * c[first][i] % 1000000007 * c[h][j] % 1000000007; if (ret.first > res.first) ret = res; else if (ret.first == res.first && res.first != 1000000000ll) ret.second = (ret.second + res.second) % 1000000007; } return ret; } int main() { initFact(); scanf("%d%d", &n, &k), k /= 50; for (int i = (1); i <= (int)(n); ++i) { int x; scanf("%d", &x); if (x == 50) ++a; else ++b; } pair<long long, long long> ans = solve(a, b, 0); if (!ans.second) printf("-1\n0\n"); else printf("%lld\n%lld\n", ans.first, ans.second); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int a; while (scanf("%d", &a) != EOF) { long long ans(1), dx(3); for (int i = 2; i <= a; ++i) { ans += 6 * dx - 6; dx += 2; } printf("%I64d\n", ans); } }
2
#include <iostream> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { double x1, y1, x2, y2, x3, y3, x4, y4; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; double a = x2 - x1, b = y2 - y1, c = x4 - x3, d = y4 - y3; if (a * d - b * c == 0.0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; n *= 2; int a[301]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); bool res = (a[n / 2 - 1] != a[n / 2]); if (res) cout << "YES"; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 500 + 5; char mp[maxn][maxn]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf(" %c", &mp[i][j]); int x1, y1, x2, y2; for (int i = 0; i < n; i++) { bool bb = false; for (int j = 0; j < m; j++) if (mp[i][j] == 'X') { x1 = i; y1 = j; bb = true; break; } if (bb) break; } for (int i = n - 1; i >= 0; i--) { bool bb = false; for (int j = m - 1; j >= 0; j--) { if (mp[i][j] == 'X') { x2 = i; y2 = j; bb = true; break; } } if (bb) break; } if (x1 > x2 || y1 > y2) { puts("NO"); return 0; } for (int i = x1; i <= x2; i++) for (int j = y1; j <= y2; j++) { if (mp[i][j] == '.') { puts("NO"); return 0; } mp[i][j] = '.'; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (mp[i][j] == 'X') { puts("NO"); return 0; } puts("YES"); return 0; }
2
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) typedef long long ll; typedef pair<int, int> P; #define MAX 100005 #define INF 1001001001 void out(vector<int> a) { int n = a.size(); rep(i,n) { cout << a[i]; if (i==n-1) cout << endl; else cout << " "; } } int main(int, char**) { int n; cin >> n; vector<int> a(n); rep(i,n) cin >> a[i]; if(prev_permutation(a.begin(), a.end())) out(a); next_permutation(a.begin(), a.end()); out(a); if(next_permutation(a.begin(), a.end())) out(a); return 0; }
0
#include<iostream> #include<string> using namespace std; int main() { int n; string b[10] = { "0111111","0000110","1011011","1001111", "1100110","1101101","1111101","0100111","1111111","1101111" }; while (cin >> n&&n != -1) { string c = "0000000"; for (int z = 0; z < n; z++) { int a; cin >> a; string d = ""; for (int i = 0; i < 7; i++) { if (b[a][i] != c[i]) d += "1"; else d += "0"; } c = b[a]; cout << d << endl; } } }
0
#include <bits/stdc++.h> using namespace std; const int inf = (int)2 * 1e9; const int MOD = (int)1e9 + 7; const int p = 239; const int MAX_N = 1000000; const long double pi = 3.1415926; const long double eps = 1e-6; int main() { int n; cin >> n; string s; cin >> s; int cnt1 = 0; for (int i = 0; i < n; i++) if (s[i] == 'H') cnt1++; int ans1 = inf; for (int i = 0; i <= cnt1; i++) { int curr = 0; for (int j = 0; j < i; j++) if (s[j] == 'T') curr++; for (int j = i; j < n - cnt1 + i; j++) if (s[j] == 'H') curr++; for (int j = n - cnt1 + i; j < n; j++) if (s[j] == 'T') curr++; ans1 = min(ans1, curr / 2); } int cnt2 = 0; for (int i = 0; i < n; i++) if (s[i] == 'T') cnt2++; int ans2 = inf; for (int i = 0; i <= cnt2; i++) { int curr = 0; for (int j = 0; j < i; j++) if (s[j] == 'H') curr++; for (int j = i; j < n - cnt2 + i; j++) if (s[j] == 'T') curr++; for (int j = n - cnt2 + i; j < n; j++) if (s[j] == 'H') curr++; ans2 = min(ans2, curr / 2); } cout << min(ans1, ans2); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxN = 100100; struct Tnode { int id, h, t, size; Tnode *son[2], *f; } tree[2 * maxN], *root, *nul; struct Person { int t, s, f, id; } p[maxN]; long long ans[maxN]; int trees, n, m; Tnode *newnode(int id, int h, int t) { trees++; tree[trees].id = id; tree[trees].h = h; tree[trees].t = t; tree[trees].son[0] = tree[trees].son[1] = tree[trees].f = nul; tree[trees].size = 1; return tree + trees; } bool cmp(Person a, Person b) { return a.t < b.t; } void Readln() { scanf("%d%d\n", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d%d%d\n", &p[i].t, &p[i].s, &p[i].f); p[i].id = i; } sort(p + 1, p + 1 + n, cmp); } void updata(Tnode *u) { u->size = 1; if (u->son[0] != nul) u->size += u->son[0]->size; if (u->son[1] != nul) u->size += u->son[1]->size; } void rot(Tnode *u, int t) { Tnode *v = u->f, *w = v->f; if (w != nul) { if (w->son[0] == v) w->son[0] = u; if (w->son[1] == v) w->son[1] = u; } u->f = w; v->son[t ^ 1] = u->son[t]; if (u->son[t] != nul) u->son[t]->f = v; u->son[t] = v; v->f = u; updata(v); } void splay(Tnode *u, Tnode *fa) { while (u->f != fa && u->f != nul) { Tnode *v = u->f; Tnode *w = v->f; if (w == fa) { rot(u, v->son[0] == u); break; } bool p = (v->son[0] == u), q = (w->son[0] == v); if (p ^ q) { rot(u, p); rot(u, q); } else { rot(v, q); rot(u, p); } } updata(u); if (fa == nul) root = u; } Tnode *findpred(int h) { Tnode *now = root, *ret = nul; while (now != nul) { if (now->h <= h) { ret = now; now = now->son[1]; } else now = now->son[0]; } return ret; } Tnode *findsucc(int h) { Tnode *now = root, *ret = nul; while (now != nul) { if (now->h >= h) { ret = now; now = now->son[0]; } else now = now->son[1]; } return ret; } void inserttree(int id, int h, int t) { if (root == nul) { root = newnode(id, h, t); return; } Tnode *pre = findpred(h); Tnode *suc = findsucc(h + 1); if (pre == nul) { splay(suc, nul); root->son[0] = newnode(id, h, t); root->son[0]->f = root; updata(root); return; } if (suc == nul) { splay(pre, nul); root->son[1] = newnode(id, h, t); root->son[1]->f = root; updata(root); return; } splay(pre, nul); splay(suc, pre); root->son[1]->son[0] = newnode(id, h, t); root->son[1]->son[0]->f = root->son[1]; updata(root->son[1]); updata(root); } int asktree(int h) { if (root == nul) return 2; Tnode *pre = findpred(h); if (pre == nul) return 1; if (pre->h == h) return 1; splay(pre, nul); return (2 * root->son[1]->size - root->size >= 0); } void deletetree(Tnode *u) { splay(u, nul); Tnode *root1 = root->son[0]; Tnode *root2 = root->son[1]; if (root1 == nul && root2 == nul) { root = nul; return; } root1->f = root2->f = nul; if (root1 == nul) { root = root2; return; } if (root2 == nul) { root = root1; return; } root = root1; Tnode *tmp = findpred(m + 1); splay(tmp, nul); root->son[1] = root2; root2->f = root; updata(root); } void Solve() { int nowh = 1; nul = newnode(0, 0, 0); root = nul; nul->size = 0; for (int i = 1; i <= n; i++) { long long nowt = p[i].t; inserttree(i, p[i].s, 0); for (;;) { int t = asktree(nowh); if (t == 2) break; long long ttt = (long long)p[i + 1].t - nowt; if (t == 1) { Tnode *nex = findsucc(nowh); if (nex->h - nowh <= ttt || i == n) { deletetree(nex); nowt += nex->h - nowh; if (nex->t == 1) ans[p[nex->id].id] = nowt; else inserttree(nex->id, p[nex->id].f, 1); nowh = nex->h; } else { nowh += ttt; break; } } else { Tnode *nex = findpred(nowh); if (nowh - nex->h <= ttt || i == n) { deletetree(nex); nowt += nowh - nex->h; if (nex->t == 1) ans[p[nex->id].id] = nowt; else inserttree(nex->id, p[nex->id].f, 1); nowh = nex->h; } else { nowh -= ttt; break; } } } } for (int i = 1; i <= n; i++) printf("%I64d\n", ans[i]); } int main() { Readln(); Solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; bool arr[1000000]; void getprim() { arr[0] = 1; arr[1] = 1; int i; long long j; for (i = 2; i < 1000000; i++) { if (arr[i] == 0) { for (j = i, j = j * i; j < 1000000; j += i) { arr[j] = 1; } } } } int n, v[1005], i, j, p, k; int main() { int i, j, arr[4], cot, ans = 0, a, b; scanf("%d %d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3], &a, &b); for (i = a; i <= b; i++) { int num = 0; for (cot = 0; cot < 24; cot++) { next_permutation(arr, arr + 4); if ((((i % arr[0]) % arr[1]) % arr[2]) % arr[3] == i) num++; } if (num >= 7) ans++; } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, ai; cin >> n; vector<int> c(5, 0); for (int i = 0; i < n; ++i) { cin >> ai; ++c[ai]; } int answer = 0; int m12 = min(c[1], c[2]); c[0] += m12; c[1] -= m12; c[2] -= m12; c[3] += m12; answer += m12; if (c[2]) { int m23 = c[2] / 3; c[0] += m23; c[2] -= 3 * m23; c[3] += 2 * m23; answer += 2 * m23; if (c[2] == 2) { c[0] += 1; c[2] -= 2; c[4] += 1; answer += 2; } if (c[2] && c[4]) { c[2] -= 1; c[3] += 2; c[4] -= 1; answer += 1; } if (c[2] && c[3] >= 2) { c[0] += 1; c[2] -= 1; c[3] -= 2; c[4] += 1; answer += 2; } } if (c[1]) { int m13 = c[1] / 3; c[0] += 2 * m13; c[1] -= 3 * m13; c[3] += m13; answer += 2 * m13; if (c[1] == 2 && c[4]) { c[0] += 2; c[1] -= 2; c[3] += 1; c[4] -= 1; answer += 2; } if (c[1] == 2 && c[3] >= 2) { c[0] += 2; c[1] -= 2; c[3] -= 2; c[4] += 2; answer += 2; } if (c[1] && c[3]) { c[0] += 1; c[1] -= 1; c[3] -= 1; c[4] += 1; answer += 1; } if (c[1] && c[4] >= 2) { c[1] -= 1; c[3] += 3; c[4] -= 2; answer += 2; } } if (!c[1] && !c[2]) { cout << answer; } else { cout << -1; } return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> adjList[200005]; vector<pair<int, bool>> intervals; int visited[200005]; void bfs(int source) { queue<int> q; int minimo = source; int maximo = source; q.push(source); visited[source] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adjList[u]) { if (!visited[v]) { visited[v] = 1; minimo = min(v, minimo); maximo = max(v, maximo); q.push(v); } } } intervals.push_back({minimo, false}); intervals.push_back({maximo, true}); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adjList[u].push_back(v); adjList[v].push_back(u); } for (int i = 0; i < n; i++) { if (!visited[i]) { bfs(i); } } sort(intervals.begin(), intervals.end()); int sum = 0; int cnt = 0; int ans = 0; for (auto p : intervals) { if (p.second) { sum--; if (sum == 0) { ans += cnt - 1; cnt = 0; } } else { sum++; cnt++; } } cout << ans << "\n"; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, q; struct lb { int val[65], sz; lb() { sz = 0; } void gauss() { int j = 31; for (register int i = 1; i <= sz; ++i) { int x = 0; do { for (register int z = i; z <= sz; ++z) if ((val[z] >> j) & 1) { x = z; break; } } while (x == 0 && j--); if (j < 0) break; swap(val[x], val[i]); for (register int w = i + 1; w <= sz; ++w) { if ((val[w] >> j) & 1) val[w] ^= val[i]; } } while (sz && val[sz] == 0) --sz; } void upd(int x) { for (register int i = 1; i <= sz; ++i) if (val[i] & 1) val[i] ^= x << 1; } void addnum(int x) { val[++sz] = x << 1 | 1; gauss(); } long long cnt() { return 1LL << (sz); } }; void merge(lb &a, lb b) { for (register int i = 1; i <= b.sz; ++i) a.val[++a.sz] = b.val[i]; a.gauss(); } int a[N]; struct segment_tree { int tag[N * 4]; lb v[N * 4]; void push_up(int x) { v[x] = v[x << 1]; merge(v[x], v[x << 1 | 1]); } void build(int x, int l, int r) { if (l == r) { v[x].addnum(a[l]); return; } build(x << 1, l, (l + r) / 2); build(x << 1 | 1, (l + r) / 2 + 1, r); push_up(x); } void push_down(int x) { if (!tag[x]) return; v[x << 1].upd(tag[x]); v[x << 1 | 1].upd(tag[x]); tag[x << 1] ^= tag[x]; tag[x << 1 | 1] ^= tag[x]; tag[x] = 0; } inline void update(int x, int l, int r, int ql, int qr, int z) { if (ql > r || qr < l) return; if (ql <= l && r <= qr) { tag[x] ^= z; v[x].upd(z); return; } push_down(x); update(x << 1, l, (l + r) / 2, ql, qr, z); update(x << 1 | 1, (l + r) / 2 + 1, r, ql, qr, z); push_up(x); } lb query(int x, int l, int r, int ql, int qr) { if (ql > r || qr < l) return lb(); if (ql <= l && r <= qr) return v[x]; push_down(x); lb res; merge(res, query(x << 1, l, (l + r) / 2, ql, qr)); merge(res, query(x << 1 | 1, (l + r) / 2 + 1, r, ql, qr)); return res; } } s; int main() { scanf("%d%d", &n, &q); for (register int i = 1; i <= n; ++i) scanf("%d", a + i); s.build(1, 1, n); for (register int i = 1; i <= q; ++i) { int id; scanf("%d", &id); if (id == 1) { int l, r, x; scanf("%d%d%d", &l, &r, &x); s.update(1, 1, n, l, r, x); } else { int l, r; scanf("%d%d", &l, &r); lb w = s.query(1, 1, n, l, r); for (register int i = 1; i <= w.sz; ++i) w.val[i] /= 2; w.gauss(); printf("%lld\n", w.cnt()); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 100; const long double pi = acos(-1.0); inline int read() { int 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; } inline long long readll() { long long x = 0; int 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 main() { int T = read(); while (T--) { int n = read(); printf("%d\n", n / 2); } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct mpoint { int x; int y; int s; int v; } mp[200005]; struct point { int x; int y; int v; } p[200005]; bool c(point a, point b) { if (a.x == b.x) return a.y < b.y; else return a.x < b.x; } bool c1(mpoint a, mpoint b) { if (a.x == b.x) return a.y < b.y; else return a.x < b.x; } bool c2(mpoint a, mpoint b) { return a.s < b.s; } bool c3(mpoint a, mpoint b) { if (a.v == b.v) return a.s < b.s; else return a.v < b.v; } bool c4(point a, point b) { if (a.v == b.v) { if (a.x == b.x) return a.y < b.y; else return a.x < b.x; } else return a.v < b.v; } int main() { int i, j, k, l, n; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &p[i].x, &p[i].y); p[i].v = p[i].y - p[i].x; } for (i = 0; i < n; i++) { scanf("%d", &mp[i].v); mp[i].s = i; } sort(p, p + n, c4); sort(mp, mp + n, c3); int f1 = 0; for (i = 0; i < n; i++) { if (p[i].v == mp[i].v) { mp[i].x = p[i].x; mp[i].y = p[i].y; } else { f1 = 1; break; } } int f2 = 0; sort(mp, mp + n, c1); int tempx = mp[0].x; int tempy = mp[0].y; int temps = mp[0].s; for (i = 1; i < n; i++) { if (tempx <= mp[i].x && tempy <= mp[i].y) { if (temps > mp[i].s) { f2 = 1; break; } else { tempx = mp[i].x; tempy = mp[i].y; temps = mp[i].s; } } } sort(mp, mp + n, c2); if (f1 || f2) printf("NO\n"); else { printf("YES\n"); for (int i = 0; i < n; i++) printf("%d %d\n", mp[i].x, mp[i].y); } }
3
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void omae_wa_mou_shindeiru(int tc) { int n; cin >> n; string s; cin >> s; vector<bool> check(n, 1); vector<vector<char>> v(n, vector<char>(n, '#')); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (i == j) v[i][j] = 'X'; if (v[i][j] != '#') continue; else if (s[i] == '2' and s[j] == '2') { if (check[i]) { v[i][j] = '+'; v[j][i] = '-'; check[i] = 0; } else { v[i][j] = '-'; v[j][i] = '+'; check[j] = 0; } } else if (s[i] == '2' and s[j] == '1') { v[i][j] = '='; v[j][i] = '='; } else if (s[i] == '1' and s[j] == '2') { v[i][j] = '='; v[j][i] = '='; } else { v[i][j] = '='; v[j][i] = '='; } } if (s[i] == '2' and check[i]) { cout << "NO" << "\n"; return; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s[i] == '1' and v[i][j] == '-') { cout << "NO" << "\n"; return; } } } cout << "YES" << "\n"; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << v[i][j]; } cout << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tc = 1; cin >> tc; for (int i = 1; i < tc + 1; i++) { omae_wa_mou_shindeiru(i); } }
2
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { cin >> n; string s1(n, '0'); string s2(n, '0'); char x; bool b = false; for (int i = 0; i < n; i++) { cin >> x; if (x == '2') { if (b) { s1[i] = '0'; s2[i] = '2'; } else { s1[i] = '1'; s2[i] = '1'; } } else if (x == '1') { if (b) { s1[i] = '0'; s2[i] = '1'; } else { s1[i] = '1'; s2[i] = '0'; } b = true; } } cout << s1 << endl; cout << s2 << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int w, h, u1, d1, u2, d2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> w >> h >> u1 >> d1 >> u2 >> d2; for (int i = h; i >= 0; i--) { w += i; if (i == d1) { w -= u1; } else if (i == d2) { w -= u2; } if (w < 0) w = 0; } cout << w << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; char s[200010], t[200010]; int main() { int n, m; scanf("%d%d", &n, &m); scanf("%s %s", s, t); int idx = -1; for (int i = 0; i < n; i++) if (s[i] == '*') idx = i; if (idx == -1) { if (n != m) return 0 * puts("NO"); for (int i = 0; i < n; i++) if (s[i] != t[i]) return 0 * puts("NO"); return 0 * puts("YES"); } if (m < n - 1) return 0 * puts("NO"); for (int i = 0; i < idx; i++) if (s[i] != t[i]) return 0 * puts("NO"); for (int i = n - 1; i > idx; i--) if (s[i] != t[i + m - n]) return 0 * puts("NO"); puts("YES"); return 0; }
1
#include <bits/stdc++.h> using namespace std; inline void writeln2() { cout << "\n"; } inline void writeln() { cout << "\n"; } inline void readln() {} template <typename T> inline void read(T&); template <typename T> inline void priws(T); template <typename T> inline void print(T); void err(vector<string>::iterator it) { ++it; } template <typename Head, typename... Tail> inline void readln(Head& head, Tail&... tail) { read(head); readln(tail...); } template <typename Head, typename... Tail> inline void writeln2(Head head, Tail... tail) { print(head); writeln2(tail...); } template <typename Head, typename... Tail> inline void writeln(Head head, Tail... tail) { priws(head); writeln2(tail...); } template <typename T> inline void writeln_range(T f, T s) { if (f != s) for (auto i = f; i != s; ++i) writeln(*i); } template <typename Head, typename... Tail> inline void err(vector<string>::iterator it, Head head, Tail... tail) { writeln((*it).substr((*it)[0] == ' '), "=", head); err(++it, tail...); } vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.push_back(x); return move(v); } void run() { int n, T; double c; readln(n, T, c); vector<int> a(n); readln(a); vector<int> p; readln(p); int m = p.size(); int j = 0; double real = 0; double app = 0; long long sum = 0; cout.precision(8); for (int i = 0; i < (int)(n); ++i) { if (j == m) break; sum += a[i]; if (i >= T) sum -= a[i - T]; real = (0.0 + sum) / T; app = (app + (0.0 + a[i]) / T) / c; if (p[j] == i + 1) j++, cout << fixed << real << " " << app << " " << fabs(app - real) / real << "\n"; } } int main() { ios_base::sync_with_stdio(false); run(); return 0; } template <typename T> inline ostream& operator<<(ostream& os, vector<T>& _a); template <typename T1, typename T2> inline istream& operator>>(istream& is, pair<T1, T2>& _a) { return is >> _a.first >> _a.second; } template <typename T1, typename T2> inline ostream& operator<<(ostream& os, pair<T1, T2>& _a) { return os << _a.first << " " << _a.second; } template <typename T> inline ostream& operator<<(ostream& os, vector<T>& _a) { if (_a.size()) os << _a[0]; else os << "\n"; for (int i = 1; i < _a.size(); ++i) os << "\n "[is_fundamental<T>::value] << _a[i]; return os; } template <typename T> inline istream& operator>>(istream& is, vector<T>& _a) { if (_a.size() == 0) { int _n; is >> _n; _a.resize(_n); } for (int i = 0; i < _a.size(); ++i) is >> _a[i]; return is; } template <typename T> inline void print(T _a) { cout << " " << _a; } template <typename T> inline void priws(T _a) { cout << _a; } template <typename T> inline void read(T& _a) { cin >> _a; }
2
#include <bits/stdc++.h> using namespace std; vector<int> graph[2001]; vector<int> level[2001]; int visited[2001]; int a, b; void dfs(int u, int depth) { visited[u] = 1; level[depth].push_back(u); for (int v : graph[u]) { if (!visited[v]) { dfs(v, depth + 1); } } } int query = 0; void ask(vector<int>& v) { cout << "? " << v.size() << " "; for (int x : v) { cout << x << " "; } cout << "\n" << flush; cin >> a >> b; } void bs(int l, int r, int ref) { int curr_node = 0, curr_level = 0; while (l <= r) { if (r - l <= 1) { ask(level[r]); if (b == ref) { curr_level = r; curr_node = a; } else { ask(level[l]); curr_node = a; curr_level = l; } break; } int mid = (l + r) / 2; ask(level[mid]); if (b == ref) { l = mid; } else { r = mid - 1; } } for (int i = 0; i <= 2000; i++) { visited[i] = 0; level[i].clear(); } dfs(curr_node, 1); ask(level[ref + 1]); cout << "! " << curr_node << " " << a << "\n" << flush; string s; cin >> s; } int main() { int t; cin >> t; while (t--) { query = 0; int n, x, y; cin >> n; for (int i = 0; i <= 2000; i++) { graph[i].clear(); level[i].clear(); visited[i] = 0; } for (int i = 0; i < n - 1; i++) { cin >> x >> y; graph[x].push_back(y); graph[y].push_back(x); } cout << "? " << n << " "; for (int i = 1; i <= n; i++) { cout << i << " "; } cout << "\n" << flush; cin >> x >> y; dfs(x, 1); int len = 0; for (int i = n; i >= 1; i--) { if (level[i].size() != 0) { len = i; break; } } bs(1, len, y); } }
6
#include <algorithm> #include <iostream> #include <cstdio> #include <vector> #define N 1000 using namespace std; int n,M[N],D[N],h[N],m[N],p[N],f,f2,s,sum,ans; char e[N]; vector<int> v[N]; int Getminute(int a,int b){ return (h[b]*60+m[b])-(h[a]*60+m[a]); } int main(){ while(1){ cin>>n; if(!n) break; for(int i=0;i<n;i++){ scanf("%d/%d %d:%d %c %d",&M[i],&D[i],&h[i],&m[i],&e[i],&p[i]); if(!p[i]) for(int j=0;j<N;j++) v[j].push_back(i); else v[p[i]].push_back(i); } ans=0; for(int i=0;i<N;i++){ s=f=f2=sum=0; for(int j=0;j<v[i].size();j++){ int x=v[i][j]; if(!p[x]&&e[x]=='I') f=1,s=x; if(!p[x]&&e[x]=='O'){ if(f2) sum+=Getminute(s,x); f=0; } if(p[x]&&e[x]=='I') f2=1,s=x; if(p[x]&&e[x]=='O'){ if(f) sum+=Getminute(s,x); f2=0; } } ans=max(ans,sum); } cout<<ans<<endl; for(int i=0;i<N;i++) v[i].clear(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; map<char, int> id; id['a'] = 0; id['e'] = 1; id['i'] = 2; id['o'] = 3; id['u'] = 4; string input[n]; int freq[n]; char LastChar[n]; map<pair<int, int>, vector<int>> m; string second; int num; char last; for (int i = 0; i < n; i++) { cin >> second; input[i] = second; num = 0; for (int j = 0; j < second.length(); j++) { if (second[j] == 'a' || second[j] == 'e' || second[j] == 'i' || second[j] == 'o' || second[j] == 'u') { num++; last = second[j]; } } freq[i] = num; LastChar[i] = last; m[make_pair(num, last)].push_back(i); } queue<int> sec; queue<int> fir; for (auto i : m) { if (i.second.size() % 2 == 1) { fir.push(i.second[0]); for (int j = 1; j < i.second.size(); j++) { sec.push(i.second[j]); } } else { for (int j = 0; j < i.second.size(); j++) { sec.push(i.second[j]); } } } vector<string> output; int a, b, c, d; while (!sec.empty() && !fir.empty()) { a = fir.front(); fir.pop(); b = -1; while (!fir.empty()) { if (freq[fir.front()] == freq[a]) { b = fir.front(); fir.pop(); break; } else { a = fir.front(); fir.pop(); } } if (b == -1) break; c = sec.front(); sec.pop(); d = sec.front(); sec.pop(); output.push_back(input[a] + ' ' + input[c] + '\n' + input[b] + ' ' + input[d]); } while (sec.size() >= 4) { a = sec.front(); sec.pop(); b = sec.front(); sec.pop(); c = sec.front(); sec.pop(); d = sec.front(); sec.pop(); output.push_back(input[a] + ' ' + input[c] + '\n' + input[b] + ' ' + input[d]); } cout << output.size() << endl; for (int i = 0; i < output.size(); i++) { cout << output[i] << endl; } }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int pre[maxn], a[maxn]; int Find(int x) { return x == pre[x] ? x : pre[x] = Find(pre[x]); } void Union(int x, int y) { int fx = Find(x), fy = Find(y); if (fx != fy) pre[fx] = fy; } int main() { int n, root = -1, cnt = 0; scanf("%d", &n); bool flag = false; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); pre[i] = i; if (i == a[i]) root = a[i], flag = true; } for (int i = 1; i <= n; i++) { if (a[i] == i) Union(i, root), a[i] = root, cnt++; else if (Find(i) != Find(a[i])) Union(i, a[i]); else { if (root == -1) root = i; Union(i, root), a[i] = root, cnt++; } } printf("%d\n", cnt - flag); for (int i = 1; i <= n; i++) printf("%d ", a[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; string s; int n, i, k, a, b, c, d; int main() { cin >> a >> b >> c >> d; if (a > b && a > c && a > d) cout << a - b << ' ' << a - c << ' ' << a - d; if (b > a && b > c && b > d) cout << b - a << ' ' << b - c << ' ' << b - d; if (c > b && c > a && c > d) cout << c - b << ' ' << c - a << ' ' << c - d; if (d > b && d > c && d > a) cout << d - b << ' ' << d - c << ' ' << d - a; }
1
#include <bits/stdc++.h> int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }; int n, m; int main() { scanf("%d%d", &n, &m); printf("%d\n", gcd(n - 1, m - 1) + 1); };
3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, m, q, ans; int w[N]; void solve() { cin >> n >> m; memset(w, 0, 4 * n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; if (u > v) swap(u, v); if (!w[u]) w[0]++; w[u]++; } cin >> q; for (int i = 0; i < q; i++) { int tp, u, v; cin >> tp; if (tp == 1) { cin >> u >> v; if (u > v) swap(u, v); if (!w[u]) w[0]++; w[u]++; } else if (tp == 2) { cin >> u >> v; if (u > v) swap(u, v); w[u]--; if (!w[u]) w[0]--; } else if (tp == 3) { ans = n - w[0]; cout << ans << endl; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int _ = 1; while (_--) solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int sz[200010]; int vis[200010]; int p[200010]; vector<int> son[200010]; void del(int x) { vis[x] = 1; cout << x << endl; for (int i = 0; i < son[x].size(); i++) if (!vis[son[x][i]]) del(son[x][i]); } void dfs(int x) { sz[x] = 1; for (int i = 0; i < son[x].size(); i++) dfs(son[x][i]), sz[x] += sz[son[x][i]]; if (!(sz[x] & 1)) del(x); } int main() { int n; int root; cin >> n; if (n & 1) cout << "YES" << endl; else return cout << "NO" << endl, 0; for (int i = 1; i <= n; i++) { cin >> p[i]; if (p[i]) son[p[i]].push_back(i); else root = i; } dfs(root); del(root); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int k, n, w; cin >> k >> n >> w; int money = (w * (w + 1) / 2) * k; if (money <= n) cout << 0; else cout << money - n; return 0; }
1
#include <bits/stdc++.h> using namespace std; set<int> s; vector<int> v; map<int, int> us; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n, k = 0, q = 0; cin >> n; int a[n + 10]; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] % 2 == 0) k++; else q++; } if (k % 2 != q % 2) { cout << "NO\n"; continue; } else if (k % 2 == 0) { cout << "YES\n"; continue; } else { long long f = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] % 2 != a[j] % 2 && abs(a[i] - a[j]) == 1) { f = 1; cout << "YES\n"; break; } } if (f) break; } if (f) continue; } cout << "NO\n"; } exit(0); }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const double eps = 0.0000000001; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int m; cin >> m; vector<int> b(m), lhs, rhs; for (int i = 0; i < m; ++i) { cin >> b[i]; } sort(b.begin(), b.end(), [](int a, int b) { return a > b; }); rhs.push_back(b[0]); int counter = 1; for (int i = 1; i < m; ++i) { if (b[i] == b[i - 1]) { if (counter == 1) { counter++; lhs.push_back(b[i]); } else { continue; } } else { counter = 1; rhs.push_back(b[i]); } } bool eq; if (lhs.size()) { eq = (rhs[0] == lhs[0]); } else { eq = 0; } cout << (lhs.size() + rhs.size() - eq) << endl; for (int i = lhs.size() - 1; i >= eq; --i) { cout << lhs[i] << ' '; } for (size_t i = 0; i < rhs.size(); ++i) { cout << rhs[i] << ' '; } }
2
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { string s; cin >> s; long long arr[4] = {0}; arr[1] = -1; arr[2] = -1; arr[3] = -1; bool ar[3] = {0}; long long c = 99999999; for (long long i = 0; i < s.size(); i++) { if (s[i] == '1' || s[i] == '2' || s[i] == '3') { arr[s[i] - '0'] = i; ar[s[i] - '0' - 1] = 1; } if ((ar[0] && ar[1] && ar[2])) { long long m = -1; long long n = 99999999; long long k = 0; for (long long i = 1; i < 4; i++) { if (arr[i] > m) m = arr[i]; if (arr[i] < n) n = arr[i]; } if (arr[1] == -1 || arr[2] == -1 || arr[3] == -1) k = 0; else { k = (m - n + 1); } if (k < c) c = k; if (k > 0) ar[s[n] - '0' - 1] = 0; } } if (c == 99999999) cout << 0 << endl; else cout << c << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; string top; string bot; cin >> top >> bot; long long ans = 0; for (long long i = 0; i < n; i++) { if (bot[i] == '1') { if (i - 1 >= 0 && top[i - 1] == '1') { ans += 1; top[i - 1] = 0; continue; } if (top[i] == '0') { ans++; continue; } if (i + 1 < n && top[i + 1] == '1') { ans += 1; top[i + 1] = 0; } } } cout << ans << "\n"; ; } int32_t main() { long long t; cin >> t; while (t--) { solve(); } }
2
#include <bits/stdc++.h> #define ri register #define int long long #define E (n+1) using namespace std; const int N=500010, Mod=1e9+7; inline int read() { int s=0, w=1; ri char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar(); return s*w; } int h,w; multiset<int> Q; map<int,int> f; #define gg map<int,int>::iterator signed main() { h=read(), w=read(); for(ri int i=0;i<w;i++) f[i]=i, Q.insert(0); for(ri int i=0;i<h;i++) { int L,R; L=read(), R=read(); gg qwq=f.lower_bound(L-1); int pc=-1; while(qwq!=f.end()&&(qwq->first)<=R) { pc=max(pc,qwq->second); Q.erase(Q.find(qwq->first-qwq->second)); f.erase(qwq++); } if((~pc) && R^w) { Q.insert(R-pc); f[R]=pc; } if(Q.empty()) puts("-1"); else printf("%lld\n",i+1+(*Q.begin())); } return 0; }
0
#include <bits/stdc++.h> unsigned GCD(unsigned u, unsigned v) { while (v != 0) { unsigned r = u % v; u = v; v = r; } return u; } int D[101000]; int main() { int N; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &D[i]); } int x = GCD(D[0], D[1]), i; if (x != 1) { for (i = 2; i < N; i++) { x = GCD(x, D[i]); if (x == 1) break; } if (i == N) { printf("YES\n0\n"); return 0; } } int cnt = 0; for (i = 0; i < N - 1; i++) { if (D[i] % 2 == 0) continue; if (D[i + 1] % 2 == 1) { cnt++; D[i + 1] = 2; continue; } cnt += 2; D[i + 1] = 2; } if (D[N - 1] % 2 == 1) { cnt += 2; } printf("YES\n%d\n", cnt); return 0; }
3
#include<iostream> using namespace std; class Goods{ public: int left; int right; int top; int bottom; char material; void check(int h, int w); }; void Goods::check(int h, int w){ if (w < left){ left = w; } if (w > right){ right = w; } if (h < top){ top = h; } if (h > bottom){ bottom = h; } return; } int main(){ int N; //データセット数 int H; //縦 int W; //横 const int num = 7; //材質数上限 int m; //登場材質数 int g; char read; cin >> N; for (int n = 0; n < N; n++){ m = 0; cin >> H >> W; Goods goods[num]; int** image = new int*[H]; for (int h = 0; h < H; h++){ image[h] = new int[W]; for (int w = 0; w < W; w++){ cin >> read; if (read == '.'){ image[h][w] = -1; } else{ g = 0; while (g < m){ if (read == goods[g].material){ image[h][w] = g; goods[g].check(h, w); break; } g++; } if (g == m){ image[h][w] = g; goods[m].material = read; goods[m].left = w; goods[m].right = w; goods[m].top = h; goods[m].bottom = h; m++; } } } }//読み取り終了 //for (int h = 0; h < H; h++){ // for (int w = 0; w < W; w++){ // cout.width(2); // cout << image[h][w]; // } // cout << endl; //} //チェック bool** near = new bool*[m]; //手前にあるか(長方形だとしたときに, 行より列が手前ならtrue) for (g = 0; g < m; g++){ near[g] = new bool[m]; for (int g2 = 0; g2 < m; g2++){ near[g][g2] = false; } } for (g = 0; g < m; g++){ for (int h = goods[g].top; h <= goods[g].bottom; h++){ for (int w = goods[g].left; w <= goods[g].right; w++){ if (image[h][w] == -1){ goto SUSPICIOUS; } else if(image[h][w] != g){ if (near[g][image[h][w]] == false){ near[g][image[h][w]] = true; for (int g2 = 0; g2 < m; g2++){ near[g][g2] = (near[g][g2] || near[image[h][w]][g2]); near[g2][image[h][w]] = (near[g2][g] || near[g2][image[h][w]]); } } } } }//1材質あたりのチェック }//全アイテムチェック for (g = 0; g < m; g++){ if (near[g][g] == true){ goto SUSPICIOUS; } } cout << "SAFE" << endl; continue; SUSPICIOUS: cout << "SUSPICIOUS" << endl; continue; }//1データセット }
0
#include <bits/stdc++.h> using namespace std; const int max_n = 60, inf = 1000111222; vector<int> v[max_n]; vector<pair<int, int> > g[max_n]; struct edge { int to; long long flow, cap; }; vector<edge> e; int n, k; void add_edge(int from, int to, int cap) { v[from].push_back(e.size()); v[to].push_back(e.size() + 1); e.push_back({to, 0, cap}); e.push_back({from, 0, 0}); } int d[max_n]; int ps[max_n]; bool vis[max_n]; bool bfs(int s, int t) { queue<int> q; q.push(s); for (int i = 0; i < n; ++i) { vis[i] = 0; d[i] = inf; } d[s] = 0; vis[s] = 1; while (!q.empty()) { int cur = q.front(); q.pop(); for (int i = 0; i < v[cur].size(); ++i) { edge& ed = e[v[cur][i]]; if (!vis[ed.to] && ed.flow < ed.cap) { vis[ed.to] = 1; d[ed.to] = d[cur] + 1; q.push(ed.to); } } } if (d[t] == inf) return 0; return 1; } long long dfs(int cur, int t, long long flow) { if (cur == t) { return flow; } vis[cur] = 1; for (int& i = ps[cur]; i < v[cur].size(); ++i) { edge& ed = e[v[cur][i]]; if (!vis[ed.to] && ed.cap > ed.flow && d[ed.to] == d[cur] + 1) { long long fl = dfs(ed.to, t, min(flow, ed.cap - ed.flow)); if (fl) { ed.flow += fl; e[v[cur][i] ^ 1].flow = -ed.flow; return fl; } } } return 0; } void dinic(int s, int t) { while (bfs(s, t)) { memset(vis, 0, sizeof(vis)); memset(ps, 0, sizeof(ps)); while (dfs(s, t, inf)) { memset(vis, 0, sizeof(vis)); } } } vector<int> dijkstra(int& res) { int d[max_n]; pair<int, int> pr[max_n]; for (int i = 0; i < n; ++i) { d[i] = inf; pr[i] = {-1, -1}; } d[0] = 0; priority_queue<pair<int, int> > q; q.push(make_pair(0, 0)); while (!q.empty()) { int cur = q.top().second; if (d[cur] != -q.top().first) { q.pop(); continue; } q.pop(); for (int i = 0; i < g[cur].size(); ++i) { int to = e[g[cur][i].first].to; int w = g[cur][i].second; if (d[to] > d[cur] + w) { d[to] = d[cur] + w; pr[to] = make_pair(cur, g[cur][i].first); q.push(make_pair(-d[to], to)); } } } vector<int> path; res = d[n - 1]; if (res == inf) { return path; } int cur = n - 1; while (cur != 0) { int prev = pr[cur].first, ee = pr[cur].second; path.push_back(ee); cur = prev; } return path; } int main() { cin >> n >> k; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int c; cin >> c; if (c == 0) continue; add_edge(i, j, c); } } dinic(0, n - 1); while (true) { for (int i = 0; i < n; ++i) { g[i].clear(); } for (int i = 0; i < n; ++i) { for (int ei : v[i]) { edge& cur = e[ei]; if (cur.flow < cur.cap) { g[i].push_back(make_pair(ei, 0)); } else { if (cur.cap == 0) { continue; } g[i].push_back(make_pair(ei, 1)); } } } int res; vector<int> path = dijkstra(res); if (res > k) break; for (int i : path) { edge& cur = e[i]; if (cur.flow == cur.cap) { ++cur.cap; } } k -= res; dinic(0, n - 1); } long long ans = 0; for (int i = 0; i < v[0].size(); ++i) { ans += e[v[0][i]].flow; } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long N, K, dp[501][501]; long long p2[501]; void pre() { long long i; p2[0] = 1; for (i = 1; i < 501; ++i) p2[i] = (p2[i - 1] * 2) % 1000000007; } long long rec(long long maxinc, long long cur) { if (maxinc > K) return 0; if (cur > N) return (maxinc == K); long long last = cur - 1; long long &ret = dp[maxinc][cur]; if (ret != -1) return ret; long long temp, mul = 1; ret = 0; long long cc = cur; for (; cur <= N; ++cur) { mul = (mul * p2[last]) % 1000000007; temp = (rec(maxinc + 1, cur + 1) * (p2[cur - last] - 1)) % 1000000007; ret = (ret + (temp * mul) % 1000000007) % 1000000007; } if (maxinc == K) ret = (ret + mul) % 1000000007; return ret; } int main() { pre(); while (scanf("%I64d %I64d", &N, &K) != EOF) { memset(dp, -1, sizeof(dp)); printf("%I64d\n", rec(0, 1)); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m, i, j, k, u, s, yes[100007], ans[100007], p[100007][3]; vector<vector<int> > v(100007); bool vis[100007]; void dfs(int x, int pos, int deep) { int i; if (v[x].size() == 0) { if (deep == 1) { puts("Win"); for (i = 0; i < pos; i++) { printf("%d ", ans[i]); } printf("%d", x); exit(0); } } else { if (p[x][deep]) return; p[x][deep] = 1; ans[pos] = x; for (i = 0; i < v[x].size(); i++) { dfs(v[x][i], pos + 1, deep ^ 1); } } } void draw(int x) { if (vis[x]) { puts("Draw"); exit(0); } vis[x] = 1; for (int i = 0; i < v[x].size(); i++) { draw(v[x][i]); } vis[x] = 0; } int main() { cin >> n >> m; for (i = 1; i <= n; i++) { cin >> k; for (j = 1; j <= k; j++) { cin >> u; v[i].push_back(u); } } cin >> s; dfs(s, 0, 0); draw(s); puts("Lose"); }
2
#include <bits/stdc++.h> using namespace std; bool used[1001][1001]; vector<int> w, l; int main() { int n, k, i, j, ck; scanf("%d%d", &n, &k); for (i = 1; i <= n; i++) { ck = k; for (j = 1; j <= n; j++) { if (i == j || used[i][j]) continue; used[i][j] = used[j][i] = 1; w.push_back(i); l.push_back(j); ck--; if (ck == 0) break; } if (ck) { printf("-1\n"); return 0; } } int m = w.size(); printf("%d\n", m); for (i = 0; i < m; i++) printf("%d %d\n", w[i], l[i]); }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1100; long long x, y; int n; int main() { std::ios::sync_with_stdio(false); while (cin >> n) { long long x1, x2, y1, y2; x1 = y1 = 1000000000; x2 = y2 = -1000000000; for (int i = 0; i < n; ++i) { cin >> x >> y; if (x < x1) x1 = x; if (y < y1) y1 = y; if (x > x2) x2 = x; if (y > y2) y2 = y; } unsigned long long s = max(abs(x2 - x1), abs(y2 - y1)); cout << s * s << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = ~0U >> 1; const long long INF = ~0ULL >> 1; template <class T> inline void read(T &n) { char c; int flag = 1; for (c = getchar(); !(c >= '0' && c <= '9' || c == '-'); c = getchar()) ; if (c == '-') flag = -1, n = 0; else n = c - '0'; for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0'; n *= flag; } const int maxn = 2100000; const int mo = 1000000007; int N, K, M, ans, vis[1000]; char s[maxn]; int nxt[maxn][26]; int dp[maxn]; void add(int &x, int y) { x += y; if (x >= mo) x -= mo; } int main(int argc, char *argv[]) { scanf("%d%d", &N, &K); scanf("%s", s + 1); M = strlen(s + 1); string temp = ""; for (int i = (M); i >= (1); --i) if (!vis[s[i]]) vis[s[i]] = true, temp = s[i] + temp; for (int i = ('a'); i <= ('a' + K - 1); ++i) if (!vis[i]) temp = (char)i + temp; for (int i = (0); i < (N); ++i) s[++M] = temp[i % temp.size()]; for (int j = (0); j < (K); ++j) nxt[M + 1][j] = M + 1; for (int i = (M); i >= (1); --i) { memcpy(nxt[i], nxt[i + 1], sizeof(nxt[i])); nxt[i][s[i] - 'a'] = i; } int cur = 0; dp[0] = 1; for (int i = (0); i < (M); ++i) { int flag = false; for (int j = (0); j < (K); ++j) if (nxt[i + 1][j] <= M) add(dp[nxt[i + 1][j]], dp[i]); } for (int i = (0); i <= (M); ++i) add(ans, dp[i]); printf("%d\n", ans); return 0; }
5
#include <cstdio> #include <cstring> #include <algorithm> #include <cassert> using namespace std; typedef long long ll; void Solve() { ll a, b, c, d, C = 0, tot; scanf("%lld%lld%lld%lld", &a, &b, &c, &d); tot = a + b; ll k = (a >= b) ? (a - 1) / (b + 1) + 1 : (b - 1) / (a + 1) + 1, ans = a + b; if(k == 1) ans = a < b ? 0 : a + b; else for(ll l = 0, r = a + b; l <= r; ) { ll md = (l + r) / 2; ll tmpa = a - md, tmpb = b - (md - 1) / k; if((tmpa + 1) * k < tmpb) r = md - 1; else { l = md + 1; ans = md; } } ans += ans / k; for(int i = c; i <= ans && i <= d; i++) putchar(i % (k + 1) ? 'A' : 'B'); for(int i = max(ans + 1, c); i <= d; i++) putchar((a + b + 1 - i) % (k + 1) ? 'B' : 'A'); putchar('\n'); } int main() { int Q; scanf("%d", &Q); while(Q--) Solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, len, mini, maxi, minj, maxj; int tot[2222][2222]; string s[2222]; int sum(int ii, int jj, int kk) { return tot[ii + kk][jj + kk] - (ii > 0 ? tot[ii - 1][jj + kk] : 0) - (jj > 0 ? tot[ii + kk][jj - 1] : 0) + (ii > 0 && jj > 0 ? tot[ii - 1][jj - 1] : 0); } bool check(int i, int j) { if (i + len >= n || j + len >= m) return false; return (sum(i, j, len) - (len > 1 ? sum(i + 1, j + 1, len - 2) : 0) == tot[n - 1][m - 1]); } void print(int ii, int jj) { for (int i = ii; i <= ii + len; i++) { if (s[i][jj] == '.') s[i][jj] = '+'; if (s[i][jj + len] == '.') s[i][jj + len] = '+'; } for (int j = jj; j <= jj + len; j++) { if (s[ii][j] == '.') s[ii][j] = '+'; if (s[ii + len][j] == '.') s[ii + len][j] = '+'; } for (int i = 0; i < n; i++) cout << s[i] << endl; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> s[i]; mini = minj = 2222; maxi = maxj = -1; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] == 'w') { mini = min(mini, i); maxi = max(maxi, i); minj = min(minj, j); maxj = max(maxj, j); } len = max(maxj - minj, maxi - mini); tot[0][0] = s[0][0] == 'w'; for (int i = 1; i < n; i++) tot[i][0] = tot[i - 1][0] + (s[i][0] == 'w'); for (int j = 1; j < m; j++) tot[0][j] = tot[0][j - 1] + (s[0][j] == 'w'); for (int i = 1; i < n; i++) for (int j = 0; j < m; j++) tot[i][j] = tot[i - 1][j] + tot[i][j - 1] - tot[i - 1][j - 1] + (s[i][j] == 'w'); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (check(i, j)) { print(i, j); return 0; } cout << -1 << endl; return 0; }
4
#include<bits/stdc++.h> using namespace std; char s[200005]; vector<int> ci[26]; bool u[200005]; int n, fw[200005]; void add(int i) { u[i++] = true; while(i<=n) { fw[i]++; i += i&-i; } } int qu(int i) { i++; int ret = 0; while(i) { ret += fw[i]; i -= i&-i; } return ret; } int main() { scanf("%s",s); for(;s[n];n++) ci[s[n]-'a'].push_back(n); int odd=0; for(int i=0;i<26;i++) if(ci[i].size()&1) odd++; if(odd != (n&1)) { puts("-1"); return 0; } long long ans = 0; int r = n-1, tr = n-1, ct = -1; for(int i=0;i<=r;i++) { if(u[i]) continue; u[i] = true; int ii = s[i]-'a'; int j = ci[ii].back(); ci[ii].pop_back(); if(i == j) { ct = i; continue; } int jj = j-qu(j); ans += tr - jj; add(j); while(u[r]) r--; tr--; if(j<ct) ans++; } if(ct>=0) { ans += abs(n/2-(ct-qu(ct))); } printf("%lld\n",ans); }
0
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); inline int rand(int x, int y) { ++y; return (rng() % (y - x)) + x; } string to_string(char c) { string second(1, c); return second; } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void degug_out() { cerr << endl; } template <typename Head, typename... Tail> void degug_out(Head H, Tail... T) { cerr << " " << to_string(H); degug_out(T...); } inline int gcd(int a, int b) { if (a > b) swap(a, b); if (a == 0) return b; return gcd(b % a, a); } int n; int read() { bool neg = false; int res = 0; char c = getchar(); while (true) { if (c == '-') break; if ('0' <= c && c <= '9') break; c = getchar(); } if (c == '-') neg = true; else res = c - '0'; while (true) { c = getchar(); if (c < '0' || c > '9') break; res = (res << 1) + (res << 3) + (c - '0'); } if (neg) return -res; return res; } const int sz = 749; int store[sz + 2][sz + 2]; int A[(500006)]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); n = read(); for (int i = (0); i <= (int)(n - 1); ++i) { int op, x, y; op = read(), x = read(), y = read(); if (op == 1) { for (int i = (1); i <= (int)(sz); ++i) { store[i][x % i] += y; } A[x] += y; } else { if (x > sz) { int ans = 0; for (int i = y; i <= 500000; i += x) ans += A[i]; cout << ans << '\n'; } else { cout << store[x][y] << '\n'; } } } }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 105; int a[maxn], n, k, q, cnt, c2[maxn]; bool f[maxn][maxn * maxn]; void dfs(int l, int r, int k, bool o) { int le = r - l + 1; if (k == c2[le]) { for (register int i = o ? r : l; o ? i >= l : i <= r; o ? i-- : i++) a[i] = ++cnt; return; } for (register int i = 2; i < le; i++) { if (k <= c2[i] || !f[le - i + 1][k - c2[i] + 1]) continue; dfs(o ? l + i - 1 : l, o ? r : r - i + 1, k - c2[i] + 1, o ^ 1); for (register int j = o ? l + i - 2 : r - i + 2; o ? j >= l : j <= r; o ? j-- : j++) a[j] = ++cnt; return; } for (register int i = 4; i <= le; i++) { if (k <= i || !f[le - i + 1][k - i]) continue; for (register int j = r - i + 2; j <= r; j += 2) a[j] = ++cnt; dfs(l, r - i + 1, k - i, 0); for (register int j = r - i + 3; j <= r; j += 2) a[j] = ++cnt; if (i & 1) swap(a[r - 2], a[r]); } } int main() { for (register int i = 1; i < maxn; i++) c2[i] = i * (i + 1) / 2; f[0][0] = 1; for (register int i = 1; i < maxn; i++) f[i][c2[i]] = 1; for (register int i = 4; i < maxn; i++) f[i][i + 1] = 1; for (register int i = 2; i < maxn; i++) for (register int j = 1; j <= c2[i]; j++) { if (!f[i][j]) continue; for (register int k = 2; k < maxn - i; k++) f[i + k - 1][j + c2[k] - 1] = 1; for (register int k = 4; k < maxn - i; k++) f[i + k - 1][j + k] = 1; } scanf("%d", &q); while (q--) { scanf("%d%d", &n, &k); if (!f[n][k]) puts("NO"); else { puts("YES"), cnt = 0, dfs(1, n, k, 0); for (register int i = 1; i <= n; i++) printf("%d%c", a[i], " \n"[i == n]); } } }
6
#include <bits/stdc++.h> using namespace std; const int N = 405; int n, m, mod, fa[N * N], pos[N * N]; char s[N][N]; struct Graph { int n; long long g[N][N]; void lnk(int x, int y) { g[x][x]++, g[y][y]++, g[x][y]--, g[y][x]--; } long long Gauss(int idx) { long long ret = 1; for (int i = 1; i <= idx - 1; i++) { for (int j = i + 1; j <= idx - 1; j++) { if (g[j][i]) { swap(g[i], g[j]), ret *= -1; break; } } for (int j = i + 1; j <= idx - 1; j++) { while (g[j][i]) { long long w = g[i][i] / g[j][i]; for (int k = 1; k <= idx - 1; k++) g[i][k] = (g[i][k] - w * g[j][k] % mod + mod) % mod; swap(g[i], g[j]), ret *= -1; } } } for (int i = 1; i <= idx - 1; i++) ret = ret * g[i][i] % mod; return (ret % mod + mod) % mod; } } G[2]; void add(long long &x, long long y) { x = (x + y) % mod; } int id(int x, int y) { return (x - 1) * (m + 1) + y; } int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); } void merge(int x, int y) { int fx = getfa(x), fy = getfa(y); if (fx == fy) return; fa[fx] = fy; } long long solve() { for (int i = 1; i <= (n + 1) * (m + 1); i++) fa[i] = i; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i][j] == '/') { merge(id(i + 1, j), id(i, j + 1)); } else if (s[i][j] == '\\') { merge(id(i, j), id(i + 1, j + 1)); } } } for (int i = 1; i <= n + 1; i++) { for (int j = 1; j <= m + 1; j++) { if (getfa(id(i, j)) == id(i, j)) { pos[id(i, j)] = ++G[(i + j) & 1].n; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i][j] == '*') { int x = id(i + 1, j), y = id(i, j + 1); if (getfa(x) != getfa(y)) { x = getfa(x), y = getfa(y); x = pos[x], y = pos[y]; G[(i + 1 + j) & 1].lnk(x, y); } x = id(i, j), y = id(i + 1, j + 1); if (getfa(x) != getfa(y)) { x = getfa(x), y = getfa(y); x = pos[x], y = pos[y]; G[(i + j) & 1].lnk(x, y); } } } } return (G[0].Gauss(G[0].n) + G[1].Gauss(G[1].n)) % mod; } int main() { cin >> n >> m >> mod; for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); } printf("%lld\n", solve()); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<pair<long long, long long> > foo(2 * n); long long c = 0; for (long long i = 0; i < 2 * n; i++) { cin >> foo[i].first; foo[i].second = i + 1; } sort(foo.begin(), foo.end()); long long a = 1; long long ans = 0; for (long long i = 0; i < 2 * n; i += 2) { ans += abs(foo[i].second - a); a = foo[i].second; } a = 1; for (long long i = 1; i < 2 * n; i += 2) { ans += abs(foo[i].second - a); a = foo[i].second; } cout << ans << endl; }
2
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> using namespace std; #define endl '\n' #define ll long long #define pi pair<int, int> #define f first #define s second const int mxn = 200000; int n; int a[mxn], b[mxn], f[2]; vector<int> v[2]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 0; i < n; i++){ int x, y, z = 0; cin >> x >> y; x--, y--; if(x > y) swap(x, y), z = 1; if(x >= n || y < n){ cout << -1 << endl; return 0; } a[x] = y - n, b[x] = z; } int ret = 0; for(int i = 0, x = n; i < n; i++){ bool t = 0; for(int j = 0; j < 2; j++){ if(v[j].empty() || a[v[j].back()] > a[i]){ t = 1, f[j] += b[i], v[j].push_back(i); break; } } if(!t){ cout << -1 << endl; return 0; } x = min(x, a[i]); if(x == n - i - 1){ int y = n; for(int j = 0; j < 2; j++) y = min(y, f[j] + (int)v[!j].size() - f[!j]); ret += y; for(int j = 0; j < 2; j++) f[j] = 0, v[j].clear(); } } cout << ret << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; string yes = "YES\n"; string no = "NO\n"; int32_t main() { long long k; cin >> k; string s = "codeforces"; if (k == 1) { cout << "codeforces" << "\n"; return 0; } long long least = 1e18, la, lb, lp, lq; for (long long a = 1; a <= 40; a++) { for (long long b = 1; b <= 40; b++) { for (long long p = 0; p <= 10; p++) { long long q = 10 - p; long long cmp = pow(a, p) * pow(b, q); if (cmp >= k and (a * p) < (least - b * q)) { least = a * p + b * q; la = a, lb = b, lp = p, lq = q; } } } } long long i = 0; for (; i < lp; i++) { for (long long j = 0; j < la; j++) cout << s[i]; } for (; i < 10; i++) { for (long long j = 0; j < lb; j++) cout << s[i]; } cout << "\n"; return 0; }
2
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll x; int main() { scanf("%lld",&x); for(ll i=1;i<=200;++i){ for(ll j=-200;j<=200;++j){ if(i*i*i*i*i-j*j*j*j*j==x){ printf("%lld %lld\n",i,j); return 0; } } } }
0
//...START BY DOING WHAT IS NECESSARY, THEN WHAT IS POSSIBLE AND SUDDENLY YOU ARE DOING THE IMPOSSIBLE... #include <bits/stdc++.h> using namespace std; #define FAST_FURIER ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pb push_back #define mk make_pair #define rep(i,a,N) for(ll i=a;i<N;i++) #define rrep(i,a,N) for(ll i=a;i>N;i--) typedef long long ll; #define M 1000000007 bool comp(int x,int y) { return x > y; } /*..............................code starts here........................*/ // C is first won in M int main() { FAST_FURIER; int tt=1; cin >> tt; ll m,d,k; //ll a,b,c; while(tt--) { cin >> d >> k; ll x = 0,y = 0; int f = 0; while(true){ ll d1 = pow(x+k,2)+pow(y,2), d2 = pow(x,2)+pow(y+k,2); f = abs(f-1); if(d1 < d2 and d1 <= d*d){ x += k; } else if(d2 <= d1 and d2 <= d*d) y += k; else break; } if(f) cout << "Utkarsh"; else cout << "Ashish"; cout << endl; } } // Author : shivam_123 // g++ -std=c++17
4
#include <bits/stdc++.h> using namespace std; int main() { int n, k; long long min; long long a[100010]; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; int flag = 0; queue<long long> b; sort(a, a + n); for (int i = 0; i < n; i++) { b.push(a[i]); } min = 0; long long t = 0; while (k--) { while (!b.empty() && b.front() == min) { b.pop(); } if (!b.empty()) { min = b.front(); cout << b.front() - t << endl; t = b.front(); b.pop(); } else cout << 0 << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int64_t p, k; cin >> p >> k; vector<int64_t> ans; while (p != 0) { int64_t a = ((p % k + k) % k - p) / k; ans.push_back(k * a + p); p = a; } cout << ans.size() << "\n"; for (int64_t x : ans) cout << x << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, c = 0; char sr, sc; string s; vector<vector<char>> v; int main() { cin.tie(0); int n, c = 0, cc = 0; cin >> n; string s = "", s1 = "", s2; char ch; for (int i = 0; i < n; i++) { cin >> ch; s += ch; } for (int i = 0; i < n; i++) { cin >> ch; s1 += ch; } sort(s.begin(), s.end()); sort(s1.begin(), s1.end()); for (int i = 0; i < n; i++) { if (s[i] - '0' < s1[i] - '0') { c++; } } if (c == n) { cout << "YES"; return 0; } for (int i = 0; i < n; i++) { if (s[i] - '0' > s1[i] - '0') { cc++; } } if (cc == n) { cout << "YES"; return 0; } cout << "NO"; return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<long long> a, b; vector<pair<long long, long long> > P; long long ans[2000005]; priority_queue<long long> A, B; map<long long, stack<int> > pos; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long aux; pair<long long, long long> pa; int m; cin >> m; for (int i = 0; i < m; i++) { cin >> aux; a.push_back(aux); A.push(aux); } for (int i = 0; i < m; i++) { cin >> aux; b.push_back(aux); B.push(aux * -1); } for (int i = 0; i < m; i++) { pos[b[i]].push(i); P.push_back(make_pair(A.top(), B.top() * -1)); A.pop(); B.pop(); } for (int i = 0; i < m; i++) { pa = P[i]; int ind = pos[pa.second].top(); ans[ind] = pa.first; pos[pa.second].pop(); } for (int i = 0; i < m; i++) { cout << ans[i] << " "; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[1001]; int b[1001]; long long int d[1001]; int n, k1, k2; int k; bool ok; long long int s; int main() { cin >> n >> k1 >> k2; k = k1 + k2; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 1; i <= n; i++) d[i] = abs(a[i] - b[i]); sort(d + 1, d + n + 1, greater<int>()); while (k) { ok = 0; for (int i = 1; i <= n && k; i++) { if (d[i] > 0) d[i]--, k--, ok = 1; if (d[i] + 1 != d[i + 1]) break; } if (!ok) break; } if (k) { if (k % 2) s = 1; else s = 0; } for (int i = 1; i <= n; i++) s += d[i] * d[i]; cout << s << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k = 0, d, i; pair<int, int> co[3]; cin >> n >> m; char a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; if (a[i][j] == '*') { co[k].first = i; co[k].second = j; k++; } } } k = 0; for (i = 0; i < 2; i++) { if (co[i].first == co[i + 1].first) { k = abs(co[i].second - co[i + 1].second); if (i == 0) d = 2; else d = 0; break; } } if (co[d].second == co[i].second) cout << co[d].first + 1 << " " << co[d].second + k + 1; else cout << co[d].first + 1 << " " << co[d].second - k + 1; }
1
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse4") #pragma GCC optimize("unroll-loops") using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k, low, mid, high, x; int p; cin >> n >> k >> p; if (2 * k >= n) x = 0; else x = (n - 2 * k + 1) / 2; low = 1, high = n; while (low <= high) { mid = (low + high) >> 1; if ((n - mid + 2) / 2 > k) low = mid + 1; else high = mid - 1; } high = n - k + 1; while (low <= high) { mid = (low + high) >> 1; if ((mid - 1) / 2 <= x) low = mid + 1; else high = mid - 1; } while (p--) { cin >> x; if (x < high) cout << "."; else if (x % 2 == high % 2) cout << "X"; else if ((n - x + 2) / 2 <= k - (n - high + 2) / 2) cout << "X"; else cout << "."; } return 0; }
3
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 200005, M = 1000000007; int ast[N]; void pre() { ast[0] = 1; for (int i = 1; i < N; ++i) ast[i] = (ast[i - 1] * 2) % M; } int n; int x[N], y[N]; int p[N]; int qk[N], qg[N], ql[N]; int first(int x) { if (x == p[x]) return x; return p[x] = first(p[x]); } void kpc(int x, int y) { x = first(x); y = first(y); if (x == y) return; p[x] = y; qg[y] += qg[x]; qk[y] += qk[x]; ql[y] += ql[x]; } map<int, vector<int> > xx, yy; void solv() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { p[i] = i; qg[i] = 1; scanf("%d%d", &x[i], &y[i]); xx[x[i]].push_back(i); yy[y[i]].push_back(i); } for (auto it = xx.begin(); it != xx.end(); ++it) { for (int i = 0; i < ((int)(it->second).size()) - 1; ++i) { kpc(it->second[i], it->second[i + 1]); } qk[first(it->second[0])] += (((int)(it->second).size()) - 1); ++ql[first(it->second[0])]; } for (auto it = yy.begin(); it != yy.end(); ++it) { for (int i = 0; i < ((int)(it->second).size()) - 1; ++i) { kpc(it->second[i], it->second[i + 1]); } qk[first(it->second[0])] += (((int)(it->second).size()) - 1); ++ql[first(it->second[0])]; } int ans = 1; for (int i = 1; i <= n; ++i) { if (i != first(i)) continue; if (qk[i] == qg[i] - 1) ans = (ans * 1LL * (ast[ql[i]] - 1 + M)) % M; else ans = (ans * 1LL * ast[ql[i]]) % M; } printf("%d\n", ans); } int main() { pre(); solv(); return 0; }
5
#include <bits/stdc++.h> using namespace std; #define r(i,n) for (int i=0; i < (n); i++) double dp[303][303]; int n,h; int main(){ r(i,303)r(j,303)dp[i][j]=1e15; cin>>n; dp[0][1]=0; for(int i=0;i<n;i++){ cin>>h; for(int j=1;j<303;j++){ if(dp[i][j]==1e15)continue; for(int k=0;k<303;k++){ if(j==k){ if(j!=h)dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+abs(h-j) ); else dp[i+1][k] = min( dp[i+1][k] , dp[i][j] ); } if(j<k){ if(k<=h) dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(double)abs(k-j)/2+h-k ); else if(h<=j) dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(double)abs(k-j)/2+j-h ); else{ double x=(double)(h-j)/(k-j); dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(x*(h-j))/2+(1.0-x)*(k-h)/2 ); } } if(j>k){ if(j<=h) dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(double)abs(k-j)/2+h-j ); else if(h<=k) dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(double)abs(k-j)/2+k-h ); else{ double x=-(double)(h-j)/(j-k); dp[i+1][k] = min( dp[i+1][k] , dp[i][j]+(x*(j-h))/2+(1.0-x)*(h-k)/2 ); } } } } } double ans = 1e15; r(i,302) ans=min(ans,dp[n][i+1]); printf("%.9f\n",ans); }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int n, ans; cin >> n; ans = (3 * n * (n + 1)) + 1; cout << ans; return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") using namespace std; struct UF { vector<long long int> e; UF(int n) : e(n, -1) {} bool same_set(int a, int b) { return find(a) == find(b); } int size(int x) { return -e[find(x)]; } int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); } void join(int a, int b) { a = find(a), b = find(b); if (a == b) return; if (e[a] > e[b]) swap(a, b); e[a] += e[b]; e[b] = a; } }; long long int p = 998244353; long long int power_(long long int a, long long int b) { if (!b) return 1; long long int ans = power_(a, b / 2); ans = (ans * ans) % p; if (b % 2) ans = (ans * a) % p; return ans; } long long int inf = -1e15; struct Node { Node *l = 0, *r = 0; long long int lo, hi, mset = inf, madd = 0, val = -inf; Node(long long int lo, long long int hi) : lo(lo), hi(hi) {} Node(vector<long long int>& v, long long int lo, long long int hi) : lo(lo), hi(hi) { if (lo + 1 < hi) { long long int mid = lo + (hi - lo) / 2; l = new Node(v, lo, mid); r = new Node(v, mid, hi); val = min(l->val, r->val); } else val = v[lo]; } long long int query(long long int L, long long int R) { if (R <= lo || hi <= L) return -inf; if (L <= lo && hi <= R) return val; push(); return min(l->query(L, R), r->query(L, R)); } void set(long long int L, long long int R, long long int x) { if (R <= lo || hi <= L) return; if (L <= lo && hi <= R) mset = val = x, madd = 0; else { push(), l->set(L, R, x), r->set(L, R, x); val = min(l->val, r->val); } } void add(long long int L, long long int R, long long int x) { if (R <= lo || hi <= L) return; if (L <= lo && hi <= R) { if (mset != inf) mset += x; else madd += x; val += x; } else { push(), l->add(L, R, x), r->add(L, R, x); val = min(l->val, r->val); } } void push() { if (!l) { long long int mid = lo + (hi - lo) / 2; l = new Node(lo, mid); r = new Node(mid, hi); } if (mset != inf) l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf; else if (madd) l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0; } }; struct Tree { const long long int LOW = 1e8; long long int f(long long int a, long long int b) { return min(a, b); } long long int n; vector<long long int> s; Tree() {} Tree(long long int m, long long int def = 1e8) { init(m, def); } void init(long long int m, long long int def) { n = 1; while (n < m) n *= 2; s.assign(n + m, def); s.resize(2 * n, LOW); for (long long int i = n; i-- > 1;) s[i] = f(s[i * 2], s[i * 2 + 1]); } void update(long long int pos, long long int val) { pos += n; s[pos] = val; for (pos /= 2; pos >= 1; pos /= 2) s[pos] = f(s[pos * 2], s[pos * 2 + 1]); } long long int query(long long int l, long long int r) { return que(1, l, r, 0, n); } long long int que(long long int pos, long long int l, long long int r, long long int lo, long long int hi) { if (r <= lo || hi <= l) return LOW; if (l <= lo && hi <= r) return s[pos]; long long int m = (lo + hi) / 2; return f(que(2 * pos, l, r, lo, m), que(2 * pos + 1, l, r, m, hi)); } }; int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); long long int n, k; cin >> n >> k; long long int a, b, c, d, e, f; vector<pair<int, int>> ad; for (int i = 0; i < (n); ++i) { cin >> a >> b; ad.push_back({a, b}); } bool dp[n + 3][k + 4]; long long int t = 0; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { a = ad[i].first; b = ad[i].second; t += a; t += b; for (int j = 0; j < k; j++) { bool flag = false; if (i == 0) { c = t - j; d = (c % k); e = (t - j - d); if (a >= j && e % k == 0 && d <= b) { dp[0][j] = 1; } continue; } for (int l = 0; l <= k && flag == false; l++) { if (dp[i - 1][l]) { long long int t1 = t - a - b; c = (t1 - l) % k; d = (t - j) % k; e = (a + b - ((j - l + k) % k) - ((d - c + k) % k)); if (e % k == 0 && e >= 0 && (j - l + k) % k <= a && (d - c + k) % k <= b) { dp[i][j] = 1; flag = true; } } } } } long long int ans = 0; for (int i = 0; i < k; i++) { if (dp[n - 1][i]) { ans = max(ans, (t - i) / k); } } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); vector<int> sp; string s; cin >> s; unordered_map<char, bool> kek; for (char lol = 'a'; lol <= 'z'; lol++) kek[lol] = true; kek['a'] = false; kek['e'] = false; kek['i'] = false; kek['o'] = false; kek['u'] = false; if (s.length() == 1) { cout << s; return 0; } for (int i = 0; i < s.length() - 2; i++) { if (kek[s[i]] && kek[s[i + 1]] && kek[s[i + 2]]) { if (s[i] == s[i + 1] && s[i + 1] == s[i + 2]) continue; sp.push_back(i + 1); i++; } } int id = 0; if (sp.size() == 0) { cout << s; return 0; } for (int i = 0; i < s.length(); i++) { cout << s[i]; if (sp[id] == i) { cout << ' '; id++; if (id == sp.size()) { sp.push_back(-1); } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 1000000007; char str[1000005]; string s[2005]; pair<int, int> p[2][2005]; int a[2005], n, m; vector<int> u[2005][26]; long long sum[2005][26]; int cnt[2005][26]; int jmp[2005][26]; pair<int, int> gao(pair<int, int> x, int c) { int i = x.first, j = x.second; if (i >= m || j >= INF) return {INF, INF}; int k = u[a[i]][c][j]; if (k >= 0) { if (++k == s[a[i]].size()) i++, k = 0; return {i, k}; } else { i = jmp[i][c]; if (i >= 0) { k = u[a[i]][c][0]; if (++k == s[a[i]].size()) i++, k = 0; return {i, k}; } } return {INF, INF}; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", str); s[i] = str; int l = strlen(str); for (int j = 0; j < l; j++) cnt[i][str[j] - 'a']++; for (int c = 0; c < 26; c++) { u[i][c].resize(l); if (str[l - 1] - 'a' == c) u[i][c][l - 1] = l - 1; else u[i][c][l - 1] = -1; for (int j = l - 2; ~j; j--) if (str[j] - 'a' == c) u[i][c][j] = j; else u[i][c][j] = u[i][c][j + 1]; } } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", a + i); a[i]--; for (int c = 0; c < 26; c++) { if (i) sum[i][c] = sum[i - 1][c]; sum[i][c] += cnt[a[i]][c]; } } for (int i = 0; i < m; i++) for (int c = 0; c < 26; c++) { int lo = i + 1, hi = m; while (lo < hi) { int x = (lo + hi) / 2; if (sum[x][c] != sum[i][c]) hi = x; else lo = x + 1; } if (hi == m) jmp[i][c] = -1; else jmp[i][c] = hi; } scanf("%s", str); memset(p, 63, sizeof(p)); p[0][0] = {0, 0}; int ans = 0; for (int i = 0; str[i]; i++) { memset(p[~i & 1], 63, sizeof(p[~i & 1])); p[~i & 1][0] = {0, 0}; for (int j = 0; j <= i; j++) { p[~i & 1][j + 1] = min(p[i & 1][j + 1], gao(p[i & 1][j], str[i] - 'a')); if (p[~i & 1][j + 1].first <= 2005) ans = max(ans, j + 1); } } printf("%d\n", ans); }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio( false ); int H, W, N; cin >> H >> W >> N; map<pair<int, int>, int> mp; for( int i = 0; i < N; ++i ) { int a, b; cin >> a >> b; a -= 1; b -= 1; for( int p = a-2; p <= a; ++p ) { if( p < 0 || p >= H-2 ) { continue; } for( int q = b-2; q <= b; ++q ) { if( q >= 0 && q < W-2 ) { mp[{p, q}] += 1; } } } } vector<int64_t> sums( 10 ); sums.at( 0 ) = (int64_t)(H-2)*(W-2) - mp.size(); for( const auto& v: mp ) { sums.at( v.second ) += 1; } for( auto v: sums ) { cout << v << '\n'; } }
0
#include <bits/stdc++.h> using namespace std; long long n; int main() { int k; while (cin >> n >> k) { while (k--) { if (n % 10) n--; else n /= 10; } cout << n << endl; } return 0; }
1
#include <iostream> #define rep(i, a, b) for(int i = a; i < b; i ++) using namespace std; int h, w; char m[100][100]; int m2[100][100] = {0}; void exp(int x, int y, char f) { if(x < 0 || w <= x || y < 0 || h <= y) return; if(m[x][y] != f || m2[x][y] == 1) return; m2[x][y] = 1; exp(x-1, y, f); exp(x+1, y, f); exp(x, y-1, f); exp(x, y+1, f); return; } int main() { while(cin >> h >> w, h) { rep(i, 0, h) { rep(j, 0, w) { cin >> m[j][i]; m2[j][i] = 0; } } int ans = 0; rep(i, 0, h) { rep(j, 0, w) { if(m2[j][i] == 0) { exp(j, i, m[j][i]); ans ++; } } } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int x, y, z, x1, y1, z1; cin >> x >> y >> z >> x1 >> y1 >> z1; int a[6]; for (int i = 0; i < 6; i++) cin >> a[i]; int ans = 0; if (y < 0) ans += a[0]; if (y > y1) ans += a[1]; if (z < 0) ans += a[2]; if (z > z1) ans += a[3]; if (x < 0) ans += a[4]; if (x > x1) ans += a[5]; cout << ans << "\n"; }
4
#include <iostream> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <iomanip> #include <map> #include <queue> #include <functional> using namespace std; struct Data { int num, pos, len; }; typedef pair<int, int> P; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while(cin >> n, n) { map<int, Data> datamap; //[pos]->Data map<int, vector<Data> > nummap; //[num]->vec<idx> priority_queue<P, vector<P>, greater<P> > q; q.push(P{ 0, int(1e9 + 1) }); for(int i = 0; i < n; i++) { char op; cin >> op; if(op == 'W') { int num, len; cin >> num >> len; while(len > 0) { P p = q.top(); q.pop(); Data d{ num, p.first, len }; if(p.second >= len) { if(p.second > len) q.push(P(p.first + len, p.second - len)); len = 0; } else if(p.second < len) { d.len = p.second; len -= p.second; } datamap[d.pos] = d; nummap[num].push_back(d); } } else if(op == 'D') { int num; cin >> num; for(const Data& d : nummap[num]) { datamap.erase(d.pos); q.push(P(d.pos, d.len)); } nummap[num].clear(); } else {// 'R' int pos; cin >> pos; auto it = datamap.upper_bound(pos); if(it == datamap.end() && datamap.size() == 0) { cout << "-1" << endl; continue; } --it; Data d = it->second; if(d.pos <= pos && pos < d.pos + d.len) { cout << d.num << endl; } else { cout << "-1" << endl; } } } cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int x1, x2, y1, y2, x, y; cin >> x1 >> y1 >> x2 >> y2; y2 -= y1; x2 -= x1; cin >> x >> y; if (y2 < 0) y2 = -y2; if (x2 < 0) x2 = -x2; if (x2 == 0 || y2 == 0) { if (x2 == 0 && y2 == 0) { cout << "YES"; return 0; } if (x2 == 0) { if (x == 0 && y2 % y == 0) { cout << "YES"; return 0; } if (y2 % (y * 2) == 0) { cout << "YES"; return 0; } } if (y2 == 0) { if (y == 0 && x2 % x == 0) { cout << "YES"; return 0; } if (x2 % (x * 2) == 0) { cout << "YES"; return 0; } } } if (x == 0 || y == 0) { cout << "NO"; return 0; } if (x2 % (x * 2) == 0 && y2 % (y * 2) == 0) { cout << "YES"; return 0; } if (x2 % (x * 2) != 0 && y2 % (y * 2) != 0 && x2 % x == 0 && y2 % y == 0) { cout << "YES"; return 0; } { cout << "NO"; return 0; } }
1
#include<iostream> using namespace std; int main() { int n, k;cin >> n >> k; if (n % k == 0) cout << 0; else cout << 1; }
0
#include <bits/stdc++.h> const long long mod = 1e9 + 7; using namespace std; long long a[55]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long n, k; cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); reverse(a + 1, a + 1 + n); for (long long j = 1; j <= n; j++) { cout << 1 << " " << a[j] << endl; k--; if (!k) return 0; for (long long i = j + 1; i <= n; i++) { k--; cout << j + 1 << " "; for (long long w = 1; w <= j; w++) { cout << a[w] << " "; } cout << a[i] << endl; if (!k) return 0; } } return 0; }
3
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll dp[50]; ll N; int main(){ cin >> N; dp[1] = 3; int i; for(i=1;dp[i]<N;i++){ dp[i+1] = dp[i]*3; } cout << i << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n; cin >> k; if ((n / k) % 2 == 0) cout << "NO"; else cout << "YES"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, m, x, y; cin >> n >> m >> x >> y; char arr[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> arr[i][j]; } long long count_dots = 0; long long cost = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] == '.') { if (j + 1 < m && 2 * x > y) { if (arr[i][j + 1] == '.') { cost += y; j++; } else cost += x; } else cost += x; } } } cout << cost << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct h { int be; int we; }; vector<h> gr[1000]; vector<int> ne[1000]; int knap[1005][1005]; int n, m, w; int wh[10000]; int bh[10000]; int x, y; int k; bool bfsed[1000]; vector<int> q; void bfs(int beg) { int u = beg; bfsed[u] = true; int v; q.clear(); q.push_back(u); int ind = 0; int g = q.size(); int wu = wh[u], bu = bh[u]; h pom; bool m1 = false; pom.we = wu; pom.be = bu; gr[k].push_back(pom); while (ind < g) { u = q[ind]; for (int j = 0; j < ne[u].size(); j++) { v = ne[u][j]; if (!bfsed[v]) { bfsed[v] = true; m1 = true; pom.we = wh[v]; pom.be = bh[v]; gr[k].push_back(pom); q.push_back(v); g++; wu += wh[v]; bu += bh[v]; } } ind++; } if (m1) { pom.we = wu; pom.be = bu; gr[k].push_back(pom); } k++; } int main() { scanf("%d %d %d", &n, &m, &w); for (int i = 0; i < n; i++) { scanf("%d", &wh[i]); bfsed[i] = false; } for (int i = 0; i < n; i++) scanf("%d", &bh[i]); for (int i = 0; i < m; i++) { scanf("%d %d", &x, &y); x--; y--; ne[x].push_back(y); ne[y].push_back(x); } k = 0; for (int i = 0; i < n; i++) { if (!bfsed[i]) { bfs(i); } } for (int i = 0; i < k; i++) { for (int j = 0; j < gr[i].size(); j++) { if (gr[i][j].we <= w && knap[i + 1][gr[i][j].we] < gr[i][j].be) knap[i + 1][gr[i][j].we] = gr[i][j].be; for (int l = 0; l <= w; l++) { if (knap[i][l] != 0) { if (l + gr[i][j].we <= w && knap[i + 1][l + gr[i][j].we] < knap[i][l] + gr[i][j].be) knap[i + 1][l + gr[i][j].we] = knap[i][l] + gr[i][j].be; } } } for (int l = 0; l <= w; l++) { if (knap[i + 1][l] < knap[i][l]) knap[i + 1][l] = knap[i][l]; } } int res = -1; for (int i = 0; i <= k; i++) { for (int l = 0; l <= w; l++) { if (knap[i][l] > res) res = knap[i][l]; } } cout << res << endl; return 0; }
4
#include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; bool cmp(pair<double,string>& a, pair<double,string>& b){ if(a.first != b.first){ return a.first > b.first; }else{ return a.second < b.second; } } int main(){ while(1){ int n; cin >> n; if(n==0) break; vector<pair<double, string> > v(n); for(int i=0; i<n; i++){ string name; int p,a,b,c,d,e,f,s,m; cin >>name>>p>>a>>b>>c>>d>>e>>f>>s>>m; int get = f*m*s; int time = a+b+c +(d+e)*m; double rate = (get-p)/(double)time; v[i]=make_pair(rate, name); } sort(v.begin(), v.end(), cmp); for(int i=0; i<n; i++){ cout << v[i].second << endl; } cout << "#" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, l, m, n, sum, page, s[8]; cin >> page; j = 0; sum = 0; for (i = 1; i <= 7; i++) scanf("%d", &s[i]); while (sum <= page) { for (j = 1; j <= 7; j++) { sum = sum + s[j]; if (sum >= page) { l = 0; break; } } if (l == 0) break; } while (j > 7) j = j - 7; printf("%d\n", j); }
1
#include <bits/stdc++.h> using namespace std; long double pb[105]; long double now[105]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int p; scanf("%d", &p); pb[i] = 1.0 - (long double)p / 100.0; } long double ret = (long double)n, mul = 1.0; priority_queue<pair<long double, int> > que; for (int i = 0; i < n; i++) { now[i] = pb[i]; mul *= 1.0 - now[i]; que.push( pair<long double, int>((1.0 - now[i] * pb[i]) / (1.0 - now[i]), i)); } ret += 1.0 - mul; for (int i = 0; i < 500000; i++) { pair<long double, int> p = que.top(); que.pop(); mul *= p.first; ret += 1.0 - mul; int v = p.second; now[v] *= pb[v]; que.push( pair<long double, int>((1.0 - now[v] * pb[v]) / (1.0 - now[v]), v)); } printf("%.10f\n", (double)ret); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int ll_max(long long int a, long long int b, long long int c) { return max(a, max(b, c)); } int int_max(int a, int b, int c) { return max(a, max(b, c)); } long long int ll_min(long long int a, long long int b, long long int c) { return min(a, min(b, c)); } int int_min(int a, int b, int c) { return min(a, min(b, c)); } long long int max(int a, long long int b) { return max((long long int)a, b); } long long int min(int a, long long int b) { return min((long long int)a, b); } long long int min(long long int a, int b) { return min(a, (long long int)b); } long long int max(long long int a, int b) { return max(a, (long long int)b); } long long int dx[] = {0, 0, 1, -1}; long long int dy[] = {1, -1, 0, 0}; long long int power(long long int x, long long int y, long long int m) { if (y == 0) return 1; long long int a = power(x, y / 2, m); if (y % 2) { return (a * ((a * x) % m)) % m; } else { return (a * a) % m; } } long long int mod_inverse(long long int x, long long int m) { return power(x, m - 2, m); } long long int fact(long long int n, long long int m) { if (n <= 1) return 1; return (fact(n - 1, m) * n) % m; } long long int ncr(long long int n, long long int r, long long int m) { if (r > n) return 0; long long int n1 = 1, d1 = 1, d2 = 1; n1 = fact(n, m); d1 = fact(r, m); d2 = fact(n - r, m); long long int ans = mod_inverse((d1 * d2) % m, m); ans = (ans * n1) % m; return ans; } int gcd(int a, int b) { if (a < b) return gcd(b, a); if (a % b == 0) return b; return gcd(b, a % b); } int ispal(string s) { int len = s.size(); int flag = 1; for (int i = 0; i < len; ++i) { if (s[i] != s[len - i - 1]) { flag = 0; break; } } return flag; } long long int sroot(long long int n, long long int low = 1, long long int high = 1e9 + 1) { if (low == high) return low; if (low == high - 1) { if (high * high <= n) return high; else return low; } long long int mid = (low + high) / 2; long long int a = mid * mid; if (a > n) return sroot(n, low, mid - 1); else return sroot(n, mid, high); } long long int croot(long long int n, long long int low = 1, long long int high = 1e6 + 1) { if (low == high) return low; if (low == high - 1) { if (high * high * high <= n) return high; else return low; } long long int mid = (low + high) / 2; long long int a = mid * mid * mid; if (a > n) return croot(n, low, mid - 1); else return croot(n, mid, high); } bool sortbysec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); long long int p, y; cin >> p >> y; long long int ans = -1; for (long long int i = y; i >= (p + 1); i--) { long long int min1 = LLONG_MAX; for (long long int j = 1; j <= sqrt(i); j++) { if (j == 1) continue; if (i % j == 0) { min1 = min(min1, j); min1 = min(min1, i / j); } } if (min1 > p) { ans = i; break; } } cout << ans; return 0; }
2