solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e5 + 7; char s[N]; const int MOD = 1e9 + 9; int A, B, C; struct Node { int lc, rc, x, y; bool operator==(const Node &other) const { if (lc == -1 && other.lc == -1) return true; return (lc == other.lc) && (rc == other.rc) && (...
17
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int t; cin >> t; for (int tt = 0; tt < t; tt++) { int n; cin >> n; vector<int> a(n); vector<long long> f(n, 0); for (int i = 0; i < n; i++) { cin >> a[i]; f[i] = a[i]; ...
5
#include <bits/stdc++.h> using namespace std; const int N = 100004; char s[N], t[N], str[N]; int main() { int n, tr; scanf("%d %d", &n, &tr); scanf("%s", s + 1); scanf("%s", t + 1); int cnt = 0; for (int i = 1; i <= n; i++) if (s[i] != t[i]) cnt++; if (tr < cnt) { int val = cnt / 2 + cnt % 2; ...
9
#include <bits/stdc++.h> using namespace std; inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; if (p1 == p2) { p2 = (p1 = buf) + fread(buf, 1, 100000, stdin); if (p1 == p2) return EOF; } return *p1++; } inline void read(int &x) { char c = nc(), b = 1; for (; !(c >= '0' && c <= '9');...
19
#include <bits/stdc++.h> using namespace std; long long faktori[30][65]; long long djelitelji[2][30]; int main() { long long n, b; cin >> n >> b; int brd = 0; for (long long i = 2; i * i <= b; i++) { if (b % i == 0) { djelitelji[0][brd] = i; while (b % i == 0) { djelitelji[1][brd]++; ...
9
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, i, mid, cnt = 0; cin >> n >> a >> b; int str[n]; for (i = 0; i < n; i++) { cin >> str[i]; } sort(str, str + n); mid = n - a; for (i = str[mid - 1]; i < str[mid]; i++) { cnt++; } cout << cnt; return 0; }
0
#include <bits/stdc++.h> #pragma GCC optimize("O2,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2") using namespace std; const int MAXN = 1e6 + 5; int a[MAXN]; int ct[MAXN]; int f[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; std::cout <...
13
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; inline long long rd() { long long x = 0, flag = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + (ch ^ 48); ch = getchar(); } ...
11
#include <bits/stdc++.h> using namespace std; int main() { string s; int a, b, c, d, n, i, k, m, j; cin >> n; for (i = 1; i <= n; i++) { cin >> s >> a >> b; if (a >= 2400 && a < b) { cout << "YES"; exit(0); } } cout << "NO"; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int cnt[N]; int main() { int T; scanf("%d", &T); for (; T--;) { memset(cnt, 0, sizeof cnt); int n; scanf("%d", &n); long long m = 1; while (m * (m + 1) / 2 <= n) m++; m--; for (int i = m; i; i--) { int res = i *...
11
#include <bits/stdc++.h> int main() { int a, b, c, d, e, num; scanf("%d%d%d%d%d", &a, &b, &c, &d, &e); num = a; if (b < num) num = b; if (c / 2 < num) num = c / 2; if (d / 7 < num) num = d / 7; if (e / 4 < num) num = e / 4; printf("%d\n", num); return 0; }
9
#include <bits/stdc++.h> using namespace std; int T; double pi = acos(-1.0); vector<long long> v; long long factorial(long long x) { long long total = 1; for (int i = 2; i <= x; i++) total *= i; return total; } bool isprime(long long x) { vector<bool> prime(x + 1, 1); prime[0] = prime[1] = 0; for (long long...
5
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; short size = s.size(); int n = 0; for (int i = 0; i < size; i++) if (s[i] == 'a') n++; while (n <= size / 2) size--; cout << size << endl; return 0; }
0
#include <cstring> #include <iostream> #include <vector> using namespace std; const int N = 1001; const int K = 1001; int n, k; const int mod = 1e9 + 7; int dp[N][K][2]; int solve(int curr, int k, int dir) { if (k == 1) { return 1; } if (dp[curr][k][dir] != -1) { return dp[curr][k][dir...
8
#include <bits/stdc++.h> using namespace std; int const N = 10e5 + 5; int const mod = 998244353; long double min(long double x, long double y) { if (x < y) return x; else return y; } long double max(long double x, long double y) { if (x > y) return x; else return y; } long long gcd(long long x, ...
9
#include <bits/stdc++.h> using namespace std; long long int fact(long long int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } int gcd(int a, int b) { if (a < 0) a = -a; if (b < 0) b = -b; if (a == 1 || b == 1) return 1; else { int r = a > b ? a % b : b % a; if (r == ...
5
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } signed main() { long long n, a, b, c, d, ans = 0; cin >> n >> a >> b >> c >> d; for (int i = 1; i < n + 1; ++i) { long long x = i + a - d; long long y = i + b - c; long long z = x + b - c; if (x ...
6
#include <bits/stdc++.h> using namespace std; using namespace std; const long long mod = 1e9 + 7; const int inf = 1e9; int n, m, k, fort[200007], num[200007]; set<pair<long double, int>> s; vector<int> G[200007]; vector<int> ans; int idx; long double mx, cur; int main() { ios::sync_with_stdio(0); cin.tie(0); cin ...
15
#include <bits/stdc++.h> using namespace std; int n, k; bool was[(int)1e6 + 10]; vector<int> g[(int)1e6 + 10]; int up[(int)1e6 + 10][30]; void dfs(int v, int par = -1) { for (int to : g[v]) { if (to != par) { up[to][0] = v; dfs(to, v); } } } void solve() { was[n] = true; k = n - k - 1; for...
14
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const long long oo = 1 << 30; const lo...
19
#include <bits/stdc++.h> using namespace std; int n, m; int v[1005]; int x[2005], y[2005]; int sum; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> v[i]; for (int i = 1; i <= m; i++) cin >> x[i] >> y[i]; sum = 0; for (int i = 1; i <= m; i++) { s...
6
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int k; cin >> k; if (s.size() % k != 0) { cout << "NO" << endl; return 0; } for (int i = 0; i < s.size(); i += s.size() / k) { string ss = s.substr(i, s.size() / k); for (int j = 0, l = ss.size() - 1; j < l; j++...
3
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 1e9 + 7; const int inf = (1 << 30) - 1; const ll infll = (1LL << 61) - 1; template <typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} }; template <typename T> using Edges = vector...
11
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } int s, i, e, ans; void solve(void) { cin >> s >> i >> e; if ((s + e) <= i) { cout << "0\n"; return; } double deli = ((s + e - i) / 2....
5
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, ty, val, idx; int op[N] = {0}; int bl[N]; stack<pair<int, int> > seq; stack<int> ans; class node { public: int mbal, lz; } seg[4 * N]; void upd(int id, int l, int r, int v) { seg[id].mbal += v; seg[id].lz += v; } void shift(int id, int l,...
14
#include <bits/stdc++.h> using namespace std; void solution(string wonderful) { int size = wonderful.length(); int count[size]; for (int i = 0; i < size; i++) { count[i] = 0; } for (int i = size - 1; i >= 1; i--) { for (int j = i - 1; j >= 0; j--) { if (wonderful[j] > wonderful[i]) { cou...
0
#include <bits/stdc++.h> using namespace std; long long k, d, t; long long T, T1, T2; long long v; double res; int main() { cin >> k >> d >> t; if (k % d == 0) { res = 1.0 * t; printf("%f\n", res); } else { T = k + d - k % d; T2 = d - k % d; T1 = k; t = t * 2; v = 2 * k + T2; res =...
9
#include <bits/stdc++.h> using namespace std; const int maxn = (1 << 14) + 10; int a[2][maxn]; bool b[2][maxn] = {0}; int vis[maxn]; int g[maxn]; inline int find(int p, int x) { return a[p][x] == x ? x : a[p][x] = find(p, a[p][x]); } inline void join(int p, int x, int y) { x = find(p, x); y = find(p, y); if (x ...
17
#include <bits/stdc++.h> using namespace std; int n, t, len, a[10001], b[10001], f[10001]; int main() { cin >> n >> t; for (int i = 1; i <= n; i++) cin >> a[i]; int len = min(n, t) * n; for (int i = 1; i <= n; i++) b[a[i]]++; int mxa = 1; for (int i = n + 1; i <= len; i++) a[i] = a[i - n]; for (int i = 1;...
11
#include <bits/stdc++.h> using namespace std; const long long N = 105; const long long oo = 1e18 + 7, mod = 1e9 + 7; long long n, m, k; long long arr[N][N]; long long tol_diff[N]; long long builtin[1LL << 20], msk[N]; void process() { cin >> n >> m >> k; for (long long i = 1; i <= n; i++) { for (long long j = 1...
14
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; struct timearray { int gt = 1; int val[MAXN]; int dhgdg[MAXN]; timearray() { memset(dhgdg, 0, sizeof(dhgdg)); } void clear() { gt++; } int get(int i) { if (dhgdg[i] < gt) return -1; return val[i]; } void set(int i, int x) { ...
16
#include <bits/stdc++.h> using namespace std; int main() { int a, b; scanf("%d %d", &a, &b); string s, crap; getline(cin, crap); getline(cin, s); if (a == 0 && b == 0) { printf("Yes"); return 0; } s += s; s += s; s += s; s += s; s += s; s += s; long long i, len, x = 0, y = 0; len =...
9
#include <bits/stdc++.h> struct Edge2 { int first, sec, weight; bool operator<(Edge2 other) const { return weight < other.weight; } }; struct DisjointSet { std::vector<int> par; struct Change { int index, old; }; std::vector<Change> history; DisjointSet(int number) : par(number, -1), history() {} vo...
15
#include <bits/stdc++.h> using namespace std; const int OO = (int)2e9; const double eps = 1e-9; int main() { int n, k, s, e, mid, sum, tmp; cin >> n >> k; s = 1, e = n; while (s < e) { mid = (s + (e - s) / 2); sum = 0, tmp = mid; while (tmp) sum += tmp, tmp /= k; if (sum >= n) e = mid; ...
7
#include <bits/stdc++.h> using namespace std; char s[1010]; int t, n, c0, c1, h; int main() { scanf("%d", &t); while (t--) { scanf("%d%d%d%d", &n, &c0, &c1, &h); scanf("%s", s + 1); int sum = 0; if (h + c0 < c1) { for (int i = 1; i <= n; i++) { if (s[i] == '0') sum += c0; ...
0
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int M = 10010; const int K = 510; const int LIT = 2500; const long long INF = 1LL << 60; int n, m, q; vector<int> r[N], nr[N]; vector<pair<int, int> > qr[N]; int output[N], col[N]; bool vis[N]; set<pair<int, int> > brige; int cut[N]; int deep[N],...
8
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; using ull = unsigned long long; using pii = pair<int, int>; int main(int argc, const char *argv[]) { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; ...
3
#include <bits/stdc++.h> using namespace std; int cnt[100], cnt1[100], cnt2[100], c[100]; vector<int> ans1, ans2; int ansv; int c1[100], c2[100]; int main() { string str; int s, bst, ans; cin >> str; for (int i = 0; i < str.size(); i++) { cnt[str[i] - 48]++; } ansv = -1; ans = cnt[0]; bst = ans; f...
11
#include <bits/stdc++.h> using namespace std; const int N = 1e2 + 10; int n, m, dp[N][N], L[N][N], R[N][N]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { int k; cin >> k; while (k--) { int l, r; cin >> l >> r; for (int j = l; j <= r; j++) { L[i][j] = l; R[i...
21
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; string s; cin >> n >> s; s += "A"; int kek = 0; for (int i = 0; i < n; ++i) { if (s[i] > s[i + 1] && !kek) { kek = 1; continue; } cout << s[i]; } return ...
4
#include <bits/stdc++.h> using namespace std; const int N = 1 << 20; long long ans[N + N], add[N + N], sum[N + N]; int a[N], b[N], chk[N]; int main() { ios::sync_with_stdio(0); int q; cin >> q; int n = 1000001; const long long inf = 1e15; auto push_up = [&](int u) { ans[u] = max(ans[u + u], ans[u + u + 1]);...
16
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2 * 1e5 + 10; ll res; ll a[N]; void solve() { ll n, m, d, x, ans = 0; cin >> n >> m >> d; std::vector<ll> v; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> x; v.push_back(x); } } sort(v....
6
#include <bits/stdc++.h> using namespace std; vector<int> G[150001]; vector<pair<int, long long> > queries; int N; int tin[150001], tout[150001]; int filhos[150001]; vector<pair<int, int> > toutfilhos[150001]; int t; long long val[150001]; int dfsinout(int u, int pai = 0) { filhos[u] = 1; tin[u] = t++; for (auto ...
19
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, m; vector<int> v[101]; bool is_square(int x) { int s = sqrt(x); while (s * s < x) s++; while (s * s > x) s--; return s * s == x; } void add(int x, int y, int t) { while (t--) v[x].push_bac...
16
#include <bits/stdc++.h> const int MaxN = 1010; int mod_v, pw[MaxN], cnt[MaxN], val[MaxN]; int main() { int n, m, t, flag = 0; std::scanf("%d %d %d", &n, &m, &t); if (n < m) std::swap(n, m), flag = 1; for (int i = 0; i != t; ++i) { int x, y, v; std::scanf("%d %d %d", &x, &y, &v); if (flag) std::swap...
17
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int dp[20][300300]; int dmin[20][300300]; int v[300300]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", v + i); for (int i = 0; i < n; i++) dp[0][i] = dmin[0][i] = v[i]; for (int...
12
#include <bits/stdc++.h> using namespace std; int n, m; char s[20][105]; int dp[20][3]; int num[20]; int main(int argc, const char* argv[]) { while (cin >> n >> m) { memset(num, 0, sizeof(num)); for (int i = n; i >= 1; i--) { scanf("%s", s[i]); for (int j = 1; j <= m; j++) { if (s[i][j] ==...
8
#include <bits/stdc++.h> using std::cin; using std::cout; const int N = 100100; int n, a[N]; inline int can(int x) { std::unordered_map<int, int> map; for (int i = 1; i <= x; ++i) ++map[a[i]]; for (int i = 1; i <= n; ++i) { int rev = n + 1 - i; if (rev > x) { if (i > x) { if (a[i] != a[rev])...
17
#include <bits/stdc++.h> using namespace std; long long quick1(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) { ans = ans * a; b--; } b /= 2; a = a * a; } return ans; } int main() { long long n; char c[100]; cin >> n; cin >> c; long long len = strlen(...
12
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; string A; cin >> A; if (k > 0 && n == 1) { cout << 0; return 0; } if (k > 0 && A[0] != '1') { A[0] = '1'; --k; } long long i; for (i = 1; i < A.length() && k > 0; ++i) { if (A[i] != '0') { ...
2
#include <bits/stdc++.h> using namespace std; const int N = 25; int main() { int n, m; cin >> n >> m; string s, t; int x; pair<int, int> a[m]; for (int i = (0), _b = (m); i < (_b); ++i) { cin >> s >> s >> s >> t >> x; if (s == "left") { a[i].first = 0; a[i].second = x; if (x == 1) ...
5
#include <bits/stdc++.h> using namespace std; namespace fastIO { bool IOerror = 0; inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } inline long long read() { bool sign = 0; char ch = getchar(); long long x = 0; for (; blank(ch); ch = getchar()) ; if (IOerror) re...
24
#include <bits/stdc++.h> using namespace std; bool pri[1000010]; bool prime[1000010]; int main() { pri[0] = 1; pri[1] = 1; for (int i = 2; i <= sqrt(1000000); i++) { if (pri[i] == 0) { for (int j = i + i; j <= 1000000; j += i) { pri[j] = 1; } } } long long n, ans = 0; cin >> n; ...
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int a[27][5005][27]; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) { int k = s[i] - 'a'; int idx = 0; while (idx < s.size()) { int kk = s[(i + idx) % s.size()] - 'a'; a[k][idx][kk]++; idx++; ...
8
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 7; long long lzy[N << 2], a[N]; pair<long long, long long> t[N << 2]; void push(long long v, long long l, long long r) { if (lzy[v]) { t[v].first += lzy[v]; if (l != r) { lzy[v * 2] += lzy[v]; lzy[v * 2 + 1] += lzy[v]; } ...
17
#include <bits/stdc++.h> using namespace std; const int N = 105; int a[N]; map<int, int> cnt[N]; int n; int main() { cin >> n; map<int, int> num; for (int i = 0; i < n; ++i) { cin >> a[i]; num[a[i]]++; } if (num.size() == 2) { cout << n; return 0; } cnt[0][0] = 1; int maxidx = 0; int p...
13
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, m, a[1005][1005], b[1005][1005], mare; cin >> n >> m; if (n >= m) mare = n; else mare = m; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) cin >> a[i + j][i]; for (i = 1; i <= 2 * mare; i++) sort(a[i] + 1, a[i] + n + 1); ...
7
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; string to_str(int n) { string res, tmp; while (n) { tmp += n % 10 + '0'; n /= 10; } for (int i = 0; i < tmp.size(); ++i) res += tmp[tmp.size() - i - 1]; return res; } int main() { int n, m, s, d; cin >> n >> m >> s >> d; vect...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; int n, a[MAXN], s[10]; void Print() { puts("-1"); exit(0); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); if (a[i] == 5 || a[i] == 7) Print(); s[a[i]]++; } if (s[1] != n / 3) Print(); ...
4
#include <bits/stdc++.h> using namespace std; string s, p; int t, n; vector<int> v, c; set<int> let; int main() { cin >> s >> p; if (s == p) return cout << 0, 0; for (int i = 0; i < s.size(); i++) if (s[i] != p[t]) n++, v.push_back(i + 1), let.insert(s[i]); else t++; if (v.size() > 1) return...
7
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 5; int n, x, y; int c1[2020], c2[2020]; void solve() { int cnt = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &x, &y); cnt += (c1[x - y + 1000]++) + c2[x + y]++; } printf("%d", cnt); } int main() { ios_base::sy...
5
#include <bits/stdc++.h> using namespace std; const int N = (int)2e5; const int MOD[2] = {(int)1e9 + 7, (int)1e9 + 9}; int n, offset[N], mark[N], e[N][40]; char s[N]; long long p[2][N]; string foo[50]; vector<int> h[50][2]; string read() { scanf("%s", s); return s; } vector<int> build(string &now, long long mul, lo...
24
#include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; struct node *new_node(struct node *head, int num); void print_node(struct node *head); int main() { long long int i; long long int j; long long int n; long long int m; long long int idx; long long int id1; long...
4
#include <bits/stdc++.h> using namespace std; template <typename T> inline void _read(T& x) { char ch = getchar(); bool sign = true; while (!isdigit(ch)) { if (ch == '-') sign = false; ch = getchar(); } for (x = 0; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; if (!sign) x = -x; }...
14
#include <bits/stdc++.h> using namespace std; int main() { char str[4][4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) cin >> str[i][j]; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (str[i][j] == '.') { if (i == 0 && j == 0) { if (str[i + 1][j] =...
3
#include <bits/stdc++.h> using namespace std; ifstream in; ofstream out; const long long kk = 1000; const long long ml = kk * kk; const long long mod = ml * kk + 7; const long long inf = 5 * ml * ml * ml + 7; long long n, i, j, x, y; vector<pair<long long, long long> > m; set<pair<long long, long long> > s; multiset<lo...
12
#include <bits/stdc++.h> using namespace std; vector<int> L[200000 + 1]; pair<int, int> edge[200000 + 1]; long long value_temp[200000 + 1]; long long value[200000 + 1]; long long path[2][1000]; int rmq[18 + 1][2 * 200000 + 1]; int euler[2 * 200000 + 1]; int logK[2 * 200000 + 1]; int parent[200000 + 1]; int height[20000...
16
#include <bits/stdc++.h> using namespace std; const double eps(1e-8); int n, m; int f[6000]; int ans; int a[6000]; int main() { scanf("%d %d", &n, &m); double x; for (int i = (1); i <= (n); ++i) scanf("%d %lf", &a[i], &x); f[1] = 1; ans = 1; for (int i = (2); i <= (n); ++i) { f[i] = 1; for (int j = ...
9
#include <bits/stdc++.h> using namespace std; int n, K, nums[100005], pl = 1, pr, a[100005]; long long res; inline void getin(int x) { res += nums[x]; ++nums[x]; } inline void getout(int x) { --nums[x]; res -= nums[x]; } inline long long calc(int l, int r) { while (pr < r) getin(a[++pr]); while (pl > l) get...
17
#include <bits/stdc++.h> using namespace std; const int N = 5003; int l[N], r[N]; int sum[N], ones[N], twos[N]; pair<int, int> getIntersection(int l1, int r1, int l2, int r2) { if (l2 < l1) { swap(l1, l2); swap(r1, r2); } pair<int, int> intersection = {1, 0}; if (l2 <= r1) { intersection.first = l2;...
9
#include <bits/stdc++.h> using namespace std; int n, m; int a[1000003]; int memo[10003][1003][2]; int calcMemo(int i, int x, bool ok) { if (x == 0 && ok) { return 1; } if (i == min(n + 1, 10000)) { return 0; } int &ret = memo[i][x][ok]; if (ret != -1) return ret; int res1 = calcMemo(i + 1, (x + a[...
11
#include <bits/stdc++.h> using namespace std; long long ara[2003], co[2003], fact[2003], val[2003]; long long bigmod(long long b, long long p) { if (!p) return 1ll; long long ret = bigmod(b, p / 2); ret = (ret * ret) % 1000000007ll; if (p & 1ll) ret = (ret * b) % 1000000007ll; return ret; } int main() { fac...
11
#include <bits/stdc++.h> using namespace std; vector<int> vec; vector<bool> us; queue<int> q; vector<queue<int> > vq; void dfs(int v) { us[v] = true; q.push(v); if (!us[vec[v]]) { dfs(vec[v]); } } int main() { int n; cin >> n; int a[n]; vec.resize(n); us.resize(n, false); map<int, int> m; for ...
6
#include <bits/stdc++.h> using namespace std; const long long inf = ((long long)1) << 60; const double dinf = 1e50; const int INF = 1000000000; const int N = 100005; struct node { int L, R, mid, Max; }; node a[N << 2]; int b[N], c[N]; vector<int> V[N]; int n, m, K; long long h, pos[N], p[N], cnt, d[N]; void Add(long ...
17
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, k, n, m, x, t, y, s; cin >> n >> s >> t; int a[n + 1]; bool vis[n + 1]; memset(vis, false, sizeof(vis)); for (i = 1; i <= n; i++) { cin >> a[i]; } int c = 0; vis[s] = true; i = s; while (i != t) { i = a[i]; c++;...
4
#include <bits/stdc++.h> using namespace std; const int maxN = 100000 + 10; const int maxM = 200000 + 10; struct edge { edge *pre; int other; edge() { pre = NULL; } } * last[maxN], edgeData[maxM], *totEdge; int a[maxN]; int vis[maxN], leaf[maxN]; long long f[maxN]; int cnt[maxN]; int N; long long sum, ans; int fl...
13
#include <bits/stdc++.h> int min(int x, int y) { return x < y ? x : y; } int max(int x, int y) { return x > y ? x : y; } bool Chkmin(int &x, int y) { return y < x ? x = y, true : false; } bool Chkmax(int &x, int y) { return y > x ? x = y, true : false; } const int MAXN = 4000, oo = 0x3f3f3f3f; bool c[MAXN][MAXN]; int U...
20
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const long long INF = 1e18; const long double PI = 4 * atan((long double)1); long long N; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> N; vector<long long> A(N); for (int i = 0; i < (N); i++) ci...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int main() { int n; scanf("%d", &n); string s; cin >> s; map<char, int> np; int k = 0; for (int i = 0; i < (int)s.size(); i++) { if (!np[s[i]]) k++; np[s[i]]++; } int left = 0, right = 0, cnt = 0; map<char, int> mp; i...
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, i = 1, x, c = 0, c1 = 1; cin >> n; while (n >= c1) { n = n - c1; i++; c1 += i; c++; } cout << c << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-14; const int maxn = 100500; const int mod = 1e9 + 7; int get(int L, int R, int md) { int lo = (L % 5 == 0 ? L : L + 5 - L % 5), hi = (R % 5 == 0 ? R : R - R % 5); int d = hi / 5 - max(0, (lo - 1)) / 5; if (R - hi < md) d--; if (lo - L >= ...
17
#include <bits/stdc++.h> using namespace std; double a, b; double cal() { if (fabs(b) < 1e-6) return 1.0; if (fabs(a) < 1e-6) return 0.5; if (4.0 * b >= a) return 0.5 + a / b / 16.0; return 1.0 - b / a; } int main() { int n; while (~scanf("%d", &n)) { while (n--) { scanf("%lf%lf", &a, &b); p...
10
#include <bits/stdc++.h> using namespace std; const int mx = (int)1e5 + 100; deque<int> q; long long sum[mx]; int n, to[mx]; int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) scanf("%d", &to[i]); long long as = 0; q.push_back(n - 1), sum[n - 1] = as = 1; for (int i = n - 2; i; --i) { int x = upper...
15
#include <bits/stdc++.h> int n, k; const int maxn = 200010; int a[26] = {0}; char s[100010]; using namespace std; int main() { cin >> n >> k; cin >> s + 1; for (int i = 1; i <= n; i++) { int t = s[i] - 'A'; a[t]++; } int mi = 1e5; for (int i = 0; i < k; i++) { mi = min(mi, a[i]); } cout << m...
0
#include <bits/stdc++.h> using namespace std; vector<int> v[2002], ans, s; bool c[5555]; int x[5555], n; string t[5555]; vector<pair<int, pair<int, int> > > a; inline void pr(vector<int> a) { for (int i = 0; i < a.size(); i++) cout << (a[i]) << " "; cout << ("") << endl; } inline vector<int> add(vector<int> a, vect...
12
#include <bits/stdc++.h> bool ok(int32_t x, int32_t n, const std::vector<int32_t>& a) { int32_t count = 0; for (int32_t t : a) { count += t / x; } return count >= n; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int32_t n; size_t m; std::cin >> n >> m; std::map<int32_t,...
4
#include <bits/stdc++.h> using namespace std; priority_queue<int> q; int n; long long ans = 0; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } return x *...
15
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; int A[3005], B[3005], sz[3005]; pair<ll, ll> dp[3005][3005], t[3005]; vector<int> edge[3005]; void dfs(int x, int p) { sz[x] = 1; dp[x][1] = {0, A[x] - B[x]}; for (auto i : edge[x]) { if (i == p) continue; dfs(...
17
#include <bits/stdc++.h> using namespace std; #define ll long long const int MOD = 1e9 + 7; const ll INF = 1e18; const char nl = '\n'; /*READ THE QUESTION CORRECTLY (ll vs int)*/ void solve() { string c; cin >> c; int x = 0, y = 0; for (auto r : c) { if (r == 'L') x--; else if (r == 'R') x++; else if (r ==...
8
#include <bits/stdc++.h> const int N = 310; const int inf = 0x3f3f3f3f; using namespace std; int n, m, k; struct data { int x, y, r; data() {} data(int _x, int _y, int _r) : x(_x), y(_y), r(_r) {} bool operator<(const data &a) const { if (a.r != r) return r < a.r; if (a.x != x) return x < a.x; retur...
8
#include <bits/stdc++.h> using namespace std; const int Mx = 100005; int N; vector<int> g[Mx]; int ar[Mx]; void solve() { cin >> N; for (int i = 1; i <= N; i++) { int p, c; cin >> p >> c; if (p != -1) { g[p].push_back(i); } ar[i] = c; } vector<int> v; for (int i = 1; i <= N; i++) { ...
6
#include <bits/stdc++.h> using namespace std; long long n; long long niz[300005][2]; long long gore, dole; long long obican[300005]; long long sranje = 0, res; int main() { scanf("%I64d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &niz[i][0]); for (int i = 1; i <= n; i++) scanf("%I64d", &niz[i][1]); for (...
10
#include <bits/stdc++.h> using namespace std; long long int fact[500005]; long long int fact_(long long int n) { long long int ans = 1; fact[0] = fact[1] = 1; for (int i = 2; i <= n; i++) { ans = (ans * i) % 1000000007; fact[i] = ans % 1000000007; } return ans; } long long int mod_pow(long long int a,...
3
#include <bits/stdc++.h> using namespace std; int main() { string s; map<char, long long> m; long long a[30], k, maxi = -1, x, res = 0; char z = 'a'; cin >> s; cin >> k; for (char i = 1; i <= 26; i++) { cin >> a[i]; if (a[i] > maxi) maxi = a[i]; m[z] = a[i]; z++; } for (int j = 0; j < ...
2
#include <bits/stdc++.h> using namespace std; const long long INF = 1e7; const long long N = 5e5 + 1; const long long mod = 1e9 + 7; const long double eps = 1E-7; long long n, m, cnt, x, y, a[N]; string s; int main() { cin >> n >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == 'n') x++; if (s[i] == 'z'...
0
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807LL; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); vector<int> ans; if (n == 2 && m == 2 && k == 1) { ans.push_back(1); ans.push_back(2); ans.push_back(3); ans.push_back(4...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 100100; int head[maxn], nxt[maxn << 1], ver[maxn << 1], tot; inline void addedge(int a, int b) { nxt[++tot] = head[a]; ver[tot] = b; head[a] = tot; nxt[++tot] = head[b]; ver[tot] = a; head[b] = tot; } int fa[maxn][21], size[maxn], dep[maxn]; lon...
15
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(100001); int t; int mx = -1; vector<bool> vis(100001, false); int dfs(int x, int m) { vis[x] = true; int m1 = 0, m2 = 0; for (int i = 0; i < adj[x].size(); i++) if (!vis[adj[x][i]]) { int n = dfs(adj[x][i], m + 1); if (n > m1) ...
7
#include <bits/stdc++.h> using namespace std; int n, m, x; double v, dp1[105][105], dp2[105][10005], sum[105][105], k; vector<double> a[105]; double num(int _, int i, int j) { int z = a[_].size(); return sum[_][z] - sum[_][z - j] + sum[_][i]; } signed main() { cin >> n >> m; for (int i = 1; i <= n; i++) { c...
11
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<vector<int>, int> m; while (n--) { int h, mi; cin >> h >> mi; vector<int> v; v.push_back(h); v.push_back(mi); m[v]++; } vector<int> v1; for (auto it = m.begin(); it != m.end(); it++) v1.push_back(it->se...
2