solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int n, m; vector<pair<int, int> > A, B; void merge(vector<pair<int, int> >& A) { int i; for ((i) = (0); (i) != (A.size()); ++(i)) { int tx = A[i].first, ty = A[i].second; A[i].first = tx + ty; A[i].second = tx - ty; } } vector<pair<int, pair<int, int> > > ...
18
#include <bits/stdc++.h> using namespace std; const int mod = 1000000009; const int N = 1e5 + 10; int g[N]; int main() { int n, m; cin >> n >> m; if (n == 2) { cout << 0 << endl; cout << "1 1" << endl; return 0; } vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; int idx = min_element...
10
#include <bits/stdc++.h> using namespace std; int t; string a, b, s, c, k, ans, anw; bool is(string s) { for (int i = 0; i <= s.size() / 2; ++i) { if (s[i] != s[s.size() - 1 - i]) { return false; } } return true; } int main() { cin >> t; while (t--) { cin >> s; int l = 0, r = (int)s.size...
7
#include <bits/stdc++.h> using namespace std; const int N = 1000000; const int INF = 100000000; int main() { int n, a[105]; while (~scanf("%d", &n)) { int i, d, Max = -INF, Min = INF; for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (i >= 1) Max = max(Max, a[i] - a[i - 1]); if (i >= 2) Min...
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, k; char a[N]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } k = 1; for (int i = 1; i <= n; ++i) { if (a[i] == a[i + 1] && (a[i] == 'o' || a[i] == 'e' || a[i] == 'a' || a[i] =...
8
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); puts(n & 1 ? "black" : "white\n1 2"); return 0; }
9
#include <bits/stdc++.h> using namespace std; template <class L, class R> ostream& operator<<(ostream& cout, pair<L, R> P) { return cout << '(' << P.first << ',' << P.second << ')'; } template <class T> ostream& operator<<(ostream& cout, vector<T> V) { cout << "[ "; for (auto v : V) cout << v << ' '; return cou...
11
#include <bits/stdc++.h> using namespace std; int main() { string st = ""; int n, c = 0; cin >> n; char empty = '.', coder = 'C'; int i = 1; while (i <= n) { if (i % 2 != 0) { for (int i = 1; i <= n; i++) { if (i % 2 != 0) { st += coder; c++; } if (i % 2...
0
#include <bits/stdc++.h> using namespace std; int main() { int k, q; cin >> k >> q; double probs[10 * k + 1][k + 1]; probs[1][1] = 1; double ka = k; for (int i = 2; i < 10 * k + 1; i++) { probs[i][1] = probs[i - 1][1] / ka; for (int j = 2; j < min(i + 1, k + 1); j++) { double ja = j; pro...
14
#include <bits/stdc++.h> using namespace std; int a[30], b[30]; int io() { int x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) ; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x; } int main() { int T, n; scanf("%d", &T); while (T--) { scanf("%d", &n); set<int> S...
6
#include <bits/stdc++.h> using namespace std; int in[1010][1010]; int a[1010][1010]; int b[1010][1010]; int c[1010][1010]; int d[1010][1010]; int n, m; int get(int arr[1010][1010], int a, int b) { if (0 <= a && 0 <= b && a < n && b < m) { return arr[a][b]; } return 0; } int main() { cin >> n >> m; for (in...
8
#include <bits/stdc++.h> using namespace std; const int N = 2e4 + 10; int n, m, a[55][N], k; long long s[55][N], dp[55][N]; long long ans; long long laz[N << 2], t[N << 2]; void pd(int o, int l, int r) { t[o << 1] += laz[o]; t[o << 1 | 1] += laz[o]; laz[o << 1] += laz[o]; laz[o << 1 | 1] += laz[o]; laz[o] = 0...
15
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cin.get(); string s; for (int i = 0; i < n; i++) { cin >> s; if (s.length() <= 10) cout << s << endl; else cout << s[0] << s.length() - 2 << s[s.length() - 1] << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 500; const int maxm = 500000 + 10; struct Edge { int next, v; Edge() {} Edge(int next, int v) : next(next), v(v) {} } e[maxm << 1]; int n, m, timer, num, len; int head[maxm], cnt; int dfn[maxn], low[maxn], belo[maxn]; bool inst[maxn], used[maxn]; int ...
18
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[1005], b[1005], c[1005]; scanf("%d", &n); long long sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } int minn1 = 0, minn2 = 0, maxi = 0; for (int i = 0; i < n; i++) { b[i] = a[i]; for (int k1...
7
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 5; int ch[N][27], fa[N], d[N], ct, last; void extend(int, int); char str[N]; int len; int pos[N], g[N]; long long val[N]; const int M = 1e5 + 5; string s[M]; int C[M]; int c[N], id[N], cn[N]; int main() { int n; cin >> n; for (int i = 1; i <= n; i+...
19
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, i, j, x, k; cin >> n; int a[n * n]; map<int, int> m; map<int, int>::iterator it; for (i = 0; i < n * n; i++) { cin >> a[i]; m[a[i]]++; } int matrix[n][n]; for (i = 0; i < n; i...
9
#include <bits/stdc++.h> using namespace std; int n, m; int fa[500010], ind[500010]; struct node { int to, next; } edge[500010 * 10]; int head[500010], sumedge, poi[500010], tot, pos[500010]; int cnt, num[500010]; vector<int> v[500010]; bool used[500010], g[2005][2005]; map<pair<int, int>, bool> mmm; int getfa(int x)...
13
#include <bits/stdc++.h> #pragma warning(disable : 4996) template <typename T> T min(T x, T y) { return x < y ? x : y; } template <typename T> T max(T x, T y) { return x > y ? x : y; }; const int INF = 2000000005; const long long mod = 1000000007; const long long MAXN = 1000005; const double eps = 1e-6; int sgn(dou...
19
#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; long long ai,sum=0; cin>>n; for(int i=1;i<=n;++i) { cin>>ai; sum+=ai; } long long ng=sum...
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n >> ws; string ss; getline(cin, ss); int longestWord = 0, noOfWord = 0, currentLen = 0; bool inBracket = false, preLetter = false; for (int i = 0; i < n; i++) { if (ss[i] == 95 && !inBracket) { if (preLetter) { if ...
3
#include <bits/stdc++.h> using namespace std; inline void pisz(int n) { printf("%d\n", n); } template <typename T, typename TT> ostream& operator<<(ostream& s, pair<T, TT> t) { return s << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream& operator<<(ostream& s, vector<T> t) { for (int(i) = ...
7
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; using namespace chrono; void ip(vector<long long int> &arr) { for (long long int i = 0; i < arr.size(); i++) { cin >> arr[i]; } } void solve() { long long int...
1
#include <bits/stdc++.h> using namespace std; vector<vector<long double>> expected_score; vector<vector<long double>> prob; vector<vector<long long>> v; long long N; void solve() { cin >> N; v.resize((1 << N), vector<long long>((1 << N))); for (long long i = 0; i < 1 << N; i++) { for (long long j = 0; j < 1 <...
13
#include <bits/stdc++.h> using namespace std; const int Inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fll; const int maxn = 1 << 17; struct Segbeats { int mx[maxn << 1], nx[maxn << 1], mxs[maxn << 1]; long long sm[maxn << 1]; void inline init() { memset(mx, Inf, sizeof(mx)); } void inline push(int no...
23
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, deg[N], flag, sum[N][6], num[N]; vector<int> g[N]; int calc(int x) { if (sum[x][5] || sum[x][3] > 2 || sum[x][4]) return 5; if (sum[x][3] > 1) return 4; if (sum[x][3] || sum[x][2] || sum[x][1] > 2) return 3; if (sum[x][1] > 1) return 2...
15
#include <bits/stdc++.h> inline int read() { char c; int x; for (c = getchar(); !isdigit(c); c = getchar()) ; for (x = 0; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } return x; } const int mod = 1e9 + 7; inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; } inline int s...
18
#include <bits/stdc++.h> #pragma GCC optimize("O2,unroll-loops") using namespace std; const int inf = 1000000010; const long long INF = 10000000000000010LL; const int mod = 1000000007; const int MAXN = 100010, LOG = 18; int n, m, k, u, v, x, y, t, a, b, ans; int A[MAXN], ps[MAXN]; vector<pair<int, pair<int, int> > > se...
21
#include <bits/stdc++.h> using namespace std; const int N = 200005; long long mod = 1000000009; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } bool is_vowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o...
13
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); std::ios::sync_with_stdio(false); int n, m, k, x, y; bool a[2000][2000]; cin >> n >> m >> k; for (int i = 0; i < k; i++) { cin >> x >> y; a[x][y] = true; if (a[x][y] + a[x + 1][y] + a[x][y + 1] + a[x + 1][y + 1] == 4 || ...
3
#include <bits/stdc++.h> const int MAXN = 1e6 + 5; bool p[MAXN]; int prime[MAXN], cnt, tot; long long n; int lim; int id1[MAXN], id2[MAXN]; long long sm[MAXN], di[MAXN]; inline int getid(long long x) { if (x <= lim) return id1[x]; return id2[n / x]; } inline void prework(int lim) { for (int i = 2; i <= lim; ++i) ...
16
#include <bits/stdc++.h> using namespace std; struct outFloat { int precision; long double value; outFloat(long double v, int p) : precision(p), value(v) {} }; template <typename T> struct debugInfo { string name; T val; debugInfo(const char* n, T v) : name(n), val(v){}; }; template <typename T> debugInfo<T...
8
#include <bits/stdc++.h> using namespace std; long long cnt[10]; long long ans = 0; long long W; void dfs(long long id, long long temp) { if (id == 9) { ans = max(ans, temp); return; } long long down = min(cnt[id], (W - temp) / id); long long up = 0; up = max((long long)0, down - 10); for (; up <= d...
15
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; int i = 1; int j = n; int z; for (z = 0; z < k - 1; z += 2) { cout << i << " " << j << " "; i++; j--; } if (z == k - 1) { while (i <= j) { cout << i << " "; i++...
4
#include<bits/stdc++.h> #define ll long long int using namespace std; int main(){ ll t; cin>>t; while(t--){ ll n; cin>>n; string r,s; cin>>r>>s;...
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; int n, m, k, par[N], portal[N]; long long dist[N]; vector<pair<int, int> > gr[N]; vector<pair<int, pair<int, int> > > edges; vector<pair<long long, pair<int, int> > > new_edges; inline int get(int x) { if (par[x] < 0) return x; return par[x] = g...
18
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int a = n / 11, d = 0; for (int i = 0; i < n; i++) { if (s[i] == '8') { d++; } } cout << min(d, a); }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; void sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio; struct FAIL { FAIL() { cout << "CHANGE!!!" ...
11
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 5; vector<pair<long long int, long long int> > g[N]; int a[N][2]; vector<int> s; bool used[N], ex[N]; int n; int cv, ce; bool tr(int v) { if (used[v]) return 0; used[v] = 1; for (pair<long long int, long long int> it : g[v]) { int u = it.first,...
16
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 7; const int inf = INT_MAX; const long long inff = 1e18; const long long mod = 1e9 + 7; int n, d, e; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> d >> e; e *= 5; int ans = inf; for (int i = 0;; i++) { if (e * i > n...
6
#include <bits/stdc++.h> using namespace std; long long a, b, c, d, e, f; template <class T> inline void read(T &x) { x = 0; char ch = getchar(), w = 0; while (!isdigit(ch)) w = ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x = w ? -x : x; return; } int ...
0
#include <bits/stdc++.h> using namespace std; int main() { long long k, n, w; cin >> k >> n >> w; long long x = 1; long long ans = 0; while (x <= w) { ans += x * k; x++; } cout << max((long long)0, ans - n); }
0
#include <bits/stdc++.h> using namespace std; bool check(int x, int l, int r) { if (x >= l && x <= r) return true; return false; } const int maxn = 1e5 + 10; int n, m, s, f, d, pos; map<int, int> mp; pair<int, int> p[maxn]; string ans; int main() { cin >> n >> m >> s >> f; for (int i = 1; i <= m; ++i) { int...
7
#include <bits/stdc++.h> using namespace std; int n, s, t; int main() { int _; scanf("%d", &_); while (_--) { scanf("%d%d%d", &n, &s, &t); int m = s + t - n; s -= m; t -= m; printf("%d\n", max(s, t) + 1); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, len, k; long long result; int a[100001], q[100001], id[100001], idx[100001]; long long f[100001], t[100001]; long long max(long long a, long long b) { if (a > b) return a; else return b; } bool cmp(int u, int v) { return a[u] > a[v]; } void insert(int i) ...
12
#include <bits/stdc++.h> using namespace std; bool comp(pair<long long, long long> p1, pair<long long, long long> p2) { if (p1.second != p2.second) { return p1.second < p2.second; } return p1.first < p2.first; } void solve() { long long n, m; cin >> n >> m; vector<long long> adj[n + 1]; while (m--) { ...
9
#include <bits/stdc++.h> using namespace std; int n, arr[3], ans, a, b, c, ma, t; multiset<int> s; int main() { scanf("%d", &n); for (int i = 0; i < 3; i++) scanf("%d", arr + i); for (int i = 0; i < n; i++) { scanf("%d", &t); s.insert(t); ma = max(t, ma); } sort(arr, arr + 3); a = arr[0]; b = ...
16
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double PI = acos(0.) * 2; const int lim = 1000000000; const int inf = ~0U >> 1; template <class T> T abs(T x) { return x < 0 ? -x : x; } int a[10], b[10]; int solve(int p, int q) { int ret = 0; for (int i = 0; i < 5; ++i) b[i] = a[i]; ...
13
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { if (x == 0) return y; return gcd(y % x, x); } long long fast_power(long long x, long long y, long long mo) { long long p = 1; while (y) { if (y % 2 != 0) p = (p * x) % mo; y /= 2; x = (x * x) % mo; } return p...
10
#include <bits/stdc++.h> using namespace std; int main(void) { int v1, v2, l, d; scanf("%d %d %d %d", &d, &l, &v1, &v2); printf("%lf\n", (l - d) / ((double)(v1 + v2))); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int N, T; cin >> T; double O = 0, D = 0; while (T--) { cin >> N; O += N; D += 100; } printf("%.12f ", (100 * O / D)); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; struct Vector { int x, y; Vector(void) {} Vector(int _x, int _y) : x(_x), y(_y) {} }; inline bool operator==(const Vector &p, const Vector &q) { return p.x == q.x && p.y == q.y; } inline Vector operator-(const Vector &p, const Vector &q) { ...
19
#include <bits/stdc++.h> using namespace std; long long p[200010]; long long x, a, y, b; long long n; long long k; bool rev(long long i, long long j) { return i >= j; } long long gcd(long long a, long long b) { while (b != 0) { a %= b; swap(a, b); } return a; } bool check(long long f) { if (f > n) retur...
8
#include <bits/stdc++.h> using namespace std; const long long INF = 1e17 + 7; long long t, n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> m; cout << (n * m + 1) / 2 << '\n'; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, flag = 1; char c = getchar(); while ((c > '9' || c < '0') && c != '-') c = getchar(); if (c == '-') flag = 0, c = getchar(); while (c <= '9' && c >= '0') { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } return flag ? x : -x...
21
#include <bits/stdc++.h> using namespace std; const int N = 3 * 1e4 + 5; int n, m, p = 0, topol[N]; bool vis[N]; vector<int> G[N]; void dfs(int u) { vis[u] = true; for (int i = 0; i < G[u].size(); i++) if (!vis[G[u][i]]) dfs(G[u][i]); topol[p++] = u; } int main() { cin >> n >> m; int a, b; for (int i = ...
12
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; cin >> n; vector<int> a(n); map<int, vector<int>> pr; for (int i = 0; i < n; ++i) { cin >> a[i]; for (int j = 2; j * j <= a[i]; ++j) { int cnt = 0; while (a[i] % j == 0) ...
8
#include <bits/stdc++.h> using namespace std; vector<int> v; set<int> st; int main() { int n; cin >> n; string s; cin >> s; int flag = 0; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1] && s[i] != '?') { flag = 1; break; } } if (flag) { cout << "No" << endl; return 0; } ...
5
#include <bits/stdc++.h> const int MAX_N = 222; int max(int a, int b) { return a < b ? b : a; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } int N; int next[MAX_N]; int vis[MAX_N]; int main() { scanf("%d", &N); f...
12
#include <bits/stdc++.h> using namespace std; int p[42] = {0, 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 217...
13
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n; string s; int main() { int t; cin >> t; while (t--) { cin >> s; int num0 = 0; int num1 = 0; int len = s.size(); for (int i = 0; i < len; i++) { if (s[i] == '0') num0++; else num1++...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n = 0, k = 0; cin >> n >> k; string s; cin >> s; vector<long long> a(n); for (long long i = 0; i < n; i++) { a[i] = s[i] - 'a' + 1; } sort(a.begin(), a.end()); ...
1
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; template <class T> using vec = vector<T>; template <class T> using v2d = vector<vector<T>>; template <class T> using v3d = vector<vector<vector<T>>>; template <class T> using v4d = vector<vector<vector<vector<T>>>>; template <cl...
16
#include <bits/stdc++.h> using namespace std; const int MAXN = 700030; const int MOD = 1e9 + 7, H = 3137; int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } int sub(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } int mul(int a, int b) { return (int)(((long long)a * b) % MOD); } int...
18
#include <bits/stdc++.h> using namespace std; pair<long long, long long> calc(int n, long long m) { if (n == 0) return make_pair(0, 0); long long len = 1LL << (n - 1), cnt = 1LL << (2 * n - 2); pair<long long, long long> pos = calc(n - 1, m % cnt); long long x = pos.first, y = pos.second; long long z = m / cn...
9
#include <bits/stdc++.h> using namespace std; int n, k; vector<pair<int, int> > g[200055]; int col[200055]; void dfs(int v, int par, int res, int was = -1) { bool bad = (int)g[v].size() > res; int cur = 0; for (auto p : g[v]) { int to = p.first; if (to != par) { if (!bad && cur == was) { cur...
11
#include <bits/stdc++.h> using namespace std; int main() { int n, a[1005], i, j, k, m, l, r, ll, rr; while (~scanf("%d", &n)) { for (i = 0; i < n; i++) scanf("%d", &a[i]); k = m = 0; for (i = 0; i < n - 1; i++) { if (a[i] + 1 == a[i + 1]) { if (k == 0) { k = 2; ll = a[i...
5
#include <bits/stdc++.h> using namespace std; long long n, d[100005], c1, c2, g; vector<int> vec; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> d[i]; } sort(d, d + n); for (int i = 2; i < n; i++) { if (d[i] == d[i - 1] && d[i] == d[i - 2]) { d[i - 2] = -1; } } if (d[n - 1] ...
3
#include <bits/stdc++.h> using namespace std; char str[11][20] = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"}; char s[102]; int main() { int n, tem, i, j, ans = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", s);...
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; cin.ignore(); string query; vector<string> tree; for (int i = 0; i < n; i++) { getline(cin, query); if (query == "pwd") { cout << "/"; for (auto x : tree) cout...
6
#include <bits/stdc++.h> using namespace std; const int MX = 1e6 + 5, ALPH = 30; string s; int t[ALPH], n; long long pref[MX], res; map<long long, int> dp[ALPH]; int main() { ios_base::sync_with_stdio(false); for (int i = 0; i < 26; ++i) cin >> t[i]; cin >> s; n = s.size(); pref[0] = 0; for (int i = 1; i <=...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, k, x, p = 1; cin >> n >> k >> x; vector<long long> a(n + 1), orstg(n + 1), ordrp(n + 1); for (int i = 0; i < n; i++) cin >> a[i]; orstg[0] = a[0]; for (int i = 1; i < n; i++) orstg[i] = orstg[i - 1] | a[i]; ordrp[n - 1] = a[n - 1]; for (i...
9
#include <bits/stdc++.h> using namespace std; void solve() { long long ans = 0; long long n; cin >> n; vector<long long> v(n); int neg = 0, pos = 0, z = 0; for (int i = 0; i < (n); i += 1) { int num; cin >> num; if (num >= 1) { ans += num - 1; v[i] = 1; pos++; } else if (nu...
1
#include <bits/stdc++.h> using namespace std; template <class T> bool chkmin(T& a, T b) { return a > b ? a = b, true : false; } template <class T> bool chkmax(T& a, T b) { return a < b ? a = b, true : false; } const int maxn = 1000000 + 10; int n; char S[maxn]; namespace Code { char rev[maxn]; int fail[maxn]; int l...
20
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int x; cin >> x; long long int n = sqrt(x); if (n * n == x) cout << 2 * n; else if (x - (n * n) > n) cout << 2 * n + 2; else cout << 2 * n + 1; }
3
#include <bits/stdc++.h> using namespace std; long long modpow(long long x, long long y, long long m) { long long z = 1; while (y) { if (y & 1) z = (z * x) % m; x = (x * x) % m; y >>= 1; } return z; } int egcd(int a, int b, int& x, int& y) { if (a == 0) { x = 0; y = 1; return b; } ...
8
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1); const long double eps = 1e-9; const int seed = 131; const int M = 1e2 + 7; char s[M], t[M]; int ns[M], nt[M]; int main() { ios::sync_with_stdio(false); cin.tie(0); int q; cin >> q; while (q--) { int n; cin >> n; cin >> ...
14
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int MAXN = 1000000; int n, k; bool visited[MAXN + 3]; int len[MAXN + 3]; int main() { scanf("%d %d", &n, &k); int ans = 1; if (k != 1) { int r = 1; int v = k; while (v != 1 && v != 0) { v = (int)(((long long)v * k) %...
10
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 2e5 + 5; int n, k; set<int> G[N]; set<int> L[N]; int ans; void AddLeaf(int u) { int p = *G[u].begin(); L[p].insert(u); if ((int)L[p].size() >= k) { assert((int)L[p].size() == k); ++ans; for (auto &v : L[p]) { ...
15
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin>>t; while(t--){ int n; cin>>n; int l[2], p[2]; int c=0; for(int i...
0
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n; cin >> n; string a; cin >> a; int l = n, r = 0; for (size_t i = 0; i < a.size(); i++) { if (a[i] == '1') { l = (l == n ? (int)i : l); r = (int)i + 1; } } cou...
2
#include <bits/stdc++.h> using namespace std; const int N = 10005, E = 200005; int Head[N], Next[E], Adj[E], Flow[E], tot = 1; inline void addedge(int u, int v, int w) { Next[++tot] = Head[u], Head[u] = tot, Adj[tot] = v, Flow[tot] = w; Next[++tot] = Head[v], Head[v] = tot, Adj[tot] = u, Flow[tot] = 0; } int S, T, ...
17
#include <bits/stdc++.h> using namespace std; int main() { int n; int k; cin >> n; int sum = 0, mx = 214000000; for (int i = 0; i < n; i++) { cin >> k; sum += k; if (k % 2 == 1 && k < mx) { mx = k; } } if (mx == 214000000 && sum % 2 == 0) { cout << 0; return 0; } if (sum ...
4
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, set<T> S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } t...
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; long long t[MAXN]; set<string> se; string s1(string s) { string str = ""; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'u') { str += "oo"; } else { str += s[i]; } } return str; } string s2(string s) { strin...
5
#include <bits/stdc++.h> using namespace std; template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } const int maxn = 300005; vector<int> g[maxn]; int n, m, q; int d[maxn], f[maxn]; bool vis[maxn]; void init() { for (int i = 1; i <= n; ++i) f[i] = i, d[i] = 0; } int getf(int x) { return x == f[x] ? x...
13
#include <bits/stdc++.h> using namespace std; static const int MAXN = 6e5 + 10; int n, d; bool dp[MAXN]; int main() { while (~scanf("%d%d", &n, &d)) { memset(dp, 0, sizeof(dp)); dp[0] = 1; int sum = 0; for (int i = 1; i <= n; ++i) { int x; scanf("%d", &x); for (int j = (sum += x); j ...
14
#include <bits/stdc++.h> using namespace std; int n, m, K, v; int a[111][111]; int ans = 0; int main() { cin >> n >> m >> v >> K; int x, y; for (int i = 0; i < v; i++) { cin >> x >> y; a[x][y] = 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { for (int k = 1; k <= n; k++) ...
3
#include <bits/stdc++.h> using namespace std; struct st { int x; int y; int in; }; bool compx(st a, st b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } bool compy(st a, st b) { return a.y == b.y ? a.x < b.x : a.y < b.y; } int ar[1000006]; int br[1000006]; map<int, int> M; vector<int> ans[1000006]; int n, m; vect...
14
#include <bits/stdc++.h> using namespace std; const int N = 1234567; struct point { int x; int y; }; point a[N]; int parent[N]; int key(point p) { return p.x * 1000 + p.y; } int find_parent(int v) { if (v != parent[v]) { parent[v] = find_parent(parent[v]); } return parent[v]; } void disjoint_set_union(int...
4
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { int r; while (a != 0) { r = a; a = b % a; b = r; } return b; } int main() { int t, k; long long a[10005]; cin >> t; k = t; int i = 0; while (k--) { cin >> a[i]; i++; } long long count = 0;...
8
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, x, ans = 2; cin >> n >> x; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; vector<long long int> res; long long int sum = 0; for (long long int i = 0; i < n; i++) { if (a[i] != x) res.push_back(a[i]); ...
7
#include <bits/stdc++.h> using namespace std; int n, m; long long l[200000], r[200000]; long long ans[200000]; pair<pair<long long, long long>, int> segments[200000 - 1]; long long ba[200000]; set<pair<long long, long long> > bridges; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ...
12
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > ans; const int inf = 3e3 + 9; int a[inf]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int j; for (int i = 0; i < n; i++) { j = i; for (int z = i; z < n; z++) if (a[z] < a[j]) j = z; if ...
4
#include <bits/stdc++.h> using namespace std; long long pos[1000100], sum[1000100], mang[1000100], trai[1000100], base = 1000000007; int n, a[1000100]; long long get(int x) { long long ans = 1; while (x != 0) { ans = (ans + mang[x]) % base; x = x - (x & (x ^ (x - 1))); } return ans; } void push(int ...
12
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b, long long m) { long long c = 1; while (b > 0) { if (b % 2) c *= a, c %= m; b /= 2; a *= a; a %= m; } return c; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v ...
13
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n; vector<int> tim; string tmp, s; int ans = 1; int main() { scanf("%d", &n); cin.ignore(); while (n--) { getline(cin, s); tmp = s.substr(1, 2); int h = stoi(tmp); tmp = s.substr(4, 2); int m = stoi(tmp); if (h == 12...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(); long long n; cin >> n; long long l1 = 1, l2 = 1; int val = -1; while (1) { if (l1 > n) break; long long temp = l2; l2 = l1; l1 = l1 + temp; val++; } cout << val; }
8
#include <bits/stdc++.h> using namespace std; long long b1[200010], b2[200010]; int main(void) { string a, b; cin >> a >> b; memset(b1, 0, sizeof(b1)); memset(b2, 0, sizeof(b2)); long long ans = 0; for (int i = 0; i < b.length(); i++) { b1[i] = b1[i - 1] + (b[i] == '1'); b2[i] = b2[i - 1] + (b[i] ==...
7
#include <bits/stdc++.h> using namespace std; int v[312345]; int main() { ios::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; } int m1 = 0, m2 = 0; for (int i = 0; i < n; i++) { if (v[i] != v[0]) m1 = max(m1, abs(i - 0)); } for (int i = 0; i < n; i++) { if ...
3