solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> #pragma GCC target("sse2") #pragma GCC optimization("O2") #pragma GCC optimization("unroll-loops") #pragma GCC optimize "trapv" using namespace std; const int maxn = 1000500; inline int g() { int x; cin >> x; return x; } inline long long gl() { long long x; cin >> x; return x; } inline char gc() { char x; cin >> x; return x; } inline string gs() { string s; cin >> s; return s; } template <typename T> inline vector<T> gv(int len) { vector<T> a(len); for (auto &it : a) cin >> it; return a; } inline vector<int> gv(int len) { vector<int> a(len); for (auto &it : a) cin >> it; return a; } inline vector<pair<int, int>> gv_andPairCnt(int len) { vector<pair<int, int>> a(len); int i = 1; for (auto &it : a) { it.second = i; i++; cin >> it.first; } return a; } double power_log(double power, double val) { return log(val) / log(power); } template <typename T> T sqr(T x) { return x * x; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it : v) os << it << ", "; os << "\n"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { os << "deque:{"; for (auto it : v) os << it << " "; os << "}\n"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "set:{"; for (auto it : v) os << it << " "; os << "}"; return os; } template <typename T, typename T2> ostream &operator<<(ostream &os, const map<T, T2> &v) { os << "map:{\n"; for (auto it : v) os << " {" << it.first << " <> " << it.second << "}\n"; os << "}\n"; return os; } template <typename T, typename T2> ostream &operator<<(ostream &os, const pair<T, T2> &v) { os << "{" << v.first << ", " << v.second << "}"; return os; } template <typename T, typename H> inline vector<pair<T, H>> gv_andBiPair(int len) { vector<pair<T, H>> a(len); for (auto &it : a) { cin >> it.first >> it.second; } return a; } template <typename T> inline set<T> vector_to_set(const vector<T> &vec) { set<T> s; for (auto &it : vec) s.insert(it); return s; } const long long md = 1e9 + 7; int dp[1001][513], a[1010]; vector<int> arr[10]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n = g(); for (int i = 1; i <= n; i++) { a[i] = g(); arr[a[i]].push_back(i); } auto ok = [&](int mid) { memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { dp[i][0] = 0; } int ans = -1; for (int i = 1; i <= n; i++) { vector<int> pos1(10, -1), pos2(10, -1); for (int j = 1; j <= 8; j++) { auto it = lower_bound(arr[j].begin(), arr[j].end(), i) - arr[j].begin(); if (mid == 0) { pos1[j] = i; } else { if (it + mid - 1 < arr[j].size() && arr[j][it + mid - 1] >= i) { pos1[j] = arr[j][it + mid - 1]; } } if (it + mid < arr[j].size() && arr[j][it + mid] >= i) { pos2[j] = arr[j][it + mid]; } } for (int msk = 0; msk < ((1 << 8)); ++msk) { if (dp[i][msk] == -1) { continue; } if (__builtin_popcount(msk) == 8) { ans = max(ans, dp[i][msk]); } for (int j = 0; j < (8); ++j) { if (((1 << j) & msk) == 0) { if (pos1[j + 1] != -1) { dp[pos1[j + 1]][msk | (1 << j)] = max(dp[pos1[j + 1]][msk | (1 << j)], dp[i][msk] + mid); } if (pos2[j + 1] != -1) { dp[pos2[j + 1]][msk | (1 << j)] = max(dp[pos2[j + 1]][msk | (1 << j)], dp[i][msk] + mid + 1); } } } } } return ans; }; for (int i = n; i >= 0; i--) { int x = ok(i); if (x != -1) { cerr << i << endl; return cout << ok(i), 0; } } }
7
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int grand(int x) { return uniform_int_distribution<int>(0, x - 1)(rng); } const int maxn = 1e6 + 7; const long double inf = 1e9; const long long md = 1e9 + 7; long long gcd(long long x, long long y) { if (x < y) return gcd(y, x); if (y == 0) return x; return gcd(y, x % y); } long double x[maxn]; long double y[maxn]; long double f[maxn]; long double g[maxn]; int n; long double calc(long double X) { long double r = 0.0; for (int i = 1; i <= n; i++) { long double rc = y[i] * y[i] + (X - x[i]) * (X - x[i]); rc /= y[i] + y[i]; r = max(r, rc); } return r; } int main() { { ios ::sync_with_stdio(false); cin.tie(0); }; cin >> n; int pos = 0, neg = 0; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i]; if (y[i] > 0) pos++; if (y[i] < 0) neg++; } if (neg > 0 && pos > 0) { cout << -1 << "\n"; return 0; } if (neg > 0) { for (int i = 1; i <= n; i++) y[i] = -y[i]; } long double l = -inf, r = inf; for (int j = 1; j <= 200; j++) { long double m1 = (l + l + r) / 3, m2 = (l + r + r) / 3; long double r1 = calc(m1), r2 = calc(m2); if (r1 < r2) { r = m2; } else { l = m1; } } cout << fixed << setprecision(12) << calc(l) << "\n"; }
7
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9 + 9; struct Node { Node *l, *r; int sz; int mx, mn; int comp, lcomp, rcomp; Node(vector<int>& a, int tl, int tr) { sz = tr - tl; if (sz == 1) { l = r = nullptr; mx = mn = a[tl]; lcomp = 0, rcomp = 1; comp = 1; } else { int tm = (tl + tr) / 2; l = new Node(a, tl, tm); r = new Node(a, tm, tr); recalc(); } } void show() { printf("sz = %d, mn = %d, mx = %d\n", sz, mn, mx); printf("comp = %d, lcomp = %d, rcomp = %d\n", comp, lcomp, rcomp); printf("\n"); if (l) l->show(); if (r) r->show(); } int push_right(int x) { if (sz == 1) { return 1; } if (r->mx < x || l->mn < x) { int res = l->push_right(max(x, r->mx)); if (l->mn > x) res += 1; return res; } return comp - (r->comp - r->push_right(x)); } int push_left(int x) { if (sz == 1) { return 1; } if (x < l->mn || x < r->mx) { int res = r->push_left(min(x, l->mn)); if (x > r->mx) res += 1; return res; } return comp - (l->comp - l->push_left(x)); } void recalc() { mx = max(l->mx, r->mx); mn = min(l->mn, r->mn); lcomp = l->push_right(r->mx); rcomp = r->push_left(l->mn); comp = lcomp + rcomp; if (l->mn < r->mx) comp -= 1; } void modify(int pos, int x) { if (sz == 1) { mn = mx = x; lcomp = 0; rcomp = 1; comp = lcomp + rcomp; return; } if (pos < sz / 2) l->modify(pos, x); else r->modify(pos - sz / 2, x); recalc(); } }; int main() { int n, q; scanf("%d %d", &n, &q); vector<int> a(n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); Node* root = new Node(a, 0, n); while (q--) { int pos, x; scanf("%d %d", &pos, &x); --pos; root->modify(pos, x); printf("%d\n", root->comp); } }
12
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, L; char s[1010], A[55], B[55]; int f[55][1010 * 25][2], g[55], h[55]; void add(int &x, int y) { x += y; if (x >= 1000000007) x -= 1000000007; } queue<int> q; struct AC_Automation { int ch[1010 * 25][12], endp[1010 * 25], tot, fail[1010 * 25]; void insert(int x) { int now = 0; for (int i = x; i <= x + L / 2 - 1; i++) { int d = s[i] - '0'; if (!ch[now][d]) ch[now][d] = ++tot; now = ch[now][d]; } endp[now] = 1; } void get_fail() { for (int i = 0; i < 10; i++) if (ch[0][i]) q.push(ch[0][i]); while (q.size()) { int now = q.front(); q.pop(); endp[now] |= endp[fail[now]]; for (int i = 0; i < 10; i++) { if (ch[now][i]) { fail[ch[now][i]] = ch[fail[now]][i]; q.push(ch[now][i]); } else ch[now][i] = ch[fail[now]][i]; } } } int DP(char *S) { memset(f, 0, sizeof(f)); f[0][0][1] = 1; for (int i = 0; i < L; i++) { for (int j = 0; j <= tot; j++) { for (int k = 0; k < 2; k++) { if (!f[i][j][k]) continue; if (endp[j]) continue; int lim = k ? (S[i + 1] - '0') : 9; for (int d = 0; d <= lim; d++) { add(f[i + 1][ch[j][d]][(d == lim) & k], f[i][j][k]); } } } } h[L + 1] = g[L + 1] = 1; for (int i = L; i >= 1; i--) h[i] = 1ll * h[i + 1] * 10 % 1000000007; for (int i = L; i >= 1; i--) g[i] = (g[i + 1] + 1ll * (S[i] - '0') * h[i + 1] % 1000000007) % 1000000007; int res = 0; for (int i = 0; i <= L; i++) { for (int j = 0; j <= tot; j++) { if (!endp[j]) continue; res = (res + 1ll * f[i][j][0] * h[i + 1] % 1000000007) % 1000000007; res = (res + 1ll * f[i][j][1] * g[i + 1] % 1000000007) % 1000000007; } } return res; } } AC; int main() { scanf("%s", s + 1); n = strlen(s + 1); scanf("%s", A + 1); scanf("%s", B + 1); L = strlen(A + 1); for (int i = 1; i <= n - L / 2 + 1; i++) AC.insert(i); int now = L; while (A[now] == '0') A[now] = '9', now--; A[now]--; AC.get_fail(); cout << (AC.DP(B) - AC.DP(A) + 1000000007) % 1000000007 << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; inline int nxt() { int x; scanf("%d", &x); return x; } int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n = nxt(); vector<int> d(n); for (int i = 0; i < n; ++i) { d[i] = nxt(); } d.push_back(0); sort((d).begin(), (d).end()); reverse((d).begin(), (d).end()); vector<int> ids; for (int i = 0, idx = n; i < n; ++i) { while (idx > 0 && d[idx - 1] == i) { --idx; } ++d[idx]; ids.push_back(idx); } d.push_back(0); ++n; d.erase(d.begin()); vector<long long> p(n + 1); for (int i = 0; i < n; ++i) { p[i + 1] = p[i] + d[i]; } bool found = true; int l = 0, r = n; for (int k = 1; k < n; ++k) { long long initial = 1ll * k * (k - 1) - p[k]; int fst_lk = lower_bound((d).begin(), (d).end(), k, greater<int>()) - d.begin(); fst_lk = max(fst_lk, k); initial += p[n] - p[fst_lk] + 1ll * k * (fst_lk - k); int fst_left = lower_bound((ids).begin(), (ids).end(), k - 1, greater<int>()) - ids.begin(); int fst_rt = min(k, fst_left); long long need = max(0ll, -initial); if (fst_rt < need) { found = false; break; } int sl = need; initial += fst_rt; int sr = min<long long>(n, fst_left + initial + 1); l = max(l, sl); r = min(r, sr); } long long s = accumulate((d).begin(), (d).end(), 0ll); if (l % 2 != s % 2) { ++l; } if (r % 2 == s % 2) { --r; } if (l >= r) { found = false; } if (!found) { cout << "-1\n"; return 0; } for (int i = l; i < r; i += 2) { printf("%d ", i); } printf("\n"); return 0; }
8
#include <bits/stdc++.h> using namespace std; long long n, m, l; void speedup() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { speedup(); cin >> n >> m >> l; vector<long long> a(n); long long k = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; if (i == 0 && a[i] > l) { ++k; } else if (i > 0 && a[i] > l) { if (!(a[i - 1] > l)) { ++k; } } } for (int i = 0; i < m; ++i) { int x; cin >> x; if (x == 1) { long long p, d; cin >> p >> d; --p; if (a[p] > l) { a[p] += d; continue; } else { a[p] += d; if (a[p] > l) { if (n > 1) { if (p > 0 && p < n - 1) { if (a[p - 1] > l && a[p + 1] > l) --k; else if (a[p + 1] > l || a[p - 1] > l) continue; else ++k; } if (p == 0) { if (a[p + 1] > l) continue; else ++k; } if (p == n - 1) { if (a[p - 1] > l) continue; else ++k; } } else ++k; } } } if (x == 0) { cout << k << '\n'; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 3000 + 10; const long long inf = 1e18 + 1; pair<int, int> p[maxn]; long long dp[maxn], s[maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> p[i].first >> p[i].second; sort(p + 1, p + 1 + n); for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + p[i].first; dp[n + 1] = 0; s[n + 1] = s[n]; for (int i = n; i >= 1; --i) { dp[i] = inf; for (int j = i + 1; j <= n + 1; ++j) { dp[i] = min(dp[i], dp[j] + s[j - 1] - s[i - 1] - (j - i) * 1ll * p[i].first + p[i].second); } } cout << dp[1] << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, i; char A[2000011]; char B[2000011]; char work[2000011]; unordered_map<char, char> M; int pr[2000011]; int q; void reverse(char *v) { M['W'] = 'E'; M['E'] = 'W'; M['S'] = 'N'; M['N'] = 'S'; for (int i = 1; i <= n; i++) v[i] = M[v[i]]; int i = 1; int j = n; while (i < j) swap(v[i++], v[j--]); } void combine(char *v1, char *v2, char *rez) { for (int i = 1; i <= n; i++) rez[i + n] = v1[i]; for (i = 1; i <= n; i++) rez[i] = v2[i]; } void kmp_pr() { q = 0; pr[1] = 0; for (i = 2; i <= n; i++) { while (q > 0 && work[q + 1] != work[i]) q = pr[q]; if (work[q + 1] == work[i]) q++; pr[i] = q; } } int main() { scanf("%d\n%s\n%s\n", &n, A + 1, B + 1); n--; reverse(B); combine(A, B, work); n <<= 1; kmp_pr(); if (pr[n] == 0) printf("YES"); else printf("NO"); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e; cin >> a >> b >> c >> d >> e; cout << min(min(a, b), min(c / 2, min(d / 7, e / 4))) << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long __gcd(long long a, long long b) { long long c = a % b; while (c != 0) { a = b; b = c; c = a % b; } return b; } void doit() { long long n; cin >> n; if (360 % (180 - n) == 0) { cout << "YES" << "\n"; return; } cout << "NO" << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long tc; cin >> tc; for (int i = 1; i <= tc; i++) { doit(); } }
1
#include <bits/stdc++.h> using namespace std; int a[100111]; vector<int> presents; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &a[i]); a[n++] = 1e9 + 1; sort(a, a + n); int j = 1; int s = 0; for (int i = 0; i < n; i++) { while (j < a[i] && s + j <= m) { presents.push_back(j); s += j; j++; } j = a[i] + 1; } printf("%d\n", presents.size()); for (int i = 0; i < presents.size(); i++) printf("%d ", presents[i]); printf("\n"); }
2
#include <bits/stdc++.h> #include <cmath> #define floop(b) for(long long i=0;i<b;i++) #define test long long t_c;cin>>t_c;for(long long ks=1;ks<=t_c;ks++) #define pb push_back #define sc(a) scanf("%lld",&a) #define vi vector<int> #define all(x) x.begin(),x.end() #define cy cout<<"YES\n"; #define cn cout<<"NO\n"; #define nl cout<<endl; typedef long long ll; typedef long long int lli; using namespace std; int main() { ios::sync_with_stdio(0); test { int n,k; cin>>n>>k; int x[n+1],y[n+2]; floop(n) { cin>>x[i]>>y[i]; } int f=0,c=0; for(int i=0;i<n;i++) { f=0; for(int j=0;j<n;j++) { int d=abs(x[i]-x[j])+abs(y[i]-y[j]); if(d<=k) { f++; //close[i][j]=1; } else { break; } } // cout<<"i="<<i<<" "<<f<<endl; if(f==n) { c=1; break; } } if(c==1) { cout<<1<<endl; } else { cout<<-1<<endl; } } }
1
#include <bits/stdc++.h> using namespace std; int main() { int i, n, res = 0; cin >> n; for (i = 1; i <= n; i++) res += (n - i) * i + 1; cout << res; }
1
#include <bits/stdc++.h> using namespace std; string s[11]; int len[11], id[11]; vector<string> ans; void upd(vector<string> &cur) { if (!ans.size()) { ans = cur; return; } for (int i = 0; i < (int)ans.size(); i++) { if (cur[i] < ans[i]) break; if (cur[i] > ans[i]) return; } ans = cur; } int main() { for (int i = 0; i < 6; i++) { cin >> s[i]; len[i] = s[i].length(); } for (int i = 0; i < 6; i++) id[i] = i; do { if (len[id[0]] + len[id[2]] != len[id[1]] + 1) continue; if (len[id[3]] + len[id[5]] != len[id[4]] + 1) continue; if (s[id[0]][0] != s[id[3]][0]) continue; if (s[id[0]][len[id[0]] - 1] != s[id[4]][0]) continue; if (s[id[1]][0] != s[id[3]][len[id[3]] - 1]) continue; if (s[id[1]][len[id[0]] - 1] != s[id[4]][len[id[3]] - 1]) continue; if (s[id[1]][len[id[1]] - 1] != s[id[5]][0]) continue; if (s[id[2]][0] != s[id[4]][len[id[4]] - 1]) continue; if (s[id[2]][len[id[2]] - 1] != s[id[5]][len[id[5]] - 1]) continue; vector<string> cur; cur.clear(); string t = s[id[0]]; for (int i = len[id[0]]; i < len[id[1]]; i++) t += '.'; cur.push_back(t); for (int i = 1; i < len[id[3]] - 1; i++) { t = s[id[3]][i]; for (int j = 1; j < len[id[0]] - 1; j++) t += '.'; t += s[id[4]][i]; for (int j = len[id[0]]; j < len[id[1]]; j++) t += '.'; cur.push_back(t); } cur.push_back(s[id[1]]); for (int i = len[id[3]]; i < len[id[4]] - 1; i++) { t = ""; for (int j = 0; j < len[id[0]] - 1; j++) t += '.'; t += s[id[4]][i]; for (int j = 1; j < len[id[2]] - 1; j++) t += '.'; t += s[id[5]][i - len[id[3]] + 1]; cur.push_back(t); } t = ""; for (int i = 0; i < len[id[0]] - 1; i++) t += '.'; t += s[id[2]]; cur.push_back(t); upd(cur); } while (next_permutation(id, id + 6)); if (!ans.size()) cout << "Impossible" << endl; else { for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int m, q, p[400005]; char s[200005]; int pp[400005], son0[15000000], son1[15000000], sum[15000000], n1; int update(int l, int r, int k, int x) { if (!k) k = ++n1; ++sum[k]; if (l == r) return k; int mid = (l + r) >> 1; if (x <= mid) son0[k] = update(l, mid, son0[k], x); if (x > mid) son1[k] = update(mid + 1, r, son1[k], x); return k; } int merge(int x, int y) { if (!x || !y) return x + y; int z = ++n1; sum[z] = sum[x] + sum[y]; son0[z] = merge(son0[x], son0[y]); son1[z] = merge(son1[x], son1[y]); return z; } int find(int l, int r, int k, int x, int y) { if (l >= x && r <= y) return sum[k]; int mid = (l + r) >> 1, ans = 0; if (x <= mid) ans += find(l, mid, son0[k], x, y); if (y > mid) ans += find(mid + 1, r, son1[k], x, y); return ans; } int n = 1, le, nex[400005], wen[400005], hea[400005], fa[400005], len[400005]; map<int, int> son[400005]; void add(int x, int y) { ++le; nex[le] = hea[x]; wen[le] = y; hea[x] = le; } int insert(int x, int last) { int p1 = last; if (son[p1][x] && len[son[p1][x]] == len[p1] + 1) return son[p1][x]; int p2 = ++n, boo = 0; len[p2] = len[p1] + 1; for (; p1 && !son[p1][x]; p1 = fa[p1]) son[p1][x] = p2; if (!p1) fa[p2] = 1; else { int q1 = son[p1][x]; if (len[q1] == len[p1] + 1) fa[p2] = q1; else { if (p1 == last) boo = 1; int q2 = ++n; fa[q2] = fa[q1]; son[q2] = son[q1]; fa[q1] = fa[p2] = q2; len[q2] = len[p1] + 1; for (; p1 && son[p1][x] == q1; p1 = fa[p1]) son[p1][x] = q2; } } return boo ? n : p2; } void build() { for (int i = 2; i <= n; i++) add(fa[i], i); } void dfs(int x) { for (int i = hea[x]; i; i = nex[i]) { dfs(wen[i]); pp[x] = merge(pp[x], pp[wen[i]]); } } int main() { scanf("%d%d", &m, &q); for (int i = 1; i <= m; i++) { p[i] = 1; scanf("%s", s + 1); int len = strlen(s + 1); for (int j = 1; j <= len; j++) { p[i] = insert(s[j] - 97, p[i]); pp[p[i]] = update(1, m, pp[p[i]], i); } } build(); dfs(1); for (int i = 1; i <= q; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); printf("%d\n", find(1, m, pp[p[z]], x, y)); } return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, p, a[3]; int main() { scanf("%d %d", &n, &p); n %= 6, a[1] = 1, a[2] = 2; for (int i = 1; i <= n; i++) { if (i & 1) { swap(a[0], a[1]); } else { swap(a[1], a[2]); } } putchar(a[p] | 48); return 0; }
1
#include <bits/stdc++.h> using namespace std; int N; void solve() { scanf("%d", &N); int minx, maxx, miny, maxy; long long d = 0; minx = miny = 987654321; maxx = maxy = -1; for (int i = 0; i < N; i++) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); minx = min(minx, x1); minx = min(minx, x2); miny = min(miny, y1); miny = min(miny, y2); maxx = max(maxx, x1); maxx = max(maxx, x2); maxy = max(maxy, y1); maxy = max(maxy, y2); d += (x2 - x1) * (y2 - y1); } if (maxy - miny != maxx - minx) { printf("NO\n"); return; } if ((maxx - minx) * (maxy - miny) != d) { printf("NO\n"); return; } printf("YES\n"); } int main() { solve(); return 0; }
3
#include<bits/stdc++.h> using namespace std; #define FOR(i, j, k) for(int i = j; i <= k; i++) #define ROF(i, j, k) for(int i = j; i >= k; i--) #define PB push_back #define MEM(n, val) memset((n), val, sizeof(n)) #define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define F first #define S second #define MP make_pair #define LL long long #define MOD 1000000007 #define MX 200010 #define INFL 1000000000000000000LL #define INF 1000000000 typedef pair<int, int> PII; typedef pair<LL, LL> PLL; int fx[] = {0, 0, 1, -1}; int fy[] = {1, -1, 0, 0}; int n, x; vector<int> adjl[100010]; int mxlev; int mxfreq; int lev_freq[100010]; int freq[100010]; vector<PII> frv; bool is_lev_a[100010]; int lev[100010]; vector<int> ultalv[100010]; void func(int u, int lv) { mxlev = max(lv, mxlev); lev_freq[lv]++; lev[u] = lv; ultalv[ lv ].PB(u); for(int v: adjl[u]) { func(v, lv + 1); } } int dp[450][100001]; //PII bt[320][100001]; map<int, int> mp_cnt; int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); FastIO; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cin >> n >> x; FOR(v, 2, n) { int u; cin >> u; adjl[u].PB(v); // cout << u << " : " << v << "\n"; } func(1, 1); FOR(i, 1, mxlev) { freq[ lev_freq[i] ]++; mxfreq = max(mxfreq, lev_freq[i]); } FOR(i, 1, mxfreq) { if(freq[i]) { frv.PB({i, freq[i]}); } } // FOR(i, 1, mxlev) { // cout << i << " : " << lev_freq[i] << "\n"; // } // cout << "\n"; // FOR(i, 1, mxfreq) { // cout << i << " : " << freq[i] << "\n"; // } // cout << "\n"; // for(PII val: frv) { // cout << val.F << " : " << val.S << "\n"; // } // if(x == 237) { // cout << "sz : " << frv.size() << endl; // } MEM(dp, -1); // MEM(bt, -1); int turn = 0; for(int i = 0; i < frv.size(); i++) { int val = frv[i].F; int fr = frv[i].S; dp[i][0] = 0; for(int j = 1; j <= x; j++) { if(i != 0 && dp[i - 1][j] >= 0) { dp[i][ j ] = 0; // bt[i][j] = {i - 1, j}; continue; } if((j - val >= 0) && (dp[i][j - val] >= 0) && (dp[i][j - val] < fr)) { dp[i][j] = dp[i][j - val] + 1; // bt[i][j] = {i, j - val}; } } } // cout << "dp : \n"; // for(int i = 0; i < frv.size(); i++) { // for(int j = 0; j <= x; j++) { // cout << dp[i][j] << " "; // } // cout << "\n"; // } // cout << "\n"; // // cout << dp[ (int)frv.size() - 1 ][x] << "\n\n"; if(dp[ (int)frv.size() - 1 ][x] >= 0) { int a = frv.size() - 1, b = x; while(a != -1 && b != -1) { if(dp[a][b]) { // cout << frv[a].F << " : " << dp[a][b] << "\n"; mp_cnt[ frv[a].F ] = max(mp_cnt[ frv[a].F ], dp[a][b]); } // cout << a << " " << b << " : " << frv[a].F << " : " << dp[a][b] << "\n"; PII nn = {-1, -1}; if(a != 0 && dp[a][b] == 0 && dp[a - 1][b] >= 0) { nn = {a - 1, b}; } int val = frv[a].F; int fr = frv[a].S; if((b - val >= 0) && (dp[a][b - val] >= 0) && (dp[a][b - val] < fr) && (dp[a][b] == dp[a][b - val] + 1)) { // dp[i][j] = dp[i][j - val] + 1; nn = {a, b - val}; // bt[i][j] = {i, j - val}; } a = nn.F; b = nn.S; } FOR(i, 1, mxlev) { if(mp_cnt[ lev_freq[i] ]) { mp_cnt[ lev_freq[i] ]--; is_lev_a[i] = 1; } } cout << mxlev << "\n"; FOR(i, 1, n) { if(is_lev_a[ lev[i] ]) { cout << 'a'; } else { cout << 'b'; } } return 0; } map<int, int> exp; int sum = n; FOR(i, 1, mxlev) { if(lev_freq[i] <= x) { is_lev_a[i] = 1; x -= lev_freq[i]; sum -= lev_freq[i]; } else if(lev_freq[i] <= sum - x) { sum -= lev_freq[i]; } else { vector<int> leaf, nonleaf; for(int u: ultalv[i]) { if(adjl[u].size() > 0) { nonleaf.PB(u); } else { leaf.PB(u); } } if(x >= nonleaf.size()) { /// use all x sum -= x; for(int u: nonleaf) { exp[u] = 1; } x -= nonleaf.size(); for(int j = 0; j < x; j++) { int u = leaf[j]; exp[u] = 1; } sum -= leaf.size() - x; x = 0; } else { /// use all sum - x // sum = x; // x = lev_freq[i] - (sum - x); sum -= nonleaf.size(); int leafbaki = (int)leaf.size() - (sum - x); for(int j = 0; j < leafbaki; j++) { int u = leaf[j]; exp[u] = 1; } x -= leafbaki; sum = x; } } } cout << mxlev + 1 << "\n"; FOR(i, 1, n) { if(exp[i]) { cout << 'a'; continue; } if(is_lev_a[ lev[i] ]) { cout << 'a'; } else { cout << 'b'; } } return 0; } /** 2000000000000 */
11
#include <bits/stdc++.h> using namespace std; const int N = 1007; int n, m, sum[N]; pair<int, int> rs[N]; string junk, dir; bool contains(int l1, int r1, int l2, int r2) { return l2 <= l1 && l1 <= r2 || l2 <= r1 && r1 <= r2; } int main() { scanf("%d", &n); scanf("%d", &m); for (int i = (1), _i = (m); i <= _i; ++i) { int p; cin >> junk >> junk >> dir >> junk >> p; if (dir == "left") { rs[i] = {1, p - 1}; } else { rs[i] = {p + 1, n}; } if (rs[i].first > rs[i].second) { cout << "-1"; return 0; } } for (int i = (1), _i = (m); i <= _i; ++i) { for (int j = (i + 1), _j = (m); j <= _j; ++j) { if (!contains(rs[i].first, rs[i].second, rs[j].first, rs[j].second)) { cout << "-1"; return 0; } } } int res = 0; for (int i = (1), _i = (m); i <= _i; ++i) { sum[rs[i].first]++; sum[rs[i].second + 1]--; } for (int i = (1), _i = (n); i <= _i; ++i) { sum[i] += sum[i - 1]; } int max = *max_element(sum + 1, sum + n + 1); for (int i = (1), _i = (n); i <= _i; ++i) { res += sum[i] == max; } cout << res; }
2
#include <bits/stdc++.h> using namespace std; struct Edge { int v, u; long long cap, cost; Edge() : v(), u(), cap(), cost() {} Edge(int _v, int _u, long long _cap, long long _cost) : v(_v), u(_u), cap(_cap), cost(_cost) {} }; const long long INF1 = (long long)1e9; const long long INF2 = (long long)1e15; const int E = (int)4e4; const int V = 200; int S, T; int n, k; int a[V]; long long c[V]; Edge ed[E]; int edSz; long long dist[V]; int par[V]; void addEdge(int v, int u, long long cap, long long cost) { ed[edSz++] = Edge(v, u, cap, cost); ed[edSz++] = Edge(u, v, 0, -cost); } long long flow() { for (int i = 0; i <= S; i++) dist[i] = INF2; dist[S] = 0; bool ch = true; while (ch) { ch = false; for (int i = 0; i < edSz; i++) { Edge e = ed[i]; if (e.cap <= 0) continue; if (dist[e.v] == INF2) continue; long long w = dist[e.v] + e.cost; if (w >= dist[e.u]) continue; dist[e.u] = w; par[e.u] = i; ch = true; } } long long res = 0; int v = T; while (v != S) { int id = par[v]; ed[id].cap--; ed[id ^ 1].cap++; res += ed[id].cost; v = ed[id].v; } return res; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); a[i]--; } for (int i = 0; i < n; i++) scanf("%lld", &c[i]); S = 2 * n; T = 2 * n - 1; for (int i = 0; i < n; i++) addEdge(2 * i, 2 * i + 1, 1, -INF1); for (int i = 0; i < 2 * n - 1; i++) addEdge(i, T, INF1, 0); addEdge(S, T, INF1, 0); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { long long w = (a[i] == a[j] ? 0 : c[a[j]]); addEdge(2 * i + 1, 2 * j, INF1, w); } addEdge(S, 2 * i, INF1, c[a[i]]); } long long ans = INF1 * n; for (int i = 0; i < k; i++) ans += flow(); printf("%lld\n", ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; const long long INFL = LLONG_MAX; const long double pi = acos(-1); string s; int dp2[10010]; int dp3[10010]; int main() { ios_base::sync_with_stdio(0); cout.precision(15); cout << fixed; cout.tie(0); cin.tie(0); cin >> s; int N = int((s).size()); dp2[N - 2] = 1; dp3[N - 3] = 1; set<string> ans; for (int i = N - 1; i >= 5; i--) { if (dp2[i]) { ans.insert(s.substr(i, 2)); dp3[i - 3] = 1; if (s.substr(i, 2) != s.substr(i - 2, 2)) { dp2[i - 2] = 1; } } if (dp3[i]) { ans.insert(s.substr(i, 3)); dp2[i - 2] = 1; if (s.substr(i, 3) != s.substr(i - 3, 3)) { dp3[i - 3] = 1; } } } cout << int((ans).size()) << '\n'; for (auto t : ans) { cout << t << '\n'; } }
5
#include<stdio.h> #include<math.h> char s1[1001]; char s2[1001]; int main() { int n,i,j,k; scanf("%d",&n); int t,d; int o1=0; int o2=0; for(i=1;i<=n;i++) { scanf("%d",&d); scanf("%s",s1); scanf("%s",s2); for(int k=0;k<=d-1;k++) { if(s1[k]>s2[k]) o1++; else if(s1[k]<s2[k]) o2++; } if(o1>o2) printf("RED\n"); if(o1==o2) printf("EQUAL\n"); if(o1<o2) printf("BLUE\n"); o1=0; o2=0; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; char s[maxn]; int a[maxn]; long long dp[maxn][2]; int ls; long long get(int f) { memset(dp, 0, sizeof(dp)); dp[0][f] = 1; for (int i = 1; i <= ls; i++) { if (a[i] == 0) { dp[i][0] = dp[i - 1][0]; } else { if (a[i - 1] == 1) { dp[i][1] = dp[i - 1][0] + dp[i - 1][1]; } else { dp[i][1] = dp[i - 1][0] + dp[i - 1][1]; dp[i][0] = dp[i - 2][0] + dp[i - 2][1]; } } } return dp[ls][f]; } int main() { cin >> s; ls = strlen(s); int num = 0; for (int i = 0; i < ls; i++) { if (s[i] == 'B') break; num++; } for (int i = 0; i < ls; i++) { a[i + 1] = (s[(i + num) % ls] == 'A'); } if (num >= ls - 1) { puts("1"); } else { printf("%lld\n", get(0) + get(1)); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int s = 2; long long int l = 1; if (n == 1) cout << 2 << endl; if (n == 2) { cout << 2 << endl; cout << 17 << endl; } if (n > 2) { cout << 2 << endl; cout << 17 << endl; for (long long int i = 4; i <= n + 1; i++) { long long ans = (i * i * (i - 1) - (i - 2)); cout << ans << endl; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, a, b; cin >> n; while (n--) { cin >> a >> b; cout << a + b << endl; } }
0
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int nega = 1; while (!isdigit(ch)) { if (ch == '-') nega = -1; ch = getchar(); } int ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; return ans; } inline int add(int x, int y) { return x + y >= 998244353 ? x + y - 998244353 : x + y; } inline int sub(int x, int y) { return x - y < 0 ? x - y + 998244353 : x - y; } inline int mul(int x, int y) { return 1LL * x * y % 998244353; } signed main() { int n, m; cin >> n >> m; vector<char> v; for (int i = 1; i < n; i++) v.push_back('U'); for (int i = 1; i < m; i++) v.push_back('L'); for (int i = 1; i <= n; i++) { if (i != 1) v.push_back('D'); if (i & 1) for (int j = 1; j < m; j++) v.push_back('R'); else for (int j = 1; j < m; j++) v.push_back('L'); } cout << v.size() << endl; for (char i : v) putchar(i); return 0; }
4
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); template <class T> T sqr(T x) { return x * x; } template <class T> T alex(T x) { return (x > 0) ? x : -x; } template <class T> void read(T& x) { x = 0; T f = 1; char ch = getchar(); while ((ch < 48 || ch > 57) && ch != 45) ch = getchar(); if (ch == 45) f = -1, ch = getchar(); while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar(); x *= f; } int n; char s[300005]; struct Tree { int que[20]; vector<char> ss[300005]; int cnt; struct AC { struct Node { int son[26]; Node() {} }; vector<Node> son; vector<int> fail, cnt; int rot, len; Node Null; void init() { rot = len = 1; son.clear(); son.push_back(Null); son.push_back(Null); fail.resize(2); cnt.resize(2); fail[0] = fail[1] = cnt[0] = cnt[1] = 0; } int Newnode() { len++; son.push_back(Null), fail.push_back(0), cnt.push_back(0); return len; } void insert(vector<char> s) { int now = rot; for (int i = (0); i < (s.size()); i++) { if (son[now].son[s[i] - 'a']) now = son[now].son[s[i] - 'a']; else { int p = Newnode(); son[now].son[s[i] - 'a'] = p; now = p; } } cnt[now]++; } int que[300005]; void work() { int head = 0, tail = 0; for (int i = (0); i <= (25); i++) if (son[rot].son[i]) fail[que[++tail] = son[rot].son[i]] = rot; else son[rot].son[i] = rot; while (head != tail) { head++; for (int i = (0); i <= (25); i++) if (son[que[head]].son[i]) { que[++tail] = son[que[head]].son[i]; fail[son[que[head]].son[i]] = son[fail[que[head]]].son[i]; } else son[que[head]].son[i] = son[fail[que[head]]].son[i]; } for (int i = (1); i <= (len - 1); i++) cnt[que[i]] += cnt[fail[que[i]]]; } long long query(char* s) { int len = strlen(s + 1); int now = rot; long long res = 0; for (int i = (1); i <= (len); i++) now = son[now].son[s[i] - 'a'], res += cnt[now]; return res; } } AC[20]; void insert(char* s) { que[++que[0]] = 1; cnt++; int len = strlen(s + 1); for (int i = (1); i <= (len); i++) ss[cnt].push_back(s[i]); while (que[0] > 1 && que[que[0]] == que[que[0] - 1]) que[que[0] - 1] <<= 1, que[0]--; AC[que[0]].init(); for (int i = (cnt - que[que[0]] + 1); i <= (cnt); i++) AC[que[0]].insert(ss[i]); AC[que[0]].work(); } long long query(char* s) { long long res = 0; for (int i = (1); i <= (que[0]); i++) res += AC[i].query(s); return res; } } s1, s2; int main() { read(n); for (int i = (1); i <= (n); i++) { int op; read(op); scanf("%s", s + 1); if (op == 1) s1.insert(s); else if (op == 2) s2.insert(s); else printf("%lld\n", s1.query(s) - s2.query(s)), fflush(stdout); } return 0; }
8
#include <bits/stdc++.h> using namespace std; long long A; vector<long long> divis; vector<pair<long long, vector<long long> > > pri_ref; map<long long, vector<long long> > primes; map<long long, vector<long long> >::iterator it; long long dp[7007][2002]; void get_divisors() { for (long long i = 1; i * i <= A; ++i) if (A % i == 0) { divis.push_back(i); if (i != A / i) divis.push_back(A / i); } sort(divis.begin(), divis.end()); } bool is_prime(long long x) { if (x == 0 || x == 1) return false; for (long long i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } long long get_prime_pow(long long x) { if (is_prime(x)) return x; for (long long i = 2; i * i <= x; ++i) if (x % i == 0) { while (x % i == 0) x /= i; if (x == 1) return i; else return -1; } return -1; } void get_good_primes() { for (long long i = 0; i < (long long)divis.size(); ++i) { long long d = divis[i] - 1, prm = get_prime_pow(d); if (prm != -1) primes[prm].push_back(d + 1); } } void get_dp() { for (long long i = 0; i <= (long long)pri_ref.size(); ++i) dp[0][i] = 1; for (long long i = 1; i < (long long)divis.size(); ++i) for (long long j = 1; j <= (long long)pri_ref.size(); ++j) { dp[i][j] = dp[i][j - 1]; for (long long k = 0; k < (long long)pri_ref[j - 1].second.size(); ++k) if (divis[i] % (pri_ref[j - 1].second)[k] == 0) { long long pos = lower_bound(divis.begin(), divis.end(), divis[i] / (pri_ref[j - 1].second)[k]) - divis.begin(); dp[i][j] += dp[pos][j - 1]; } } } int main() { cin >> A; get_divisors(); get_good_primes(); for (it = primes.begin(); it != primes.end(); ++it) pri_ref.push_back(make_pair(it->first, it->second)); get_dp(); cout << dp[divis.size() - 1][primes.size()] << '\n'; return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { long long n, h, i, j, t, m, l, r, q; long long p = 0, k, w, z, x, y, d; q = 1; for (l = 0; l < q; l++) { string s; std::cin >> s; k = s.length(); transform(s.begin(), s.end(), s.begin(), ::tolower); std::cin >> n; string a[n]; x = 0; for (i = 0; i < n; i++) { cin >> a[i]; m = a[i].length(); if (m == k && x == 0) { transform(a[i].begin(), a[i].end(), a[i].begin(), ::tolower); for (j = 0; j < m; j++) { if (s[j] != a[i][j]) { if ((s[j] == '0' && a[i][j] == 'O') || (a[i][j] == 'o' && s[j] == '0')) continue; else if ((s[j] == 'O' && a[i][j] == '0') || (a[i][j] == '0' && s[j] == 'o')) continue; else if (s[j] == '1' && (a[i][j] == 'l' || a[i][j] == 'i' || a[i][j] == 'L' || a[i][j] == 'I')) continue; else if ((s[j] == 'I' || s[j] == 'i') && (a[i][j] == 'l' || a[i][j] == 'L' || a[i][j] == '1')) continue; else if ((s[j] == 'l' || s[j] == 'L') && (a[i][j] == '1' || a[i][j] == 'I' || a[i][j] == 'i')) continue; else break; } } if (j == m) x = 1; } } if (x == 0) std::cout << "YES" << std::endl; else std::cout << "NO" << std::endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; void compute(int n, int m, int k) { int i = 1, j = 1, rnd = 1, cnt = n * m; int h = n * m / k; int r = n * m % k == 0 ? 0 : n * m - h * k + h; vector<int> ans; ans.push_back(h); bool right = true; while (cnt-- > 0) { if (cnt == r - 1) { rnd = 1; h = r; ans.push_back(h); } if (rnd > h) { rnd = 1; ans.push_back(h); } ans.push_back(i); ans.push_back(j); if (right) { j++; } else { j--; } if (j == m + 1) { i++; j--; right = false; } else if (j == 0) { i++; j++; right = true; } rnd++; } for (int i = 0; i < ans.size();) { cout << ans[i] << ' '; int j = ans[i]; int m; for (m = i + 1; m <= i + j * 2; m++) { cout << ans[m] << ' '; } i = m; cout << endl; } } int main(int argc, char** argv) { int n, m, k; cin >> n >> m >> k; compute(n, m, k); return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[105]; int main() { ios_base::sync_with_stdio(false); int n; string second, t, x, y; cin >> n >> second >> t; x = second, y = t; sort(x.begin(), x.end()); sort(y.begin(), y.end()); if (x != y) return cout << -1, 0; vector<int> v; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (second[j] == t[i]) { for (int m = j; m > i; m--) { swap(second[m], second[m - 1]); v.push_back(m); } break; } } } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; }
2
#include <bits/stdc++.h> using namespace std; template <class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> T sqr(T x) { return x * x; } template <class T> T cube(T x) { return x * x * x; } template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0; int ptr = 0; char gc() { if (rsz <= 0) { ptr = 0; rsz = (int)fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; } void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); } int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; } template <class T> bool gi(T &v) { v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc(); while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true; } const double PI = acos(-1.0); const double eps = 1e-9; const int inf = (int)1e9 + 5; const long long linf = (long long)1e17 + 5; const long long mod = 10003; long long n1[33], n2[33]; int a; int n, m; int main() { gi(n); gi(m); memset(n1, 0, sizeof(n1)); memset(n2, 0, sizeof(n2)); for (int i = 0; i < (n); ++i) { gi(a); for (int j = 0; j < (31); ++j) if (getbit(a, j)) n1[j]++; } for (int i = 0; i < (m); ++i) { gi(a); n2[a]++; } long long res = 0; bool flag = true; for (int i = 0; i < (31); ++i) { if (!flag) break; while (n2[i] > 0) { while (n1[i] > 0 && n2[i] > 0) { n1[i]--; n2[i]--; res++; } if (n2[i] > 0) { flag = false; for (int j = i + 1; j <= 30; j++) { if (n1[j] > 0) { flag = true; n1[j]--; for (int k = (i); k <= (j - 1); ++k) n1[k]++; n1[i]++; break; } } } if (!flag) break; } } cout << res; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long valid(long long k, long long n) { long long s1 = 0, s2 = 0, take; while (true) { take = min(k, n); s1 += take; n -= take; take = n / 10; s2 += take; n -= take; if (n == 0) break; } return s1 >= s2; } int main() { long long n; cin >> n; long long lo = 1LL, hi = 1000000000000000000LL, me; while (lo < hi) { me = lo + (hi - lo) / 2; if (valid(me, n)) hi = me; else lo = me + 1; } cout << lo << endl; }
3
#include <bits/stdc++.h> using namespace std; string str; struct myst { int m, v, p; bool operator<(const myst a) const { return m == a.m ? v > a.v : m > a.m; } }; priority_queue<myst> lq, rq; int n, k, h; myst val[100005]; const double gap = 0; vector<int> v; int temp[100005]; int fv[100005]; bool check(double d) { int bs = k; for (int i = 0; i < n; ++i) { if (1.0 * bs * h <= val[i].v * (d + gap)) { temp[k - bs] = val[i].p; bs--; if (bs == 0) { memcpy(fv, temp, k * sizeof(int)); return true; } } } return false; } void output(double d) { for (int i = k - 1; i >= 0; i--) cout << fv[i] << " "; cout << endl; } double ef(double b, double e) { if (b + gap >= e) return e; double half = (b + e) / 2; if (check(half)) e = half; else b = half; return ef(b, e); } int main() { cin >> n >> k >> h; for (int i = 0; i < n; ++i) { cin >> val[i].m; } for (int i = 0; i < n; ++i) cin >> val[i].v; for (int i = 0; i < n; ++i) val[i].p = i + 1; sort(val, val + n); double b = 0, e = k * h; for (int i = 0; i < 500; i++) { double half = (b + e) / 2; if (check(half)) e = half; else b = half; } output(e); return 0; }
6
#include <bits/stdc++.h> using namespace std; struct ab { int a; int b; }; stack<ab> s; void print() { if (s.empty()) return; struct ab var; var = s.top(); s.pop(); print(); printf("%d %d\n", var.a + 1, var.b + 1); } int main(void) { int n, i, count, last; struct ab av; scanf("%d", &n); count = 0; vector<int> a(n); set<int> myset; set<int>::iterator it; for (i = 0; i < n; i++) scanf("%d", &a[i]); last = 0; for (i = 0; i < n; i++) { if (myset.find(a[i]) == myset.end()) myset.insert(a[i]); else { av.a = last; av.b = i; s.push(av); last = i + 1; count++; myset.clear(); } } if (count == 0) printf("-1"); else { av = s.top(); if (av.b != n - 1) av.b = n - 1; s.pop(); s.push(av); printf("%d\n", count); print(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long ksm(long long a, long long b) { if (!b) return 1; long long ns = ksm(a, b >> 1); ns = ns * ns % 998244353; if (b & 1) ns = ns * a % 998244353; return ns; } struct th { int x, y; th(int a, int b) { x = a, y = b; } bool operator<(const th &r) const { if (x != r.x) return x < r.x; return y < r.y; } }; multiset<int> r1, r2; long long work(vector<th> r) { int n = r.size(); r1.clear(), r2.clear(); sort(r.begin(), r.end()); for (int i = 0; i < n; i++) r2.insert(r[i].y); long long nans = 0; for (int i = 0; i < n; i++) { r2.erase(r2.find(r[i].y)), r1.insert(r[i].y); if (r1.empty() || r2.empty()) continue; int dv = (r[i + 1].x - r[i].x) / 2; int l1 = max(*r1.begin(), *r2.begin()) + 1; int rr = min(*(--r1.end()), *(--r2.end())) - 1; if (l1 > rr) continue; nans += 1ll * dv * ((rr - l1) / 2 + 1); } return nans; } int main() { int n; cin >> n; vector<th> r1, r2; for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); th r = th(a + b, a - b); if ((a + b) & 1) r1.push_back(r); else r2.push_back(r); } long long ans = work(r1) + work(r2); cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const bool debug = !false; const double epsilon = 1e-9; double f(double x) { if (x < 0) return -f(-x); return int(x + epsilon); } bool ok(complex<double> x) { if (debug) cerr << x << endl; double a = x.imag(), b = x.real(); return fabs(a - f(a)) <= epsilon && fabs(b - f(b)) <= epsilon; } int main() { double ax, ay, bx, by, cx, cy; cin >> ax >> ay >> bx >> by >> cx >> cy; complex<double> A(ax, ay), B(bx, by), C(cx, cy), i(0, 1); if (fabs(cx) < epsilon && fabs(cy) < epsilon) { for (int k = 0; k < 4; k++, A *= i) if (A == B) { cout << "YES" << endl; return 0; } cout << "NO" << endl; return 0; } if (ok((B - A) / C) || ok((B - A * i) / C) || ok((B + A) / C) || ok((B + A * i) / C)) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 300; const long long Mod = 998244353, OO = 2e9; int root, A[N]; unordered_map<int, int> freq; unordered_map<int, bool> ok1, ok2; vector<int> Sol(N); struct query { int l, r, idx; query() {} bool operator<(const query &qu) const { if (l / root != qu.l / root) { return l < qu.l; } return r < qu.r; } }; int ans = 0; void add(int idx) { freq[A[idx]]++; if (freq[A[idx]] == A[idx]) { ++ans; ok1[A[idx]] = true; } if (freq[A[idx]] > A[idx] && ok1[A[idx]]) { --ans; ok1[A[idx]] = false; } } void remove(int idx) { freq[A[idx]]--; if (freq[A[idx]] == A[idx]) { ++ans; ok1[A[idx]] = true; } if (freq[A[idx]] < A[idx] && ok1[A[idx]]) { --ans; ok1[A[idx]] = false; } } query qu[N]; int main() { int n, m; scanf("%d%d", &n, &m); root = sqrt(n) + 1; for (int i = 1; i <= n; ++i) { scanf("%d", A + i); } for (int i = 1; i <= m; ++i) { scanf("%d%d", &qu[i].l, &qu[i].r); qu[i].idx = i; } sort(qu + 1, qu + 1 + m); int l = 1, r = 0; for (int i = 1; i <= m; ++i) { while (r < qu[i].r) { r++; add(r); } while (r > qu[i].r) { remove(r); --r; } while (l < qu[i].l) { remove(l); ++l; } while (l > qu[i].l) { --l; add(l); } Sol[qu[i].idx] = ans; } for (int i = 1; i <= m; ++i) { printf("%d ", Sol[i]); } printf("\n"); }
5
#include <bits/stdc++.h> using namespace std; template <class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; } template <class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; } const int MAXN = (1 << 20); int n; pair<int, int> a[MAXN]; void read() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; } vector<pair<int, int> > answer; void rec(int l, int r) { if (l == r) return; int mid = (l + r) >> 1; for (int i = l; i <= r; i++) answer.push_back({a[mid].first, a[i].second}); rec(l, mid); rec(mid + 1, r); } void solve() { sort(a + 1, a + n + 1); rec(1, n); for (int i = 1; i <= n; i++) answer.push_back(a[i]); sort(answer.begin(), answer.end()); answer.erase(unique(answer.begin(), answer.end()), answer.end()); cout << answer.size() << '\n'; for (auto it : answer) cout << it.first << " " << it.second << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 10; long long n, a[N], m; long long mxl[N << 2], mxr[N << 2], mx[N << 2], cl[N << 2], cr[N << 2], tag[N << 2]; long long lans[N << 2], rans[N << 2]; void psd(long long x) { if (tag[x]) { tag[x << 1] += tag[x], tag[x << 1 | 1] += tag[x]; cl[x << 1] += tag[x], cr[x << 1] += tag[x]; cl[x << 1 | 1] += tag[x], cr[x << 1 | 1] += tag[x]; tag[x] = 0; } } void upd(long long x, long long l, long long r) { long long mid = (l + r) >> 1; cl[x] = cl[x << 1], cr[x] = cr[x << 1 | 1]; mx[x] = max(mx[x << 1], mx[x << 1 | 1]); mxl[x] = mxl[x << 1], mxr[x] = mxr[x << 1 | 1]; if (mxl[x << 1] == mid - l + 1 && cr[x << 1] > cl[x << 1 | 1]) mxl[x] = mxl[x << 1] + mxl[x << 1 | 1]; if (mxr[x << 1 | 1] == r - mid && cr[x << 1] < cl[x << 1 | 1]) mxr[x] = mxr[x << 1 | 1] + mxr[x << 1]; lans[x] = lans[x << 1], rans[x] = rans[x << 1 | 1]; if (cr[x << 1] > cl[x << 1 | 1]) { if (lans[x] == mid - l + 1) lans[x] += mxl[x << 1 | 1]; mx[x] = max(mx[x], rans[x << 1] + mxl[x << 1 | 1]); } if (cr[x << 1] < cl[x << 1 | 1]) { if (rans[x] == r - mid) rans[x] += mxr[x << 1]; mx[x] = max(mx[x], lans[x << 1 | 1] + mxr[x << 1]); } if (mxr[x << 1] == mid - l + 1 && cr[x << 1] < cl[x << 1 | 1]) lans[x] = max(lans[x], mid - l + 1 + lans[x << 1 | 1]); if (mxl[x << 1 | 1] == r - mid && cr[x << 1] > cl[x << 1 | 1]) rans[x] = max(rans[x], r - mid + rans[x << 1]); mx[x] = max(mx[x], max(lans[x], rans[x])); } void build(long long x, long long l, long long r) { if (l == r) { mxl[x] = mxr[x] = mx[x] = lans[x] = rans[x] = 1; cl[x] = cr[x] = a[l]; return; } long long mid = (l + r) >> 1; build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r); upd(x, l, r); } void modify(long long x, long long l, long long r, long long lx, long long rx, long long d) { if (lx <= l && r <= rx) { tag[x] += d, cl[x] += d, cr[x] += d; return; } psd(x); long long mid = (l + r) >> 1; if (lx <= mid) modify(x << 1, l, mid, lx, rx, d); if (rx > mid) modify(x << 1 | 1, mid + 1, r, lx, rx, d); upd(x, l, r); } signed main() { cin >> n; for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); build(1, 1, n); cin >> m; while (m--) { long long l, r, d; scanf("%lld%lld%lld", &l, &r, &d); modify(1, 1, n, l, r, d); printf("%lld\n", mx[1]); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; int n, m, u, v, z; vector<int> V[N], P[N]; int d[N], dp[N], pre[N]; int ans; struct Node { int x; int w; bool operator<(const Node& rhs) const { return w > rhs.w; } }; struct Line { int u; int v; int z; } e[N]; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d %d %d", &u, &v, &z); e[i].u = u; e[i].v = v; e[i].z = z; V[u].push_back(v); P[u].push_back(z); V[v].push_back(u); P[v].push_back(z); if (z == 1) ans++; } for (int i = 1; i <= n; i++) d[i] = INF; priority_queue<Node> Q; Q.push(Node{1, 0}); d[1] = 0; while (!Q.empty()) { Node u = Q.top(); Q.pop(); int x = u.x; int w = u.w; if (w > d[x]) continue; for (int i = 0; i < V[x].size(); i++) { int to = V[x][i]; if (w + 1 >= d[to]) continue; d[to] = w + 1; Q.push(Node{to, w + 1}); } if (x == 1) dp[x] = 0; else dp[x] = INF; for (int i = 0; i < V[x].size(); i++) { int from = V[x][i]; if (d[from] + 1 != w) continue; int upd = dp[from] + (P[x][i] == 0 ? 1 : -1); if (upd < dp[x]) { dp[x] = upd; pre[x] = from; } } } cout << ans + dp[n] << endl; int cur = n; set<pair<int, int> > S; while (pre[cur]) { S.insert(pair<int, int>(pre[cur], cur)); S.insert(pair<int, int>(cur, pre[cur])); cur = pre[cur]; } for (int i = 1; i <= m; i++) { if (S.count(pair<int, int>(e[i].u, e[i].v))) { if (e[i].z == 0) printf("%d %d %d\n", e[i].u, e[i].v, 1); } else { if (e[i].z == 1) { printf("%d %d %d\n", e[i].u, e[i].v, 0); } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long infLL = 0x3f3f3f3f3f3f3f3fLL; const int maxn = 1000000 + 5; vector<int> multiply(const vector<int> &p1, const vector<int> &p2) { vector<int> res(p1.size()); for (int i = 0; i < res.size(); ++i) res[i] = p1[p2[i]]; return res; } vector<int> pow(vector<int> a, long long b) { vector<int> res(a.size()); for (int i = 0; i < res.size(); ++i) res[i] = i; for (; b; a = multiply(a, a), b >>= 1) if (b & 1) res = multiply(res, a); return res; } int n, m, d, k; char s[maxn]; vector<int> S, P, C; int main() { scanf("%s%d", s, &m); n = strlen(s); S.resize(n); P.resize(n); C.resize(n); for (int i = 0; i < (n); ++i) S[i] = i, C[i] = (i + 1) % n; while (m--) { scanf("%d%d", &k, &d); int now = 0; for (int i = 0; i < d; ++i) for (int j = i; j < k; j += d) P[now++] = j; for (int i = k; i < n; ++i) P[i] = i; vector<int> t = multiply(S, pow(multiply(P, C), n - k + 1)); for (int i = 0; i < (n); ++i) S[i] = t[(i + k - 1) % n]; for (int i = 0; i < (n); ++i) putchar(s[S[i]]); puts(""); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int IN() { int c, f, x; while (!isdigit(c = getchar()) && c != '-') ; c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0'); while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0'; return !f ? x : -x; } const int N = 3000 + 19; char s[N][N]; int A[N], id[N]; int n, m, a, b, sum; bool check() { int sum = 0, top = 0; for (int i = 1; i < n + 1; i++) { sum += A[i]; top += 2 * (n - i); if (sum > top) return 0; } if (sum != top) return 0; for (int i = 1; i < n; i++) if (A[i] < A[i + 1]) return 0; return 1; } bool cmp(int a, int b) { return A[a] > A[b]; } void Work(int x) { if (x == n) return; sort(id + x, id + n + 1, cmp); if (A[id[x]] & 1) { int k = (A[id[x]] - 1) / 2; for (int i = n; i > x; i--) { int a = k--; if (a > 0) { s[id[x]][id[i]] = 'W', s[id[i]][id[x]] = 'L'; } else if (a == 0) { s[id[x]][id[i]] = s[id[i]][id[x]] = 'D'; A[id[i]]--; } else { s[id[x]][id[i]] = 'L', s[id[i]][id[x]] = 'W'; A[id[i]] -= 2; } } } else { int k = A[id[x]] / 2; for (int i = n; i > x; i--) if (k-- > 0) { s[id[x]][id[i]] = 'W', s[id[i]][id[x]] = 'L'; } else { s[id[x]][id[i]] = 'L', s[id[i]][id[x]] = 'W'; A[id[i]] -= 2; } } Work(x + 1); } int main() { n = IN(), m = IN(); for (int i = 1; i < m + 1; i++) { A[i] = IN(); sum += A[i]; } if (n > m) { sum = n * (n - 1) - sum; a = sum / (n - m); b = sum % (n - m); for (int i = m + 1; i < n + 1; i++) if (b-- > 0) A[i] = a + 1; else A[i] = a; } if (!check()) { puts("no"); return 0; } puts("yes"); for (int i = 1; i < n + 1; i++) { id[i] = i; s[i][i] = 'X'; } Work(1); for (int i = 1; i < n + 1; i++) { puts(s[i] + 1); } }
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int n, w, h; struct Node { int id, pos, ti; int ansx, ansy; } x[MAXN], y[MAXN]; int tx, ty; struct Node2 { int x, y; } ans[MAXN]; bool cmp1(Node a, Node b) { return a.pos < b.pos; } bool cmp2(Node a, Node b) { return a.pos > b.pos; } map<int, vector<int> > mp; int main() { scanf("%d%d%d", &n, &w, &h); tx = ty = 0; int g, p, t; for (int i = 0; i < n; i++) { scanf("%d%d%d", &g, &p, &t); if (g == 1) { x[tx].id = i; x[tx].pos = p; x[tx].ti = t; x[tx].ansx = p; x[tx].ansy = h; tx++; } else { y[ty].id = i; y[ty].pos = p; y[ty].ti = t; y[ty].ansx = w; y[ty].ansy = p; ty++; } } sort(x, x + tx, cmp1); sort(y, y + ty, cmp2); for (int i = 0; i < ty; i++) { mp[y[i].pos - y[i].ti].push_back(y[i].id); } for (int i = 0; i < tx; i++) { mp[x[i].pos - x[i].ti].push_back(x[i].id); } int id; for (int i = 0; i < tx; i++) { id = *mp[x[i].pos - x[i].ti].begin(); mp[x[i].pos - x[i].ti].erase(mp[x[i].pos - x[i].ti].begin()); ans[id].x = x[i].ansx; ans[id].y = x[i].ansy; } for (int i = 0; i < ty; i++) { id = *mp[y[i].pos - y[i].ti].begin(); mp[y[i].pos - y[i].ti].erase(mp[y[i].pos - y[i].ti].begin()); ans[id].x = y[i].ansx; ans[id].y = y[i].ansy; } for (int i = 0; i < n; i++) printf("%d %d\n", ans[i].x, ans[i].y); }
5
#include <bits/stdc++.h> using namespace std; const int maxn = (1e5) + 9; int h[20]; int an[160]; int n, a, b; bool dfs(int ans, int x, int l) { if (ans == 0) { for (int i = 1; i <= n; i++) { if (h[i] >= 0) return false; } return true; } if (h[x] < 0) return dfs(ans, x + 1, l); if (x == n) { h[n] -= b; an[ans] = n - 1; if (dfs(ans - 1, x, l)) return true; h[n] += b; } else { for (int i = max(x, l); i <= x + 1; i++) { if ((1 < i) && (i < n)) { an[ans] = i; h[i] -= a; h[i - 1] -= b; h[i + 1] -= b; if (dfs(ans - 1, x, i)) return true; h[i] += a; h[i - 1] += b; h[i + 1] += b; } } } return false; } int main() { scanf("%d%d%d", &n, &a, &b); int i, j; for (i = 1; i <= n; i++) { scanf("%d", h + i); } int ans = 1; while (true) { if (dfs(ans, 1, 2)) { printf("%d\n", ans); i = ans; while (i) { printf("%d ", an[i]); i--; } printf("\n"); return 0; } ans++; } return 0; }
9
#include <bits/stdc++.h> using namespace std; char s[50]; int ptr = 0; int getNext() { int i = 0; while (s[ptr] >= '0' && s[ptr] <= '9') { i = i * 10 + s[ptr++] - '0'; } return i; } int tenpow(int n) { int r = 1; while (n-- > 0) { r *= 10; } return r; } int digits(int n) { int r = 0; while (n >= 10) { n /= 10; r++; } return r; } int main() { scanf("%s", s); int n = getNext(); while (s[ptr] != '\0') { if (s[ptr++] == '+') { n += getNext(); } else { n -= getNext(); } } for (int i = digits(n); i >= 0; i--) { int k = (n / tenpow(i)) % 10; for (int j = 0; j < k; j++) { printf("+"); } printf("++++++++++++++++++++++++++++++++++++++++++++++++.>"); } return 0; }
6
#include <bits/stdc++.h> using namespace std; using ll = long long; vector<int> t; int n; void init(int nn) { n = nn; t.assign(n, 0); } int sum(int r) { int result = 0; for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r]; return result; } void inc(int i, int delta) { for (; i < n; i = (i | (i + 1))) t[i] += delta; } int main() { ios_base::sync_with_stdio(false); cin >> n; init(n); vector<int> a(n); for (int i = 0; i < (int)(n); ++i) { inc(i, 1); cin >> a[i]; } vector<int> b((a).begin(), (a).end()); sort((b).begin(), (b).end()); ll res = 0; for (int i = 0; i < (int)(n); ++i) { int pos = lower_bound((b).begin(), (b).end(), a[i]) - b.begin(); inc(pos, -1); res += sum(pos); } if ((n & 1) == (res & 1)) { cout << "Petr\n"; } else { cout << "Um_nik\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; const int mod = 1e9 + 7; const int inf = 1e9; const int N = 2e3 + 10; double dp[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, t; double p; cin >> n >> p >> t; dp[0][0] = 1.0; for (int i = 0; i < t; i++) { for (int j = 0; j <= n; j++) { if (j == n) { dp[i + 1][j] += dp[i][j]; } else { dp[i + 1][j] += dp[i][j] * (1.0 - p); dp[i + 1][j + 1] += dp[i][j] * p; } } } double ans = 0; for (int i = 1; i <= n; i++) { ans += dp[t][i] * i; } cout << fixed << setprecision(6) << ans << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int M = 15; const int INF = 1 << 30; int n, m[N], p[N]; bool ok[2][N]; bool init() { if (!(cin >> n)) return 0; for (int i = 1; i <= n; i++) scanf("%d", &m[i]); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= n; i++) ok[0][i] = ok[1][i] = 0; return 1; } priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > que; long long num[N] = {0}; pair<long long, long long> gettop(int step) { while (!que.empty()) { pair<long long, long long> now = que.top(); que.pop(); if (now.second < step) continue; else que.push(now); return now; } cout << "ERROR" << endl; pair<long long, long long> err; return err; } void cal(bool f) { num[1] = m[1] - p[1]; for (int i = 2; i <= n; i++) num[i] = num[i - 1] + m[i] - p[i]; for (int i = 1; i <= n; i++) que.push(make_pair(num[i], i)); long long sum = 0; for (int i = 1; i <= n; i++) { pair<long long, long long> now = gettop(i); long long x = now.first; x -= sum; if (x >= 0) ok[f][i] = 1; sum += (m[i] - p[i]); que.push(make_pair(num[i], i + n)); } while (!que.empty()) que.pop(); } void solve() { cal(0); for (int i = 1; i <= n / 2; i++) swap(p[i], p[n - i + 1]); for (int i = 2; i <= (n - 1) / 2 + 2; i++) swap(m[i], m[n - i + 2]); cal(1); } void out() { if (ok[1][1]) ok[0][1] = 1; for (int i = 2; i <= n; i++) if (ok[1][i]) ok[0][n + 2 - i] = 1; int res = 0; for (int i = 1; i <= n; i++) if (ok[0][i]) res++; cout << res << endl; for (int i = 1; i <= n; i++) if (ok[0][i]) printf("%d ", i); printf("\n"); } int main() { while (init()) { solve(); out(); } }
6
#include <bits/stdc++.h> using namespace std; const long long Mod = 1000000007; const int N = 1e6 + 5; int sz, mx, id, ii, used[N]; map<int, string> ID; vector<string> Lev[N]; vector<long long> n, d; long long nn(string &s) { long long res = 0; for (auto i : s) { res = res * 10 + i - '0'; } return res; } void f(string &s) { string t = ""; for (int i = 0; s[i]; i++) { if (s[i] == ',') { if (t[0] >= '0' && t[0] <= '9') { n.push_back(nn(t)); } else { d.push_back(++ii); ID[ii] = t; } t = ""; } else t += s[i]; } } void dfs(int lev) { if (id >= sz) return; mx = max(mx, lev); used[d[id]] = true; Lev[lev].push_back(ID[d[id]]); int k = n[id]; while (k--) { ++id; dfs(lev + 1); } } int main() { string s; cin >> s; s += ',', f(s); sz = d.size(); for (int i = 0; i < sz; i++) { if (!used[d[i]]) { dfs(1); id++; } } printf("%d\n", mx); for (int i = 1; i <= mx; i++) { if (Lev[i].size()) { for (auto u : Lev[i]) cout << u << ' '; cout << '\n'; } } }
4
#include <bits/stdc++.h> using std::priority_queue; template <class T> inline void read(T &x) { T f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = (x * 10) + (s & 15); s = getchar(); } x *= f; } const int maxn = 1e5 + 1; struct Edge { int u, v; } e[maxn << 1]; int head[maxn], ecnt; inline void addedge(int u, int v) { e[++ecnt].v = v; e[ecnt].u = head[u]; head[u] = ecnt; } inline void add(int u, int v) { addedge(u, v); addedge(v, u); } int dep[maxn], n, a, b, root, dfn[maxn], s[maxn], siz[maxn], idx, ans, F[maxn]; int val[maxn << 2], id[maxn << 2]; inline void pushup(int rt) { if (val[rt << 1] > val[rt << 1 | 1]) val[rt] = val[rt << 1], id[rt] = id[rt << 1]; else val[rt] = val[rt << 1 | 1], id[rt] = id[rt << 1 | 1]; } inline void insert(int rt, int l, int r, int k, int ch) { if (l == r) return (void)(val[rt] = ch, id[rt] = k); if (k <= ((l + r) >> 1)) insert(rt << 1, l, ((l + r) >> 1), k, ch); else insert(rt << 1 | 1, ((l + r) >> 1) + 1, r, k, ch); pushup(rt); } inline int query(int rt, int l, int r, int L, int R) { if (L <= l && r <= R) return id[rt]; int ans = 0; if (L <= ((l + r) >> 1)) { int ql = query(rt << 1, l, ((l + r) >> 1), L, R); if (dep[s[ql]] > dep[s[ans]]) ans = ql; } if (R > ((l + r) >> 1)) { int qr = query(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R); if (dep[s[qr]] > dep[s[ans]]) ans = qr; } return ans; } inline void pre(int x, int f) { for (int i = head[x], v; i && (v = e[i].v); i = e[i].u) { if (v == f) continue; dep[v] = dep[x] + 1; pre(v, x); } } inline void dfs(int x, int f) { dfn[x] = (++idx); siz[x] = 1; F[x] = f; s[idx] = x; for (int i = head[x], v; i && (v = e[i].v); i = e[i].u) { if (v == f) continue; dep[v] = dep[x] + 1; dfs(v, x); siz[x] += siz[v]; } if (siz[x] == 1) insert(1, 1, n, dfn[x], dep[x]); } struct Node { int x, to; inline bool operator<(Node yny) const { return (dep[to] - dep[x] + (x != root)) < (dep[yny.to] - dep[yny.x] + (yny.x != root)); } } tp; priority_queue<Node> q; inline void dfs(int x, int f, int goal) { for (int i = head[x], v; i && (v = e[i].v); i = e[i].u) { if (v == F[x]) continue; if ((dfn[v] <= dfn[goal]) && ((dfn[v] + siz[v] - 1) >= dfn[goal])) dfs(v, f, goal); else q.push((Node){v, s[query(1, 1, n, dfn[v], dfn[v] + siz[v] - 1)]}); } } int main() { read(n); for (int i = 1; i <= n - 1; i++) read(a), read(b), add(a, b); pre(1, 0); for (int i = 1; i <= n; i++) if (dep[i] > dep[root]) root = i; dep[root] = 0; dfs(root, 0); q.push((Node){root, s[query(1, 1, n, 1, n)]}); ans = 1; for (int i = 1; i <= n; i++) { if (q.empty()) { printf("%d ", n); continue; } tp = q.top(); q.pop(); printf("%d ", ans); ans += (dep[tp.to] - dep[tp.x] + (tp.x != root)); dfs(tp.x, 0, tp.to); } }
7
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; long long max1(long long a, long long b, long long c) { long long d; if (a >= b && a >= c) d = a; else if (b >= c && b >= a) d = b; else if (c >= a && c >= b) d = c; return d; } long long dp[2001][2001], s = 0; vector<long long> v[2001]; long long f(long long n, long long k, long long mod) { long long sum = 0; if (k == 1) return 1; else if (dp[n][k] != -1) return dp[n][k]; else { for (int i = 0; i < v[n].size(); i++) { sum = (sum + f(v[n][i], k - 1, mod)) % mod; } } return dp[n][k] = sum; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, k, p = 1, j, l = 1, i; cin >> n >> k; long long mod = 1e9 + 7; for (i = 1; i <= n; i++) { for (j = 1; j <= k; j++) { dp[i][j] = -1; } } for (i = 1; i <= k; i++) { dp[1][i] = 1; } for (i = 2; i <= n; i++) { p = 1; l = 1; for (j = 2; j * j <= i; j++) { if (i % j == 0) { p = 0; break; } } if (p == 1) { for (j = 1; j <= k; j++) { dp[i][j] = l % mod; l = (l + 1) % mod; } } } for (i = 4; i <= 2000; i++) { for (j = 1; j * j <= i; j++) { if (i % j == 0) { if (i == j * j) v[i].push_back(j); else if (i != j * j) { v[i].push_back(j); v[i].push_back(i / j); } } } } long long sum = 0; for (i = 1; i <= n; i++) { sum = (sum + f(i, k, mod)) % mod; } cout << sum; }
3
#include <bits/stdc++.h> using namespace std; int n, w[301000]; long long Res; struct IdxTree { int M, C; } IT[1000010]; int Mx[301000], Mn[301000]; void UDT(int nd) { IT[nd] = IT[nd * 2]; if (IT[nd].M > IT[nd * 2 + 1].M) IT[nd] = IT[nd * 2 + 1]; else if (IT[nd].M == IT[nd * 2 + 1].M) IT[nd].C += IT[nd * 2 + 1].C; } void Add(int nd, int M, int C) { nd += 500005; IT[nd].M = M, IT[nd].C = C; while (nd != 1) { nd /= 2; UDT(nd); } } int Gap(int b, int e) { int r; r = 1000000000; b += 500005, e += 500005; while (b <= e) { r = min(r, IT[b].M); r = min(r, IT[e].M); b = (b + 1) / 2; e = (e - 1) / 2; } return r; } int Sum(int b, int e, int t) { int r; r = 0; b += 500005, e += 500005; while (b <= e) { if (b % 2 == 1 && IT[b].M == t) r += IT[b].C; if (e % 2 == 0 && IT[e].M == t) r += IT[e].C; b = (b + 1) / 2; e = (e - 1) / 2; } return r; } void Do(int b, int e) { int pv, m, i, MMx = -1, MMn = n + 1, bb, t, ee; if (b == e) { Res++; return; } m = (b + e) / 2; Do(b, m); Do(m + 1, e); for (i = m; i >= b; i--) { if (MMx < w[i]) MMx = w[i]; if (MMn > w[i]) MMn = w[i]; Mx[i] = MMx, Mn[i] = MMn; } MMx = -1, MMn = n + 1; for (i = m + 1; i <= e; i++) { if (MMx < w[i]) MMx = w[i]; if (MMn > w[i]) MMn = w[i]; Mx[i] = MMx, Mn[i] = MMn; } for (i = m + 1; i <= e; i++) Add(i, Mx[i] - i, 1); pv = m; for (i = m; i >= b; i--) { while (pv + 1 <= e && Mn[pv + 1] > Mn[i]) pv += 1; bb = i + Mx[i] - Mn[i]; if (bb == m) bb = m + 1; if (bb > pv) continue; t = Gap(bb, bb); if (t <= Mn[i] - i) Res++; bb += 1; if (bb > pv) continue; t = Gap(bb, pv); if (t != Mn[i] - i) continue; Res += Sum(bb, pv, t); } for (i = b; i <= m; i++) Add(i, Mx[i] + i, 1); pv = m + 1; for (i = m + 1; i <= e; i++) { while (pv - 1 >= b && Mn[pv - 1] > Mn[i]) pv -= 1; ee = i - (Mx[i] - Mn[i]); if (ee == m + 1) ee = m; if (ee < pv) continue; t = Gap(ee, ee); if (t <= Mn[i] + i) Res += 1; ee -= 1; if (ee < pv) continue; t = Gap(pv, ee); if (t != Mn[i] + i) continue; Res += Sum(pv, ee, t); } } int main(void) { int i, a, b; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d%d", &a, &b); w[a] = b; } Do(1, n); printf("%I64d\n", Res); }
11
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; char s[maxn]; queue<int> que0, que1; vector<int> ve[maxn]; int pt[maxn]; int main() { scanf("%s", s + 1); int len = strlen(s + 1); if (s[1] == '1') { puts("-1"); return 0; } que0.push(1); pt[1] = 1; ve[1].push_back(1); int flag = 0; int k = 2; for (int i = 2; i <= len; i++) { if (s[i] == '1') { if (!que0.empty()) { int v = que0.front(); que0.pop(); int zu = pt[v]; ve[zu].push_back(i); que1.push(i); pt[i] = zu; } else { flag = 1; break; } } else { if (!que1.empty()) { int v = que1.front(); que1.pop(); int zu = pt[v]; ve[zu].push_back(i); que0.push(i); pt[i] = zu; } else { que0.push(i); pt[i] = k; ve[k++].push_back(i); } } } if (!que1.empty()) { puts("-1"); return 0; } printf("%d\n", k - 1); for (int i = 1; i < k; i++) { printf("%d ", ve[i].size()); for (int j = 0; j < ve[i].size(); j++) { printf("%d%c", ve[i][j], j == ve[i].size() - 1 ? '\n' : ' '); } } return 0; }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; template <class T, class U> inline void Max(T &a, U b) { if (a < b) a = b; } template <class T, class U> inline void Min(T &a, U b) { if (a > b) a = b; } inline void add(int &a, int b) { a += b; while (a >= 1000003) a -= 1000003; } int pow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = ans * (long long)a % 1000003; a = (long long)a * a % 1000003; b >>= 1; } return ans; } char s[55][55]; int main() { int T = 100, j, k, i, ca = 0, n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%s", s[i]); int first = -1, second = -1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (s[i][j] == '4') { first = i, second = j; goto r; } r:; if (first == -1) { puts("No"); return 0; } int w = -1, h = -1; for (int i = first; i < n; i++) { if (s[i][second] != '4') { h = i - first; break; } j = second; while (j < n && s[i][j] == '4') j++; if (w == -1) w = j - second; else if (w != j - second) { puts("No"); return 0; } } if (first) for (int i = second; i < second + w; i++) if (s[first - 1][i] != '2') { puts("No"); return 0; } if (first + h < n) for (int i = second; i < second + w; i++) if (s[first + h][i] != '2') { puts("No"); return 0; } if (second) for (int i = first; i < first + h; i++) if (s[i][second - 1] != '2') { puts("No"); return 0; } if (second + w < n) for (int i = first; i < first + h; i++) if (s[i][second + w] != '2') { puts("No"); return 0; } if (first && second && s[first - 1][second - 1] != '1') { puts("No"); return 0; } if (first && second + w < n && s[first - 1][second + w] != '1') { puts("No"); return 0; } if (first + h < n && second && s[first + h][second - 1] != '1') { puts("No"); return 0; } if (first + h < n && second + w < n && s[first + h][second + w] != '1') { puts("No"); return 0; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (!(i >= first - 1 && i <= first + h && j >= second - 1 && j <= second + w) && s[i][j] != '0') { puts("No"); return 0; } puts("Yes"); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int l = s.length(), f = 0; for (int i = 0; i < l; i++) { if ((s[i] != '?') && (s[i] == s[i + 1])) { cout << -1 << endl; f = 1; break; } if (s[i] == '?') { for (char j = 'a'; j <= 'c'; j++) { if (s[i - 1] != j && s[i + 1] != j) s[i] = j; } } } if (f == 0) cout << s << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char p[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; } } int t = 0; int k = 0; int no = 0; int no2 = 0; for (int i = 0; i < n; i++) { t = 0; for (int j = 0; j < n; j++) { if (p[i][j] == 'E') { t++; } else { break; } } if (t == n) { no++; } } for (int i = 0; i < n; i++) { k = 0; for (int j = 0; j < n; j++) { if (p[j][i] == 'E') { k++; } else { break; } } if (k == n) { no2++; } } if (no > 0 && no2 > 0) { cout << -1; return 0; } if (no == 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (p[i][j] == '.') { cout << i + 1 << " " << j + 1 << "\n"; break; } } } } else if (no2 == 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (p[j][i] == '.') { cout << j + 1 << " " << i + 1 << "\n"; break; } } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int a[200004]; int main() { int n, kv; scanf("%d %d", &n, &kv); int i; for (i = 1; i <= n; i++) { cin >> a[i]; } int j = n, k = 2; long long int sm = 0; for (i = 2; i <= n; i++) { long long int cal = sm - (long long int)(j - k) * a[i] * (k - 1); if (cal < kv) { printf("%d\n", i); j--; } else { sm += (long long int)(a[i] * (k - 1)); k++; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; char a[22]; char b[22]; set<int> ans; int upper[26]; int transtlate(char tar[], int i) { int len = strlen(tar); int temp = 1; int res = 0; for (int j = len - 1; j >= 0; j--) { if (isdigit(tar[j])) { if (tar[j] - '0' >= i) return -1; res += (tar[j] - '0') * temp; } else { int x = upper[tar[j] - 'A']; if (x >= i) return -1; res += x * temp; } temp *= i; } return res; } bool check(char x[], int flag) { int len = strlen(x); bool zero = true; if (len > 1) for (int i = len - 2; i >= 0; i--) if (x[i] != '0') { zero = false; break; } if (len == 1) { if (isdigit(x[len - 1])) return true; else if (flag == 0) { if (upper[x[len - 1] - 'A'] < 24) return true; else return false; } else return true; } if (zero) { if (isdigit(x[len - 1])) return true; else if (flag == 0 && upper[x[len - 1] - 'A'] < 24) return true; else if (flag == 1) return true; else return false; } else return false; } int main() { for (char i = 'A'; i <= 'Z'; i++) upper[i - 'A'] = i - 'A' + 10; scanf("%s", a); int cut; for (int i = 0; i < strlen(a); i++) if (a[i] == ':') { cut = i + 1; a[i] = '\0'; } strcpy(b, a + cut); if (check(a, 0) && check(b, 1)) { cout << -1 << endl; return 0; } for (int i = 2; i <= 80; i++) { int a_10 = transtlate(a, i); int b_10 = transtlate(b, i); if (a_10 != -1 && b_10 != -1 && a_10 < 24 && b_10 < 60) ans.insert(i); } if (ans.size() == 0) cout << 0 << endl; else { set<int>::iterator it = ans.begin(); for (it = ans.begin(); it != ans.end();) { cout << *it; it++; if (it != ans.end()) cout << " "; else cout << endl; } } ans.clear(); return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template <class t> using vvc = vc<vc<t>>; using pi = pair<ll, ll>; using vi = vc<ll>; template <class t, class u> ostream& operator<<(ostream& os, const pair<t, u>& p) { return os << "{" << p.first << "," << p.second << "}"; } template <class t> ostream& operator<<(ostream& os, const vc<t>& v) { os << "{"; for (auto e : v) os << e << ","; return os << "}"; } using uint = unsigned; using ull = unsigned long long; template <class t, size_t n> ostream& operator<<(ostream& os, const array<t, n>& first) { return os << vc<t>(first.begin(), first.end()); } template <ll i, class T> void print_tuple(ostream&, const T&) {} template <ll i, class T, class H, class... Args> void print_tuple(ostream& os, const T& t) { if (i) os << ","; os << get<i>(t); print_tuple<i + 1, T, Args...>(os, t); } template <class... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) { os << "{"; print_tuple<0, tuple<Args...>, Args...>(os, t); return os << "}"; } template <class t> void print(t x, ll suc = 1) { cout << x; if (suc == 1) cout << "\n"; if (suc == 2) cout << " "; } ll read() { ll i; cin >> i; return i; } vi readvi(ll n, ll off = 0) { vi v(n); for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off; return v; } template <class T> void print(const vector<T>& v, ll suc = 1) { for (ll i = ll(0); i < ll(v.size()); i++) print(v[i], i == ll(v.size()) - 1 ? suc : 2); } string readString() { string s; cin >> s; return s; } template <class T> T sq(const T& t) { return t * t; } void yes(bool ex = true) { cout << "Yes" << "\n"; if (ex) exit(0); } void no(bool ex = true) { cout << "No" << "\n"; if (ex) exit(0); } void possible(bool ex = true) { cout << "Possible" << "\n"; if (ex) exit(0); } void impossible(bool ex = true) { cout << "Impossible" << "\n"; if (ex) exit(0); } constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; } const ll infLL = LLONG_MAX / 3; const ll inf = infLL; ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } ll popcount(signed t) { return __builtin_popcount(t); } ll popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(ll i) { return i && (i & -i) == i; } ll mask(ll i) { return (ll(1) << i) - 1; } bool inc(ll first, ll second, ll c) { return first <= second && second <= c; } template <class t> void mkuni(vc<t>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } ll rand_int(ll l, ll r) { static random_device rd; static mt19937_64 gen(rd()); return uniform_int_distribution<ll>(l, r)(gen); } template <class t> ll lwb(const vc<t>& v, const t& first) { return lower_bound(v.begin(), v.end(), first) - v.begin(); } signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); vc<pi> xy; { ll x, y; cin >> x >> y; xy.emplace_back(x, y); } { const ll V = ten(16) * 2 + 10; ll ax, ay, bx, by; cin >> ax >> ay >> bx >> by; while (1) { ll x, y; tie(x, y) = xy.back(); if (x > V || y > V) break; xy.emplace_back(x * ax + bx, y * ay + by); } } void(0); auto dist = [&](pi first, pi second) { return min( abs(first.first - second.first) + abs(first.second - second.second), inf); }; ll x, y, t; cin >> x >> y >> t; ll ans = 0; for (ll i = ll(0); i < ll(ll(xy.size())); i++) for (ll j = ll(i); j < ll(ll(xy.size())); j++) { ll d1 = min(inf, dist(xy[i], xy[j])); ll d2 = min({inf, dist(xy[i], pi(x, y)), dist(xy[j], pi(x, y))}); if (d1 + d2 <= t) chmax(ans, j - i + 1); } cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long linf = 0x3f3f3f3fLL; const int EPS = 1e-6; int x[1010], y[1010], r[1010]; int n, s; double dist(int x, int y) { return hypot(x, y); } int megacity(double mid) { int ret = 0; for (int i = 0; i < n; i++) { if (mid - dist(x[i], y[i]) >= EPS) ret += r[i]; } return ret; } int main() { scanf("%d %d", &n, &s); for (int i = 0; i < n; i++) { scanf("%d %d %d", &x[i], &y[i], &r[i]); } double ini = 0, fim = 1000010; for (int i = 0; i <= 200; i++) { double mid = (ini + fim) / 2.0; if ((megacity(mid) + s) >= (int)1e6) fim = mid; else ini = mid; } if (megacity(ini) + s != (int)1e6) ini = fim; if (megacity(ini) + s < (int)1e6) puts("-1"); else printf("%.7lf\n", ini); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int SZ = 1010; const int INF = 1e9 + 10; const int mod = 1e9 + 7; const long double eps = 1e-8; long long read() { long long n = 0; char a = getchar(); bool flag = 0; while (a > '9' || a < '0') { if (a == '-') flag = 1; a = getchar(); } while (a <= '9' && a >= '0') { n = n * 10 + a - '0', a = getchar(); } if (flag) n = -n; return n; } const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; struct haha { int x, y, s; }; queue<haha> q[11]; int dist[11][SZ][SZ], s[11], n, m, p; char maps[SZ][SZ]; bool vis[SZ][SZ]; bool isin(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; } int ans[22]; bool bfs(queue<haha> &q, int s, int id) { if (q.empty()) return false; int d = q.front().s; int fuck = 0; while (q.size()) { haha u = q.front(); if (u.s == d + s) break; q.pop(); for (int i = 0; i < 4; i++) { int nx = u.x + dx[i]; int ny = u.y + dy[i]; if (isin(nx, ny) && maps[nx][ny] == '.' && !vis[nx][ny]) { ans[id]++; fuck = 1; vis[nx][ny] = 1; q.push((haha){nx, ny, u.s + 1}); } } } if (!fuck) return false; return true; } int main() { n = read(), m = read(), p = read(); for (int i = 1; i <= p; i++) s[i] = read(); for (int i = 1; i <= n; i++) scanf("%s", maps[i] + 1); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (maps[i][j] != '.' && maps[i][j] != '#') { int id = maps[i][j] - '0'; q[id].push((haha){i, j, 0}); ans[id]++; } } } while (233) { bool fuck = 0; for (int k = 1; k <= p; k++) if (bfs(q[k], s[k], k)) fuck = 1; if (!fuck) break; } for (int k = 1; k <= p; k++) cout << ans[k] << " "; }
5
#include <bits/stdc++.h> using namespace std; signed main() { int n, d; vector<bool> ans; cin >> n >> d; int m; cin >> m; while (m) { int x, y; cin >> x >> y; if (y <= d + x && y >= x - d && y <= -x + (n - d) + n && y >= -x + d) ans.push_back(1); else ans.push_back(0); m--; } for (int i = 0; i < ans.size(); i++) if (ans[i]) cout << "YES" << "\n"; else cout << "NO" << "\n"; }
1
#include <bits/stdc++.h> using namespace std; using lld = long double; using ll = long long int; using ar = vector<ll>; using mat = vector<vector<ll>>; mat cn(ll n, ll m) { return vector<vector<ll>>(n, vector<ll>(m)); } ll const MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool chmadp2(T &a, T b) { return a < b ? a = b, 1 : 0; } template <class T> inline void add(T &a, T b) { a += b; a >= MOD ? a - MOD : a; } template <class T> inline void sub(T &a, T b) { a -= b; a < 0 ? a + MOD : a; } template <class T> inline void mul(T &a, T b) { return (a * b) % MOD; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { for (auto &dp2 : v) out << dp2 << ' '; return out; } template <class T> void remDup(vector<T> &v) { sort((v).begin(), (v).end()); v.erase(unique((v).begin(), (v).end()), end(v)); } bool comp1(char &s1, char &s2) { return s1 > s2; } bool comp2(const pair<ll, pair<ll, ll>> &a, const pair<ll, pair<ll, ll>> &b) { if (a.first > b.first) return 1; if (a.first == b.first && a.second.second > b.second.second) return 1; return 0; } class Pair { public: ll first, second; }; bool comp3(const Pair &a, const Pair &b) { if (a.first > b.first) return 1; return 0; } class Trips { public: ll first, second, third; }; bool comp4(const Trips &a, const Trips &b) { if (a.third > b.third) return 1; return 0; } void readArray(ll a[], int n) { for (ll i = 0; i < n; i++) cin >> a[i]; } ll const inf = 1e9 + 7; ll const maxn = 5e5 + 1; bool comp(string &a, string &b) { ll m = a.size(); for (int i = 0; i < m; i++) { if (a[i] != b[i]) { if (i & 1) { if (a[i] < b[i]) return 0; else return 1; } else { if (a[i] < b[i]) return 1; else return 0; } } } return 0; } void solve() { ll t1 = 1; for (ll tt = 1; tt <= t1; tt++) { ll n, m; cin >> n >> m; unordered_map<string, ll> pos; string s[n]; for (ll i = 0; i < n; i++) { cin >> s[i]; pos[s[i]] = i + 1; } sort(s, s + n, comp); for (ll i = 0; i < n; i++) cout << pos[s[i]] << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 100050; const int M = 2 * N; int ls[M], rs[M], tsz, root, mx[M], lzy[M]; void Add(int &c, int ss, int se, int qs, int qe, int f) { if (qs > qe || qs > se || ss > qe) return; if (!c) c = ++tsz; if (qs <= ss && qe >= se) { lzy[c] += f; mx[c] += f; return; } int mid = ss + se >> 1; Add(ls[c], ss, mid, qs, qe, f); Add(rs[c], mid + 1, se, qs, qe, f); mx[c] = max(mx[ls[c]], mx[rs[c]]) + lzy[c]; } vector<int> in[N], out[N]; int l[N], r[N], a[N]; int main() { int n, i, m; scanf("%i %i", &n, &m); for (i = 1; i <= n; i++) scanf("%i", &a[i]), Add(root, 1, n, i, i, a[i]); for (i = 1; i <= m; i++) scanf("%i %i", &l[i], &r[i]), in[l[i]].push_back(i), out[r[i]].push_back(i); int ans = 0, pos = 1, cnt = 0; for (i = 1; i <= n; i++) { cnt += in[i].size(); for (int id : in[i]) Add(root, 1, n, l[id], r[id], -1); int dif = mx[root] - (a[i] - cnt); if (ans < dif) ans = dif, pos = i; for (int id : out[i]) Add(root, 1, n, l[id], r[id], 1); cnt -= out[i].size(); } printf("%i\n", ans); vector<int> my; for (int i = 1; i <= m; i++) if (l[i] <= pos && r[i] >= pos) my.push_back(i); printf("%i\n", my.size()); for (int id : my) printf("%i ", id); return 0; }
6
#include <bits/stdc++.h> using namespace std; map<int, long long> x, y; map<pair<int, int>, long long> xy; int main() { int n; cin >> n; while (n--) { int xx, yy; cin >> xx >> yy; x[xx]++; y[yy]++; xy[make_pair(xx, yy)]++; } long long ans = 0; for (auto i : x) { ans += i.second * (i.second - 1) / 2; } for (auto i : y) { ans += i.second * (i.second - 1) / 2; } for (auto i : xy) { ans -= i.second * (i.second - 1) / 2; } cout << ans << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); vector<string> v; string s; int maxi = 0; while (getline(cin, s)) { v.push_back(s); maxi = max(maxi, (int)s.length()); } for (int i = 1; i <= maxi + 2; i++) cout << '*'; cout << '\n'; bool l = 0, r = 1; for (auto i : v) { cout << '*'; if (maxi % 2 != i.length() % 2) { for (int j = 1; j <= (maxi - i.length() + l) / 2; j++) cout << ' '; cout << i; for (int j = 1; j <= (maxi - i.length() + r) / 2; j++) cout << ' '; l ^= 1, r ^= 1; } else { for (int j = 1; j <= (maxi - i.length()) / 2; j++) cout << ' '; cout << i; for (int j = 1; j <= (maxi - i.length()) / 2; j++) cout << ' '; } cout << "*\n"; } for (int i = 1; i <= maxi + 2; i++) cout << '*'; cout << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, k, ans = 1; int main() { scanf("%lld %lld", &n, &k); if (k >= 2) ans += n * (n - 1) / 2; if (k >= 3) ans += 2 * n * (n - 1) * (n - 2) / 6; if (k >= 4) ans += 9 * n * (n - 1) * (n - 2) * (n - 3) / 24; printf("%lld\n", ans); }
4
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& a) { char c = getchar(); int f = 1; a = 0; for (; c > '9' || c < '0'; c = getchar()) if (c == '-') f = -1; for (; c <= '9' && c >= '0'; c = getchar()) a = a * 10 + c - 48; a *= f; } const int o = 1e5 + 10; int n, m1, m2, fa1[o], fa2[o], xx[o], yy[o], cnt; set<pair<int, int> > rows; set<int> row[o], col[o]; map<int, int> mp[o]; int fr1(int x) { return fa1[x] == x ? x : fa1[x] = fr1(fa1[x]); } int fr2(int x) { return fa2[x] == x ? x : fa2[x] = fr2(fa2[x]); } inline void mergerow(int x, int y) { for (set<int>::iterator iter = row[y].begin(); iter != row[y].end(); ++iter) mp[x][*iter] = mp[y][*iter], row[x].insert(*iter), col[*iter].erase(y), col[*iter].insert(x); } inline void mergecol(int x, int y) { for (set<int>::iterator iter = col[y].begin(); iter != col[y].end(); ++iter) mp[*iter][x] = mp[*iter][y], col[x].insert(*iter), row[*iter].erase(y), row[*iter].insert(x); } int main() { read(n); read(m1); read(m2); for (int i = 1; i <= n; ++i) fa1[i] = fa2[i] = i; for (int i = 1, j, k; i <= m1; ++i) read(j), read(k), fa1[fr1(j)] = fr1(k); for (int i = 1, j, k; i <= m2; ++i) read(j), read(k), fa2[fr2(j)] = fr2(k); if (m1 < m2) for (int i = 1, j; i <= n; ++i) j = fa1[i], fa1[i] = fa2[i], fa2[i] = j; for (int i = 1, j, k; i <= n; ++i) mp[j = fr1(i)][k = fr2(i)] = i, row[j].insert(k), col[k].insert(j); for (int i = 1; i <= n; ++i) if (fr1(i) == i) rows.insert(make_pair(-row[i].size(), i)); for (int i, j, c1, c2, t; rows.size() > 1;) { i = (*rows.begin()).second; rows.erase(rows.begin()); j = (*rows.begin()).second; rows.erase(rows.begin()); set<int>::iterator iter = row[i].begin(); c1 = *iter; c2 = *row[j].begin(); if (c1 == c2) c1 = *++iter; xx[++cnt] = mp[i][c1]; yy[cnt] = mp[j][c2]; if (col[c1].size() < col[c2].size()) t = c1, c1 = c2, c2 = t; mergerow(i, j); mergecol(c1, c2); rows.insert(make_pair(-row[i].size(), i)); } printf("%d", cnt); for (int i = 1; i <= cnt; putchar('\n'), printf("%d", xx[i]), putchar(' '), printf("%d", yy[i++])) ; return 0; }
8
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > used[200011]; char s[200001]; char t[200001]; int count1[26]; int count2[26]; char temp[400011]; int z[400011]; int l1, l2; vector<int> list1; vector<int> list2; vector<int> answer1; void calcZ(const char* s) { int n = strlen(s); z[0] = n; int l = 0, r = 0; for (int i = 1; i < n; i++) { if (i > r) { l = r = i; while (r < n && s[r - l] == s[r]) r++; z[i] = r - l; r--; } else { int k = i - l; if (z[k] < r - i + 1) z[i] = z[k]; else { l = i; while (r < n && s[r - l] == s[r]) r++; z[i] = r - l; r--; } } } } void solveall(int val1, int val2) { for (int i = 0; i < l2; i++) { if ((t[i] - 'a') == val2) { temp[i] = 1 + 'a'; } else { temp[i] = 0 + 'a'; } } temp[l2] = '$'; for (int i = 0; i < l1; i++) { if ((s[i] - 'a') == val1) { temp[i + l2 + 1] = 1 + 'a'; } else { temp[i + l2 + 1] = 0 + 'a'; } } temp[l1 + l2 + 1] = '\0'; calcZ(temp); for (int i = 0; i < l1; i++) { if (z[i + l2 + 1] == l2) { used[i].push_back(make_pair(val1, val2)); } } } int main() { scanf("%d%d", &l1, &l2); scanf("%s", s); scanf("%s", t); for (int i = 0; i < l2; i++) { count1[t[i] - 'a']++; } for (int i = 0; i < l1; i++) { count2[s[i] - 'a']++; } for (int i = 0; i < 26; i++) { if (count1[i] != 0) { list1.push_back(i); } } for (int i = 0; i < 26; i++) { if (count2[i] != 0) { list2.push_back(i); } } for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (count2[i] != 0 && count1[j] != 0) { solveall(i, j); } } } for (int i = 0; i < (l1 - l2 + 1); i++) { bool used1[26] = {0}; bool used2[26] = {0}; bool poss = true; int index[26] = {0}; for (vector<pair<int, int> >::iterator it = used[i].begin(); it != used[i].end(); it++) { pair<int, int> t1 = (*it); if ((used1[t1.first] ^ used1[t1.second]) == 1) { poss = false; } if (used1[t1.first] && used1[t1.second]) { if (index[t1.first] != t1.second || index[t1.second] != t1.first) { poss = false; } } used1[t1.second] = true; used2[t1.second] = true; index[t1.first] = t1.second; index[t1.second] = t1.first; used1[t1.first] = true; } for (vector<int>::iterator it = list1.begin(); it != list1.end(); it++) { if (!used2[(*it)]) { poss = false; } } if (poss) { answer1.push_back(i + 1); } } printf("%d\n", (int)answer1.size()); sort(answer1.begin(), answer1.end()); for (vector<int>::iterator it = answer1.begin(); it != answer1.end(); it++) { printf("%d ", (*it)); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long n, l, ls = 0, lm = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> l; ls = ls + l; if (lm < l) lm = l; } cout << 2 * lm - ls + 1; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 75; const int MAXM = 205; const int INF = 2e9; struct Path { int S, cur, dis; bool operator<(const Path &p) const { return dis > p.dis; } }; int n, m, a, b, tot; int u[MAXM]; int v[MAXM]; int w[MAXM]; int fa[MAXN]; int ans[MAXN]; int sz[MAXN]; int match[MAXN]; int f[1 << 17][MAXN]; bool mark[1 << 17][MAXN]; priority_queue<Path> Q; vector<pair<int, int> > e[MAXN]; int getroot(int u) { return u == fa[u] ? u : fa[u] = getroot(fa[u]); } void Dijkstra() { ans[1] = 0; for (int i = 2; i <= n; i++) ans[i] = INF; for (int i = 0; i < (1 << tot); i++) for (int j = 1; j <= n; j++) f[i][j] = INF; if (sz[fa[1]] >= 4) { f[1 << match[fa[1]]][1] = 0; Q.push((Path){1 << match[fa[1]], 1, 0}); } else { f[0][1] = 0; Q.push((Path){0, 1, 0}); } while (!Q.empty()) { int u = Q.top().cur, S = Q.top().S, d = Q.top().dis; Q.pop(); if (mark[S][u]) continue; mark[S][u] = 1; for (int i = 0; i < e[u].size(); i++) { int to = e[u][i].first, dis = d + e[u][i].second; if (sz[fa[to]] >= 4 && (fa[to] == fa[u] || (~S >> match[fa[to]] & 1)) && dis < f[S | (1 << match[fa[to]])][to]) { ans[to] = min(ans[to], dis); Q.push((Path){S | (1 << match[fa[to]]), to, f[S | (1 << match[fa[to]])][to] = dis}); } if (sz[fa[to]] < 4 && dis < f[S][to]) { ans[to] = min(ans[to], dis); Q.push((Path){S, to, f[S][to] = dis}); } } } } int main() { ios::sync_with_stdio(0); cin >> n >> m >> a >> b; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { cin >> u[i] >> v[i] >> w[i]; if (w[i] == a) { e[u[i]].push_back(make_pair(v[i], w[i])); e[v[i]].push_back(make_pair(u[i], w[i])); fa[getroot(u[i])] = getroot(v[i]); } } for (int i = 1; i <= m; i++) if (w[i] == b && getroot(u[i]) != getroot(v[i])) { e[u[i]].push_back(make_pair(v[i], w[i])); e[v[i]].push_back(make_pair(u[i], w[i])); } for (int i = 1; i <= n; i++) sz[getroot(i)]++; for (int i = 1; i <= n; i++) if (sz[i] >= 4) match[i] = tot++; Dijkstra(); for (int i = 1; i <= n; i++) cout << ans[i] << ' '; cout << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:16777216") #pragma warning(disable : 4786) struct node { int d, x, y; node() {} node(int a, int b, int c) { x = a; y = b; d = c; } bool operator<(const node &a) const { return d > a.d || (d == a.d && x > a.x) || (d == a.d && x == a.x && y > a.y); } }; bool vi[200009]; int a[200009], l[200009], r[200009], n; priority_queue<node> q; string s; vector<pair<int, int> > res; int main() { int i; node u; cin >> n >> s; s = "a" + s; for (i = 1; i <= n; i++) { cin >> a[i]; l[i] = i - 1; r[i] = i + 1; if (i > 1 && s[i] != s[i - 1]) { q.push(node( i - 1, i, ((a[i] - a[i - 1]) < 0 ? (-(a[i] - a[i - 1])) : (a[i] - a[i - 1])))); } } while (!q.empty()) { u = q.top(); q.pop(); if (vi[u.x] || vi[u.y]) continue; res.push_back(pair<int, int>(u.x, u.y)); vi[u.x] = vi[u.y] = 1; if (l[u.x] > 0 && r[u.y] <= n && s[l[u.x]] != s[r[u.y]]) q.push(node(l[u.x], r[u.y], ((a[l[u.x]] - a[r[u.y]]) < 0 ? (-(a[l[u.x]] - a[r[u.y]])) : (a[l[u.x]] - a[r[u.y]])))); r[l[u.x]] = r[u.y]; l[r[u.y]] = l[u.x]; } cout << res.size() << endl; for (i = 0; i < res.size(); i++) { cout << res[i].first << " " << res[i].second << endl; } return 0; }
5
#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0);cin.tie(0); int main() { fast; int t; cin>>t; while(t--) { int x; cin>>x; int arr[x]; int mini=INT_MAX; int count=0; for(int i=0;i<x;i++) { cin>>arr[i]; if(arr[i]<mini)mini=arr[i]; } for(int i=0;i<x;i++) { if(arr[i]==mini)count++; } cout<<x-count<<'\n'; } }
0
#include <bits/stdc++.h> using namespace std; pair<vector<int>, vector<int>> suffixArray(const string& str, int mx = 256) { int n = str.size(); vector<int> sa(n), rev(n), nsa(n), nrev(n), cou(n + mx + 1); for (int i = 0; i < n; ++i) sa[i] = i; for (int i = 0; i < n; ++i) rev[i] = str[i]; for (int s = 0; s < n; s = (s ? 2 * s : s + 1), rev = nrev) { for (int i = 0; i < n; ++i) nsa[i] = (sa[i] - s + n * (sa[i] < s)); for (auto& c : cou) c = 0; for (auto v : rev) ++cou[v + 1]; for (int j = 1; j <= n + mx; ++j) cou[j] += cou[j - 1]; for (auto i : nsa) { sa[cou[rev[i]]] = i; ++cou[rev[i]]; } for (int r = 0, j = 0; j < n; ++j) { r += (j && (rev[sa[j]] != rev[sa[j - 1]] || (rev[(sa[j] + s) % n] != rev[(sa[j - 1] + s) % n]))); nrev[sa[j]] = r; } } return {sa, rev}; } int compare(int i, const string& a, const string& b) { for (int j = 0; j < b.size(); ++j) { if (i + j >= a.size()) return -1; if (a[i + j] != b[j]) return (a[i + j] < b[j] ? -1 : 1); } return 0; } int countOccs(const vector<int>& sa, const string& str, const string& pat) { int low1 = 0; int high1 = sa.size(); while (low1 != high1) { int mid = (low1 + high1) >> 1; if (compare(sa[mid], str, pat) == -1) low1 = mid + 1; else high1 = mid; } int low2 = -1; int high2 = (int)sa.size() - 1; while (low2 != high2) { int mid = (low2 + high2 + 1) >> 1; if (compare(sa[mid], str, pat) == 1) high2 = mid - 1; else low2 = mid; } return low2 - low1 + 1; } void kmpSub(const string& str, const string& pat, vector<int>& aux, vector<int>& res, bool phase) { int c = (phase ? -1 : str[0] == pat[0]); res[0] = c; for (int i = 1; i < res.size(); ++i) { char t = str[i - phase]; while ((c > -1) && (pat[c] != t)) c = aux[c]; res[i] = ++c; if (c == pat.size()) c = aux[c]; } } vector<int> kmp(const string& str, const string& pat) { vector<int> aux(pat.size() + 1); vector<int> res(str.size()); kmpSub(pat, pat, aux, aux, 1); kmpSub(str, pat, aux, res, 0); return res; } const int N = 4 * (int)1e5; const int Q = 4 * (int)1e5; vector<int> conns[N]; char c_add[N]; int par[N]; int siz[N]; bool used[N]; vector<int> q_ind[N]; string queries[Q]; int ans[Q]; void recAnswer(int i, const vector<int>& sa, const string& str, string& path) { for (auto qi : q_ind[i]) { const string& pat = queries[qi]; int n = (int)str.size() - 1; int m = pat.size(); if (n >= m) { int occs = countOccs(sa, str, pat); ans[qi] += occs; } int ai = max(0, n - (m - 1)); int bi = min(n + (m - 1), (int)path.size()); int len = bi - ai; if (len >= m) { string subpath(len, '_'); for (int j = 0; j < len; ++j) subpath[j] = path[ai + j]; vector<int> matches = kmp(subpath, pat); int match_cou = 0; for (int j = 0; j < len; ++j) { if (matches[j] == m) ++match_cou; } ans[qi] += match_cou; } } for (auto t : conns[i]) { if (used[t]) continue; path.push_back(c_add[t]); recAnswer(t, sa, str, path); path.pop_back(); } } void handle(int i, int p) { string str; int j = i; for (int j = i;; j = par[j]) { str.push_back(c_add[j]); if (j == p) break; } reverse(str.begin(), str.end()); str.push_back('#'); int n = str.size(); vector<int> sa = suffixArray(str).first; string path = str; path.pop_back(); recAnswer(i, sa, str, path); } int calcSiz(int i) { siz[i] = 1; for (auto t : conns[i]) { if (!used[t]) siz[i] += calcSiz(t); } return siz[i]; } bool solve(int i, int p = -1, int n = -1) { if (n == -1) { n = calcSiz(i); p = i; } bool found = false; for (auto t : conns[i]) { if (!used[t]) found |= solve(t, p, n); } if ((!found) && (2 * siz[i] >= n)) { handle(i, p); found = true; used[i] = true; for (auto t : conns[i]) { while (!used[t]) solve(t); } } return found; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { int t; cin >> t; if (t == 1) { par[i] = i; } else { cin >> par[i]; --par[i]; conns[par[i]].push_back(i); } cin >> c_add[i]; } int q; cin >> q; for (int qi = 0; qi < q; ++qi) { int i; cin >> i >> queries[qi]; q_ind[i - 1].push_back(qi); } for (int i = 0; i < n; ++i) { if (par[i] == i) { while (!used[i]) solve(i); } } for (int qi = 0; qi < q; ++qi) cout << ans[qi] << '\n'; }
9
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vector<int>>; using vvvi = vector<vector<vector<int>>>; using vl = vector<ll>; using vvl = vector<vector<ll>>; using vvvl = vector<vector<vector<ll>>>; using vs = vector<string>; using vb = vector<bool>; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline void prn(vector<T>& v) { int n = (int)v.size(); for (int i = (0); i < ((n)); i++) cout << v[i] << ' '; } template <class T> inline void printv(vector<T>& v) { int n = (int)v.size(); for (int i = (0); i < ((n)); i++) cout << v[i] << (i == n - 1 ? '\n' : ' '); } template <class T> inline void printvv(vector<vector<T>>& v) { for (auto u : v) printv(u); } template <class T> inline void printlnv(vector<T>& v) { int n = (int)v.size(); for (int i = (0); i < ((n)); i++) cout << v[i] << '\n'; } const int MOD = 1000000007; const int INF = 1000000001; const ll LINF = 1000000001000000001LL; void solve(); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(numeric_limits<double>::max_digits10); solve(); return 0; } class SegmentTree { int n; vector<int> node; public: SegmentTree(vector<int> v) { n = 1; int s = v.size(); while (n < s) n *= 2; node.resize(2 * n - 1, INF); for (int i = 0; i < s; i++) node[i + n - 1] = v[i]; for (int i = n - 2; i >= 0; i--) node[i] = min(node[2 * i + 1], node[2 * i + 2]); } void update(int x, int val) { x += (n - 1); node[x] = val; while (x > 0) { x = (x - 1) / 2; node[x] = min(node[2 * x + 1], node[2 * x + 2]); } } void inc(int x) { x += (n - 1); node[x]++; while (x > 0) { x = (x - 1) / 2; node[x] = min(node[2 * x + 1], node[2 * x + 2]); } } void dec(int x) { x += (n - 1); node[x]--; while (x > 0) { x = (x - 1) / 2; node[x] = min(node[2 * x + 1], node[2 * x + 2]); } } int find(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return INF; if (a <= l && r <= b) return node[k]; int vl = find(a, b, 2 * k + 1, l, (l + r) / 2); int vr = find(a, b, 2 * k + 2, (l + r) / 2, r); return min(vl, vr); } pair<int, int> find_with_min_index(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return make_pair(INF, INF); if (k >= n - 1) return make_pair(node[k], k - n + 1); if (node[2 * k + 1] <= node[2 * k + 2]) return find_with_min_index(a, b, 2 * k + 1, l, (l + r) / 2); else return find_with_min_index(a, b, 2 * k + 2, (l + r) / 2, r); } }; void solve() { int t; cin >> t; while (t--) { int n; cin >> n; vi a(n); vi b(n + 1); SegmentTree c(vi(n + 1, 0)); bool ok = true; for (int i = (0); i < ((n)); i++) { cin >> a[i]; if (i > 0 and a[i] < a[i - 1]) ok = false; b[a[i]]++; if (a[i] == i) c.inc(i); } if (ok) { cout << 0 << '\n' << '\n'; continue; } SegmentTree st(b); vi ans; for (int i = (0); i < ((n * 2)); i++) { auto mex = st.find_with_min_index(0, n + 1); if (mex.second >= n) { auto idx = c.find_with_min_index(0, n); st.inc(mex.second); st.dec(a[idx.second]); a[idx.second] = mex.second; ans.emplace_back(idx.second + 1); } else { st.inc(mex.second); st.dec(a[mex.second]); a[mex.second] = mex.second; ans.emplace_back(mex.second + 1); c.inc(mex.second); } auto f = c.find_with_min_index(0, n); if (f.second >= n) break; } cout << (int)ans.size() << '\n'; printv(ans); } }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; pair<int, int> a[MAXN]; int n, p, q; double ans = 1e20; int sgn(long long x) { if (x == 0) return 0; if (x < 0) return -1; else return 1; } long long cross(int p, int q, int r) { return (long long)(a[q].first - a[p].first) * (-a[r].second + a[p].second) - (long long)(a[r].first - a[p].first) * (-a[q].second + a[p].second); } int dir(int p, int q) { return sgn(cross(n + 1, p, q)); } void work(int x, int y) { long long ax = a[x].first, bx = -a[x].second, ay = a[y].first, by = -a[y].second; ans = min(ans, ((double)p * by - q * ay + q * ax - p * bx) / (ax * by - ay * bx)); } int main() { cin >> n >> p >> q; a[0] = pair<int, int>(p, -q); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].first, &a[i].second); a[i].second = -a[i].second; } a[n + 1] = pair<int, int>(0, 0); sort(a + 1, a + n + 1); vector<int> Q; for (int i = 1; i <= n; i++) { if (Q.size() < 2) Q.push_back(i); else { while (Q.size() >= 2 && cross(Q[Q.size() - 1], Q[Q.size() - 2], i) <= 0) Q.pop_back(); Q.push_back(i); } } for (int i = 1; i < Q.size(); i++) { if (dir(Q[i - 1], 0) != dir(Q[i], 0)) { work(Q[i - 1], Q[i]); } } for (int i = 1; i <= n; i++) ans = min(ans, max((double)p / a[i].first, (double)q / -a[i].second)); printf("%.10lf\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; long long int binpowmod(long long int a, long long int b, long long int m) { a %= m; long long int res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } long long int binpow(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } long long int modInverse(long long int n, long long int p) { return binpowmod(n, p - 2, p); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t, i, f = 0, j, ans = 0, l, r, k, t1, t2, x, y, cnt, n, m; cin >> t; while (t--) { cin >> n; for (i = 0; i < n; ++i) cout << i + 1 << " "; cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 21; const int M = 26; const int INF = 1e9 + 7; string s[N]; int c[N][N]; int p[N][M]; int sub[N][M]; int mx[N][M]; int dp[1 << N]; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> c[i][j]; for (int i = 0; i < m; i++) for (int j = 0; j < M; j++) for (int k = 0; k < n; k++) if (s[k][i] == (j + 'a')) { p[i][j] += c[k][i]; mx[i][j] = max(mx[i][j], c[k][i]); sub[i][j] += 1 << k; } for (int mask = 1; mask < 1 << n; mask++) { dp[mask] = 1e9 + 7; int lb = __builtin_ctz(mask); for (int i = 0; i < m; i++) { dp[mask] = min(dp[mask], dp[mask ^ (1 << lb)] + c[lb][i]); dp[mask] = min(dp[mask], dp[mask & (~sub[i][(int)(s[lb][i] - 'a')])] + p[i][(int)(s[lb][i] - 'a')] - mx[i][(int)(s[lb][i] - 'a')]); } } cout << dp[(1 << n) - 1] << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; cin >> n >> k; long long int a[n]; for (long long int i = 0; i < n; i++) { a[i] = i + 1; } long long int i = n - 1; long long int t = k; while (k--) { cout << a[i] << " "; i--; } for (long long int i = 0; i < n - t; i++) { cout << a[i] << " "; } return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T, typename TT> inline ostream &operator<<(ostream &os, const pair<T, TT> &t) { return os << t.first << " " << t.second; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &t) { for (auto i : t) os << i << " "; return os; } template <typename T> inline istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; } template <typename T1, typename T2> inline istream &operator>>(istream &is, pair<T1, T2> &t) { is >> t.first >> t.second; return is; } const unsigned long long mod = 1e9 + 7; void solve() { unsigned long long n, k; cin >> n >> k; vector<unsigned long long> v(n); cin >> v; vector<unsigned long long> pre(n + 1); for (int i = 0; i < n; i++) pre[i + 1] = pre[i] + v[i]; unsigned long long ans = 0; vector<unsigned long long> pos = {0}; auto calc = [&](unsigned long long p, unsigned long long s) { if (p % k == 0) return p / k - s; return mod; }; ; for (int i = 1; i <= n; i++) { unsigned long long p = v[i - 1], s = v[i - 1]; if (calc(p, s) <= i - pos.back() - 1) ans++; for (int j = pos.size() - 1; j > 0; j--) { unsigned long long e = v[pos[j] - 1]; if (log10(e) + log10(p) > 18) break; p *= e; s = pre[i] - pre[pos[j] - 1]; if (calc(p, s) <= pos[j] - pos[j - 1] - 1) ans++; } if (v[i - 1] > 1) pos.push_back(i); } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ; int T = 1; while (T--) { solve(); } return 0; }
6
#include <bits/stdc++.h> const int inf = (int)2e+9 + 2; const int mod = (int)1e+5; const int h_prime = 1103; const double eps = 1e-6; using namespace std; vector<long long> primes; vector<long long> HH, pp; long double dist(pair<long double, long double> a, pair<long double, long double> b) { return sqrtl((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); } long double len(pair<long double, long double> x) { return sqrtl(x.first * x.first + x.second * x.second); } long double angle(pair<long double, long double> a, pair<long double, long double> b) { return acos((a.first * b.first + a.second * b.second) / (len(a) * len(b))); } long double ar_tr(pair<long double, long double> a, pair<long double, long double> b) { return a.first * b.second - b.first * a.second; } long long get_hash(int l, int r) { long long res = HH[r]; if (l) res -= HH[l - 1] * pp[r - l + 1]; return res; } void init_hashes(string s) { long long hash = s[0]; HH.push_back(hash); long long ppow = 1; for (size_t i = 0; i < s.length(); ++i) { pp.push_back(ppow); ppow *= h_prime; } for (size_t i = 1; i < s.length(); ++i) { hash *= h_prime; hash += s[i]; HH.push_back(hash); } } inline long long gcd(long long x, long long y) { while (y) { x %= y; swap(x, y); } return x; } void init_primes(long long maxn) { vector<bool> np(maxn, false); np[0] = np[1] = true; for (int i = 2; i * i < maxn; ++i) if (!np[i]) { for (int j = i * i; j < maxn; j += i) np[j] = true; primes.push_back(i); } } long long binpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res *= a; a *= a; b /= 2; } return res; } long long to2(vector<long long>& a) { long long k = 1; long long ans = 0; for (int i = 0; i < a.size(); ++i) { ans += k * a[i]; k *= 2; } return ans; } vector<long long> c1(30, 0), c2(30, 0); long long get(long long a, long long b) { a += to2(c1); b += to2(c2); int ans; cout << "? " << a << ' ' << b << endl; cin >> ans; return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); vector<long long> a(30, 0), b(30, 0); bool f = true; int used = -1; for (int i = 29; i >= 0; --i) { int ans1, ans2; ans1 = get(1 << i, 0); ans2 = get(0, 1 << i); if (ans1 != ans2) { if (ans1 == -1) a[i] = b[i] = 1; else a[i] = b[i] = 0; } else { if (ans1 == 0) { int ans3 = get(0, 0); if (ans3 == 1) { a[i] = 1; b[i] = 0; } else { a[i] = 0; b[i] = 1; } } if (used == -1) { int ans3 = get(0, 0); if (ans3 == 1) { a[i] = 1; b[i] = 0; } else { a[i] = 0; b[i] = 1; } used = 1; } else if (f) { a[i] = 1; b[i] = 0; } else { a[i] = 0; b[i] = 1; } if (ans1 == 1) f = true; else f = false; } c1[i] = a[i]; c2[i] = b[i]; } cout << "! " << to2(a) << ' ' << to2(b) << endl; }
6
#include <bits/stdc++.h> using namespace std; const int NR = 1e5 + 10; const int mod = 1e9 + 7; void Min(int& x, int y) { x = min(x, y); } void Max(int& x, int y) { x = max(x, y); } 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 << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * f; } int n; long long ans; int rd[NR]; int to[NR << 1], nxt[NR << 1], head[NR], tot = 1; void add(int x, int y) { to[tot] = y, nxt[tot] = head[x], head[x] = tot++; } int ksm(int a, int b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = 1ll * a * a % mod; b >>= 1; } return res; } int f[NR], dp[NR]; void dfs1(int x, int fa_) { dp[x] = 1; int t1 = 1, t2 = 0; for (int i = head[x]; i; i = nxt[i]) if (to[i] != fa_) dfs1(to[i], x); for (int i = head[x]; i; i = nxt[i]) if (to[i] != fa_) t1 = 1ll * t1 * dp[to[i]] % mod, t2++; if (t2) dp[x] = t1 * 2; } void dfs2(int x, int fa_) { int F = 1ll * ksm(dp[x], mod - 2) * f[fa_] % mod; if (rd[fa_] == 1) F = 1ll * F * ksm(2, mod - 2) % mod; f[x] = F; for (int i = head[x]; i; i = nxt[i]) { if (to[i] == fa_) continue; f[x] = 1ll * f[x] * dp[to[i]] % mod; } f[x] = f[x] * 2 % mod; ans = (ans + f[x]) % mod; for (int i = head[x]; i; i = nxt[i]) if (to[i] != fa_) dfs2(to[i], x); } int main() { n = read(); for (int i = 1, x, y; i < n; i++) x = read(), y = read(), add(x, y), add(y, x), rd[x]++, rd[y]++; dfs1(1, 0); f[1] = dp[1]; for (int i = head[1]; i; i = nxt[i]) dfs2(to[i], 1); printf("%lld\n", (ans + f[1]) % mod); return 0; }
6
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(300001); int edgeEndXors[300001], depth[300001], ancestors[19][300001]; long long weights[300001][2], crossWeights[300001], d[300001], dp[19][300001][2][2], temp[2][2], a[2][2], b[2][2], c[2][2]; void PreProcess(int n) { priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > Q; for (int i = 1; i <= n; ++i) { d[i] = crossWeights[i]; Q.push(make_pair(d[i], i)); } while (!Q.empty()) { long long di; int i; tie(di, i) = Q.top(); Q.pop(); if (di > d[i]) { continue; } for (int x : adj[i]) { int j = edgeEndXors[x] ^ i; long long dj = di + weights[x][0] + weights[x][1]; if (dj < d[j]) { d[j] = dj; Q.push(make_pair(dj, j)); } } } } void Compute(long long a[2][2], long long b[2][2], long long c[2][2]) { for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { long long &val = temp[i][j]; val = LLONG_MAX; for (int k = 0; k < 2; ++k) { val = min(val, a[i][k] + b[k][j]); } } } for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { c[i][j] = temp[i][j]; } } } void DFS(int i, int p) { depth[i] = 1 + depth[p]; for (int k = 1; k < 19; ++k) { int j = ancestors[k - 1][i]; ancestors[k][i] = ancestors[k - 1][j]; Compute(dp[k - 1][i], dp[k - 1][j], dp[k][i]); } for (int x : adj[i]) { int j = edgeEndXors[x] ^ i; if (j == p) { continue; } ancestors[0][j] = i; dp[0][j][0][0] = min(weights[x][0], d[j] + weights[x][1] + d[i]); dp[0][j][0][1] = min(d[i] + weights[x][0], d[j] + weights[x][1]); dp[0][j][1][0] = min(d[i] + weights[x][1], d[j] + weights[x][0]); dp[0][j][1][1] = min(weights[x][1], d[j] + weights[x][0] + d[i]); DFS(j, i); } } int LCA(int i, int j) { if (depth[i] > depth[j]) { swap(i, j); } for (int k = 19 - 1; k > -1 && depth[i] < depth[j]; --k) { if (depth[i] <= depth[ancestors[k][j]]) { j = ancestors[k][j]; } } if (i == j) { return i; } for (int k = 19 - 1; k > -1 && i != j && ancestors[0][i] != ancestors[0][j]; --k) { if (ancestors[k][i] != ancestors[k][j]) { i = ancestors[k][i], j = ancestors[k][j]; } } return ancestors[0][i]; } void ComputeTillKthAncestors(int i, int l, long long a[2][2]) { for (int x = 0; x < 2; ++x) { for (int y = 0; y < 2; ++y) { a[x][y] = 0; } } a[0][1] = a[1][0] = d[i]; for (int k = 19 - 1; k > -1 && l > 0; --k) { if (l < (1 << k)) { continue; } Compute(a, dp[k][i], a); i = ancestors[k][i]; l -= (1 << k); } } long long Query(int i, int j) { int pi = (i + 1) / 2, pj = (j + 1) / 2; int qi = (i + 1) % 2, qj = (j + 1) % 2; int pk = LCA(pi, pj); ComputeTillKthAncestors(pi, depth[pi] - depth[pk], a); ComputeTillKthAncestors(pj, depth[pj] - depth[pk], b); swap(b[0][1], b[1][0]); Compute(a, b, c); return c[qi][qj]; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%lld", &crossWeights[i]); } for (int x = 1; x < n; ++x) { int i, j; scanf("%d%d%lld%lld", &i, &j, &weights[x][0], &weights[x][1]); edgeEndXors[x] = (i ^ j); adj[i].push_back(x); adj[j].push_back(x); } PreProcess(n); DFS(1, 0); int q; scanf("%d", &q); while (q--) { int i, j; scanf("%d%d", &i, &j); printf("%lld\n", Query(i, j)); } return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long nr = 6e5 + 500; inline long long read() { long long ret = 0; long long x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -1; ch = getchar(); } while (isdigit(ch)) { ret = ret * 10 + ch - '0'; ch = getchar(); } return ret * x; } long long fir[nr]; long long item = 0; struct edge { long long nxt, to, val; } p[nr * 2]; void add(long long a, long long b, long long c) { ++item; p[item].val = c; p[item].to = b; p[item].nxt = fir[a]; fir[a] = item; } struct node { long long id; long long val; }; bool operator<(node A, node B) { return A.val > B.val; } long long dis[nr]; bool mark[nr]; long long start; long long pre[nr]; void DIJ() { memset(dis, 999999, sizeof(dis)); memset(mark, 0, sizeof(mark)); priority_queue<node> Q; Q.push((node){start, 0}); dis[start] = 0; while (!Q.empty()) { long long now = Q.top().id; Q.pop(); if (mark[now]) continue; mark[now] = 1; for (int tmp = fir[now]; tmp; tmp = p[tmp].nxt) { long long v = p[tmp].to; if (dis[now] + p[tmp].val < dis[v]) { pre[v] = tmp; dis[v] = p[tmp].val + dis[now]; Q.push((node){v, dis[v]}); } else { if (dis[now] + p[tmp].val == dis[v]) { long long kk = pre[v]; if (p[kk].val > p[tmp].val) { pre[v] = tmp; } } } } } } long long n, m; long long k; long long di[nr]; long long cnt = 0; void dfs(long long now, long long fat) { if (cnt == k) { return; } for (int tmp = fir[now]; tmp; tmp = p[tmp].nxt) { long long v = p[tmp].to; if (v == fat) continue; if (cnt == k) { return; } if (pre[v] == tmp) { cnt++; cout << (tmp + 1) / 2 << " "; dfs(v, now); } } } int main() { n = read(); m = read(); k = read(); for (int i = 1; i <= m; i++) { long long a, b, c; a = read(); b = read(); c = read(); add(a, b, c); add(b, a, c); } start = 1; DIJ(); long long tot = 0; for (int i = 1; i <= n; i++) { tot += p[pre[i]].val; } long long g = min(n - 1, k); cout << g << endl; for (int i = 1; i <= n; i++) { di[i] = (pre[i] + 1) / 2; } dfs(1, 0); return 0; }
5
#include <bits/stdc++.h> using namespace std; long double eps = 0.000000001; int main() { int n; cin >> n; vector<long double> c1(n), c2(n); for (int i = 0; i < n; ++i) cin >> c1[i]; for (int i = 0; i < n; ++i) cin >> c2[i]; long double sa = 0, sb = 0; vector<long double> a(n), b(n); for (int i = 0; i < n; ++i) { long double c11 = 0, c12 = 0; c11 = sb; c12 = sa; long double c21 = 1 - sb; long double c22 = 1 - sa; long double a1 = c1[i]; long double a2 = c2[i]; long double A = a1 + a2; long double k = -(c11 + c21) / (c12 + c22); long double dop = A / (c12 + c22); long double al1 = c11 + c12 * k + dop; long double al2 = k; long double a0 = -a1 + dop * c12; long double d; if (abs(al1 * al1 - 4 * al2 * a0) < eps) d = 0; else d = sqrt(al1 * al1 - 4 * al2 * a0); a[i] = (-al1 + d) / (2 * al2); b[i] = dop + k * a[i]; sa += a[i]; sb += b[i]; } for (int i = 0; i < n; ++i) printf("%.10lf ", (double)a[i]); cout << endl; for (int i = 0; i < n; ++i) printf("%.10lf ", (double)b[i]); cout << endl; }
8
#include <bits/stdc++.h> long long int pow(long long int a, long long int b, long long int m) { a %= m; long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y); if (x > m) x %= m; } y = (y * y); if (y > m) y %= m; b /= 2; } return x % m; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; else return gcd(b, a % b); } long long int gen(long long int start, long long int end) { long long int diff = end - start; long long int temp = rand() % start; return temp + diff; } using namespace std; string s; int nxt[200002][27]; void build_nxt() { int n = s.length(); for (int i = 0; i < 26; i++) { if (i != (s.back() - 'a')) nxt[n - 1][i] = n; else nxt[n - 1][i] = n - 1; } for (int i = n - 2; i >= 0; i--) { for (int j = 0; j < 26; j++) { nxt[i][j] = nxt[i + 1][j]; } nxt[i][s[i] - 'a'] = i; } } int len[4]; string r[4]; int dp[252][252][252]; void solve(int i, int j, int k) { dp[i][j][k] = s.length(); int n = s.length(); if (i - 1 >= 0) { int pos = dp[i - 1][j][k]; if (pos <= n - 2) dp[i][j][k] = min(dp[i][j][k], nxt[pos + 1][r[1][i - 1] - 'a']); } if (j - 1 >= 0) { int pos = dp[i][j - 1][k]; if (pos <= n - 2) dp[i][j][k] = min(dp[i][j][k], nxt[pos + 1][r[2][j - 1] - 'a']); } if (k - 1 >= 0) { int pos = dp[i][j][k - 1]; if (pos <= n - 2) dp[i][j][k] = min(dp[i][j][k], nxt[pos + 1][r[3][k - 1] - 'a']); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; cin >> s; build_nxt(); dp[0][0][0] = -1; while (q--) { char tp; cin >> tp; if (tp == '+') { int i; char c; cin >> i >> c; ++len[i]; r[i].push_back(c); if (i == 1) { for (int j = 0; j <= len[2]; j++) { for (int k = 0; k <= len[3]; k++) { solve(len[i], j, k); } } } if (i == 2) { for (int j = 0; j <= len[1]; j++) { for (int k = 0; k <= len[3]; k++) { solve(j, len[i], k); } } } if (i == 3) { for (int j = 0; j <= len[1]; j++) { for (int k = 0; k <= len[2]; k++) { solve(j, k, len[i]); } } } if (dp[len[1]][len[2]][len[3]] < (int)s.length()) { cout << "YES" << "\n"; } else { cout << "NO" << "\n"; } } else { int i; cin >> i; len[i]--; r[i].pop_back(); if (dp[len[1]][len[2]][len[3]] < (int)s.length()) { cout << "YES" << "\n"; } else { cout << "NO" << "\n"; } } } }
7
#include <bits/stdc++.h> using namespace std; const int imax = 1e9 + 7; const long long lmax = 1e18; vector<int> v; string s; int calc() { int i, j, k, n; cin >> n; v.resize(n); for (i = 0; i < ((int)v.size()); ++i) cin >> v[i]; i = 0; while (i < v.size()) { j = min_element(v.begin() + i, v.end()) - v.begin(); if (i == j) { i++; continue; } k = j; for (; j > i; j--) swap(v[j - 1], v[j]); i = k; } for (i = 0; i < ((int)v.size()); ++i) cout << v[i] << " "; cout << endl; return 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) calc(); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 10; int a[MAXN], b[MAXN], plc[MAXN], seg[MAXN * 4]; bool mark[MAXN]; set<int> s; void add(int plc, int cur, int s, int e) { if (e - s < 2) { seg[cur]++; return; } int mid = (s + e) / 2; if (plc < mid) add(plc, cur * 2, s, mid); else add(plc, cur * 2 + 1, mid, e); seg[cur] = seg[cur * 2] + seg[cur * 2 + 1]; } int get(int lo, int hi, int cur, int s, int e) { if (lo == s && hi == e) return seg[cur]; int mid = (s + e) / 2, ret = 0; if (lo < mid) ret += get(lo, min(mid, hi), cur * 2, s, mid); if (hi > mid) ret += get(max(mid, lo), hi, cur * 2 + 1, mid, e); return ret; } int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i], plc[a[i]] = i; for (int j = 0; j < k; j++) cin >> b[j], mark[b[j]] = true; s.insert(0); s.insert(n + 1); long long ans = 0; for (int i = 1; i <= n; i++) { if (mark[i]) s.insert(plc[i]); else { set<int>::iterator it = s.lower_bound(plc[i]); int hi = *it; it--; int lo = *it; lo++; ans += hi - lo - get(lo, hi, 1, 0, n + 1); add(plc[i], 1, 0, n + 1); } } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int a[200000 + 1]; struct info { int sum, prop; } t[3 * 200000 + 3]; void build(int node, int b, int e) { if (b >= e) { if (b == e) t[node].sum = a[b]; return; } int left = node << 1; int right = left | 1; int mid = (b + e) >> 1; build(left, b, mid); build(right, mid + 1, e); t[node].sum = t[left].sum + t[right].sum; } void update(int node, int b, int e, int i, int j) { if (i > e || j < b) return; if (b > e) return; if (b >= i && e <= j) { t[node].sum += ((e - b + 1) * 1); t[node].prop += 1; return; } int left = node << 1; int right = left | 1; int mid = (b + e) >> 1; update(left, b, mid, i, j); update(right, mid + 1, e, i, j); t[node].sum = t[left].sum + t[right].sum + (e - b + 1) * t[node].prop; } int query(int node, int b, int e, int pos, int carry = 0) { if (pos < b || e < pos) return 0; if (b > e) return 0; if (b == pos && pos == e) return t[node].sum + carry * (e - b + 1); int left = node << 1; int right = left | 1; int mid = (b + e) >> 1; int p1 = query(left, b, mid, pos, carry + t[node].prop); int p2 = query(right, mid + 1, e, pos, carry + t[node].prop); return p1 + p2; } int chk[200000 + 1]; int main() { int n, k, q, i, l, r; cin >> n >> k >> q; for (i = 0; i < n; i++) { scanf("%d %d", &l, &r); update(1, 1, 200000, l, r); } for (i = 1; i <= 200000; i++) { if (query(1, 1, 200000, i) >= k) chk[i] = 1; } for (i = 1; i <= 200000; i++) chk[i] += chk[i - 1]; for (i = 0; i < q; i++) { scanf("%d %d", &l, &r); cout << chk[r] - chk[l - 1] << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; int Read() { char c; c = getchar(); int tsqua = 0; while (c != ' ' && c != '\n') { tsqua = (((tsqua << 1) << 1) << 1) + (tsqua << 1) + (int)(c - '0'); c = getchar(); } return tsqua; } struct portals { int x1, x2, x3, x4; portals(int _x1, int _x2, int _x3, int _x4) : x1(_x1), x2(_x2), x3(_x3), x4(_x4) {} }; vector<portals> ans; int n; int a[1010], cnta[1010]; int b[1010], cntb[1010]; int c[1010], cntc[1010]; int d[1010], cntd[1010]; int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", b + i); cntb[b[i]] = i; } for (int i = 1; i <= n; i++) { scanf("%d", a + i); cnta[a[i]] = i; } for (int i = 1; i <= n; i++) c[i] = i; for (int i = 1; i <= n; i++) cntc[i] = i; for (int i = 1; i <= n; i++) d[i] = i; for (int i = 1; i <= n; i++) cntd[i] = i; for (int i = 1; i < n; i++) { if (c[i] == cnta[i] && d[i] == cntb[i]) continue; ans.push_back(portals(i, cntc[cnta[i]], cntd[cntb[i]], i)); int tmpci = c[i], tmpdi = d[i]; swap(c[i], c[cntc[cnta[i]]]); swap(d[i], d[cntd[cntb[i]]]); swap(cntc[cnta[i]], cntc[tmpci]); swap(cntd[cntb[i]], cntd[tmpdi]); } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i].x1 << ' ' << ans[i].x2 << ' ' << ans[i].x3 << ' ' << ans[i].x4 << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; template <typename T> void chmin(T &x, const T &y) { if (x > y) x = y; } template <typename T> void chmax(T &x, const T &y) { if (x < y) x = y; } char readc() { char c; while (isspace((c = getchar()))) ; return c; } int read() { char c; while ((c = getchar()) < '-') ; if (c == '-') { int x = (c = getchar()) - '0'; while ((c = getchar()) >= '0') x = x * 10 + c - '0'; return -x; } int x = c - '0'; while ((c = getchar()) >= '0') x = x * 10 + c - '0'; return x; } const int N = 30000 + 5; int a[N], n; bool work() { int n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); int d = a[1]; for (int i = 1; i <= n; ++i) d = min({d, a[i], a[i] - (a[i - 1] - d)}); return d >= 0; } int main() { int case_cnt = read(); for (int case_id = 1; case_id <= case_cnt; ++case_id) puts(work() ? "YES" : "NO"); }
5
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int mod = 1000000007; const int inf = 0x3f3f3f3f; const int maxn = 200005; void task() { string s; cin >> s; int n = s.size(); int R = 0, S = 0, P = 0; for (auto ch : s) { if (ch == 'R') R++; else if (ch == 'S') S++; else P++; } string ans; if (R >= S and R >= P) { for (int i = 0; i < n; i++) ans += 'P'; } else if (S >= R and S >= P) { for (int i = 0; i < n; i++) ans += 'R'; } else if (P >= R and P >= S) { for (int i = 0; i < n; i++) ans += 'S'; } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; cin >> T; while (T--) { task(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; bool compare(int s, int z) { return s > z; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v, v1; set<int> s; s.clear(); bool flag = false; for (int i = 0; i < n; i++) { int x; cin >> x; if (s.count(x)) { flag = true; } else s.insert(x); v.push_back(x); v1.push_back(x); } if (flag == true) { cout << "YES" << endl; continue; } sort(v.begin(), v.end(), compare); if (v == v1) cout << "NO" << endl; else cout << "YES" << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string second; cin >> second; int n = second.length(); for (char c = 'a'; c <= 'z'; c++) { string x = ""; for (int i = 0; i < n; i++) { x = second.substr(0, i); x = x + c; x = x + second.substr(i, n); int k = 0, l = x.length() - 1, fl = 1; while (k <= l) { if (x[k] != x[l]) { fl = 0; break; } k++; l--; } if (fl) return cout << x << '\n', 0; } x = second; x = x + c; int k = 0, l = x.length() - 1, fl = 1; while (k <= l) { if (x[k] != x[l]) { fl = 0; break; } k++; l--; } if (fl) return cout << x << '\n', 0; } cout << "NA\n"; return 0; }
1
#include <bits/stdc++.h> long long mpow(long long a, long long n, long long mod) { long long ret = 1; long long b = a; while (n) { if (n & 1) ret = (ret * b) % mod; b = (b * b) % mod; n >>= 1; } return (long long)ret; } using namespace std; using namespace std; int a[(int)(1e6 + 5)], belong[(int)(1e6 + 5)], c = 1; vector<int> G[(int)(1e6 + 5)]; vector<int> comp[(int)(1e6 + 5)]; bool vis[(int)(1e6 + 5)]; void dfs(int x) { vis[x] = 1; comp[c].push_back(a[x]); belong[x] = c; for (int i = 0; i < G[x].size(); i++) { int to = G[x][i]; if (!vis[to]) { dfs(to); } } } void solve() { int n, m; scanf("%d", &n); scanf("%d", &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= m; i++) { int x, y; scanf("%d", &x); scanf("%d", &y); G[x].push_back(y); G[y].push_back(x); } for (int i = 1; i <= n; i++) { if (!vis[i]) { dfs(i); sort(comp[c].begin(), comp[c].end()); c++; } } for (int i = 1; i <= n; i++) { a[i] = comp[belong[i]].back(); comp[belong[i]].pop_back(); printf("%d", a[i]); printf(" "); } } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int k[10], k2[10]; int ans[2][100010]; int sz[2]; char c; int bst = 0, mx = -1, cur; string s; int main() { sz[0] = sz[1] = 0; memset(k, 0, sizeof(k)); cin >> s; for (int(i) = 0; (i) < (int)(s.length()); ++(i)) k[s[i] - '0']++; for (int(i) = 0; (i) < (int)(10); ++(i)) k2[i] = k[i]; for (int i = (1); i < (int)(10); ++i) { cur = 0; if (k[i] > 0 && k2[10 - i] > 0) { k[i]--, k2[10 - i]--; cur = 1; for (int(j) = 0; (j) < (int)(10); ++(j)) cur += min(k[j], k2[9 - j]); cur += min(k[0] - min(k[0], k2[9]), k2[0] - min(k[9], k2[0])); k[i]++, k2[10 - i]++; if (cur > mx) { mx = cur; bst = i; } } } if (bst != 0) { k[bst]--, k2[10 - bst]--; ans[0][sz[0]++] = bst, ans[1][sz[1]++] = 10 - bst; for (int(j) = 0; (j) < (int)(10); ++(j)) { while (k[j] > 0 && k2[9 - j] > 0) { ans[0][sz[0]++] = j, ans[1][sz[1]++] = 9 - j; k[j]--, k2[9 - j]--; } } } int k0 = min(k[0], k2[0]); k[0] -= k0, k2[0] -= k0; for (int(i) = 0; (i) < (int)(10); ++(i)) { while (k[i] > 0) { ans[0][sz[0]++] = i; k[i]--; } while (k2[i] > 0) { ans[1][sz[1]++] = i; k2[i]--; } } for (int i = sz[0] - 1; i >= 0; --i) printf("%d", ans[0][i]); for (int(i) = 0; (i) < (int)(k0); ++(i)) printf("0"); printf("\n"); for (int i = sz[1] - 1; i >= 0; --i) printf("%d", ans[1][i]); for (int(i) = 0; (i) < (int)(k0); ++(i)) printf("0"); printf("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; const unsigned long long hash1 = 201326611; const unsigned long long hash2 = 50331653; const int N = 200000 + 10; const int M = 20; vector<int> g[N]; int n, vis[N]; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; int u, v; for (int i = 1; i < n; i++) { cin >> u >> v; vis[u]++; vis[v]++; } int flag = 0; for (int i = 1; i <= n; i++) { if (vis[i] == 2) flag = 1; } if (flag) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
4
#include <bits/stdc++.h> int main() { int n, m; scanf("%d%d", &n, &m); if (m == 3 && (n == 5 || n == 6)) { puts("-1"); return 0; } for (int i = 1; i <= m; i++) printf("%d %d\n", i, i * i + 10000000); for (int i = m + 1; i <= n; i++) printf("%d %d\n", i, -i * i - 10000000); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1000005; vector<pair<string, int>> afis; priority_queue<int, vector<int>, greater<int>> pq; int H[maxn]; int n; void up(int poz) { if (poz / 2 == 0) return; if (H[poz] < H[poz / 2]) { swap(H[poz], H[poz / 2]); up(poz / 2); } return; } void push(int x) { H[++n] = x; up(n); } void down(int F) { int S = F * 2; if (S > n) return; if (S < n && H[S] > H[S + 1]) S++; if (S <= n && H[F] > H[S]) { swap(H[F], H[S]); down(S); } return; } void pop() { swap(H[1], H[n]); n--; down(1); } inline bool emptyh() { if (n == 0) return 1; return 0; } int main() { int T; cin >> T; for (int i = 1; i <= T; i++) { string op; int nr; cin >> op; if (op == "insert") { cin >> nr; push(nr); afis.push_back(make_pair("insert", nr)); } else if (op == "removeMin") { if (emptyh()) afis.push_back(make_pair("insert", 0)); afis.push_back(make_pair("removeMin", 0)); if (!emptyh()) pop(); } else { cin >> nr; while (!emptyh() && H[1] < nr) { afis.push_back(make_pair("removeMin", 0)); pop(); } if (emptyh() || H[1] > nr) { afis.push_back(make_pair("insert", nr)); push(nr); } afis.push_back(make_pair("getMin", nr)); } } cout << afis.size() << "\n"; for (auto it : afis) { if (it.first == "removeMin") cout << it.first << "\n"; else cout << it.first << " " << it.second << "\n"; } return 0; }
4