solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; vector<int> cs; vector<vector<int>> memo; int F(int l, int r) { if (l > r) { return 0; } if (l == r) { return 1; } if (memo[l][r] >= 0) { return memo[l][r]; } int res = 100000000; if (cs[l] == cs[r]) { if (l + 1 == r) { res = 1; } e...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t, m, n, x; cin >> t >> m; string op; vector<int> mem(m + 1, 0); vector<pair<int, int> > a(120, {0, 0}); int id = 1; for (int i = 0; i < t; ++i) { cin >> op; if (op == "alloc") { ...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t))...
11
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n; cin >> n; long long k; cin >> k; ; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } long long res = a[n - 1] - a[0]; vector<long long> v; for (long long i = 1; i < n; i++) { v.push_back(a[i - 1] -...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; const int mod = 1e9 + 7; int n, a[maxn]; int k, ansl[5], ansr[5]; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x ...
19
#include <bits/stdc++.h> using namespace std; map<long long, long long> b; int main() { int n, q, k; scanf("%d %d", &n, &q); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); b[x]++; } int a = 1 << 30; for (int i = 0; i < q; i++) { int m; int ans = 0; int step = 0; scanf("%d"...
8
#include <bits/stdc++.h> using namespace std; long long int mod = 998244353; long double eps = 1e-12; long long int dx[] = {0, 0, 1, -1}; long long int dy[] = {1, -1, 0, 0}; long long int m = 0, n, res = 0, k = 0, h; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); }...
14
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int n, m, k; vector<int> e[MAXN]; int part[2], visit[MAXN], color[MAXN]; int ans[MAXN]; void dfs(int u, int col) { color[u] = col; visit[u] = true; for (int i = 0; i < e[u].size(); i++) { int v = e[u][i]; if (visit[v]) continue; df...
17
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); string s; cin >> s; for (int i = 0; i < s.length() - 1; i++) { if (s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u') { if (s[i] != 'n') { if (s[i + 1] != 'a' && s[i + 1] ...
1
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, x, m, k; int q; cin >> n >> q; double a, b; while (q--) { cin >> x; k = 2 * n - x; while (k % 2 == 0) k /= 2; cout << n - (k - 1) / 2 << '\n'; } } int main() { ios::sync_with_stdio(false); int t = 1; while (t...
9
#include <bits/stdc++.h> using namespace std; int main() { int k, n, j, c, i; cin >> k; int ans[50]; for (i = 0; i < k; i++) { c = 0; cin >> n; string str; cin >> str; for (j = 0; j < (n / 2); j++) { int a = str[j]; int b = str[n - 1 - j]; if ((a - b) != 0 && (a - b) != 2 &...
2
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<ll> a(n), b(m), v(35); for (int i = 0; i < n; i++) { cin >> a[i]; for (ll j = 0; j < 35; j++) if ((1LL << j) & a[i]) v[j]++; } f...
11
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { string p, temp, h, po; cin >> p >> h; if (p.size() > h.size()) { cout << "NO\n"; continue; ...
2
#include <bits/stdc++.h> using namespace std; struct node { int x, y; } a[10010]; bool cmp(node aa, node bb) { return aa.x < bb.x; } int main() { int n; long long sum = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i].x); sum += a[i].x; a[i].y = i; } sort(a, a + n, cmp); i...
5
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); long long n, k; cin >> n >> k; vector<long long> v; for (int i = 0; i < n; i++) { long long a; cin >...
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 1 * 1000 + 10, INF = 1e8; int n, m; int dis[5][MAXN][MAXN]; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; char a[MAXN][MAXN]; bool is_true(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < m); } void bfs(int c) { queue<int> q; for (int i = ...
14
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { lon...
5
#include <bits/stdc++.h> int main() { size_t n, k; std::cin >> n >> k; std::vector<std::pair<size_t, size_t>> results(n, std::make_pair(0, 0)); for (size_t i = 0; i < n; ++i) { size_t link, ans, firstPos, lastPos; std::cin >> link; firstPos = std::max(0, (int)i - (int)k); lastPos = std::min(n - ...
6
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int sum = 0, a; for (int i = 0; i < n; i++) { if (scanf("%d", &a) && a) sum++; } if (n == 1) { if (sum == 1) printf("YES\n"); else printf("NO\n"); } else if (sum == n - 1) printf("YES\n"); else printf("NO\n"); ...
2
//#pragma GCC optimize ("O3") //#pragma GCC optimize ("unroll-loops") //#pragma GCC optimize ("Ofast") #include <bits/stdc++.h> //#include <ctime> using namespace std; #define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define REP(n) FOR(O, 1, (n)) #de...
5
#include <bits/stdc++.h> using namespace std; int main() { int x, i = 0, s = 0; scanf("%d", &x); if (x < 0) x = -x; while (s < x || (s - x) % 2) s += ++i; printf("%d\n", i); }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k; cin >> n >> k; long long arr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); long long left = 0; long long right = n - 1; long long lc = 1, r...
12
#include <bits/stdc++.h> const int N = 2010; const int mv4[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; const int mv8[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1}; bool G[N][N], vis[N][N], cir[N][N]; int n, m; std::vector<std::pair<int, int> > V; void reset() { for (int i = 0; i < (int)V.size(); i++) cir[V[i...
17
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; long long pow2[35]; int n; pair<pair<long long, long long>, bool> doggo() { string str; cin >> str; bool black = 0; if (str[0] == '-') black = true; long long ret = 0, curr = 0, sz = -1; for (int i = 1; i < str.size(); i++) { if (...
16
#include <bits/stdc++.h> using namespace std; int main() { int n, s = 0, mn = 1001, arr[105], c = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; s += arr[i]; if (arr[i] % 2 != 0) { mn = min(mn, arr[i]); } else { c++; } } if (s % 2 != 0) { cout << s; } else if (...
4
#include <bits/stdc++.h> using namespace std; const int MAX_N = 17; int dp[MAX_N * MAX_N][1 << MAX_N]; struct card { char c; int r, b; }; card a[MAX_N]; int n; int sumR, sumB; const int inf = -1e9; int main() { scanf("%d", &n); int MAXR = n * n; for (int i = 0; i < n; i++) { char s[2]; scanf("%s %d %d...
16
#include <bits/stdc++.h> using namespace std; int n, q, t, k, d, res, a[102], r; int main() { cin >> n >> q; for (int i = 1; i <= q; i++) { scanf("%d%d%d", &t, &k, &d); res = 0; r = 0; for (int j = 1; j <= n; j++) if (a[j] < t) r++; if (r >= k) { for (int j = 1; j <= n; j++) ...
5
#include <bits/stdc++.h> using namespace std; const long long N = 1000010; inline long long read() { long long s = 0, w = 1; register char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch ...
14
#include <bits/stdc++.h> using namespace std; const int MXN = 1e5 + 1; const int TMXN = 2e6 + 10; const long long INF = 2e9 + 7; const long long INFL = 1e18; int n, ans, res, cnt[4], x, y, k, add; string second = "BGR"; char a[MXN]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i]...
5
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, m, p, i, j, k, q, t, ans, x, y; cin >> t; string s; while (t--) { cin >> n; vector<long long int> v[n]; p = 0, ans = 0; for (i = 0; i < n; i++) { cin ...
6
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long len = s.length(); for (int i = 0; i < len; i++) { string a, b; if (s.length() % 2 == 0) { a = s.substr(0, s.length() / 2); b = s.substr(s.length() / 2, s.length() / 2); reverse(b.begin(), b.end());...
1
#include <bits/stdc++.h> using namespace std; int main() { int n, c; cin >> n >> c; int pi[n], ti[n]; for (int i = 0; i < n; i++) { cin >> pi[i]; } int arr[1001] = {0}; for (int i = 0; i < n; i++) { cin >> ti[i]; if (i > 0) arr[i] = ti[i] + arr[i - 1]; else arr[i] = ti[i]; } ...
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-6; const int maxn = 1e3 + 5; const int maxm = 1e6 + 5; const int dx[...
0
#include <bits/stdc++.h> using namespace std; int arr[19][19]; int main() { int n, a, b; cin >> n; a = 1; b = n * n; for (int j = 0; j < n; ++j) { for (int i = 0; i < n / 2; ++i) cout << a++ << " " << b-- << " "; cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const long long linf = (long long)1e18; const int mod = (int)1e9 + 7; const long double eps = (long double)1e-8; const int maxn = (int)2e5 + 5; const long double pi = acos(-1); vector<pair<pair<int, int>, int>> ans; int centre, centre2; vector<int>...
18
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e18; const long long N = 2e5 + 33; long long power(long long x, long long y) { long long p = MOD; long long 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; ...
7
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; int matrica[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> matrica[i][j]; } } if (n == 1) { for (int i = 0; i < m; i++) { if (matrica[0][i] != 0) { cout << "TAK\n"...
8
#include <bits/stdc++.h> using namespace std; int main() { int w, h, n; cin >> w >> h >> n; multiset<long long> height, width, maxw, maxh; width.insert(0); width.insert(w); height.insert(0); height.insert(h); maxw.insert(w); maxh.insert(h); char c; int x; for (int i = 0; i < n; i++) { cin >>...
7
#include <bits/stdc++.h> using namespace std; long long A, B, n, l, m, t; bool check(long long r) { long long k = r - l + 1LL; long long rate = min(k, m); if (A + B * (r - 1) > t) return false; long long total = A * (r - l + 1LL) + B * (l + r - 2LL) * (r - l + 1LL) / 2LL; if (total > rate * t) return false; ...
11
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:108777216") using ll = long long; using ull = unsigned long long; using ld = long double; template <typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; } u...
7
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } const long long inv = power(100, mod - 2); int main() { int n; scanf...
13
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int N = 2e5; int p[N]; int b[N]; bool visited[N]; int cycles = 0; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int(i) = 0; (i) < (n); ++(i)) { cin >> p[i]; p[i]--; } int cnt1 = 0; for (int(i) = 0; (i) < (n...
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> m(n); for (auto &u : m) { cin >> u; } vector<int> min_marks(n); for (int i = m.size() - 1; i >= 0; --i) { min_marks[i] = m[i] + 1; if (i != m.size() - 1) { min_marks[i] = ((min_marks[i] > mi...
9
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define fo(i,start,n,step) for(ll i = start; i < n; i+=step) #define ends cout << "\n"; #define pb(val) push_back(val); #define all(v) v.begin(), v.end() using namespace std; int main() { int t; cin >> t; vector<...
0
#include <bits/stdc++.h> using namespace std; int const N = 200 * 1000 + 123; int const B = 60; int const MOD = 1000 * 1000 * 1000 + 7; int const INVTWO = (MOD + 1) / 2; int n, m, f[N], s[N]; long long t[N], x[N]; int p[N], w[N], take[N]; vector<int> e[N]; vector<long long> a[N]; int two[B]; int dp[N][B][2], cnt[N][B][...
18
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll t; cin >> t; while (t--) { double n; cin >> n; ll c = ceil(log2(n)); ll f = floor(log2(n)); if (c == f) cout << n - 1 <<"\n"; else { cout <<...
0
#include <bits/stdc++.h> using namespace std; inline void prep() { cin.tie(0); cin.sync_with_stdio(0); } const int MX = 1e5 + 10; const int mood = ((int)1e9 + 7); bool instack[5005]; int res[5005][5005]; int mem[5005]; int main() { prep(); int n; cin >> n; int a[n], fs[5001], ls[5001]; memset(fs, 0xff, si...
11
#include <bits/stdc++.h> using namespace std; long long d, a, m, b, x; void solve() { int n; cin >> n; int a[n + 1], b[n + 1]; for (int i = 0; i < n; i++) { char ch; cin >> ch; a[i + 1] = ch - '0'; } for (int i = 0; i < n; i++) { char ch; cin >> ch; b[i + 1] = ch - '0'; } vector<...
5
#include <bits/stdc++.h> using namespace std; const long long LLINF = 8e18; const int MOD = 1e9 + 7; const int INF = 2e9; const int N = 300; int n, t; int p[N + 1]; vector<int> v; string s; int get(int l, int r) { int ans; cout << '?' << ' ' << l << ' ' << r << '\n'; cin >> ans; cout << '?' << ' ' << l << ' ' <...
18
#include <bits/stdc++.h> const long long inf = 1e9; const long double eps = 1e-6; const long long N = 1e6; const long long M = 1e6; using namespace std; void ret(string s) { cout << s; exit(0); } string s; int a[100][100], b, ans = 0; int mx, mn, len = 0, pos, n, k, h; bool u[1000]; int main() { cin >> n >> k; ...
1
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; long long n; void Input() { cin >> n; } void Solve() { vector<long long> a(2 * n); for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = n; i < 2 * n; i++) a[i] = a[i - n]; long long t = 0, ans = 0; for (long long i = 0; i...
1
#include <bits/stdc++.h> using namespace std; inline long long read() { register long long x = 0; register bool f = 0; register char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = 1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - 48; c = getchar(...
9
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 1; const int C = 7070; bitset<C> s[N]; bitset<C> divs[C], mu[C]; bool sqrFree(int x) { for (int d = 2; d * d <= x; ++d) { if (x % d) continue; x /= d; if (x % d == 0) return false; } return true; } int main() { for (int x = 1; x ...
17
#include <bits/stdc++.h> using namespace std; char buf[1 << 20], *p1, *p2; template <class T> inline void read(T &n) { char ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++); T w = 1, x = 0; while (!isdigit(ch)) { if (ch == '-') w = -1; ...
19
#include <bits/stdc++.h> using namespace std; struct Status { int x, k, m, bit; Status(int _x = 0, int _k = 0, int _m = 0, int _bit = 0) : x(_x), k(_k), m(_m), bit(_bit) {} bool operator<(const Status& rhs) const { return k < rhs.k; } } x[110]; long long dp[2][1 << 20]; int main() { int n, m, b; scanf("...
11
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; while (a--) { int b; cin >> b; int arr[b]; for (int j = 0; j < b; j++) cin >> arr[j]; int cont = 0; for (int j = 0; j < b - 1; j++) { for (int k = j + 1; k < b; k++) { if (arr[k] % 2 != 0 && k % 2 == ...
0
#include <bits/stdc++.h> using namespace std; vector<short int> M[3002][3002]; vector<short int> L[3002]; int n, m; short int D[3002][3002], PD[3002][3002], res[3002]; void BFS(int source) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) D[i][j] = -1; D[source][source] = 0; deque<pair<short int, shor...
12
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(uns...
10
#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...
7
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 5; int nodes, monsters, d; vector<int> tree[N]; vector<bool> Ghosts; int e[2][N] = {}; void dfs(int node, int parent, int id, int cost) { e[id][node] = cost; for (auto u : tree[node]) { if (u != parent) dfs(u, node, id, cost + 1); } } int ...
12
#include <bits/stdc++.h> using namespace std; set<long long> cutH, cutW; multiset<long long> cutMH, cutMW; void solve() { cutH.clear(), cutW.clear(); cutMH.clear(), cutMW.clear(); long long H, W, K; cin >> W >> H >> K; cutH.insert(0), cutH.insert(H); cutW.insert(0), cutW.insert(W); cutMH.insert(H - 0); ...
7
#include <bits/stdc++.h> using namespace std; int head[301]; struct Edge { int v; int next; } edge[600]; int edge_ind = 0; int a[301], b[301]; int parent[301]; long long s[301]; int step_cnt = 0; int from[180000], to[180000], contain[180000]; int find(int x) { if (x == parent[x]) return x; return parent[x] = fi...
17
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long double PI = (long double)(3.1415926535897932384626433832795); inline bool ispow2(int x) { return (x != 0 && (x & (x - 1)) == 0); } int msb(unsigned x) { union { double a; int b[2]; }; a = x; return (b[1] >> 20) - 1023; } t...
13
// Problem: D. Pairs // Contest: Codeforces - Educational Codeforces Round 100 (Rated for Div. 2) // URL: https://codeforces.com/contest/1463/problem/D // Memory Limit: 256 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) /* @ author: dragon_bra @ QQ: 1277037638 @ email: ...
11
#include <bits/stdc++.h> using namespace std; const int maxn = 2111111; char buf[maxn]; int sum[maxn]; bool ok[maxn]; int order[maxn]; template <typename T> void debug(T a[], int n) { for (int i = 1; i <= n; ++i) { cout << setw(3) << a[i]; } puts(""); } void fix(int& i, int n) { while (!ok[i] && i <= n) { ...
19
#include <bits/stdc++.h> using namespace std; int a[100005], dp[200005]; int lo[100005], hi[100005]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d", &a[i]); if (n == 1) return !printf("0\n"); for (int i = m; i >= 1; i--) dp[a[i] + i] = dp[a[i] + i + 1] + 1; for (int i ...
17
#include <bits/stdc++.h> using namespace std; const int MAX = 100000; int n, tag[MAX + 5], cnt[MAX + 5], a[MAX + 5], b[MAX + 5]; long long ans; char s[MAX + 5]; int amin(int a, int b) { return a > b ? b : a; } struct node { int num, op; node(int num, int op) : num(num), op(op) {} node() : num(0), op(0) {} }; vect...
19
#include <bits/stdc++.h> using namespace std; long long T, a, b, c, d; inline int read() { int ret = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -f; ch = getchar(); } while (isdigit(ch)) ret = (ret << 1) + (ret << 3) + ch - '0', ch = getchar(); return ret * f; } int main...
13
#include <bits/stdc++.h> using namespace std; int n, m; map<char, int> char2num = {{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}}; string num2char = "ACGT"; int a[300020]; char s[300020]; int temp_a[300020], best_a[300020]; int best_ans; int get(int row, int col, int tran) { if (!tran) return row * m + col; return col * m...
13
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, x, y, z; cin >> a >> b >> c; cin >> x >> y >> z; long long need_a = 0, need_b = 0, need_c = 0, canmake_a = 0, canmake_b = 0, canmake_c = 0; if (x > a) need_a = x - a; else if (x < a) canmake_a = (a - x) / 2; ...
4
#include <bits/stdc++.h> using namespace std; int a[100001], b[100001], n, p; inline int sum(double lambda) { int i; double s1 = 0.0, s2 = 0.0; for (i = 1; i <= n; i++) if (double(a[i] * lambda) > double(b[i])) { s1 += double(a[i] * lambda); s2 += double(b[i]); } return (s1 <= lambda * p + s...
10
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, k; vector<long long> a; scanf("%lld %lld %lld", &n, &x, &k); for (int i = 0; i < n; ++i) { long long t; scanf("%lld", &t); a.push_back(t); } sort(a.begin(), a.end()); long long ans = 0, cur; for (int i = 0; i < n; ++i...
9
#include <bits/stdc++.h> int main() { int n, a1, a2, a3, count = 2, max = 2, final = 2, i; scanf("%d", &n); scanf("%d", &a1); scanf("%d", &a2); for (i = 2; i < n; i++) { scanf("%d", &a3); if (a1 + a2 == a3 && i != n - 1) { count++; } else { if (i == n - 1 && a1 + a2 == a3) count++; ...
3
#include <bits/stdc++.h> using namespace std; const int INF = 1000; int main() { int bit[60]{}; int n; cin >> n; vector<long long> a; for (int i = 0; i < n; ++i) { long long x; cin >> x; if (!x) continue; a.push_back(x); for (int b = 0; b < 60; ++b) { if ((1LL << b) & x) ++bit[b]; ...
11
#include <bits/stdc++.h> using namespace std; inline long long gi() { long long x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) { f |= ch == '-', ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48, ch = getchar(); } return f ? -x : x; } inline void pi(long long x) { if (x < 0) ...
16
#include <bits/stdc++.h> using namespace std; int main() { int x, y, z, X, Y, Z; scanf("%d %d %d %d %d %d", &x, &y, &z, &X, &Y, &Z); int a[3]; a[0] = x - X, a[1] = y - Y, a[2] = z - Z; sort(a, a + 3); if (a[0] >= 0 && a[1] >= 0 && a[2] >= 0) { printf("Yes"); return 0; } if (a[2] <= 0) { prin...
4
#include <bits/stdc++.h> using namespace std; const int lim = 20000; struct query { int type, cost, happy; query() {} query(int type, int cost, int happy) : type(type), cost(cost), happy(happy) {} }; void add(int *dp, int W, int V) { for (int j = 4000; j >= W; j--) dp[j] = max(dp[j], dp[j - W] + V); } int items...
19
#include <bits/stdc++.h> using namespace std; int main() { int n, m, l, w, h[500], c[500], wl[500], ww[500], wp[500]; int i, j, min, sum, cost = 0; cin >> n; for (i = 0; i < n; i++) { cin >> l >> w >> h[i]; c[i] = 2 * (l + w); } cin >> m; for (i = 0; i < m; i++) { cin >> wl[i] >> ww[i] >> wp[i...
8
#include <bits/stdc++.h> using namespace std; string N; pair<int, pair<int, int>> dp[101010][3]; int main() { cin >> N; int n = N.length(); for (int i = 0; i < n + 1; i++) for (int m = 0; m < 3; m++) dp[i][m] = {-1, {-1, -1}}; dp[0][0] = {0, {-1, -1}}; for (int i = 0; i < n; i++) for (int m = 0; m < 3...
12
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> oo; int S = 1e9; int n; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { string s; cin >> s; map<char, int> mp; for (int i = 0; s[i]; i++) { mp[s[i]]++; } if (mp['A...
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector") #pragma GCC target("avx,tune=native") #pragma GCC diagnostic ignored "-Wunused-result" #pragma GCC diagnostic ignored "-Wunused-function" using namespace std; namespace { namespace shik { inline int read_char() { static char buf[1 ...
23
#include <bits/stdc++.h> using namespace std; int n, korijen; string s; long long int rj; int f(int a, int b, int t) { if (a > b) swap(a, b); if (t < 0) return 0; if (t > a + b) return 0; if (t <= a) return t + 1; if (t >= b) return a + b - t + 1; return a + 1; } int main() { cin >> s; n = (int)s.size()...
18
#include <bits/stdc++.h> using namespace std; const long long MN = 200111; long long deg[MN]; vector<long long> ke[MN]; vector<long long> ls[MN]; map<long long, long long> id[MN]; void dfs(long long u, long long fu, long long last) { long long c = 0; for (long long v : ke[u]) { if (v == fu) continue; ++c; ...
10
#include <bits/stdc++.h> using namespace std; int getMin(int a, int b) { if (a <= b) return a; else return b; } int getMax(int a, int b) { if (a >= b) return a; else return b; } int main() { int a, b; cin >> a >> b; int min = getMin(a, b); int max = getMax(a, b); cout << min << " " << ...
0
#include <bits/stdc++.h> int main() { int a, b, c, d, y; scanf("%d", &y); while (y > 1) { y = y + 1; a = y / 1000; b = y / 100 % 10; c = y / 10 % 10; d = y % 10; if (a != b && a != c && a != d && b != c && b != d && c != d) { break; } } printf("%d", y); return 0; }
0
#include <bits/stdc++.h> using namespace std; struct node { int x, y; } a[200200]; long long ans = 0; int n; int cmp(const node& a, const node& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } void count() { for (int i = 1, j; i <= n; i = j + 1) { j = i; while (a[j + 1].x == a[i].x && j + 1 <= n) j++...
6
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long p, ans = 0; for (long long i = 1; i <= n; i++) { cin >> p; ans = ans ^ p; } long long x[n + 1]; x[0] = 0; for (long long i = 1; i <= n; i++...
8
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const size_t MAX_N = (size_t)1e5 + 5; pair<int, string> inp[MAX_N]; pair<long long, long long> mp[10]; string s; inline long long& up_add(long long& x, const long long& add) { return (x += add) %= MOD; } inline long long& up_mul(long long& x...
13
#include <bits/stdc++.h> using namespace std; string str[200001]; int main() { int i; map<char, char> m; string s, ss, t; cin >> s >> t >> ss; for (i = 0; i < 26; i++) { m[s[i]] = t[i]; } for (i = 0; i < ss.length(); i++) { if (ss[i] >= '0' && ss[i] <= '9') cout << ss[i]; else if (ss[i] ...
0
#include <bits/stdc++.h> using namespace std; long long n, x, y, c, ans, switchedOn; int i; inline int next(int x) { x = (x + 1) % 4; return x; } int main() { scanf("%lld %lld %lld %lld", &n, &x, &y, &c); long long S[4] = {y - 1, x - 1, n - y, n - x}; ans = 0, switchedOn = 1; while (1) { for (i = 0; i <...
10
#include <bits/stdc++.h> using namespace std; void __print(int x) { cout << x; } void __print(long x) { cout << x; } void __print(long long x) { cout << x; } void __print(unsigned x) { cout << x; } void __print(unsigned long x) { cout << x; } void __print(unsigned long long x) { cout << x; } void __print(float x) { cou...
5
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n; cin >> t; while (t--) { cin >> n; if (n == 1 || n == 2) cout << 1 << endl; else if (n == 3) cout << 2 << endl; else if (n % 2 == 0) cout << n / 2 << endl; else cout << n / 2 + 1 << endl; } ...
0
#include <bits/stdc++.h> using namespace std; double PI = 3.1415926535897932384626433832795; int arr[100004], ans[100005], vis[100005]; int main() { int n, m, d, ans = 0; cin >> n >> d; for (int i = 0; i < n; cin >> arr[i], ++i) ; sort(arr, arr + n); cin >> m; int i = 0; while (m && i < n) { ans +...
2
#include <bits/stdc++.h> using namespace std; struct node { int value; int index; int color = -1; }; bool compare1(node a, node b) { return a.value > b.value; } bool compare2(node a, node b) { return a.index < b.index; } int main() { int n, k; cin >> n >> k; node arr[n]; int count[5001], flag = 0, check[k...
6
#include <bits/stdc++.h> using namespace std; const int N = 1004, P = 1000000007; const int sg[102][3] = {{0, 1, 2}, {1, 3, 3}, {2, 4, 4}, {1, 5, 6}, {2, 7, 8}, {0, 9, 12}, {1,...
18
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int, int>> v; for (int i = 0; i < n; ++i) { int a, b, c, d; cin >> a >> b >> c >> d; v.emplace_back(a + b + c + d, -i); ...
0
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; long long n; int k; vector<vector<int> > mul(const vector<vector<int> > &a, const vector<vector<int> > &b) { vector<vector<int> > c(3, vector<int>(3)); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) for ...
13
#include <bits/stdc++.h> const int N = 100010; const long long INF = 1ll << 60; const int inf = 0x3f3f3f3f; class Discretization { public: Discretization() { vec_.clear(); } void Init() { vec_.clear(); } void Add(int x) { vec_.push_back(x); } void Build() { std::sort(vec_.begin(), vec_.end()); vec_.era...
15
#include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7, N = 110, LOG = 32; int n, b, k, x, help, dp[LOG][N], final_dp[N], copy_dp[N]; int sum(int a, int b, int mod) { long long res = a + b; while (res >= mod) res -= mod; while (res < 0) res += mod; return res; } int mul(int a, int ...
12
#include <bits/stdc++.h> using namespace std; map<long long, int> mp; bool ck[600000]; int len[600000]; char s[50005]; int sz, q; vector<int> pos[600000]; vector<int> E1, E2; int getId(char* str, int sz) { int ret = 0; for (int i = 0; i < sz; ++i) ret *= 27, ret += str[i] - 'a' + 1; return ret; } int getAns(int i...
17