solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; long long cal_weight(long long x) { long long t = 1; while (t <= x) t *= 10; return (t - x - 1) * x; } int main() { long long im1, im2; cin >> im1 >> im2; long long res = max(cal_weight(im1), cal_weight(im2)); long long t = 5; for (int i = 0; i < 9; ++i) { ...
8
#include <bits/stdc++.h> using namespace std; int main() { double i, j, d, l, v1, v2, t; cin >> d >> l >> v1 >> v2; printf("%.8lf\n", (l - d) / (v1 + v2)); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxint = -1u >> 1; const double pi = acos(-1.0); long long tmp; int n; int main() { while (cin >> n) { long long cnt = 0LL; long long ans = 0LL; for (int i = 0; i < n; i++) { cin >> tmp; ans += tmp; ans += (tmp - 1LL) * cnt; c...
3
#include <bits/stdc++.h> using namespace std; const int N = 404; int T, md, n, fac[N], f[N], h[N], g[N][N]; void init() { const int n = 400; for (int i = *fac = 1; i <= n; ++i) fac[i] = (long long)i * fac[i - 1] % md; for (int i = 1; i <= n; ++i) { h[i] = fac[i]; for (int j = 1; j < i; ++j) h[i] = (...
18
#include <bits/stdc++.h> using namespace std; int n; int a[2005][2005]; bool used[2005]; int mn[2005]; int pr[2005]; vector<int> e[2005]; int tin[2005], tout[2005]; int timer; int d[2005][2005]; bool res = true; int lca[2005][2005]; void dfsPr(int v, int pr = -1) { tin[v] = ++timer; for (int i = 0; i < (int)e[v].si...
11
#include <bits/stdc++.h> using namespace std; int vis[10][10]; char s[5]; int dir[8][2] = {-1, -2, -1, 2, 1, -2, 1, 2, -2, 1, -2, -1, 2, 1, 2, -1}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); while (cin >> s) { memset(vis, 0, sizeof(vis)); int x = s[0] - 'a'; int y = s[1] - '1'; vis[x][...
4
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int judge(int a[], int n, int k) { for (int i = 0; i < n; i++) { if (a[i] != k) { return false; } } return true; } int main() { int n, k; while (~scanf("%d %d", &n, &k)) { int a[100 + 10], vis[100 + 10]; for (in...
4
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int cnt[2][(1 << 20) + 5]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } cnt[1][0] = 1; int x = 0; long long ans = 0; for (int i = 0...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; string t[9]; bool s[69][10000]; vector<int> adg[69]; map<int, bool> mp; bool dfs(int v, int sa) { mp[v] = 1; if (v == 8) return true; while (s[v][sa] != 1) { for (int i = 0; i < adg[v].size(); i++) { if (mp[adg[v][i]] == 0 && s[adg...
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (int i = 0; i < n; i++) { long lon...
11
#include <bits/stdc++.h> using namespace std; vector<int> g[300010], ch[300010]; vector<pair<int, int> > up[300010]; int p[300010], dp[300010][30], val[300010]; int u, v, n; long long ans; void dfs1(int u, int f) { p[u] = f; for (int v : g[u]) if (v != f) { ch[u].emplace_back(v); dfs1(v, u); } }...
18
#include <bits/stdc++.h> using namespace std; int s[405][405]; bitset<405> ans[405], f[28][405]; int n, m, r, c; int main() { scanf("%d%d", &n, &m); for (int i = 0; i <= n - 1; i++) for (int j = 0; j <= m - 1; j++) { char c = getchar(); while (c < 'a' || c > 'z') c = getchar(); ans[i][j] = 1; ...
18
#include <bits/stdc++.h> using namespace std; int main() { long long a, b; string s; cin >> a >> b; cin >> s; long long dx = 0, dy = 0; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'D') { dy--; } if (s[i] == 'U') { dy++; } if (s[i] == 'R') { dx++; } if ...
9
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; vector<long long> vote[3001]; priority_queue<long long, vector<long long>, greater<long long>> pq; bool yee(long long a, long long b) { return a > b; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long n, m, p, c, tmp...
9
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") const long long maxn = 400 + 5, N = 8e2 + 10, SQ = 320, base = 800287, mod = 1e9 + 7, INF = 1e9 + 5, lg = 22; const long double eps = 1e-4; long long n, dp[maxn][maxn]; inline long long fix(l...
20
#include <bits/stdc++.h> using namespace std; map<string, int> myMP; string myName; void processString(string x) { string firstName = "", secondName = ""; int operation = 0; int i; for (i = 0;; i++) { if (x[i] == ' ') break; firstName += x[i]; } i++; if (x[i] == 'p') { operation = 1; i += ...
7
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed; cout << setprecision(10); } int i, j, n, min = 0; cin >> n; int a[n]; for (i = 0; i < n; i++) cin >> a[i]; min = abs(a...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); int long long n; cin >> n; vector<int long long> a(n + 1), t(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; int long long w = 0; for (int i = 1; i <= n; i++) { w = min(w, a[i] - i...
8
#include <bits/stdc++.h> using namespace std; long long t = 1; long long bitshift(long long x) { long long ans = 1; for (long long i = 0; i < x; i += 1) ans *= 2; return ans; } long long height(long long n) { long long ans = 0; for (long long i = 0; i < 60; i += 1) if (bitshift(i) == n + 1) return i; } lo...
11
#include <bits/stdc++.h> unsigned long long p[16] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; unsigned long long ans; unsigned long long n; void dfs(unsigned long long depth, unsigned long long temp, unsigned long long num, unsigned long long up) { if (num ...
12
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7; int t; string l, r; long long mem[19][5][2]; long long dp(int i, int n, int pre, string s, int sz) { if (i == sz) { if (n <= 3) return 1; return 0; } if (n > 3) return 0; if (mem[i][n][pre] != -1) return...
11
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, d, i, j, k, l; cin >> a >> b; long long int a1[a], a2[a]; for (i = 0; i < a; i++) { cin >> a1[i]; } a2[a - 1] = a1[a - 1]; for (i = a - 2; i >= 0; i--) { a2[i] = max(a1[i], a2[i + 1]); } long long int ans = -1; ...
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n; int arr[N], old[N]; vector<int> adj[N]; bool vis[N]; int ans; vector<vector<int>> vv; vector<int> s; void DFS(int i) { if (vis[i]) { return; } vis[i] = 1; s.push_back(i); for (int x = 0; x < adj[i].size(); ++x) DFS(adj[i][x]); } i...
6
#include <bits/stdc++.h> using namespace std; const int mx = 4000 + 5; const int mx1 = 36000; bool vis[mx1]; map<int, long long int> mapa; int main() { int i, j, k, n; long long int a; string s; cin >> a >> s; n = s.size(); int sum; int nint = 0; for (i = 0; i < n; i++) { sum = 0; for (j = i; j ...
8
#include <bits/stdc++.h> using namespace std; int main() { int t, n, u, v; string str; scanf("%d", &t); while (t--) { scanf("%d", &n); cin >> str; int i; for (i = 0; i < n / 2; ++i) { u = abs((str[n - i - 1] - str[i]) % 26); if (u != 2 && u != 0) break; } if (i != n / 2) ...
2
#include <bits/stdc++.h> using namespace std; long long const inf = 0x7fffffffffff; int const maxn = 100003, maxP = maxn * 20; int n = 0; int a[maxn], b[maxn]; int head[maxn], nxt[maxn << 1], to[maxn << 1], cnt = 0; inline void insert(int u, int e) { nxt[++cnt] = head[u]; head[u] = cnt; to[cnt] = e; } struct Line...
19
#include <bits/stdc++.h> using namespace std; long long int Encrypt(long long int a) { vector<long long int> vv; for (long long int n = a; n > 0; n /= 10) { vv.push_back((n % 10) % 2); } long long int ans = 0; for (long long int i = vv.size() - 1; i >= 0; --i) { ans = ans * 10 + vv[i]; } return an...
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int mx[N * 4], flag[N * 4], a[N]; void push_down(int root) { if (!flag[root]) return; mx[root << 1] += flag[root]; mx[root << 1 | 1] += flag[root]; flag[root << 1] += flag[root]; flag[root << 1 | 1] += flag[root]; flag[root] = 0; } void up...
14
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; char s[500005], t[500005]; int nxt[500005], cnt1 = 0, cnt0 = 0; void ov() { for (int i = 0; i < cnt1; i++) printf("1"); for (int j = 0; j < cnt0; j++) printf("0"); exit(0); } void print(int i) { if (t[i] == '0') { if (cnt0) { ...
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int l[n]; for (int i = 0; i < n; i++) cin >> l[i]; long long int sum = 0, lesum = 0; for (int i = 0; i < n; i++) sum += l[i]; sort(l, l + n); vector<int> res(2); long long int diff = INT_MAX; for (int i = 0; i < n ...
3
#include <bits/stdc++.h> using namespace std; void solve() { string s1, s2; cin >> s1 >> s2; if (s1 == s2) cout << s1 << endl; else cout << 1 << endl; } int main() { cin.tie(0); cin.sync_with_stdio(0); solve(); }
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const int N = 2e3 + 5; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qmi(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (...
9
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long int myRand(long long int B) { return (unsigned long long)rng() % B; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m;...
12
#include <bits/stdc++.h> using namespace std; int main() { int n, lo = 0, lc = 0, ro = 0, rc = 0, l, r; cin >> n; while (n--) { cin >> l >> r; if (l == 0) lo++; else lc++; if (r == 0) ro++; else rc++; } if (lo > lc) lo = lc; if (ro > rc) ro = rc; cout << ro + lo...
0
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); } template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); } template <class T> inline T ison(T n...
13
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, i, j, k, d, x, p, q, f = 0; cin >> x >> d; long long a[10005], h = 0, g = 1; while (x > 0) { if (x == 1) { a[h++] = g; break; } else if ((x & x + 1) == 0) { k = ceil((log(x) / log(2))); } else { k = fl...
9
#include <bits/stdc++.h> using namespace std; int n, m; bool debug = false; int k; struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} double abs() const { return hypot(x, y); } double arg() const { return atan2(y, x); } Point operator*(double o) const { return Point(x * o, y * ...
21
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; string vow = "AEIOUY"; string s; int res = INT_MIN; cin >> s; for (int i = 0; i < s.length(); i++) for (int j = 0; j < vow.length(); j++) if (s[i] == vow[j]) v.push_back(i); if (v.size() == 0) res = s.length() + 1; e...
2
#include <bits/stdc++.h> using namespace std; int a[400][400]; int ans[400]; bool vis[400]; int main() { int n, i, j, k, z, fuck; bool ok; while (cin >> n) { memset(vis, 0, sizeof(vis)); memset(ans, -1, sizeof(ans)); for (i = 1; i <= n + 1; ++i) for (j = 1; j <= n; ++j) cin >> a[i][j]; for (...
10
#include <bits/stdc++.h> using namespace std; struct cpl { double r, i; cpl() { r = i = 0; } cpl(double a, double b) { r = a, i = b; } cpl operator+(cpl a) { return cpl(r + a.r, i + a.i); } cpl operator-(cpl a) { return cpl(r - a.r, i - a.i); } cpl operator*(cpl a) { return cpl(r * a.r - i * a.i, r * a.i + ...
23
#include <bits/stdc++.h> using namespace std; const int N = 15e4 + 5; int n, m, p; inline int in() { char c; int f = 1; while ((c = getchar()) < '0' || c > '9') if (c == '-') f = -1; int x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0'; return x * f; } struct hh { int num, ...
24
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int maxn = 3005; long double x[maxn], y[maxn]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; long long res = (long long)n * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 24; long double pi = acos(-1.0L); f...
17
#include <bits/stdc++.h> using namespace std; const int maxn = 1011000, mod = 1e5 + 100; long long int a1[maxn]; long long int b1[maxn]; long long int dp[maxn]; long long int sum(long long int a) { long long int s = 0; while (a != 0) { s += a; a--; } return s; } int main() { std::ios_base::sync_with_s...
8
#include <bits/stdc++.h> using namespace std; int main() { long int i, n, j = 0, k, l; int a[100000]; char x[100001]; cin >> n; cin >> x; for (i = 0; i < n; i++) { if (x[i] == 'n') { a[j] = 1; j++; } else if (x[i] == 'z') { a[j] = 0; j++; } } k = j; for (i = 0; i < ...
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 500000 + 1000; int n, m, ans, par[MAXN]; vector<int> adj[MAXN], out[MAXN]; bool mark[MAXN]; int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void dfs(int x) { mark[x] = 1; par[x] = x + 1; for (int i = 0; i < adj[x].size...
13
#include <bits/stdc++.h> using namespace std; int sauske() { int n, m, a = 0; cin >> n >> m; while (n < m) { m % 2 ? m++ : m /= 2, a++; } cout << a + n - m; return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; while (t--) sauske(); }
6
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector") #pragma GCC target("avx") using namespace std; using namespace rel_ops; using ll = int64_t; using Pii = pair<int, int>; using ull = uint64_t; using Vi = vector<int>; void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); ...
25
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long long MXN = 2e5 + 10; const long long LOG = 20; long long n, m; long long A[MXN], lg[MXN], rmq[LOG][MXN], Rmq[LO...
21
#include <bits/stdc++.h> template <typename T> inline void read(T &x) { x = 0; char c = getchar(); bool flag = false; while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); } while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); if (flag) x = -x; } using namespace std; int n, m, P; inl...
21
#include <bits/stdc++.h> using namespace std; const int oo = 1000000009; const double eps = 1e-9; const int mod = 1000000007; int nos[10]; int main() { nos[0] = 6; nos[1] = 2; nos[2] = nos[3] = 5; nos[4] = 4; nos[5] = 5; nos[6] = 6; nos[7] = 3; nos[8] = 7; nos[9] = 6; int a, b, ans = 0; cin >> a >...
2
#include <bits/stdc++.h> using namespace std; const long long INF = 2e9; const long long MOD = 1e9 + 7; const long long MB = 31; long long goX[] = {0, -1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1}; long long goY[] = {3, 2, 1, 0, -1, -2, -3, -2, -1, 0, 1, 2}; vector<vector<long long>> getGraph(long long n, long long m) { vect...
14
#include <bits/stdc++.h> using namespace std; int n, cnt = -1, temp, qcnt = 0; int deg[500000], l1[500000], l2[500000]; int fir[500000], to[1000000], nxt[1000000]; int fa[500000], ans[500000], f[500000]; int id[500000], mx[2000000], o[500000]; vector<int> len[500000]; struct query { int x, val, o; } qu[1000000]; inli...
21
#include <bits/stdc++.h> using namespace std; int M[5555][5555]; char str[5555][5555]; bool Map[5555][5555]; int main() { int n, m; char s[10]; while (scanf("%d%d", &n, &m) != EOF) { char ch; for (int i = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) M[i][j] = 0; for (int i = 0; i < n; i++) { ...
8
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx,avx2,sse,sse2,sse3,sse4,popcnt,fma") using namespace std; template <typename val1> void __f(const char* name, val1&& value) { cout << name << " = " << value << endl; } template <typename val1, typename... values> void __f(const char* names, v...
0
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const long long mod = 1e9 + 7; inline long long read() { long long X = 0; bool flag = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') flag = 0; ch = getchar(); } while (ch >= '0' && ch <= '9') { X = (X <<...
15
#include <bits/stdc++.h> using namespace std; int main() { int s, v1, v2, t1, t2; cin >> s >> v1 >> v2 >> t1 >> t2; int a = t1 + s * v1 + t1; int b = t2 + s * v2 + t2; if (a < b) cout << "First" << '\n'; else if (b < a) cout << "Second" << '\n'; else cout << "Friendship" << '\n'; }
0
#include <bits/stdc++.h> using namespace std; pair<string, string> letras[100001]; int ar[100001]; bool veri(string x, string y, string a, string b) { string mini = min(x, y); string maxi = max(a, b); if (mini.compare(maxi) == -1) { return true; } return false; } int main() { int n; cin >> n; for (i...
6
#include <bits/stdc++.h> using namespace std; const int maxn = (int)1e5 + 100; const int mod = (int)1e9 + 7; const long long INF = (long long)1ll << 61; int n, m, k, q, fa[maxn]; long long dis[maxn], ans[maxn * 6]; vector<pair<int, long long> > g[maxn * 6]; vector<pair<int, int> > que[maxn]; vector<tuple<long long, int...
17
#include <bits/stdc++.h> using namespace std; vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) { v.emplace_back(x); } return move(v); } template <typename T, typename... Args> inline string arrStr(T arr, int n) { stringstream s; ...
0
#include <bits/stdc++.h> using namespace std; int h[1001]; int main() { int n, i, j, x, a = 0, k; scanf("%d", &n); for (i = 2; i <= n; i++) { int j = 2; x = i; while (j <= x) { k = 0; while (x % j == 0) { k++; x /= j; } if (k > h[j]) h[j] = k; j++; } ...
7
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long ret = 0; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); long long prefix = 1; for (int i = 1; i < n; i++) { long long curVal = (prefix * a[i]) % 100000000...
7
#include <bits/stdc++.h> using namespace std; const int LOG = 18; const int N = (1 << LOG); const long long MOD = 998244353; const long long W = 2192; long long sum(long long a, long long b) { a += b; if (a >= MOD) a -= MOD; return a; } long long sub(long long a, long long b) { a -= b; if (a < 0) a += MOD; ...
23
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long x = pow(2, n + 1) - 2; cout << x << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, k[30]; bool check(int s) { int ret = 0; for (int i = 0; i <= 26; i++) if (k[i]) ret += (k[i] - 1) / s + 1; if (ret > n) return false; return true; } string s; int main() { cin >> s >> n; int j; for (int i = 0; i < s.size(); i++) k[s[i] - 'a']++; f...
6
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFF = 1e18; const double pi = 3.141592653589793; const double inf = 1e18; const double eps = 1e-9; const long long mod = 1e9 + 7; const int maxmat = 10; inline int RI() { int ret = 0; char tmp; while (!isdigit(tmp = getchar...
6
#include <bits/stdc++.h> using namespace std; long long T, n, m, k, t; long long s[10000009]; int main() { int ncase = 0; while (scanf("%I64d", &n) == 1) { if (n % 3) { printf("0\n"); continue; } n /= 3; int cnt = 0; for (long long i = 2; i * i <= n; i++) if (n % i == 0) { ...
16
#include <bits/stdc++.h> using namespace std; stack<int> stc; set<int> st; int main() { int n, a, ans = 0; string s; cin >> n; for (int i = 0; i < 2 * n; i++) { cin >> s; if (s == "add") { cin >> a; stc.push(a); st.insert(a); } else { if (stc.empty() || (stc.top() == *st.begi...
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1]; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int pre = 1, tr = 0; while (1) { if (a[pre] > 0) { if (tr == 0) { a[pre]--; tr = 1; cout << "P"; continue; } else { ...
4
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-9; const int INF = 1E9; const int MAXN = 200500; const int ALPH = 27; struct node { int L, len, link; int go[ALPH]; node(int _L = 0, int _len = 0) { L = _L; len = _len; link = 0; memset(go, 0, sizeof(...
15
#include <bits/stdc++.h> using namespace std; const int maxn = 3 * 100000 + 10; char op[maxn]; long long sum[maxn]; int p[26]; vector<int> q; int main() { scanf("%s", op); int l = strlen(op); memset(p, -1, sizeof(p)); for (int i = 0; i < l; i++) { int c = op[i] - 'a'; p[c] = max(p[c], i); q.clear();...
12
#include <bits/stdc++.h> using namespace std; char aa[100010]; int sum[100010]; int main() { int t; cin >> t; while (t--) { int n, x, ans = 0; cin >> n >> x; for (int i = 1; i <= n; i++) { cin >> aa[i]; if (aa[i] == '0') sum[i] = sum[i - 1] + 1; else sum[i] = sum[i - ...
9
#include <bits/stdc++.h> using namespace std; const int N = 1000010, mod = 1e9 + 7, INF = 0x3f3f3f3f; const double eps = 1e-6; int n, m, v; int main() { scanf("%d%d%d", &n, &m, &v); if (m < n - 1) { puts("-1"); return 0; } if (m == 0) return 0; bool f = false; for (int i = 1; i <= n - 2; i++) { ...
9
#include <bits/stdc++.h> using namespace std; int di[8] = {1, -1, 0, 0, 1, 1, -1, -1}; int dj[8] = {0, 0, 1, -1, 1, -1, 1, -1}; const long long N = 1e5 + 5, MOD = 1e9 + 7, oo = 1e18; const double PI = 3.141592653589793238; int d, n, x, y; long long ans; int main() { cin >> d >> n; for (int i = 1; i <= n; i++) { ...
2
#include <bits/stdc++.h> using namespace std; int n, k; char s[200]; char p[200]; char ans[200]; bool used[200][30]; bool brute(int pos) { if (pos == n) { puts(ans); exit(0); } if (ans[pos] != 0) { if (pos >= strlen(s) - 1) { if (p[pos - strlen(s) + 1] == '0') { bool ok = true; f...
13
#include <bits/stdc++.h> using namespace std; bool myComparison(pair<long long, long long> f, pair<long long, long long> g) { return f.second < g.second; } int main() { ios_base::sync_with_stdio(false); vector<pair<long long, long long> > skills; long long cf, cm, n, MAX_skill, m; cin >> n >> MAX_skill >> cf ...
11
#include <bits/stdc++.h> using namespace std; int main() { int r, c; scanf("%d%d", &r, &c); char buf[1 << 10]; int i; for (i = (0); i < (r); ++i) scanf("%s", buf); int cnt = 0; int prev = 0; for (i = (1); i < (c + 1); ++i) if (i == c || buf[i] != buf[prev]) { if (buf[prev] != '.') ++cnt; ...
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 5001; const int MOD = 1000000000 + 7; int dp[MAXN][MAXN]; int sum[MAXN][MAXN]; int min_index[MAXN]; int max_index[MAXN]; long long doit(int n, int a, int b, int k) { for (int i = 0; i < n; i++) { min_index[i] = n - 1; max_index[i] = 0; for (in...
11
#include <bits/stdc++.h> using namespace std; bool fun(int a[], int n) { for (int i = 1; i <= n; i++) { if (a[i] != i) return true; } return false; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; } int...
0
#include <bits/stdc++.h> using namespace std; struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; bool sort1(const pair<pair<int, int>, int> &a, cons...
0
#include <bits/stdc++.h> using namespace std; int sg[100100] = {0, 0, 0}; int sgs[100100] = {0, 0, 0}; bool mark[2020]; int solve(int n) { memset(mark, false, sizeof(mark)); for (int i = 2; (1 + i) * i / 2 <= n; i++) { if ((n - (1 + i) * i / 2) % i != 0) continue; int k = (n - (1 + i) * i / 2) / i; int ...
12
#include <bits/stdc++.h> using namespace std; inline int iin() { int x; scanf("%d", &x); return x; } inline long long int lin() { long long int x; scanf("%lld", &x); return x; } const long long int N = 1e6 + 85; const long long int M = 1e3 + 85; const long long int mod = 1e9 + 7; const long long int pinf = ...
15
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d %d", &n, &m); vector<bool> cat(n + 10); vector<vector<int>> tree(n + 10); vector<int> max_cat(n + 10); vector<int> ans(n + 10); for (int i = 1; i <= n; ++i) { int t; scanf("%d", &t); cat[i] = t; } cat[0] = false...
7
#include <bits/stdc++.h> using namespace std; using ll = long long; const int infi = INT32_MAX / 2; const ll infl = INT64_MAX / 2; const double pi = acos(-1); vector<int> ans; class Solve { string s; void set_value() { cin >> s; } int change; void judge() { change = 0; for (int i = (1); i < (s.size()); ...
0
#include <bits/stdc++.h> using namespace std; struct Trie { int mark; Trie* son[53]; }; Trie* nul; Trie node[100000]; char s[10001], s1[10001]; int pn; Trie* root; int n; Trie* newnode() { Trie* temp; temp = &node[++pn]; temp->mark = 0; for (int i = 0; i < 53; i++) temp->son[i] = nul; return temp; } int f...
18
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; void qread(int &x) { int neg = 1; x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') neg = -1; c = getchar(); } while (c >= '0' && c <= '9') x = 10 * x + c - '0', c = getchar(); x *= neg; } const int maxn = 10...
18
#include <bits/stdc++.h> using namespace std; const long long MN = 200005; long long n, sx, sy, px, py, ans; pair<long long, long long> a[MN]; void check(long long x, long long y) { long long ret = 0; for (long long i = 1; i <= n; i++) { long long l = min(0ll, a[i].first), r = max(0ll, a[i].first); long lon...
5
#include <bits/stdc++.h> using namespace std; int N, W; int nA, nB; long long bestA[600001]; long long bestB[600001]; char ans[300001]; struct level { int a1, a2, i; } A[300000], B[300000]; struct cmp { bool operator()(const level& a, const level& b) const { if (a.a1 != b.a1) return a.a1 < b.a1; if (a.a2 !=...
18
#include <bits/stdc++.h> using namespace std; vector<long long> vr[200005]; long long n; pair<long long, long long> func(long long); int main() { cin >> n; for (int i = 2; i < n + 1; i++) { long long x; cin >> x; vr[x].push_back(i); } cout << func(1).second; } pair<long long, long long> func(long lo...
17
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int x[n], z[n + 1]; vector<int> y; for (int i = 0; i < n; i++) { cin >> x[i]; y.push_back(x[i]); } sort(y.begin(), y.end()); int prev = y[0]; int curr = 1; ...
12
#include <bits/stdc++.h> using namespace std; const long long MAXN = 100005; long long a[MAXN]; long long d[MAXN]; long long sp[MAXN]; long long best[MAXN]; int main() { long long n, k; scanf("%lld%lld", &n, &k); for (long long i = 1; i <= n; ++i) scanf("%lld", &a[i]); for (long long i = 1; i <= n; ++i) scanf("...
14
#include <bits/stdc++.h> using namespace std; const int N = 100; const int M = 4; const int INF = N * N * 4; int n; char c[N + 10]; int a[M][N][N]; int m[M][2]; int main() { scanf("%d ", &n); for (int(i) = (0); (i) < (int)(M); (i)++) { for (int(j) = (0); (j) < (int)(n); (j)++) { fgets(c, n + 2, stdin); ...
6
#include <bits/stdc++.h> using namespace std; ifstream fin("%s.in"); ofstream fout("%s.out"); const int MAX_N = 1005; const int MAX_M = 1005; const int MAX_Q = 100005; char grid[MAX_N][MAX_M]; int N[MAX_N][MAX_M]; int S[MAX_N][MAX_M]; int E[MAX_N][MAX_M]; int W[MAX_N][MAX_M]; char dir[MAX_Q]; int len[MAX_Q]; vector<cha...
9
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; inline int read() { int x = 0, f = 1, c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f ^= 1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return f ? x : -x; } int n; char tmp[200005]; int cnt[26]; int sum[20000...
5
#include <bits/stdc++.h> using namespace std; vector<int> adjList[200000]; int leafs[200000], rez, n, k; void dfs(int cur) { while (leafs[cur] >= k) { int todo = k; ++rez; for (int ctr1 = adjList[cur].size() - 1; ctr1 >= 0; --ctr1) { if (adjList[adjList[cur][ctr1]].size() == 1) { adjList[adj...
15
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define float double #define min3(a, b, c) min(c, min(a, b)) #define min4(a, b, c, d) min(d, min(c, min(a, b))) #define rrep(i, n) for(int i=n-1;i>=0;i--) #define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); #define gc...
5
#include <bits/stdc++.h> using namespace std; const int MAX_N = 200025; long long sum[MAX_N], val[MAX_N], ans[MAX_N], ans_, Sum; int p[MAX_N], eid, cnt[MAX_N]; bool vis[MAX_N]; void init() { memset(vis, false, sizeof(vis)); memset(p, -1, sizeof(p)); eid = 0; } struct edge { int v, next; } e[MAX_N << 1]; void ad...
10
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 100; int a[maxn]; int main() { long long n; cin >> n; for (long long i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); long long res = 0; long long mmax = 0; for (long long i = 1; i < n; ++i) { if (a[i] <= a[i - 1] * 2) { r...
4
#include <bits/stdc++.h> using namespace std; const char nl = '\n'; const int INF = 0x3f3f3f3f; const long long INFLL = 0x3f3f3f3f3f3f3f3f; const long long MOD = 1e9 + 7; const long double EPS = 1e-10; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); int main() { ios::sync_with_stdio(0); ...
4
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const int maxn = 1e5 + 5; int main() { string s; long long n, k, ans = 0; set<string> str; queue<string> q; cin >> n >> k >> s; str.insert(s); q.push(s); while (!q.empty() && str.size() < k) { string n...
12
#include <bits/stdc++.h> using namespace std; int main() { int n, i, flag, count; cin >> n; int at[n]; for (i = 0; i < n; i++) { cin >> at[i]; } count = n - at[0]; if (at[0] == 0) { flag = 0; for (i = 1; i < n; i++) { if (at[i] != i) { flag = 1; break; } } i...
3