solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<ll> h(n); for (ll &i : h) cin >> i; vector<ll> diff(n + 1); diff[0] = h[0]; for (int i =...
16
#include <bits/stdc++.h> using namespace std; int main() { int n, c, m; string s; cin >> s; n = s.length(); int i = 0; for (int k = 0; k < n; k++) if (isupper(s[k])) i++; if (i == n) { for (int k = 0; k < n; k++) s[k] = tolower(s[k]); cout << s; return 0; } if ((i == n - 1) && (islower...
2
#include <bits/stdc++.h> using namespace std; int main() { string a; map<char, int> m; m['Q'] = m['q'] = 9; m['R'] = m['r'] = 5; m['B'] = m['b'] = 3; m['n'] = m['N'] = 3; m['P'] = m['p'] = 1; int b = 0, w = 0; for (int i = 0; i < 8; i++) { cin >> a; for (int i = 0; i < 8; i++) { if (isup...
1
#include <bits/stdc++.h> using namespace std; const int Maxn = 200005; void ansv_left(vector<int>& v, vector<int>& res) { stack<pair<int, int> > stk; stk.push(make_pair(INT_MIN, v.size())); for (int i = v.size() - 1; i >= 0; i--) { while (stk.top().first >= v[i]) { res[stk.top().second] = i; stk.p...
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int nxt[MAXN], pre[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < int(n); i++) cin >> a[i]; for (int i = 0; i < int(n); i++) cin >> b[i]; a.erase(find(...
5
#include <bits/stdc++.h> using namespace std; int main() { int tc; cin >> tc; while (tc--) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; long long int moves = 1, sum1 = arr[0], sum2 = 0; long long int pre1 = arr[0], pre2 = 0; int i1 = 1, i2 = n - 1; int ...
5
#include <bits/stdc++.h> using namespace std; struct S { long long prime; int q, h, ji; }; S xxx[300000]; priority_queue<S> sum[4]; bool biao[300000]; bool operator<(const S &a, const S &b) { return a.prime > b.prime; } int main() { int x, y; scanf("%d", &x); for (int a = 0; a < x; a++) { scanf("%I64d", &...
6
#include <bits/stdc++.h> using namespace std; char arr[9][9]; int main() { bool f = false; for (int i = 1; i <= 8; i++) { for (int j = 1; j <= 8; j++) { cin >> arr[i][j]; if (arr[i][j] == 'W') f = true; } } if (f == false) { cout << 8 << endl; return 0; } int cnt = 0; for (int ...
3
#include <bits/stdc++.h> using namespace std; long long n; int main(int argc, char **argv) { cin >> n; long long t = n; n %= 360; int mn = 400, dif = 400; for (int i = 0; i <= 3; ++i) { int x = min(abs((n - i * 90) % 360), ((n - i * 90) % 360 + 360) % 360); x = min(x, 360 - x); if (x < dif) { ...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, d; long long x[100010]; while (cin >> n >> d) { for (int i = (int)0; i < (int)n; i++) cin >> x[i]; long long res = 0; for (int i = (int)0; i < (int)n - 2; i++) { int inf = i + 1, sup = n - 1, med, pos = -1; while (inf <= sup...
5
#include <bits/stdc++.h> using namespace std; vector<int> a[100100]; int x[100100]; bool vis[100100]; int b[100100], pre[100100]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) scanf("%d", x + i); int tot = 0; for (int i = 1; i <= n; ++i) if (!vis[i]) { int u = i, cnt = 0; while (!...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); map<int, vector<int>> myMap; int n; cin >> n; for (int i = 0; i < n; i++) { int num; cin >> num; myMap[num].push_back(i + 1); } map<int, int> ans; for (auto &pair :...
5
#include <bits/stdc++.h> using namespace std; const long long linf = 1e18 + 5; const int mod = (int)1e9 + 7; const int inf = 1e9; long long read() { bool minus = false; long long result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getch...
1
#include <bits/stdc++.h> using namespace std; #define fo(a,b) for(int a=0;a<b;++a) #define pb push_back #define printList(v) for(auto _x:(v)) cout<<_x<<" ";cout<<endl; ////////////// DEBUG UTILS ////////////////// #define debug(...) __print(#__VA_ARGS__, __VA_ARGS__) template<class K,class V> ostream& operator<<(ost...
17
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n; cin >> n; int ans = 0; for (int i = 0; 1; ++i) { if (n < (i + 1) * (i + 1)) { if (n <= i * (i + 1)) { int left = n - i * i; ans = 4 * i; if (left) ans += 2; } else { i...
2
#include <bits/stdc++.h> using namespace std; const long long maxn = 4294967295; stack<long long> q; int main() { int l; long long ans = 0, cnt = 1, flag = 0, tot = 0; scanf("%I64d", &l); getchar(); while (l--) { char s[30]; long long x; scanf("%s", s); if (s[0] == 'a') { ans += cnt; ...
8
#include <bits/stdc++.h> using namespace std; int INP, AM, REACHEOF; const int BUFSIZE = (1 << 12) + 17; char BUF[BUFSIZE + 1], *inp = BUF; int ok[1011], saveCap[1011][1011], qu[1011], cap[1011][1011]; int answer[1011][1011], parent[1011], n; void bfs(int start) { memset(ok, 0, sizeof ok); int first = 1, last = 1; ...
21
#include <bits/stdc++.h> using namespace std; int vst[100010], flag[100010]; int p[100010]; vector<int> vt; int main() { int n; scanf("%d", &n); vst[1] = 1; for (int i = 2; i < 100010; i++) { if (vst[i]) continue; for (int j = 2 * i; j < 100010; j += i) vst[j] = 1; } int k = 0; for (int i = 1; i <...
17
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; const int mod = 1e9 + 7; int num[maxn]; int main() { int t, n, m, ans; scanf("%d", &t); while (t--) { ans = 1000000; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &num[i]); sort(num, num + n); for (int i = 1; i ...
0
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e6 + 1; const long long INF = 1e9 + 1; const long long BITS = 64; const long double EPS = 1e-9; void bye() { cout << '\n'; exit(0); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vecto...
2
#pragma GCC optimize(3) #include <cstdio> #include <queue> #include <string> #include <cstring> #include <algorithm> #include <cstdlib> #include <iostream> #include <iomanip> #include <cmath> #include <vector> #include <set> #include <map> #include <fstream> #include <cassert> #include <complex> #include <ctime> //#inc...
17
#include <bits/stdc++.h> using namespace std; const int maxn = 2000005; int tree[maxn]; int a[200005]; int n, e; int t, x, p; void buildTree(int l, int r, int pos) { if (l == r) { tree[pos] = a[l]; return; } int mid = (l + r) / 2; tree[pos] = -1; buildTree(l, mid, pos * 2); buildTree(mid + 1, r, pos...
8
#include <bits/stdc++.h> using namespace std; int main() { int i, n, a[100001], cnt = 0; int flag = 0; cin >> n; for (i = 0; i < n; ++i) { cin >> a[i]; if (a[i] == i) ++cnt; } for (i = 0; i < n; ++i) { if (a[i] != i) { flag = 1; if (a[a[i]] == i) { flag = 2; break; ...
3
#include <bits/stdc++.h> using namespace std; class A { public: int x, y, area = 0; }; bool cmp(A& a, A& b) { return a.area < b.area; } A a[3000]; int m[60][60], c, ans, v[60][60]; void dfs1(int x, int y, int n1 = 2) { m[x][y] = n1; if (m[x + 1][y] == 1) dfs1(x + 1, y, n1); if (m[x - 1][y] == 1) dfs1(x - 1, y,...
8
#include <bits/stdc++.h> using namespace std; int main() { string input; cin >> input; string result; int i = 0; while (i < input.length()) { string test(1, input[i]); if (i <= input.length() - 3) { test += input[i + 1]; test += input[i + 2]; if (test == "WUB") { string last(...
1
#include <bits/stdc++.h> using namespace std; int main() { int n = 0, k = 0; cin >> n >> k; int a[2 * n + 1]; for (int i = 0; i < 2 * n + 1; i++) { cin >> a[i]; } for (int j = 0; j < 2 * n + 1; j++) { if ((j + 1) % 2 == 0 && k != 0 && a[j - 1] < a[j] - 1 && a[j] - 1 > a[j + 1]) { k--; ...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); vector<pair<string, int> > operations; priority_queue<int> Heap; int n, num; cin >> n; string operation[n]; int Arr[n]; for (int i = 0; i < n; ++i) { cin >> operation[i]; if (operation[i][0...
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k, i, j, x, mx_dif, ans = 0; vector<long long int> vv; cin >> n >> k; mx_dif = k; for (i = 0; i < n; i++) { cin >> x; vv.push_back(x); } sort(vv.begin(), vv.end()); for (auto it : vv) { long long int temp = it, cnt =...
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int m; cin >> m; while (m--) { int x, y; cin >> x >> y; x--; if (x == 0) { int left = arr[x] - y; arr[x] = 0; arr[x + 1] += left; ...
0
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const int mod = 1e9 + 7; const int Lim = 3e3; const double eps = 1e-15; int arr[5001][5001], total[5002][5002]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; char x; memset(arr, 0, size...
6
#include <bits/stdc++.h> using namespace std; int n, s, a[110], b[110]; int main() { cin >> n >> s; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; b[i] += a[i] * 60; } if (b[1] > s) cout << "0 0" << endl; else { bool cnt = 0; for (int i = 2; i <= n; i++) { if (2 * s + 1 < b[i] -...
3
#include <bits/stdc++.h> using namespace std; int n; int a[10003]; pair<int, int> sa[10003]; vector<int> gnext[5003]; vector<int> gcap[5003]; int ans[5003]; int par[5003]; int cap[5003]; vector<int> train[5003]; int v[10003]; int mypos[10003]; void dfs(int nod, int parent = -1, int curcap = 0) { v[nod] = 1; par[nod...
12
#include <bits/stdc++.h> using namespace std; const double Pi = 3.141592653589793, eps = 1e-9; double ang(double x1, double y1) { double r = atan2(y1, x1) * 360.0 / (2 * Pi); while (r < -eps) r += 360.0; return r; } vector<double> a; bool cmp(double e, double f) { return (e - f < -eps); } double max_d(double e, d...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; int n; void update(long long *tree, int pos, long long add) { while (pos <= n) tree[pos] += add, pos += pos & (-pos); } long long query(long long *tree, int pos) { long long ans = 0; while (pos) ans += tree[pos], pos -= pos & (-pos); return...
17
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int m1 = (3 * a) / 10; int m2 = a - (a / 250) * c; int v1 = (3 * b) / 10; int v2 = b - (b / 250) * d; int misha = max(m1, m2); int vasya = max(v1, v2); if (misha == vasya) { cout << "Tie" <...
1
#include <bits/stdc++.h> using namespace std; int N; int A[104]; int B[104]; int DP[104][1 << 3]; int P[104][1 << 3]; int go(int idx, int mask) { if (idx >= N) return 0; if (DP[idx][mask] >= 0) return DP[idx][mask]; int res = go(idx + 1, mask); if ((B[idx] & mask) == 0) { int cur = 1 + go(idx + 1, mask | B[...
8
#include <bits/stdc++.h> using namespace std; char ch[long(10e5) + 5]; bool maxi[long(10e5) + 5]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cerr.tie(0), cout.tie(0); cin >> ch; long n = strlen(ch), i; char maxx; maxx = ch[n - 1]; for (i = n - 1; i >= 0; i--) { if (ch[i] >= maxx) { ...
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { long long int n; cin>>n; vector<long long int> wts; for(int i=0;i<n;i++) { long long int item; cin>>item; wts.push_back(item); } sort(wts.begin(),wts.end()); lon...
6
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; unsigned int a[MAX_N + 3], b[MAX_N + 3]; unsigned int ans; int n, k; inline bool check() { int i; for (i = 0; i < n; ++i) b[i] = a[i] & ans; sort(b, b + n); int t = 1; for (i = 1; i < n; ++i) t += b[i] != b[i - 1]; return t == k; } int ...
8
#include <bits/stdc++.h> const int MX = 10010896; const int lmt = 3164; const int N = 10000001; using namespace std; long long int extgcd(long long int a, long long int b, long long int& x, long long int& y) { if (b == 0) { x = 1; y = 0; return a; } else { int g = extgcd(b, a % ...
8
#include <bits/stdc++.h> const int maxn = 1e5 + 10; using namespace std; int n; long long rec[maxn << 1]; int main() { scanf("%d", &n); int cnt = 0; long long ans = 0; for (int i = 1; i <= n; i++) { long long a1, b1, a2, b2; scanf("%lld%lld%lld%lld", &a1, &b1, &a2, &b2); if (a1 + b1 >= a2 + b2) { ...
21
#include <bits/stdc++.h> using namespace std; long long a[3] = {2, 7, 15}; long long ff(long long n) { long long l = 1, r = n; while (l < r) { long long mid = (l + r + 1) >> 1; if ((2 * n - mid) / mid >= 3 * mid) l = mid; else r = mid - 1; } return l; } int main() { long long n, ans; ...
9
#include <bits/stdc++.h> using namespace std; int main() { char mas[120] = {0}; map<string, set<string> > f1, f2; while (gets(mas)) { int l = strlen(mas); string s = mas; l = s.find("\\", 0); while (true) { l = s.find("\\", l + 1); if (l == -1) break; int ni = l; while (tru...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 222; map<int, multiset<int> > G; inline void add(int s, int d) { G[s].insert(d); swap(s, d); G[s].insert(d); } int a[maxn]; int b[maxn]; vector<int> ans; void dfs(int s) { while (!G[s].empty()) { int d = (*G[s].begin()); G[s].erase(G[s...
16
#include <bits/stdc++.h> using namespace std; template <typename T> inline void smin(T &a, T b) { a = a < b ? a : b; } template <typename T> inline void smax(T &a, T b) { a = a > b ? a : b; } void err(istream_iterator<string> it) { cout << '\n'; } template <typename T, typename... Args> void err(istream_iterator<st...
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long x, y, m; cin >> x >> y >> m; if (x > y) swap(x, y); if (y >= m) { cout << 0 << '\n'; return 0; } if (y <= 0) { cout << -1 << '\n'; return 0; }; long long ans = 0; if (x < 0) { a...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n, m; long long da[maxn]; long long h[maxn], l[maxn]; int main() { int n, m; scanf("%d%d", &n, &m); if (n < m) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { scanf("%d", &da[i * m + j]); } } else { ...
12
#include <bits/stdc++.h> using namespace std; int n, m, a[100], b[100], s[4000], cnt; bitset<130> c[40005]; void work() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] += 10000; for (int i = 1; i <= m; i++) scanf("%d", &b[i]), b[i] += 10000; for (int i = 1; i <= n; i++) for (i...
13
#include <bits/stdc++.h> using namespace std; std::mt19937 rng( (int)std::chrono::steady_clock::now().time_since_epoch().count()); uint64_t v[64]; int k = 0; void add(uint64_t x) { int p = 0; v[k++] = x; for (int i = 63; (i >= 0) and (p < k); i--) { int e = p; for (e = p; e < k; e++) if (v[e] & ...
15
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long lgput(long long a, long long b, long long mod) { long long ret = 1; while (b) { if (b & 1) ret = (ret * a) % mod; a = (a * a) % mod; b >>= 1; } return ret; } int binarySearch(vector<int> &v, int value) { int bes...
15
#include <bits/stdc++.h> using namespace std; long long int min(long long int a, long long int b) { if (a <= b) { return a; } else { return b; } } int main() { long long int n; cin >> n; vector<vector<long long int>> v(n + 1); for (long long int i = 0; i < (2 * n); i++) { long long int x; ...
4
#include <bits/stdc++.h> using namespace std; int N, s, t; int deg[1000], pre[1000]; double d[1000], pi[1000]; int Adj[1000][1000], cap[1000][1000], fnt[1000][1000]; double cst[1000][1000]; int source(int k) { return k * 2; } int sink(int k) { return k * 2 + 1; } bool dijkstra(int n) { int i, u, v; double k; for ...
16
#include <bits/stdc++.h> using namespace std; #define ll long long int t,n; int temp; long long sum; ll a[200005]; int main(){ //solve(); int t; cin>>t; while(t--) { ll Max,sum; int n; cin>>n; Max = 0; sum = 0; for(int i=0;i<n;i++) { cin>>a[i]; sum+=a[i]...
6
#include <bits/stdc++.h> using namespace std; const int N = 26; const int M = 1600000; const int inf = 0x3f3f3f3f; int n, bord, p, cnta, cntb, ans = -inf, mapa, mapb, id, l[N], m[N], w[N]; struct node { int l, m, w, map; } tmp, a[M], b[M]; int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c >...
15
#include <bits/stdc++.h> using namespace std; int main() { long int n, k; cin >> n >> k; long int a[n + 1]; for (long int i = 1; i <= n; i++) { cin >> a[i]; } if (k == 1) { long int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + a[i]; } cout << sum << endl; } else if (k >= n...
6
#include <bits/stdc++.h> using namespace std; const int N = 200 * 1000 + 555; int n, k, a[N]; int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); a[n++] = int(2e9); int ans = 0, u = 0; for (int i = 0; i < n - 1; i++) { while (u < n && a[i] == a[u]) u++; if ...
4
#include <bits/stdc++.h> int max(int a, int b) { if (a < b) return a; return b; } int main() { int n, k, a, b, i, sm = 0, j, blck; scanf("%d%d%d\n", &n, &a, &b); char s[1000]; for (i = 0; i < n; i++) { scanf(" %c", &s[i]); } int min = max(a, b), l = n, cnt = 0; for (i = 0; i < n / 2; i++) { if...
2
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { b[i] = a[i]; } sort(a.begin(), a.end(), greater<long long>()); long long m; cin >> m; vector<long long>...
8
#include <bits/stdc++.h> using namespace std; long long int turned[1007]; long long int n, k; vector<long long int> vs, ansv; long long int recur(long long int num, long long int x) { if (num > n) return INT_MAX; vs.push_back(num); if (num >= n) return 1; return 1 + recur(num + 2 * k + 1, x + 1); } signed main(...
5
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; bool mat[55][55]; int n; int dx[] = {0, -1, -2, 0, 1, 2}; int dy[] = {-2, -1, 0, 2, 1, 0}; void location(int &x, int &y, int xt, int yt, bool t) { mat[xt][yt] = t; x = xt; y = yt; } bool querry(int x1, int y1, int x2, int y2) { if (x1...
16
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; const ll mod = 998244353; template <typename T> class bst { private: bst *l, *r, *p; bool is_root; int8_t _depth; int _size, freq; T data; int get_depth(bst* node) { return (node ? node->_dept...
9
#include <bits/stdc++.h> using std::min; using std::sort; const int maxn = 500000 + 10; int stack[maxn], p = -1; int main() { int n, x; scanf("%d", &n); long long ans = 0; for (int i = 0; i < n; i++) { scanf("%d", &x); while (p >= 1 && stack[p - 1] >= stack[p] && stack[p] < x) { ans += min(stack[p...
17
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int n, K, d[190], dis[190][190], m[190][190], dp[190][190], ans[190], path[190]; void dfs(int np, int fa) { for (int i = 1; i <= n; i++) dp[np][i] = d[dis[np][i]] + K; for (int i = 1; i <= n; i++) { if (m[np][i] && i != fa) { dfs(i,...
19
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; signed main() { long long n, k; cin >> n >> k; long long F = 0; while ((n >= 0 && k > 0) || (n > 0 && k >= 0)) { if (!F) { if (n >= 2 && k >= 2) { n -= 2; k -= 2; } else if (n >= 1 && k >= 12) { ...
4
#include <bits/stdc++.h> using namespace std; int main() { string s, t, move[8]; int h1, v1, h2, v2, ans = 0, b = 0; cin >> s >> t; h1 = (s[0] - 'a') + 1, v1 = s[1] - '0', h2 = (t[0] - 'a') + 1, v2 = t[1] - '0'; string l, r; while (h1 != h2 || v1 != v2) { l = "", r = ""; if (h1 < h2) l = "R"...
2
#include <bits/stdc++.h> using namespace std; int main() { int i, n, k, p = 1; cin >> n >> k; int arr[n]; for (i = 0; i < n; i++) { if (k > 0) { arr[i] = n - i; k--; } else { arr[i] = p; p++; } } for (i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int one[1000], two[1000]; int main() { int n, k, num; cin >> n >> k; for (int i = 0; i < n / k; ++i) for (int j = 0; j < k; ++j) { cin >> num; if (num == 1) one[j]++; else two[j]++; } int answer = 0; for (int i = 0; i < k;...
2
#include <bits/stdc++.h> using namespace std; struct _ { _() { ios_base::sync_with_stdio(0); } } _; template <class T> void PV(T a, T b) { while (a != b) cout << *a++, cout << (a != b ? " " : "\n"); } template <class T> inline bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool c...
16
#include<bits/stdc++.h> using namespace std; #define endl "\n" #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) typedef long long ll; #define prDouble(x) cout<<fixed<<setprecision(10)<<x #define test ll t;cin>>t;while(t--) #define f first #define s second #define pb push_back #define pop pop_back #d...
6
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> void print(T1 a[], T2 n) { for (T2 i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } template <class T1, class T2> void print(vector<T1> a, T2 n) { for (T2 i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; ...
10
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& _p) { return os << "(" << _p.first << "," << _p.second << ")"; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& _V) { bool f = true; os << "["; for (auto v ...
14
#include <bits/stdc++.h> using namespace std; string dg(string a) { int si = a.size(); if (si % 2 == 1) { return a; } string s1, s2, s; s1 = dg(a.substr(0, si / 2)); s2 = dg(a.substr(si / 2, si / 2)); if (s1 < s2) { s += s1; s += s2; } else { s += s2; s += s1; } return s; } strin...
9
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MX = 1.5e5 + 5; const long long INF = 1e18; const long double PI = acos((long double)-1); const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; template <class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template <...
25
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<bool> sp(1000000, false); vector<bool> spp(1000000, false); for (int i = 0; i < n; i++) { int t; scanf("%d", &t); t--; sp[t] = true; } int cnt = 0; vector<int> soln; for (int i = 0; i < 1000000; i...
9
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int i, j; vector<int> pos[26]; for (i = 0; i < s.size(); i++) { pos[s[i] - 'a'].push_back(i); } int cur = -1; for (i = 25; i >= 0; i--) { for (j = 0; j < pos[i].size(); j++) { if (pos[i][j] > cur) { char...
3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x : v) in >> x; return in;...
13
#include <bits/stdc++.h> using namespace std; struct f { int x; int y; } arr[1001]; bool co(struct f c, struct f v) { return c.x <= v.x; } int main() { long long n, r, i; scanf("%I64d %I64d", &n, &r); for (i = 0; i < n; i++) { scanf("%I64d %I64d", &arr[i].x, &arr[i].y); } sort(arr, arr + n, co); lon...
2
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template <class T, class S> inline void add(T& a, S b) { a += b; if (a >= mod) a -= mod; } template...
18
#include <bits/stdc++.h> using namespace std; bool cmp(long long a, long long b) { return (a > b); } long long ceil(const long long &a, const long long &b) { if (a % b == 0) return a / b; return a / b + 1; } long long mod = 1000000007; const long long N = 200005; int main() { ios_base::sync_with_stdio(0); cin.t...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100001; int persone[maxn]; int main() { int n, inizio, fine; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &persone[i]); scanf("%d%d", &inizio, &fine); --inizio; --fine; int somma = 0; int risposta = 0; for (int i = inizio; i < ...
8
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000000 + 7; const long long MOD1 = 998244353; string itos(long long s) { string str = to_string(s); return str; } long long stoi2(string s) { long long a = atoi(s.c_str()); return a; } long long powerm(long long a, long long b, long long MO...
6
#include <bits/stdc++.h> using namespace std; long long n, x, m, ans, fix[1001][1001], parent[10001], fixx[10001]; pair<long long, pair<int, int>> a[10001]; int P(int x) { if (parent[x] == x) return x; return parent[x] = P(parent[x]); } void dsu(int a, int b) { a = P(a); b = P(b); parent[a] = b; } int main() ...
7
#include <bits/stdc++.h> const int maxn = 100010; int n, m; int aBs(int x) { return x < 0 ? -x : x; } void denew(int &x, int y) { if (x > y) x = y; } void innew(int &x, int y) { if (x < y) x = y; } struct Random { long long gy; int hq, mf, qq; Random() { gy = 617; hq = 1102; mf = 228; qq = 199...
16
#include <bits/stdc++.h> inline long long in() { long long k = 0; char ch = getchar(); while (ch < '-') ch = getchar(); while (ch > '-') k = k * 10 + ch - '0', ch = getchar(); return k; } const int N = 1e5 + 5; long long sum[N]; inline long long min(const long long &a, const long long &b) { return a < b ? a...
15
#include <bits/stdc++.h> using namespace std; const int N = 3; vector<vector<int> > g(N); unsigned visited[N]; void ipgraph(int edges); void dfs(int x); void solve() { string s; cin >> s; int cz = 0, co = 0; int n = (int)s.length(); for (int i = 0; i < n; ++i) { if (s[i] - '0') co++; else ...
1
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { ll n, m, key; scanf("%lld%lld", &n, &m); vector<ll> a(n); for (ll i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); for (int i = 0; i < m; i++) { scanf("%lld", &key); auto pos = (ll)(upper_bound(a.begin(), a...
5
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const ll M = 1000000007; const ll N = 998244353; ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } ll inv(l...
1
#include <bits/stdc++.h> int e[2][200001][2], v[2][100001], c[100001], a[2][100001], b[2][100001]; bool use[2][100001]; int n, m; void work(int a[][100001], int o) { for (int i = 1; i <= a[0][0]; i++) use[o][a[0][i]] = true; int l = 0; while (a[l][0] != 0) { a[l ^ 1][0] = 0; for (int i = 1; i <= a[l][0]; ...
9
#include <bits/stdc++.h> using namespace std; const int N = 1025; int n; int in[N]; bool is[N]; bool check(int k) { for (int i = 1; i <= n; ++i) if (!is[in[i] ^ k]) return false; return true; } void solve() { for (int i = 0; i < N; ++i) is[i] = false; scanf("%d", &n); for (int i = 1; i <= n; ++i) { sc...
4
#include <bits/stdc++.h> using namespace std; const long long MAX = 1000; const long long MAXN = 1000; const double PI = acos(-1.0); long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long a[300000]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sor...
4
#include <bits/stdc++.h> int main() { int x, y, i, j; scanf("%d%d", &x, &y); for (i = 0; i < x; i++) { { for (j = 0; j < y; j++) if (i % 2 == 0) printf("#"); else if (i % 4 == 1 && j == y - 1) printf("#"); else if (i % 4 == 3 && j == 0) printf("#"); ...
0
#include <bits/stdc++.h> using namespace std; bool nprm[1000005]; long long di[1024]; vector<int> all[1000005]; long long get(long long X, long long P) { int mx = 1 << (int)all[P].size(); long long rt = 0; for (int i = 0; i < mx; i++) { int b = __builtin_popcount(i); if (b & 1) rt -= (X / di[i]); ...
14
#include <bits/stdc++.h> using namespace std; bool prime(long long int n) { bool x = true; int i; for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) { x = false; } } return x; } void prime1(long long int num) { int div = 0; for (int i = 1; i <= num; i++) { if (num % i == 0) div++; } if...
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using vpii = vector<pair<int, int>>; using vpll = vector<pair<ll, ll>>; usi...
11
#include <bits/stdc++.h> using namespace std; const int N = 40 + 5; int t[N][N]; int dp[N][N][N][N]; int leftt[N][N]; int n, m, q; int main() { char cc; cin >> n >> m >> q; for (int i = 1; i < n + 1; ++i) for (int j = 1; j < m + 1; ++j) { cin >> cc; t[i][j] = cc - '0'; if (!t[i][j]) leftt[i]...
11
#include <bits/stdc++.h> using namespace std; int dp[1000001][3][3][3]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> a(n), b(m + 1, 0); for (int i = 0; i < n; i++) { cin >> a[i]; b[a[i]]++; } for (int i = 0; i <= 0; i++) { for (int x = 0; x < 3;...
14
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; vector<int> p(1e6 + 1, 1); vector<int> primes; for (int i = 1; i <= 1e6; ++i) p[i] = i; for (int i = 2; i <= 1e6; ++i) { if (p[i] == i) { primes.push_back(i); for (int j = 2 * i; j <= 1e6; j += i) { p[j] = i;...
8
#include <bits/stdc++.h> using namespace std; const int INF = 1000 * 1000 * 1000; const double EPS = 1e-9; int h[100000 + 1], a[100000 + 1]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &h[i]); for (int i = 0, j = 0; i < n / 2 + (n & 1 ? 1 : 0); ++i, ++j) { ++a[h[i] - j]; ...
10
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; int cnt[100005]; int x[100005]; int main() { int n; cin >> n; int c = 1; for (int i = 0; i < (int)n; i++) { int a; cin >> a; if (cnt[a] == 0 && a <= n) { x[i + 1] = a; } cnt[a]++; } for (long long i = (long l...
4
#include <bits/stdc++.h> using namespace std; const long long inf = 9.2e18; const int maxn = 107; const int INF = 2.1e9; const int MOD = 1e9 + 7; const double dif = 1e-5; const double PI = 3.14159265358979323846; long long n, m, k; long long a[maxn]; set<long long> S; inline long long check(long long d) { long long a...
15