solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> #pragma comment(linker, "/stack:64000000") using namespace std; int v[26] = {0}; int del[26] = {0}; int main() { string s; cin >> s; int n; cin >> n; for (int(i) = 0; (i) < ((int)(s).size()); (i)++) v[s[i] - 'a']++; vector<pair<int, int> > w; for (int(i) = 0; (i) < (26); (i)++) { ...
4
#include <bits/stdc++.h> using namespace std; void collect_digits(std::vector<long long>* digits, unsigned long num) { if (num > 9) { collect_digits(digits, num / 10); } digits->push_back(num % 10); } int main(void) { long m, s; cin >> m >> s; if (m == 1 && s > 9 || s == 0 && m > 1) { cout << "-1 -1...
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (auto &i : v) cin >> i; map<int, int> lastPos; for (int i = 0; i < n; ++i) { lastPos[v[i]] = i; } int mm = INT_MAX; int ans = -1; for (auto p : lastPos) { if (mm > p.second) { mm = p.secon...
2
#include <bits/stdc++.h> using namespace std; namespace mine { long long qread() { long long ans = 0, f = 1; char c = getchar(); while (c < '0' or c > '9') { if (c == '-') f = -1; c = getchar(); } while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar(); return ans * f; } void write(lon...
20
#include <bits/stdc++.h> using namespace std; const int NMax = 1005; const long double eps = 0.0000001; struct po { long double x, y; }; po a[NMax]; int n; int l, r; long double dist_point_dr(po C, po A, po B) { long double a = A.y - B.y; long double b = B.x - A.x; long double c = A.x * B.y - A.y * B.x; retur...
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long int l, r, m; cin >> l >> r >> m; long long int x, y, z, mod = m; if (m < l) { x = l - m; cout << l << " " << l << " " << l + x << end...
7
#include <bits/stdc++.h> using namespace std; const int inf = 1e7; int main() { int n = 0; scanf("%d", &n); vector<long long> no(n, 0); long long sum = 0; for (int i = 0; i < n; ++i) { int val = 0; scanf("%d", &val); no[i] = val; sum += val; } if (sum % 3 != 0) { printf("0\n"); ret...
9
#include <bits/stdc++.h> using namespace std; int n, m, a[200100], b[200100], c[200100]; struct event { int x, id; event(int x = 0, int id = 0) : x(x), id(id) {} bool operator<(const event &e) const { return x == e.x ? id > e.id : x < e.x; } } e[200100 << 1]; long long ans[200100]; int x[200100], y[200100],...
18
#include <bits/stdc++.h> using namespace std; int INK, C = 3; bool adj[2200][2200]; void join(int i, int j) { assert(!adj[i][j]); adj[i][j] = adj[j][i] = 1; } vector<int> o, nu; int est(int K, int n = 2) { if (n == 2) { int cnt = 0, LEN = 2; for (int i = 31; i--;) if ((1 << i) & K) { LEN = m...
11
#include <algorithm> #include <iostream> #include <cmath> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int r = sqrt(n * 2 - 1); while ((r + 1) * (r + 1) <= n * 2 - 1) r++; while (r * r > n * 2 - 1) r--; cout << (r - 1) / 2 << '\n'; } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 409; const long long P = 1000000007; int n, kk; int lx[N], ly[N], rx[N], ry[N]; char CC[1000]; int d[N][N]; int to[N][N]; bool go(int x, int y) { if (x > y) { return 1; } if (ly[x] >= rx[x]) return 0; if (lx[x] < x) return 0; if (ry[x] > y) retur...
16
#include <bits/stdc++.h> const int md = 1000000007; long long inf = 1000000000001; using namespace std; using namespace std::chrono; long long pw(long long a, long long b) { int c = 1, m = a; while (b) { if (b & 1) c = (c * m); m = (m * m); b /= 2; } return c; } long long pwmd(long long a, long long...
9
#include <bits/stdc++.h> using namespace std; template <typename TYPE> inline void chkmax(TYPE &first, TYPE second) { first < second ? first = second : 0; } template <typename TYPE> inline void chkmin(TYPE &first, TYPE second) { second < first ? first = second : 0; } template <typename TYPE> void readint(TYPE &firs...
24
#include <bits/stdc++.h> char s[100000]; int main() { int N, i, j, l, v; bool ans; scanf("%d%s", &N, &s); for (int l = 1; l <= N; l++) { for (int i = 0; i < N; i++) { ans = true; for (j = 0; j < 5; j++) { v = i + j * l; if (v >= N || s[v] == '.') ans = false; } if (an...
5
#include <bits/stdc++.h> using namespace std; const int inf = ~0U >> 1, maxn = 100000 + 10; int n, ans = 0; int back[maxn], d[maxn], now[maxn], trace[maxn]; bool style[maxn], v[maxn]; void setIO(string name) {} long long gI() { char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); long long ...
7
#include <bits/stdc++.h> using namespace std; int main() { long long t, a, b, c; cin >> t; while (t--) { cin >> a >> b >> c; if (a >= c && b != 1) { cout << -1 << ' '; } else { cout << 1 << ' '; } if (c >= b * a) { cout << -1 << endl; } else { cout << b << endl; ...
2
#include <bits/stdc++.h> using namespace std; int print(int x1, int y1, int x2, int y2, int x3, int y3, char a, char b, char c) { if (y1 == y2 + y3 and x2 == x3 and x1 + x2 == y1) { cout << y1 << endl; for (int i = 0; i < x1; ++i) { for (int j = 0; j < y1; ++j) cout << a; cout << endl; ...
9
#include <bits/stdc++.h> using namespace std; const int maxx = 1e2 + 20; int a[maxx][maxx]; int main() { int k, n = 2, i = 0; cin >> k; while (k > 0) { if (k < i) { i = 0; n++; } a[n - 1][i] = 1; a[i][n - 1] = 1; k -= i; i++; if (i >= n - 1 && k > 0) { i = 0; n+...
8
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, i, j; string str; vector<pair<string, long long int>> res; cin >> n; priority_queue<long long int, vector<long long int>, greater<long long int>> ...
8
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&...
19
#include <bits/stdc++.h> using namespace std; int main() { int n, m, s, f; cin >> n >> m >> s >> f; int *t = new int[m], *l = new int[m], *r = new int[m]; for (int i = 0; i < m; i++) cin >> t[i] >> l[i] >> r[i]; char move = 'R'; if (s > f) move = 'L'; string ans = ""; int time = 1, pos = s; for (int i...
7
#include <bits/stdc++.h> using namespace std; int N; vector<int> A, odd, even; vector<vector<int> > C, flow, neigh; bool isPrime(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) { return false; } } return true; } int edmondsKarp(int s, int t) { flow = vector<vector<int> >(N + 2, vector<int>(...
15
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; int pd = 0, ph = 0, cd = 0, ch = 0, ans = 0; for (int i = 0; i < m; i++) { if (i == 0) { cin >> pd >> ph; ans = pd - 1 + ph; } else { cin >> cd >> ch; if (cd > ...
8
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #pragma G++ optimize("Ofast,no-stack-protector") #pragma G++ target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,a...
0
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000 * 1000 * 1000 + 7; const int N = 1000 * 1000 + 10; long long dp[N]; int main() { int n; cin >> n; int c1 = 0, c2 = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (x == 1) c1++; else c2++; } dp[0] = dp[...
15
#include <bits/stdc++.h> using namespace std; class Interval { public: long long start; long long end; int index; Interval(int index, long long start, long long end) { this->index = index; this->start = start; this->end = end; } }; bool operator<(const Interval& a, const Interval& b) { return (a....
12
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> g; g.resize(n); int a; for (int(i) = 0; i < int(n); ++i) { cin >> a; g[i] = a; } int mn = abs(g[0] - g[n - 1]); int f = 1, s = n; for (int i = 0; i < n - 1; i++) { if (abs((g[i + 1] - g[i])) < mn) { ...
0
#include <bits/stdc++.h> using namespace std; struct group { int sizee; int money; int num; }; struct tables { int sizee; int num; }; bool byIncomDescending(const group &g, const group gg) { return g.money > gg.money; } bool bySize(const tables &t, const tables &tt) { return t.sizee < tt.sizee; } bool FitOr...
8
#include <bits/stdc++.h> using namespace std; const int mx = 1005; const long long inf = 100000000000000ll; int n, m, tot, ntot, st, ed, fir[mx], bk[mx], v[mx], c[mx], nfir[mx]; long long d[mx]; struct node { int po, dep; node(){}; node(int _p, int _d) : po(_p), dep(_d){}; bool operator<(const node a) const { r...
11
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; const long long mod = 1e9 + 7; const long long INF = 100000000000000LL; const double PI = acos(-1.0); typedef struct EDGE { int v; int next; } Edge; Edge edge[maxn * 2]; int head[maxn], cnt; inline void add(int u, int v) { edge[cnt].v = v; ...
14
#include <bits/stdc++.h> using namespace std; inline unsigned long long read() { unsigned long long x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x; } int main() { unsigned long lo...
0
#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> void db(T a, int p = -1) { if (p >= 0) cout << fixed << setprecision(p); cout << a << ...
7
#include <bits/stdc++.h> using namespace std; int n; int arr[1000000]; int main() { scanf("%d", &n); int i = 0; while (i < n) { scanf("%d", &arr[i]); i++; } sort(arr, arr + n); int j = n - 1; i = (j + 0) / 2; int mid = i; int coun = 0; while (i >= 0 && j > mid) { if (2 * arr[i] <= arr[j]...
8
#include <bits/stdc++.h> using namespace std; int n, l, x, y, a[100005]; bool xok, yok; pair<int, int> ff(int v) { int l = 0; for (int r = 0; r < n; r++) { while (a[l] + v < a[r]) l++; if (a[l] + v == a[r]) return make_pair(a[l], a[r]); } return make_pair(-1, -1); } pair<int, int> fl(int v) { int r = ...
9
#include <bits/stdc++.h> using namespace std; int main() { long long k, n, w; cin >> k >> n >> w; long long sum = ((w * (w + 1)) / 2) * k; if (sum >= n) cout << sum - n; else cout << 0; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 55; vector<int> g[N]; int n, k; long long answer = 0; struct TResult { int arr[5] = {0}; int size = 0; TResult() {} }; TResult dfs(int u, int par) { TResult res = TResult(); res.arr[0] = 1; res.size = 1; for (auto v : g[u]) { if (v != p...
13
#include <bits/stdc++.h> using namespace std; const int maxn = 20 + 2; const int maxm = (1 << 18) + 2; int n; double dp[maxm]; double a[maxn][maxn]; void DP() { dp[(1 << n) - 1] = 1; for (int i = (1 << n) - 1; ~i; i--) { int cnt = __builtin_popcount(i); for (int j = 0; j < n; j++) if ((1 << j) & i) ...
11
#include <bits/stdc++.h> #pragma comment(linker, "/stack:225450978") #pragma GCC optimize("Ofast") using namespace std; const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18; const long double Pi = 3.141592653589793116, EPS = 1e-9, Gold = ((1 + sqrt(5)) / 2); long long keymod[] = {1000000007LL, 1...
8
#include <bits/stdc++.h> using namespace std; const int limN = 2e5 + 5; bool usd[limN]; int ans[limN]; int main() { set<int> lib; stack<int> s; int N, K; scanf("%d%d", &N, &K); for (int i = 1; i <= K; i++) { scanf("%d", &ans[i]); usd[ans[i]] = true; } for (int i = 1; i <= N; i++) if (!usd[i]) ...
12
#include <bits/stdc++.h> using namespace std; int main() { long n, a, b, num; vector<long> v; while (cin >> n) { a = b = 0; v.clear(); for (int i = 0; i < n; i++) { cin >> num; v.push_back(num); } long st = 0; long nd = n - 1; while (st <= nd) { if (v[st] < v[nd]) { ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; for (int i = 0; i < n; i++) { cin >> s; vector<int> v(26, 0); int c = 1; for (int i = 0; i <= s.length() - 1; i++) { if (s[i] == s[i + 1]) { c++; } else { if (c % 2 == 1) { ...
2
#include <bits/stdc++.h> using namespace std; int n, a[100100], b[100100]; int main() { cin >> n; for (int i = 0; i < n; i++) { int x, y; scanf("%i%i", &x, &y); ++a[x + y]; ++b[x - y + 10000]; } long long res = 0; for (int i = 0; i <= 30000; i++) res += 1ll * a[i] * (a[i] - 1) / 2 + 1ll * ...
5
#include <bits/stdc++.h> const int N = 150; int n; int k; int a[N]; void init() { std::cin >> n; std::cin >> k; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } } void work() { int s = 0; for (int i = 1; i <= n; i++) { s += a[i]; } std::cout << std::max(0, 2 * n * k - n - 2 * s) << std::endl...
1
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0), ios_base::sync_with_stdio(0); int n; cin >> n; string s; cin >> s; int x = 0, y = 0, ans = 0; vector<pair<int, int>> v; for (auto i : s) { if (i == 'U') y += 1; else x += 1; v.push_back({x, y}); } for (int...
1
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; const int inf = INT_MAX; template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } struct n...
20
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; bool vis[N]; vector<int> v[N]; int who; int maxD; int distFromFirst[N], distFromLast[N]; void dfs(int s, int d, int flag) { vis[s] = 1; if (!flag) distFromFirst[s] = d; else distFromLast[s] = d; if (d >= maxD) { maxD = d; who =...
12
#include <bits/stdc++.h> using namespace std; const int MAXN = 200000; long long n, k, x, a[MAXN + 10]; priority_queue<pair<long long, pair<int, long long> >, vector<pair<long long, pair<int, long long> > >, greater<pair<long long, pair<int, long long> > > > que; long long _abs(const l...
12
#include <bits/stdc++.h> using namespace std; vector<long long> a; int n; vector<vector<long long>> v; vector<vector<int>> cou; vector<set<int>> g; vector<long long> f(long long x, int num) { long long y = x; vector<long long> ans = {}; for (int i = 0; i < 70; i++) { if (y % 2 == 1) cou[i].push_back(num); ...
11
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int a = 0; for (int i = 1; i < 7; i++) { if (s[i] == '1' and s[i + 1] == '0') a += 10; else a += s[i] - '0'; } cout << a + 1 << endl; }
11
#include <bits/stdc++.h> using namespace std; int main() { int n, x, k; cin >> n >> k >> x; int a[n]; int tot = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - k; i++) { tot += a[i]; } int ans = tot + (k * x); cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; string s; int main() { cin >> s; int n = s.size(); int ans = 0, cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; else if (cnt) { ans += 2; cnt--; } } cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; map<int, bool> closed; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { int n, s, k; cin >> n >> s >> k; for (int i = 0; i < k; i++) { int x; cin >> x; closed[x] = 1; } int ans =...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, count = 0; int arr[200]; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); vector<int> ans; int k = 0; int l; ans.push_back(1); for (int i = 1; i < n; i++) { if (arr[i] == 0) ans.push_back(1); else...
6
#include <bits/stdc++.h> using namespace std; pair<int, int> p[200010]; int ar[200010], t, k; bool check(int f) { int z = 0; for (int i = 1; i <= k + 1; i++) { if (f < (ar[i] - ar[i - 1])) return false; if (f >= 2 * (ar[i] - ar[i - 1])) z += ar[i] - ar[i - 1]; else { z += 2 * (2 * (ar[i] - a...
9
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; cin >> n >> k; vector<long long> x(n), y(n); for (long long i = 0; i < n; ++i) { cin >> x[i]; } for (long long i = 0; i < n; ++i) { cin >> y[i]; } sort(x.begin(), x.end()); long long ans = 0; vector<long long> arr(n...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int ct = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x, y; cin >> x >> y; if (x || y) { ct++; } } } cout << ct; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long lsb(long long x) { for (long long bit = 0; bit < 64; ++bit) if ((x >> bit) & 1) return bit; return -1; } void process(map<long long, long long>& m, long long& w, vector<long long>& v) { for (long long i : v) m[lsb(i)]++; } int main() { long long t; c...
11
#include <bits/stdc++.h> using namespace std; int lex(string first, string second) { int n = first.length(); int m = second.length(); for (int i = 0; i < min(n, m); i++) { if (first[i] < second[i]) return 1; } if (n < m) return 1; return 0; } int main() { string s; cin >> s; int n = s.length(); ...
2
#include <bits/stdc++.h> using namespace std; void readi(int &x) { int v = 0, f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = v * 10 + c - '0'; while (isdigit(c = getchar())) v = v * 10 + c - '0'; x = v * f; } void readll(long long &x) { l...
19
#include <bits/stdc++.h> using namespace std; int Score(int s, int t, int x) { int p; for (int i = 0; i <= 5 && s * (1 << i) <= t; ++i, p = 500 * i) ; return p * (1 - double(x) / 250) * (x != -1); } int n, t, a, s[5], v[5], p[5]; int main() { int n; cin >> n; t = n; for (int i = 0; i < n; i++) for...
12
#include <bits/stdc++.h> using namespace std; int main() { bool test = true; int r1, r2, c1, c2, d1, d2; cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2; for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { for (int k = 1; k < 10; k++) { for (int l = 1; l < 10; l++) { if (i + j == r...
2
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int N, M, K; int ft[200...
4
#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; cin >> n; string s; cin >> s; vector<long long> ans(100000, 0); for (long long i = 0; i < n; i++) { long long a, b; cin >> a >> b; if (s[i] == '1') {...
5
#include <bits/stdc++.h> using namespace std; int a[200004], n; void solve() { cin >> n; for (int i = 0; i < n; i++) scanf("%d", &a[i]); int flag = 1, x = 1, y = 1000000000, mk = 0, ty = 1; for (int i = 1; i < n; i++) { if (a[i] == a[i - 1]) flag = 0; if (a[i - 1] != a[i] - 1 && a[i - 1] != a[i] + 1) { ...
9
#include <bits/stdc++.h> using namespace std; const int mx = 505; int arr[mx][mx]; int main() { int n; scanf("%d", &n); if (n <= 2) return !printf("-1\n"); int i, j; arr[0][0] = 9; arr[0][1] = 7; arr[0][2] = 2; arr[1][0] = 6; arr[1][1] = 1; arr[1][2] = 5; arr[2][0] = 3; arr[2][1] = 8; arr[2][2...
16
#include <bits/stdc++.h> char ch; inline void read(int &x) { x = 0; ch = getchar(); while (ch <= 32) ch = getchar(); while (ch > 32) { x = x * 10 + ch - 48; ch = getchar(); }; }; int n; int a[105]; int main() { int i, j; read(n); int ans1 = 0, ans2 = 0; int cnt = 0; for (i = 1; i <= n; i++) ...
12
#include <bits/stdc++.h> using namespace std; bool isPrime(int x) { for (int i = 2; i * i <= x; ++i) { if (x % i == 0) { return false; } } return true; } int A[200]; vector<int> G[200]; int Nod[200][200], Nok[200][200]; int C1[200][200], C2[200][200]; int U[10000], V[10000]; int N, M; int C[200]; ve...
13
#include <bits/stdc++.h> using namespace std; bool pr[1000007]; void sieve() { pr[0] = 1; pr[1] = 1; for (int i = 2; i * i < (1000007); i++) { for (int j = 2 * i; j <= 1000007; j += i) { pr[j] = 1; } } } long long fpow(long long x, long long y) { x = x % 998244353; long long res = 1; while (...
9
#include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 5; const long long MOD = 998244353; long long fact[MAX], ans[MAX]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); fact[0] = fact[1] = 1ll; for (long long i = 2; i < MAX; i++) { fact[i] = (i * fact[i - 1]) % MOD; } ...
25
#include <bits/stdc++.h> using namespace std; const long long inf = (int)1e17; const long long MX = 3001; int A[3011], B[3011]; int ep[3011], es[3011]; struct edge { int from, to; long long cap, cost; edge() {} edge(int from, int to, long long cap, long long cost) : from(from), to(to), cap(cap), cost(cost...
12
#include <bits/stdc++.h> using namespace std; struct state { int val, idx; }; bool operator<(const state &a, const state &b) { if (a.val == b.val) { return (a.idx < b.idx); } return (a.val < b.val); } state A[100]; int n, k, cnt, best_diff, best_cnt; int From[1000], To[1000]; int main() { scanf("%d %d", &...
6
#include <bits/stdc++.h> int main() { char c = '0'; int sum = 1, i = 0; for (i = 0; i < 2; i++) { scanf(" %c", &c); switch (c) { case '1': sum *= 7; break; case '2': sum *= 2; break; case '3': sum *= 3; break; case '4': sum *=...
3
#include <bits/stdc++.h> using namespace std; int n; int op[200005], val[200005]; int stk[200005], tp; int ans[200005]; int main() { scanf("%d", &n); int prefix = 0; for (int i = 1; i <= (2 * n); ++i) { char str[3]; scanf("%s", str); if (*str == '+') { op[i] = 1; ++prefix; } else { ...
9
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> dir{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int n, m; vector<int> adj[300005]; vector<int> a2[300005]; bool vis[300005]; int p[300005]; int d[300005]; int col[300005]; void dfs(int u) { if (vis[u]) return; vis[u] = 1; for (int v : adj[u]) { if (v...
14
#include <bits/stdc++.h> using namespace std; long long int M = 100000000000000; long long int M1 = 1000000007; void dfs(long long int n, vector<vector<long long int> > &v); int main() { long long int n, m, x, y, i, j; cin >> n >> m; vector<vector<long long int> > v(n + 1); for (i = 0; i < m; i++) { cin >> ...
7
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long int b[m]; for (int i = 0; i < m; i++) cin >> b[i]; long long int c[n]; c[0] = a[0]; for (int i = 1; i < n; i++) { c[i] = c[i - 1] + a[i]; } ...
2
#include <bits/stdc++.h> using namespace std; vector<double> seg; void constructSt(int n) { int x = log2(n) + 1; int size = 2 * pow(2, x) - 1; seg.resize(size); } double update(int idx, double val, int st, int end, int pos) { double t1, t2; if (end < idx || st > idx) return seg[pos]; if (st == end && st == ...
12
#include <bits/stdc++.h> using namespace std; int n; struct dd { int gt; int x; int v; }; vector<dd> a; struct Data { long long sum; int sl; }; Data it[200009 * 8]; Data ans; long long res; void update(int i, int l, int r, int pos, int data) { if (l > pos || r < pos) return; if (l == r) { it[i].sum = ...
11
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; long long fastpower(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) return fastpower((x * x), n / 2); else return (x * fastpower((x * x), (n - 1) / 2)); } long long negativepow(long long base, long long expo, l...
5
#include <bits/stdc++.h> using namespace std; int main() { int x, n, m = 0, b = 0, v, arr[100000]; cin >> x; for (int i = 0; i < x; i++) { cin >> arr[i]; } for (int i = 0; i < x; i++) { if (i == x - 1) break; else if (arr[i] < arr[i + 1] - 15) { b++; m = arr[i]; n = arr[i +...
0
#include <bits/stdc++.h> int n, a, b, ans, i, ch[26][26], f = 0, flag = 0, f1 = 0, f2 = 0, f3 = 0, k, j; char s[200009], t[200009]; int main() { scanf("%d", &n); scanf("%s", s); scanf("%s", t); for (i = 0; i < n; i++) { if (s[i] != t[i]) { flag++; ch[s[i] - 'a'][t[i] - 'a'] = i + 1; } } ...
7
#include <bits/stdc++.h> using namespace std; const int MAX = 100000 + 10; const int MAXK = 18; int rec[MAX]; inline long long getans(long long x, int k) { return (x + 1) * x / 2 * (1LL << k); } struct stree { int L[MAX << 2], R[MAX << 2], num; void pull(int n, int l, int r) { if (l == r) return; int mid ...
13
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int b[n / 2]; long long int flag = 0; long long int maxi = 0; for (int i = 0; i < n / 2; i++) { cin >> b[i]; } vector<long long int> v1, v2; long long int k = 0; for (int i = 0; i < n / 2; i++) { ...
5
#include <bits/stdc++.h> using namespace std; long long maxi(long long a, long long b) { return a > b ? a : b; } long long dp[(1 << 18)][19]; long long a[20]; long long n, m, k; map<pair<int, int>, int> mp; long long solve(int mask, int prev) { if (__builtin_popcountll(mask) == m) return 0; if (dp[mask][prev] != -1...
10
#include <bits/stdc++.h> int MM[] = {469762049, 595591169, 645922817}; int V01, V02, V12; long long power(int a, int b, int md) { long long p; if (b == 0) return 1; p = power(a, b / 2, md); p = p * p % md; if (b % 2) p = p * a % md; return p; } long long ww[3][13 + 1], ww_[3][13 + 1], vv[3]; int kk[50 + 1][...
13
#include <bits/stdc++.h> using namespace std; int pi[111], mx, n; vector<string> v; vector<int> pt; string seq; char grid[1005][1005]; void getPi() { string t = ""; int i, j, k, l, m, vl = 0, i1; stack<char> st; for (i = 0; i < (int)n; i++) { t = t + seq[i]; if (seq[i] == '[') { vl++; pi[i] ...
6
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; const int MAX_SZ = 55; const int INF = 1e6; vector<int> failure_function(string &s) { int n = (int)s.size(); vector<int> ff(n, 0); for (int i = 1; i < n; i++) { int j = ff[i - 1]; while (j > 0 && s[i] != s[j]) j = ff[j - 1]; ff[i] = ...
13
#include <bits/stdc++.h> using namespace std; unsigned long long int powe(int a, int b) { if (b == 0) return 1; unsigned long long int s = 1LL << (b); return s; } int main() { unsigned long long int m, n; cin >> m >> n; unsigned long long int a[m][n]; for (unsigned long long int i = 0; i < m; i++) { f...
5
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } vector<pair<pair<double, double>, int> > sol; vector<pair<double, double> > v; int main() { long long n; double x1, x2; double k, b; cin >> n; cin >> x1 >> x2; for (int i...
8
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; set<int> s; for (int i = 2; i * i <= n; i++) { int num = n / i; s.insert(num); s.insert(n / num); } s.insert(0); s.insert(1); s.insert(n); cout << s.size...
6
#include <bits/stdc++.h> using namespace std; int flag = 0, flag1 = 1; int p, n, v, m, k, i, j, coun = 0, ctr; int a[26] = {0}; int main() { string s; cin >> s; n = s.length(); cout << (n + 1) * 26 - n << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long i, long long j) { if (j == 0) return i; else return gcd(j, i % j); } template <typename T> inline T getint() { T val = 0; char c; bool neg = false; while ((c = getchar()) && !(c >= '0' && c <= '9')) { neg |= c == '-'; } do...
20
#include <bits/stdc++.h> using namespace std; int main() { int x1, y1, z1; int x2, y2, z2; scanf("%d %d %d", &x1, &y1, &z1); scanf("%d %d %d", &x2, &y2, &z2); if ((!x2 == x1 && (!y2 == y1) && !z2 == z1)) printf("NO"); else printf("YES"); return 0; }
3
#include <bits/stdc++.h> using namespace std; int f[27]; int main() { int n, i, j; cin >> n; char c; int flag = 0; for (i = 0; i < n; i++) { cin >> c; f[c - 'a']++; } if (n > 26 || flag == 1) cout << -1 << endl; else { int ans = 0; for (i = 0; i <= 26; i++) { if (f[i] >= 2) ans...
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> a(n); for (auto &i : a) { cin >> i; } if (k == 1) { sort(a.begin(), a.end()); cout << a[0] << "\n"; } else { if (k == 2) { int mx = -INT_MAX, nmx; ...
4
#include <bits/stdc++.h> using namespace std; const int LIM = 1e5 + 5, MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long i, j, k, n; cin >> n >> k; vector<long long> m(n), c(k + 5); vector<long long> cnt(k + 5); set<int, greater<int> > s, s1; set<int, greater...
11
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int res = 0; while (n) { res += n % 2; n >>= 1; } cout << res; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T> using vec = vector<T>; template <typename T> using Prior = priority_queue<T>; template <typename T> using prior = priority_queue<T, vec<T>, greater<T>>; const int INF = 1e9; co...
9
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const int INF = 1000000007; vector<int> binaryRepresentation(int x) { vector<int> a(32); int i = 0; while (x > 0) { if (x % 2 == 0) a[i] = 0; else a[i] = 1; i++; x = x / 2; } return a; } long long fastP...
7
#include <bits/stdc++.h> using namespace std; void __solve() { int n, x; std::cin >> n; x = n; std::cout << n - 1 << std::endl; for (int i = 2; i <= n; i++) { std::cout << i << " "; } std::cout << endl; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); std::cin.tie(nullp...
0