solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; long long int N = 1e9 + 7; int Nmax = 200004; int n; vector<int> bit(Nmax), a(Nmax), b(Nmax), c(Nmax); vector<int> p, q; void update(int idx, int val) { while (idx <= n) { bit[idx] += val; idx += (idx & -idx); } } int query(int idx) { int ans = 0; while (idx...
12
#include <bits/stdc++.h> using namespace ::std; using ll = long long; using P = pair<ll, int>; const int maxn = 1e5 + 5; int n, m, c[maxn], w[maxn], vis[maxn]; ll ans; priority_queue<P, vector<P>, greater<P> > pq; void solve() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &c[i]); for (int i = 0...
16
#include <bits/stdc++.h> using namespace std; const int mn = 2e5 + 1; const long long INF = 1e18; int p[mn], sz[mn]; long long ans = 0; int find_set(int x) { return x == p[x] ? x : p[x] = find_set(p[x]); } void merge(int a, int b, long long c) { a = find_set(a); b = find_set(b); if (a ^ b) { if (sz[a] < sz[b]...
11
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int total = 0; for (int i = 0; i < n; i++) { string str; cin >> str; if (str == "Tetrahedron") total += 4; else if (str == "Cube") total += 6; else if (str == "Octahedron") total += 8; else if (...
0
#include <bits/stdc++.h> using namespace std; int A[500010]; int k, sol; int p; void upd(int i) { if (k) { if (k & 1) { for (int j = p; j < i; j++) A[j] = A[p - 1]; } else { int pol = (p + i) / 2; for (int j = p; j < pol; j++) A[j] = A[p - 1]; for (int j = pol; j < i; j++) A[j] = A[i];...
9
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long MAX = 2e5 + 10; const long long inf = 1e18 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n, m, cont = 0, sum = 0, mini = inf; cin >> n >> m...
2
#include <bits/stdc++.h> using namespace std; inline void readInt(int &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); x *= f; } inline void readLong(long long &x) { x = 0; in...
4
#include <cstdio> #include <cstring> using namespace std; const int N = 5e5 + 5; char s[3][N]; int cnt[3]; int n; bool flag[2][N]; void run(int a, int b, bool x) { // printf("%d %d %d\n", a, b, x); int c = 0; for (int i = 0; i < n + n; ++i) { if (c < n && s[a][i] - '0' == x) { flag[0][i] = 1; ++c; } ...
11
#include <bits/stdc++.h> using namespace std; char c; int a[1010][5], p, cnt, n, m; long long ans; int main() { scanf("%d%d", &n, &m); char aa = getchar(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%c", &c); a[j][c - 'A']++; } char aa = getchar(); } for (int ...
1
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int cnt[10]; for (int i = 0; i < 10; i++) cnt[i] = 0; for (char i : s) cnt[i - '0']++; int n = s.size(); if ((cnt[0] == n - 1 && cnt[1] == 1) or (cnt[0] == n - 2 && cnt...
14
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& cout, vector<T> V) { cout << "[ "; for (auto v : V) cout << v << ' '; return cout << ']'; } template <class L, class R> ostream& operator<<(ostream& cout, pair<L, R> P) { return cout << '(' << P.first << ',' << P.secon...
4
#include <bits/stdc++.h> using namespace std; int n, a[100005], pos[100005], nxt[100005]; int root[100005]; int lson[5000005], rson[5000005]; int w[5000005]; int num; int build(int l, int r) { ++num; w[num] = 0; int ret = num; if (l != r) { int mid = (l + r) / 2; lson[ret] = build(l, mid); rson[ret]...
16
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int a, b, i, cnt = 0; cin >> a >> b; for (i = 2; i * i <= (a + b); i++) { if ((a + b) % i == 0) cnt++; } if ((a - b) == 1) { if (cnt == 0) { cout << "YES" << endl; co...
3
#include <bits/stdc++.h> using namespace std; template <class T> inline void rd(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) x = x * 10 - '0' + c, c = getchar(); x *= f; } const int N = 4010; const int mod = 1e9 + 7; ...
25
#include <bits/stdc++.h> int N; int A[(100005)]; int B[(100005)]; int main() { int i; scanf("%d", &N); for (i = 0; i < N; ++i) scanf("%d", A + i); if (N == 1) { printf("+\n"); return 0; } else if (N == 2) { printf("-+\n"); return 0; } int s = A[N - 1] - A[N - 2]; B[N - 2] = 1; B[N - 1]...
11
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, temp = 0; cin >> n; int a[n + 5]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n - 1; j++) { if (a[i] == a[j + 1]) { ...
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (n % 2 == 0) cout << a[n / 2 - 1]; else cout << a[n / 2]; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int k = 0; for (int i = 1; i <= n; i++) { int t; scanf("%d", &t); t -= k; if (t < 0) return printf("NO"), 0; k = t % 2; } if (k != 0) printf("NO"); else printf("YES"); }
3
#include <bits/stdc++.h> using namespace std; long long mark[100010]; int main() { long long n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) cin >> mark[i]; long long special = 0, page = 0, z = 0, cnt = 0; while (special < m) { for (int i = special; i < m; i++) { if (mark[i] - k * page - sp...
6
#include <bits/stdc++.h> using namespace std; const int INF = (int)1E9; const long long LINF = (long long)1E18; const long double PI = acos(-1.0); const long double EPS = 1E-12; template <typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); } template <typename T> inline T lcm(T a, T b) { return ...
6
#include <bits/stdc++.h> using namespace std; int setBit(int n, int pos) { return n = n | (1 << pos); } int resetBit(int n, int pos) { return n = n & ~(1 << pos); } bool checkBit(long long n, long long pos) { return (bool)(n & (1LL << pos)); } const int MOD = 1000003; const int MAX = 1000010; int inv[MAX], fact[MAX]; l...
10
#include <bits/stdc++.h> using namespace std; const int N = 100005, M = 255, S = 26; int n, q, a, b, c; char ch[N], sa[M], sb[M], sc[M]; int nxt[N][S]; int f[M][M][M]; int main() { scanf("%d%d", &n, &q); scanf("%s", ch + 1); for (int i = 0; i < S; ++i) { nxt[n + 1][i] = n + 1; nxt[n + 2][i] = n + 1; } ...
14
#include <bits/stdc++.h> using namespace std; const int BufSize = 1 << 21; struct FastIO { char ibuf[BufSize + 10], *p, *e; char obuf[BufSize + 10], *q, stk[128]; FastIO() { q = obuf; } ~FastIO() { fwrite(obuf, 1, q - obuf, stdout); } int getChar() { if (p == e) p = ibuf, e = ibuf + fread(ibuf, 1, BufSize...
22
#include <bits/stdc++.h> using namespace std; template <class A, class B> A cvt(B x) { stringstream ss; ss << x; A y; ss >> y; return y; } int n, m; vector<int> gr[100000]; int c[100000], d[100000], v[100000], cycn; vector<int> cyc; int n2cyc[100000]; void findCycle() { queue<int> q; for (int i = 0; i < (...
21
#include <bits/stdc++.h> using namespace std; int const N = 5e5 + 10, oo = 1e9; int mod = oo + 7; long long const OO = 1e18; int n, m, k, w = 1, vis[N]; vector<int> adj[N], lv[N]; map<int, int> mp[N]; queue<int> q; string an; void BFS() { q.push(1); vis[1] = 1; int d = 2; while (!q.empty()) { int sz = q.siz...
13
#include <bits/stdc++.h> const double eps = 0.0000001; using namespace std; inline int sgn(double x) { return (x > eps) - (x < -eps); } inline char gc() { static char buf[1 << 16], *S, *T; if (S == T) { T = (S = buf) + fread(buf, 1, 1 << 16, stdin); if (S == T) return EOF; } return *S++; } inline int re...
8
#include <bits/stdc++.h> using namespace std; int get_cost(string s) { int len = s.length(); int ans = 0; for (int i = 0; i < len; i++) { string a = s.substr(0, i); string b = s.substr(i, len - i); string s1 = b + a; string s2 = s1; reverse(s2.begin(), s2.end()); if (s1 == s2) ans++; } ...
17
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> bool compare(T x, U y) { return (abs(x - y) <= 1e-9); } const int MOD = 1e9 + 7; void solve() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (x2 == x1 || y2 == ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; int res = 0; char ls; for (int i = 0; i < n; i++) { cin >> s; if (i == 0) { ls = s[1]; res++; continue; } if (ls == s[0]) { ls = s[1]; res++; continue; } } cout <...
0
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; long long n, m, s[5050], f[5050], h[5050], l[5050], r[5050]; vector<long long> v[5050]; pair<long long, long long> sl(long long x) { if (l[x] == n + 1) { return make_pair(0, 0); } long long mx, cnt = 1; if (x == 0) { mx = 0; }...
17
#include <bits/stdc++.h> using namespace std; vector<int> g[101010]; vector<int> sta; vector<int> cyc; int u[101010]; int cp[101010]; int dfs(int x, int e) { if (u[x]) { while (1) { cyc.push_back(sta.back()); sta.pop_back(); if (cyc.back() == x) break; } return 1; } else { u[x] = 1...
21
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; int cnt[maxn], vis[maxn]; int ans[maxn][2]; int main() { int n, x, y, ct = 0, num = 0; cin >> n; for (int i = 1; i < n; i++) { cin >> x >> y; if (x == n) cnt[y]++, ct++; else if (y == n) cnt[x]++, ct++; } for (int i =...
11
#include <bits/stdc++.h> using namespace std; void init() { freopen("a.in", "r", stdin); cout.precision(9); } const double EPS = 1e-9; const int INF = (int)1e9 + 41; const int N = (int)1e6 + 34; int rnk[N], parent[N], a[N], b[N]; void make_set(int v) { parent[v] = v; rnk[v] = 0; } int find_set(int v) { if (v ...
16
#include <bits/stdc++.h> using namespace std; int n, lcnt; long long m, p, q; int a[100005], lsum[100005]; long long sum[100005], rmax[100005], lmin[100005], ls[100005]; long long ans1, ans2, ans; set<int> unu; inline long long Min(long long x, long long y) { return x < y ? x : y; } inline long long Max(long long x, lo...
23
#include <bits/stdc++.h> using namespace std; vector<int> adj[109][109]; vector<int> w; bool mark[1000]; void dfs(int v, int c) { mark[v] = true; for (int i = 0; i < adj[c][v].size(); i++) { int u = adj[c][v][i]; if (!mark[u]) dfs(u, c); } } int main() { int n, m; int v, u, c; int i, j; cin >> n >...
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i; cin >> n >> m; for (i = 0; i < n; i++) { if (i % m == 0) n++; } cout << i - 1; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t) { int a, i; cin >> a; if (a == 1) cout << "-1" << endl; else { cout << "2"; for (i = 1; i < a; i++) { cout << "3"; } cout << "\n"; } t--; } }
2
#include <bits/stdc++.h> using namespace std; int n; int a[300010]; int odd1, odd2, even1, even2; int main() { cin >> n; odd1 = 0; odd2 = 0; even1 = 0; even2 = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if ((i + 1) % 2 == 0) { even2 += a[i]; } else { odd2 += a[i]; } } int ...
4
#include <bits/stdc++.h> const int maxn = 5005; const long long P = 998244353; using namespace std; int O[maxn][maxn], C[maxn][maxn], jc[maxn]; int main() { int n; cin >> n; for (int i = 0; i <= n; i++) O[i][0] = C[i][0] = 1; jc[0] = 1; for (int i = 1; i <= n; i++) jc[i] = 1LL * jc[i - 1] * i % P; for (int ...
23
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; int d = 0; cin >> n >> d; int m = 0; cin >> m; int x = 0; int y = 0; for (int i = 0; i < m; i++) { cin >> x >> y; if ((y <= x + d) && (y <= -x + 2 * n - d) && (y >= x - d) && (y >= -x + d)) cout << "YES" << endl; els...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long test, i, j, xy, flag = 0, n, u, count, d, o1 = 0, o2 = 0, s, e, l, r, x, y, m, z, max1, x1, y1, k, x2, y2, z1, sum, f, min...
0
#include <bits/stdc++.h> using namespace std; struct point { long double x, y; }; point centroid(int n, point pnt[]); long double dis(point a, point b); point new_centroid(point pivot, long double dist); long double ang(point final, point initial, point pivot); point print(point p, point cent, long double angle, long...
18
#include <bits/stdc++.h> using namespace std; const int N = 300; int a[4], n, av, c, mx, need; string str; char ch[5] = {'A', 'C', 'G', 'T'}; int main() { cin >> n >> str; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { if (str[i] == ch[j]) a[j]++; } if (str[i] == '?') av++; } for ...
1
#include <bits/stdc++.h> using namespace std; vector<int> adj[200000]; set<int> prob[200000]; int counter[200000]; bool vis[200000]; vector<int> res; bool white[200000]; int flood(int ind) { vis[ind] = 1; for (int i = 0; i < (int)adj[ind].size(); i++) { int j = adj[ind][i]; if (vis[j]) continue; int x =...
8
#include <bits/stdc++.h> long long int o[200005]; long long int e[200005]; long long int dp[200005][2]; long long int k; long long int dpf(long long int r, long long int dif) { if (dp[r][dif] > 0) { return dp[r][dif] - 1; } if (r == 0) { return dif; } long long int ans = 0; if (dif) { ans += dpf...
14
#include <bits/stdc++.h> int s[300005]; int n, m; bool check(int x) { int tt = 0; for (int i = 1; i <= n; i++) { if (s[i] + x < tt) return false; if (s[i] > tt && (s[i] + x < m || s[i] + x - m < tt)) tt = s[i]; } return true; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { s...
9
#include <bits/stdc++.h> using namespace std; const int maxn = 300003; long long n, a, b, c, d, st, len; long long t[maxn], q[maxn]; int main() { long long rat = 0ll, pos = 1, sum = 0, m1 = 0; scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &a, &b, &c, &d, &st, &len); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &t[i...
16
#include <bits/stdc++.h> using namespace std; long long f[10005]; char s[100005]; int main() { long long maxx, n, i, ans = 1, tmp = 0; scanf("%I64d%s", &n, s); for (i = 0; i < n; i++) f[s[i]]++; maxx = max(max(f['A'], f['G']), max(f['T'], f['C'])); tmp = 0; if (f['A'] == maxx) tmp++; if (f['C'] == maxx) t...
7
#include <bits/stdc++.h> using namespace std; int grid[55][55]; int R, C; int state[55][55]; map<pair<int, int>, pair<int, int> > parent; string ans; int dr[4] = {1, 0, -1, 0}; int dc[4] = {0, 1, 0, -1}; void dfs(int r, int c) { if (ans == "Yes") return; state[r][c] = 1; for (int d = 0; d < 4; d++) { int nr =...
7
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> ostream& operator<<(ostream& output, const pair<T1, T2>& p) { output << "(" << p.first << "," << p.second << ")"; return output; } template <typename T> ostream& operator<<(ostream& output, const vector<T>& v) { unsigned int i; bo...
23
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAXN = 1e5 + 7; long long n, a[MAXN], even = -1; long long qpow(long long a, long long b) { long long ret = 1, base = a; while (b) { if (b & 1) { ret *= base; ret %= MOD; } b >>= 1; base *= base; base %=...
12
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long i; vector<long long> a(n + 10), vp, v; map<long long, long long> mp; for (i = 1; i <= n; i++) { cin >> a[i]; mp[a[i]] = 1; } for (i = 1; i <= n; i++) { if (mp[i] == 0 and a[i] == 0) vp.push_back(i); ...
7
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long dp[101][100001]; long long comb[101][101]; void init() { comb[0][0] = 1; for (int i = 1; i <= 100; i++) { for (int j = 0; j <= i; j++) { if (j - 1 >= 0) comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MO...
11
#include <bits/stdc++.h> using namespace std; int main() { long long int n, q; scanf("%lld%lld", &n, &q); long long int a[n], b[n]; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); b[i] = a[i]; } int ind = 0; vector<long long int> list; for (int i = 0; i < n; i++) { if (a[i] > a[ind]) { ...
7
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 10; long long a[MAX_N], n, m, t; vector<int> vec; long long get(long long d) { d = vec[d]; long long cnt = 0, mm = 0; long long T = 0, tmpT = 0; for (int i = 0; i < n; i++) { if (a[i] > d) { continue; } tmpT += a[i]; T +...
13
#include <bits/stdc++.h> using namespace std; char a[150000]; unsigned long long k; int b[150000]; unsigned long long mod = 1000000007; unsigned long long getA(unsigned long long b, unsigned long long base) { unsigned long long ans = 1; while (b > 0) { if (b % 2) ans *= base; b /= 2; base = base * base;...
9
#include <bits/stdc++.h> using namespace std; std::vector<int> luckynums; void bt(int num, int idx, int n) { if (idx == n) { luckynums.push_back(num); return; } bt(num * 10 + 4, idx + 1, n); bt(num * 10 + 7, idx + 1, n); return; } int upperbnd(int l, int r, int val) { int low = l, high = r, ans = r ...
11
#include <bits/stdc++.h> int bucket[256]; int n; int k; void init() { for (int i = 0; i < 256; i++) { bucket[i] = -1; } scanf("%d%d", &n, &k); bucket[0] = 0; } void process(int kk) { if (bucket[kk] != -1) { printf("%d ", bucket[kk]); return; } int found_prev = -1; int found_prev_idx = -1; ...
9
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("tune=native") using namespace std; const int MAX = 5e5 + 5; const long long MOD = 1000000007; const long long MOD2 = 2010405347; const long long INF = 9e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -...
21
#include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '{'; for (int i = (0), __i = (((int)(vec).size())); i < __i; i++) { os << vec[i]; if (i + 1 != ((int)(vec).size())) os << ','; } os << '}'; return os; } template <class T1, c...
18
#include <bits/stdc++.h> using namespace std; const int max_n = 40; int mod; int t, x, dp[max_n], cnt[max_n]; vector<int> v; int mul(int x, int y) { return 1LL * x * y % mod; } void get_v(int x) { v.clear(); while (x) { v.push_back(x % 2); x /= 2; } } int main() { cin >> t; while (t--) { cin >> x ...
9
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { return os << "(" << p.first << ", " << p.se...
16
#include <bits/stdc++.h> using namespace std; long long isprime[1234567]; long long minprime[1234567]; long long cost[1234567]; vector<long long> pf[1234567]; void prime() { long long i, j; isprime[0] = 0; isprime[1] = 0; for (long long i = 2; i <= 1e6; i++) isprime[i] = 1; for (i = 2; i <= 1e6; i++) { if...
9
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } template <class T> T gcd(T a, T b) { return a ? gcd(b % a, a) : b; } template <class T> T sqr(T a) { return a * a; } template <class T> T sgn(T a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); } int n; int m; long lo...
15
#include <bits/stdc++.h> using namespace std; multiset<int> ms[35]; long long sum[35]; char c; int q, x, cnt = 0; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> q; for (int i = 1; i <= q; i++) { cin >> c >> x; int p = 0; if (c == '+') { ++cnt; for (int y = x; y > 0; y >>= 1...
20
#include <bits/stdc++.h> const int maxN = 2e5 + 2; int h, sum; bool flag; int a[maxN]; int main() { scanf("%d %d", &h, a); for (register int i = 1; i <= h; ++i) { scanf("%d", a + i); if (!flag) { if (a[i - 1] == 1 or a[i] == 1) continue; flag = true; } } if (!flag) { puts("perfect");...
7
#include <bits/stdc++.h> using namespace std; struct EDGE { int to, next; } e[300010]; int n, head[150010], etop; long long a[150010]; void adde(int u, int v) { e[etop].to = v; e[etop].next = head[u]; head[u] = etop++; } long long ans; int vis[150010]; int rt, sz[150010], sum; void dfs(int x, int fa = 0) { sz...
19
#include <bits/stdc++.h> using namespace std; int pr[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 19...
7
#include <bits/stdc++.h> const int N = 1e6 + 7; const int INF = 1e9 + 111; const double EPS = 1e-8; using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; v[i]--; } bool f = 0; for (int i = 0; i < n; i++) { if (i == v[v[v[i]]] && v[v[i]]...
0
#include <bits/stdc++.h> using namespace std; bool comp(pair<char, int> a, pair<char, int> b) { return (a.second > b.second); } vector<int> factors(int n) { vector<int> v; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) v.push_back(i); else { v.push_back(i); ...
0
#include <bits/stdc++.h> using namespace std; bool B, bb[109]; vector<pair<int, int> > a; vector<int> b[109]; void DFS(int x, int y) { if (x == y) B = true; for (int i = 0; i < b[x].size(); i++) { if (!bb[b[x][i]]) { bb[b[x][i]] = true; DFS(b[x][i], y); } } } int main() { int i, j, n, p, x, ...
7
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int M = 2010; const int K = 670000; const int LIT = 2500; const int INF = 1 << 29; const int base = 137; const double eps = 1e-10; const int dir[5][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; const int dir2[10][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -...
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int pos; int flag = 0; for (int i = 1; i < n; i++) { if (s[i] != s[i - 1]) { flag = 1; pos = i; break; } } if (flag == 1) { cout << "YES" << endl; cout << s[pos - 1] << s[p...
2
#include <bits/stdc++.h> using namespace std; struct FastIO { inline FastIO& operator>>(int& x) { x = 0; char f = 0, ch = getchar(); while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar(); return x = (f ? -x : x), *this; ...
15
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 + 10; const int MAXM = 100 + 10; const int INF = 0x3f3f3f3f; void File(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } inline long long fread(void) { long long ret = 0, op = 1; char ch = getcha...
2
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:666000000") using namespace std; const int INF = (1 << 30) - 1; const long double EPS = 1e-9; void ML(const bool v) { if (v) return; int *ass; for (;;) { ass = new int[2500000]; for (int i = 0; i < 2500000; i++) ass[i...
12
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; string b; b = s; string t; int sz = s.size(); int ans = 0; while (s != t) { t = ""; t += b[sz - 1]; for (int i = 0; i < sz - 1; i++) { t += b[i]; } ans++; b = t; } cout << ans << endl; retu...
1
#include <bits/stdc++.h> using namespace std; int a[1000001]; bool used[1000001]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } int ans = -1; for (int i = n; i >= 1; --i) { if (!used[a[i]]) { ans = a[i]; used[a[i]] = true; } } cout << ans; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; ; long long int ans = 0; long long int flag1 = 0, c = 0; for (int i = 1; i < n - 1; i++) { ...
2
#include <bits/stdc++.h> using namespace std; const int max_num = 1e3 + 10; bool adj_mat[max_num][max_num]; bool mark[max_num][max_num]; int n; void dfs_visit(int x, int y) { for (int i = 1; i < max_num; i++) if (adj_mat[i][y] && !mark[i][y]) { mark[i][y] = true; dfs_visit(i, y); } for (int i = ...
4
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; const long double pi = 2 * acos(0.0); const int mod = 1e9 + 7; const int maxn = 2e5 + 5; const int e = 1e5 + 5; void solve() { ios::sync_with_stdio(false); cin.tie(0); unordered_map<char, int> map; string s; cin >>...
0
#include <bits/stdc++.h> using namespace std; int n, p[100001], ans[100001], isok = true, a, b, tot; vector<int> g[100001]; bool vis[100001], can[2]; map<int, int> pos; void dfs(int x) { vis[x] = true; if (!can[0]) ans[x] = 0; else ans[x] = 1; for (int i = 0; i < g[x].size(); i++) if (!vis[g[x][i]])...
12
#include <bits/stdc++.h> using namespace std; using namespace std; const int MedN = 100, MaxN = MedN << 1, MaxL = 120; const int NA = -1, MaxC = 0x3F3F3F3F; double a[MaxN + 1]; double f[2][MaxN + 1]; int n; int main(void) { int b, i, j, k, v; while (scanf(" %d", &n) != EOF) { for (i = 0; i <= n; i++) scanf(" %l...
16
#include <bits/stdc++.h> using namespace std; int n; int idxs[500500]; long long vals[500500]; long long l[500500], r[500500]; bool cmp_idx(int a, int b) { return vals[a] < vals[b]; } void rem(int idx) { l[r[idx]] = l[idx]; r[l[idx]] = r[idx]; } int main() { while (cin >> n) { for (int i = 1; i <= n; ++i) { ...
17
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, ans, tot, Q; struct node { int c, d; } a[maxn]; inline bool operator<(node a, node b) { return a.d < b.d; } priority_queue<node> q; int main() { scanf("%d", &Q); while (Q--) { scanf("%d", &n); int x, f; ans = tot = 0; ...
6
#include <bits/stdc++.h> using namespace std; const int N = 105; int n; int zero[N], one[N]; string str; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> str; zero[0] = 0, one[0] = 0; str = " " + str; for (int i = 1; i < str.size(); i++) { if (str[i] == '1') { zero[...
0
#include <bits/stdc++.h> using namespace std; int n; bool check(int m) { long long v = (m - 1) / 2; if ((v + 1) * (m - v + 1) >= n || (v + 2) * (m - v) >= n) return true; else return false; } void xuly() { int t; t = 1; while (t--) { cin >> n; int p = ceil(sqrt(n)), q = floor(sqrt(n)); i...
3
#include <bits/stdc++.h> using namespace std; int main() { char str[3] = {'H', 'Q', '9'}, sen[1000]; int a = 1; cin >> sen; for (int i = 0; i < strlen(sen); i++) { for (int j = 0; j < 3; j++) { if (sen[i] == str[j]) { cout << "YES\n"; j = 4; i = strlen(sen); a = 0; ...
1
#include <bits/stdc++.h> using namespace std; const int Maxv = 100100; const int Inf = 1000000000; int m; char str[Maxv][10]; bool flag; int l; int ls[Maxv]; int rs[Maxv]; int len; bool vis[Maxv]; void dfs(int x) { if (x >= len) { flag = false; return; } vis[x] = true; l++; if (str[x][0] == 'p') { ...
7
#include <bits/stdc++.h> using namespace std; int main() { int t, candies[4], inter, i; cin >> t; while (t--) { cin >> candies[0] >> candies[1] >> candies[2]; sort(candies, candies + 3); inter = -candies[2] + candies[1] + candies[0]; if (inter < 0) cout << +candies[1] + candies[0]; else ...
3
#include <bits/stdc++.h> using namespace std; int n, m, q; vector<long long> adj[1 << 20]; vector<long long> g[1 << 20]; multiset<long long> st_[1 << 20]; vector<long long> X_[1 << 20]; vector<long long> XS[1 << 20]; long long p[1 << 20]; vector<long long> st[1 << 20]; map<pair<long long, long long>, double> cache; boo...
17
#include <bits/stdc++.h> using namespace std; namespace zyt { const int N = 5e4 + 10; const double EPS = 1e-7, PI = acos(-1); bool equal(const double a, const double b) { return fabs(a - b) < EPS; } struct point { double x, y, theta; point(const double _x = 0, const double _y = 0) : x(_x), y(_y) { cal(); } void c...
22
#include <bits/stdc++.h> using namespace std; int n, a, b, t, dp[1000000 + 10], dp1[1000000 + 10], dp2[1000000 + 10], l, r, c, tren, br, maxx; char c1; int main() { scanf("%d %d %d %d\n", &n, &a, &b, &t); for (int i = 1; i <= n; i++) { scanf("%c", &c1); if (c1 == 'w') dp[i] = 1 + a + b; else ...
11
#include <bits/stdc++.h> using namespace std; int n, m, a[111], x[111], saven; long double f[2][200111], sum[2][200111]; int main() { ios ::sync_with_stdio(0); cin.tie(0); cout << (fixed) << setprecision(12); while (cin >> n >> m) { int sumA = 0; for (int i = (1), _b = (n); i <= _b; ++i) { int x; ...
15
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { string str; cin >> str; if (str.length() <= 10) cout << str << endl; else cout << str[0] << str.length() - 2 << str[str.length() - 1] << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 10; const long long INF = 1LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 10; int arr[600]; vector<int> barg, tool; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, one = 0; cin >> n; long long sum = 0...
10
#include <bits/stdc++.h> using namespace std; using ll = long long; template <long long mod> struct modular { long long value; modular(long long x = 0) { value = x % mod; if (value < 0) value += mod; } modular& operator+=(modular other) { if ((value += other.value) >= mod) value -= mod; return *...
18
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; char address[200005]; scanf("%s", address); vector<int> indexes[26]; for (int i = (0); i < (K); i++) { indexes[address[i] - 'a'].push_back(i); } while (N--) { long long result = 0; char potential[200005]; ...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 500010; int n, ans, fa[2][maxn], par[maxn]; vector<int> G[2][maxn]; int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } int main() { scanf("%d", &n); for (int c : {0, 1}) { for (int _ = 0, u, v; _ < n - 1; _++) { scanf("%d %d",...
24
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t, n; cin >> t; while (t--) { cin >> n; long long int a[n]; for (auto i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n, greater<long long i...
0