solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>:...
18
#include <bits/stdc++.h> using namespace std; const int maxn = 1e2 + 200; const int inf = 0x3f3f3f3f; const double eps = 1e-7; const double pi = acos(-1.0); const int mod = 1e9 + 7; inline int lowbit(int x) { return x & (-x); } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } void ex_gcd(long l...
8
#include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, long long int e) { if (e == 0) return 1; long long int x = powmod(a * a % 1000000007, e >> 1); return e & 1 ? x * a % 1000000007 : x; } long long int b[(long long int)100001]; multiset<long long int> st[60]; vector<long long int> ...
14
#include <bits/stdc++.h> using namespace std; long long gao(long long n, long long p) { long long ret; if (p & 1) ret = (p + 1) >> 1; else { long long cnt = n >> 1; long long np = ((p >> 1) - cnt) % (n - cnt); if (np <= 0) np += n - cnt; ret = cnt + gao(n - cnt, np); } return ret; } long l...
9
#include <bits/stdc++.h> using namespace std; vector<int> v[200005]; int col = 1; int ans = 0, visited[200005] = {0}, colour[200005]; void dfs(int node, int pcol) { visited[node] = 1; col = 1; for (int i = 0; i < (int)v[node].size(); i++) { if (visited[v[node][i]] == 0) { if (pcol == col || colour[node]...
8
#include <bits/stdc++.h> using namespace std; template <typename T> T abs(T a) { return a < 0 ? -a : a; } const int INF = (int)1e9; const double PI = 3.1415926535897932384626433832795; const int N = 0; vector<string> v; string tp, s; int n; int main() { scanf("%d", &n); while (n--) { cin >> tp; if (tp == ...
6
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; map<int, bool> mp; vector<int> v; int arr[100000], cnt = 0, sum = 0; int ans; for (int i = 1; i <= n; i++) { cin >> arr[i]; mp[arr[i]] = true; } int i = 1; while (k >= i) { if (!mp[i]) k -= i, v.push_back...
4
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll t; cin>>t; while(t--) { ll n,sum=0; cin>>n; n=n+2; map<ll,ll> mp; ll arr[n],i,a,b,f=0,j; for(i=0;i<n;i++) { cin>>arr[i]; mp[arr[i]]++; sum=sum+arr[...
4
#include <bits/stdc++.h> using namespace std; template <class T> inline void smax(T &x, T y) { x = max((x), (y)); } template <class T> inline void smin(T &x, T y) { x = min((x), (y)); } const int MOD = 1e9 + 7; const int SIZE = 1e5 + 10; const long long INF = 1e15; int n, m, t; int calcsecs(string &str) { for (in...
13
#include <bits/stdc++.h> using namespace std; const int maxn = 400010; int T, m; long long n, K, a[maxn]; int main() { scanf("%d", &T); while (T--) { scanf("%lld %d %lld", &n, &m, &K); for (int i = 1; i <= m; i++) { scanf("%lld", &a[i]); } a[++m] = n; for (int i = m; i; i--) { a[i] -...
25
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int a[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); int ans = 0; for (int i = 1, j = 1; i <= n; ++i) { if (i > j && a[i] < a[i - 1]) j = i; ans = max(ans, i - j + 1); } printf("%d\n", a...
1
#include <bits/stdc++.h> using namespace std; struct rect { long long l, r, u, d; rect() { l = 0, r = 0, u = 0, d = 0; } rect(long long a, long long b, long long c, long long d1) { l = a, r = b, u = c, d = d1; } }; long long n; vector<pair<rect, long long>> a; void read() { cin >> n; for (long long i = ...
22
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; void fileio(const string &s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } const long long INF = 4e18; inline long long read() { long long x = 0; bool flag = 1; char c = getchar(); while (...
12
#include <bits/stdc++.h> using namespace std; const int MAXN = 107; int n; int a[MAXN]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; set<int> ms; const int INF = 1e9 + 7; int mn = INF; int mx = -INF; map<int, int> d; for (int i = 0; i < n; ++i) { cin >> a[i]; ms.insert(a...
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; int mx = 0, mn = 1e9; vector<string> v(n); for (int i = 0; i < n; i++) cin >> v[i]; int ans = 1e9; int ma; int arr[8]; for (int i = 0; i < m; i++) arr[i] = i; do { int...
6
#include <bits/stdc++.h> #define F(x,y,z) for(int x = y ; x < z ; x++) using namespace std; const int inf = 2e9; int solve(vector<int>& a){ vector<vector<int> > dp; int n = a.size(); for(int i = 0 ; i < a.size() ; i++){ vector<int> temp; dp.push_back(temp); } F(i,0,n) dp[0].push_back(0); F(i,1,n){ F(j,i...
19
#include <bits/stdc++.h> using namespace std; int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int z[100005]; int main() { int n, a, i; i = 1; cin >> n; for (a = 0; n > a; a++) { cin >> z[a]; } qsort(z, n, sizeof(z[0]), cmp); for (a = 0; n > a; a++) { if (z[a] >= i) { ...
4
#include <bits/stdc++.h> using namespace std; int f[5555][5555], mx[7], g[101000]; int n, a[5555], ans = 0; void R(int &x, int y) { x = max(x, y); } int main() { scanf("%d", &n); for (int i = 1; i <= n; scanf("%d", &a[i++])) ; for (int i = 0; i < n; i++) { memset(mx, 0, sizeof mx); memset(g, 0, sizeof...
18
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int N = 1e5 + 10; char ch[N]; int f[N], g[N]; int n; void init() { scanf("%s", ch + 1); n = strlen(ch + 1); for (int i = 1; i <= n; i++) f[i] = ch[i] - '0', g[i] = f[i]; } bool check(int l) { for (int i = l, j = n; i < j; i++, j-...
16
#include <bits/stdc++.h> using namespace std; int main() { int n, x, a = 0, b = 0; scanf("%d", &n); if (n == 1) { cout << "NO"; return (0); } for (int i = 0; i < n; i++) { scanf("%d", &x); if (x == 100) a++; else b++; } if (a % 2) cout << "NO"; else { if (!(b % 2)...
3
#include <bits/stdc++.h> using namespace std; int n, m; int a[111111]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); printf("%d\n", a[n - m]); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long DELTA = 1000000007, maxn = 100 + 7; long long k, m, n, ans[maxn][maxn * maxn], chooseMem[maxn][maxn], powerMem[maxn][2]; ; bool isCeil[maxn]; int power(long long base, long long p) { long long ans = 1; while (p > 0) { if (p % 2) { ans *= ba...
11
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 200, D = 12; int n, m, a, q, mod; long long fac[N], inv[N], sum[N]; long long bp(long long a, long long p, long long mod) { long long r = 1; while (p > 0) { if (p & 1) r = r * a % mod; p >>= 1; a = a * a % mod; } return r; } long long...
14
#include <bits/stdc++.h> using namespace std; const int N = 300005; const int mod = 998244353; long long dp[N][3]; struct edge { int to, next; } g[N << 1]; int head[N], cnt; void add(int u, int v) { g[++cnt].to = v; g[cnt].next = head[u]; head[u] = cnt; } long long pw(long long a, long long b) { long long ans...
16
#include <bits/stdc++.h> using namespace std; struct ofl { long double value; int precision; inline ofl(long double value, int precision) : value(value), precision(precision) {} }; const int seed = hash<string>()("alex256"); const int inf = (int)1e9 + 7; const int64_t llInf = (int64_t)2e18 + 256; const int ...
10
#include <bits/stdc++.h> using namespace std; const int MAX = 1e3 + 5; vector<int> vt[MAX]; int n, m; int main() { int x, y; while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; ++i) vt[i].push_back(i); while (m--) { scanf("%d%d", &x, &y); vt[x].push_back(x * 1000 + y); vt[y].push_back...
9
#include <bits/stdc++.h> using namespace std; const int N = 150005; vector<int> vet[N]; pair<int, int> cmp[N]; int p[N]; int find(int u) { return p[u] = (u == p[u]) ? u : find(p[u]); } void une(int u, int v) { u = find(u), v = find(v); vet[cmp[v].first].push_back(cmp[u].second); vet[cmp[u].second].push_back(cmp[v...
9
#include <bits/stdc++.h> using namespace std; const int MAX = 100010; long long int num[MAX]; int main() { long long int n; cin >> n; for (int i = 0; i < n; i++) { cin >> num[i]; } sort(num, num + n); for (int i = 0; i < n; i++) { if (i == 0) { if (num[i] != 1) { num[i] = 1; } ...
4
#include <bits/stdc++.h> using namespace std; const int maxn = 300007; char s[maxn]; long long suma[maxn], sumb[maxn], mx[maxn]; int main() { int n; scanf("%d", &n); scanf("%s", s + 1); for (int i = n; i >= 1; i--) { if (s[i] == 'A') { suma[i] = suma[i + 1] + 1; sumb[i] = 0; if (suma[i] ==...
11
#include <bits/stdc++.h> int main() { int n, sum = 0, x, i; double m; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &x); sum = sum + x; } m = (sum * 1.0) / n; printf("%.12lf", m); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long n, m, arr[1000005], z[1000005], lenp; bool beginning[1000005]; string p; void z_algo() { long long i, j, k, l, r, beta; p.push_back('*'); i = 1; j = 0; r = 0; l = 0; z[0] = lenp; for (k = 1; k < lenp; ++k) { beta = r - k + 1; if ((r >= k) &...
11
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, l, c, d, p, nl, np, t1, t2, t3, t4, t5; cin >> n >> k >> l >> c >> d >> p >> nl >> np; t1 = (l * k) / nl; t2 = c * d; t3 = p / np; t4 = min(t1, t2); t5 = min(t2, t3); cout << min(t4, t5) / n; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long mxn = 3e5; long long res = 1, n; vector<long long> adj[mxn], vis(mxn), fac(mxn); void dfs(long long a) { vis[a] = 1; long long ch = 0; for (long long& x : adj[a]) { if (vis[x]) continue; ch++; dfs(x); } if (ch == 0) return; res *= fac...
11
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } int main() { int x, y, n, i, j; double p, q, m, a, b; cin >> x >> y >> n; i = gcd(x, y); x /= i; y /= i; if (n >= y) cout << x << "/" << y; else { a = x; b = y...
9
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long l; string s; cin >> l; cin >> s; long long d = 1; cout << s[0]; for (long long i = 0; i + d < l;) { i = i + d; d++; cout << s[i]; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 4000; const int mod = 1000000007; void add(int &a, long long b) { a = (a + b) % mod; } vector<int> solve(vector<int> v) { vector<int> now(2 * N + 1, 0); now[N] = 1; for (int a : v) { vector<int> nxt = now; for (int i = -N; i + a <= N; i++) add(nx...
18
#include <bits/stdc++.h> using namespace std; const int N = 105; map<string, int> mp; string name[N], s; long long w[N], h[N]; int o, n, type[N]; int border[N], space[N]; int pl, pr, pSpace, pDot, p; vector<int> v[N]; struct answer { string s; long long w, h; answer(string s = "", long long w = 0, long long h = 0...
15
#include <bits/stdc++.h> using namespace std; const int MAXN = 500005; vector<int> v[MAXN]; int st[MAXN], et[MAXN], lazy[4 * MAXN]; int c[MAXN], order[MAXN]; long long seg[4 * MAXN]; int n, m, cnt = 0; void DFS(int i, int p) { order[cnt] = i; st[i] = cnt++; for (int j = 0; j < v[i].size(); j++) { if (v[i][j] ...
13
#include <bits/stdc++.h> using namespace std; struct Point { int x, y; Point(int _x = 0, int _y = 0) : x(_x), y(_y){}; Point operator-(Point A) { return Point(x - A.x, y - A.y); } int operator^(Point A) { return x * A.y - A.x * y; } }; int main() { Point p[2][5]; for (int i = 0; i < 2; ++i) for (int j =...
8
#include <bits/stdc++.h> using namespace std; string s; int main() { cin >> s; int f1, f2, i; f1 = f2 = 0; for (i = 0; i < s.size(); i++) { if (s[i] != 'a') f1 = 1; if (f1 == 1 && s[i] == 'a') f2 = 1; if (f1 == 1 && f2 == 0) s[i]--; } if (f1 == 0) s[s.size() - 1] = 'z'; cout << s; return 0; ...
4
#include <bits/stdc++.h> using namespace std; void checkpoint1() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } bool comp(pair<long long, long long> &a, pair<long long, long long> &b) { if (a.first != b.first) return a.first < b.first; else { if (a.second > b.second) return...
6
#include <bits/stdc++.h> using namespace std; const int N = 130; const int MOD = 1e9 + 7; inline void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } struct matrix { int n, m; int a[N][N]; matrix operator*(const matrix &x) const { matrix ret; ret.n = n; ret.m = x.m; memset(ret.a, 0, siz...
19
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n, m; int dp[2][505][505]; char grid[505][505]; int half; bool chk_state(int row1, int row2, int dist) { if (row1 > row2) return 0; int taken = (m - 1) - dist + (row1) + (n - 1 - row2); int lft = (n + m - 2) - taken; if (lft < 0) return ...
15
#include <bits/stdc++.h> using namespace std; int main() { int n, vb, vs; cin >> n >> vb >> vs; long long int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long int x, y; cin >> x >> y; double tmin = pow(10, 9); double dmin = pow(10, 9); long long int imin; for (int i = 1; i < n; i++) { do...
4
#include <bits/stdc++.h> using namespace std; struct Bet { int id; int amount; int time; Bet() {} Bet(int a, int b, int c) { id = a; time = b; amount = c; } bool operator<(const struct Bet &rhs) const { return time < rhs.time; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NUL...
12
#include <bits/stdc++.h> using namespace std; inline char gc() { static char now[1 << 16], *S, *T; if (S == T) { T = (S = now) + fread(now, 1, 1 << 16, stdin); if (S == T) return EOF; } return *S++; } inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { (c == ...
17
#include <bits/stdc++.h> int main() { int n, a[102], i, j, coin = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (i = 1; i <= n; i++) { if ((a[i] == 2 && a[i + 1] == 3) || (a[i] == 3 && a[i + 1] == 2)) { printf("Infinite\n"); break; } } if (i == (n + 1)) {...
6
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int n0 = 1003; vector<string> ans; int N, ind[n0]; vector<array<int, 3>> add; void solve(vector<int> v) { int n = (int)(v).size(); int suf = 0; if (n) { suf = n - 1; v[n - 1]--; whi...
18
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; const long long INF = 1e18 + 5; const long long mxn = 1e6 + 5; const int mod = 1e9 + 7, pa = 1e6 + 7; long long n, a[555555], u[555555], mx = 0, l[mxn], pr[mxn]; long long c[555555], b[555555], p[555555]; vector<int> v[mxn]; int main() { int...
11
#include <bits/stdc++.h> using namespace std; const int N = 100100; int n, m, k; bool rev; vector<int> a[N]; const int BOUND = 11; void clear() { for (int i = 0; i < n; i++) { a[i].clear(); a[i].resize(m); for (int j = 0; j < m; j++) a[i][j] = 0; } } int calcSquare(int x, int y) { if (x < 0 || y < 0 |...
23
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; const int N = 10; vector<vector<int>> frec(2, vector<int>(N, 0)); for (char ch : s) frec[0][ch - '0']++, frec[1][ch - '0']++; string ans1 = ""; string ans2 = ""; int mx = 0, pib = ...
11
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const long long N = 1002; long long dr[4] = {0, 0, 1, -1}; long long dc[4] = {1, -1, 0, 0}; long long ddr[8] = {0, 0, 1, -1, 1, -1, 1, -1}; long long ddc[8] = {1, -1, 0, 0, 1, -1, -1, 1}; long long...
8
#include <bits/stdc++.h> using namespace std; int n; vector<int> q[100005]; int f, t; int p[100005]; bool r; int main() { cin >> n; for (int i = 1; i < n; i++) { scanf("%d%d", &f, &t); q[f].push_back(i), q[t].push_back(i); } for (int i = 1; i <= n; i++) { if (q[i].size() >= 3) { r = 1; f...
7
#include <bits/stdc++.h> using namespace std; int main() { long int n; int a; double min = 180.0; int count = 0; cin >> n; cin >> a; double degree = 180.0 / n; double tmp; for (int i = 1; i <= n - 2; i++) { tmp = i * degree; if (abs(tmp - a) < min) { count = i; min = abs(tmp - a); ...
5
#include <bits/stdc++.h> using namespace std; const long long INF = 9223372036854775807LL, SQ_MAX = 40000000000000000LL; void getmin(long long &a, const long long b) { if (b < a) a = b; } void getmax(long long &a, const long long b) { if (b > a) a = b; } long long Sq(const long long a) { return a * a; } struct Poin...
21
#include <bits/stdc++.h> using namespace std; int n, a, b; int x[110], y[110]; int f(int A, int B) { if (x[A] > a || y[A] > b || x[B] > a || y[B] > b) return 0; int x1 = x[A] - 1, y1 = y[A] - 1; int x2 = a - x[B], y2 = b - y[B]; if (x1 >= x2 && y1 >= y2) return 0; return x[A] * y[A] + x[B] * y[B]; } int main(...
7
#include <bits/stdc++.h> using namespace std; int main() { iostream::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long q; cin >> q; long long maxx = pow(10, 18) + 5; set<long long> st, st1; for (long long i = (2); i < (1000001); ++i) { long long tmp = i * i * i; while (true) {...
13
#include <bits/stdc++.h> using namespace std; const int N = 50005; struct Node { long long sum; int tag; Node *lson, *rson; } * rt[N], *null; int n, m, Q, C, f[N], c[N], pa[N], dep[N], sz[N], son[N], top[N], dfn[N], pos[N], dfc; vector<int> G[N]; long long sumd[N], sum2[N]; void dfs1(int u, int depth) { dep...
23
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int powermod(int a, long long p, int mod) { long long t = a; int r = 1; while (p) { if (p & 1) r = ((long long)r * t) % mod; t = (t * t) % mod; p >>= 1; } return r; } int n, x; int a[100000]; int cnt[2 * 100000 + 1]; int m...
11
#include <bits/stdc++.h> using namespace std; const int maxN = 2e5 + 5; int a[maxN], pf[2010], x[maxN], y[maxN], m; int dp[2010], n, k; int solve(int id) { if (id < 0) return 0; int &r = dp[id]; if (~r) return r; r = solve(id - 1) + a[id]; for (int i = int(0); i < int(m); i++) { if (id - x[i] < -1) contin...
13
#include <bits/stdc++.h> using namespace std; int A[200]; bool has[200]; int main() { int N; while (cin >> N) { for (int i = 0; i < N; i++) cin >> A[i]; long long int maxi = 0; long long int cnt = 0; vector<int> ans, tmp; bool fs = true; for (int i = 0; i < N - 1; i++) for (int j = i +...
6
#include <bits/stdc++.h> using namespace std; int main() { long long x, y, z; while (cin >> x >> y >> z) { long long sum = (x + y) / z, ans = 0; if (x % z == 0 && y % z == 0) { cout << sum << ' ' << 0 << endl; continue; } else { long long a, b; a = x % z; b = y % z; i...
2
#include <bits/stdc++.h> std::mt19937 rng( (int)std::chrono::steady_clock::now().time_since_epoch().count()); int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int a, b; std::cin >> a >> b; if (a < b) { std::cout << "-1\n"; return 0; } if (a == b) { std::cout << a << ...
9
#include <bits/stdc++.h> using namespace std; int N, P[1000010], T, Q[1000010], X[1000010]; int main() { scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d", P + i); scanf("%d", &T); for (int i = 0; i < T; i++) { scanf("%d", Q + i); Q[i]--; } sort(Q, Q + T); memset(X, -1, sizeof X); for (int...
13
#include <bits/stdc++.h> using namespace std; int a[(1 << 22) + 10], n, m, pad; bool vis[(1 << 22) + 10], has[(1 << 22) + 10]; void dfs(int x) { vis[x] = 1; if (has[x]) { int rx = (~x) ^ pad; if (!vis[rx]) dfs(rx); } for (int i = n; i >= 0; --i) { if ((x >> i) & 1) { int nx = x ^ (1 << i); ...
17
#include <bits/stdc++.h> using namespace std; int main() { int n, k, aux, i, j; cin >> n; k = n * n; aux = n; i = 1; while (aux) { j = i; if (n % 2 == 0) j += n / 2; else j = j + n / 2 + 1; for (i; i < j; i++) cout << i << " " << k - i + 1 << " "; cout << "\n"; aux--; }...
2
// Author: Pawan Raj // Problem: E. Assimilation IV // Contest: Codeforces - Educational Codeforces Round 109 (Rated for Div. 2) // URL: https://codeforces.com/contest/1525/problem/E #include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define int long long #define F(i, n) for (int i = 0; i...
13
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int M = 1000010; const int inf = 0x3f3f3f3f; const long long oo = 0x3f3f3f3f3f3f3f3fll; const int mod = 1e9 + 9; int n, a[N]; bool vis[N]; void dfs(int u, queue<int> &que) { vis[u] = 1; que.push(u); if (vis[a[u]]) return; dfs(a[u], que); ...
18
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const int kMaxn = 55; int n, a[kMaxn], b[kMaxn]; pair<int, int> c[kMaxn]; int main() { static int i, j, x, y, ans; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d", a + i); c[i].first = a[i]; c[i].second = i; } sor...
12
#include <bits/stdc++.h> using namespace std; std::vector<long long> v(100001, 0); std::vector<long long> dp(100001, 0); int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; std::vector<long long> v(n, 0); for (int i = 0; i < n; ++i) { cin >> v[i]; }...
6
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const long long INF = 1e18; const int M = 3e5 + 5; int tr[M * 29][2]; long long dp[100][2]; int cnt; vector<int> g[M * 29]; void pn() { cout << "NO" << '\n'; } void py() { cout << "YES" << '\n'; } void add(int x, int pos)...
12
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, p; cin >> n >> p; int edge_cnt = (2 * n) + p, curr_dif = 1; set<pair<int, int> > s; while (1) { for (int i = 1; i <= n; i++) { int...
7
#include <bits/stdc++.h> using namespace std; int vec[1000005]; map<long long, int> M; int main() { int n; cin >> n; for (int i = 2; i <= 1000000; i++) { if (vec[i]) continue; for (int j = i; j <= 1000000; j += i) vec[j] = i; } for (int i = 1; i <= 1000000; i++) { M[1LL * i * i * i] = i; } for...
9
#include <bits/stdc++.h> using namespace std; const int N = int(5e5) + 10, M = int(5e5) + 10, mod = int(1e9) + 7; int n, a[M], lx[M], rx[M], q, lp[N + 2], last[N]; vector<pair<int, pair<int, int> > > t[4 * M]; vector<pair<int, int> > mx[4 * M]; void build(int v, int tl, int tr) { if (tl == tr) { t[v].push_back(ma...
16
#include<iostream> #include<bits/stdc++.h> using namespace std; #define ll long long #define int long long #define pb push_back #define mp make_pair #define ff first #define ss second #define min3(a, b, c) min(c, min(a, b)) #define min4(a, b, c, d) min(d, min(c, min(a, b))) #define max3(a, b, c) max(c, max(a, b)) #defi...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, y; cin >> n; set<int> x; for (int i = 0; i < n; ++i) { cin >> y; x.insert(-y); } for (;;) { bool b = false; for (int t = -(*x.begin()) / 2; t > 0; t /= 2) if (x.find(-t) == x.end()) { x.erase(x.begin()); ...
11
#include <bits/stdc++.h> using namespace std; string f(int i) { int s = 1; if (i < 1000) s = 0; int a[4]; a[0] = i % 10; i /= 10; a[1] = i % 10; i /= 10; a[2] = i % 10; i /= 10; a[3] = i % 10; i /= 10; if (!s) a[3] = 0; string t; if ((a[1] == a[0]) || (a[0] == a[2]) || (a[0] == a[3]) || (a[1...
9
#include <bits/stdc++.h> using namespace std; void solve(void) { int n; cin >> n; vector<pair<int, string>> ps(n), ans; for (auto &p : ps) { cin >> p.second >> p.first; } sort(ps.begin(), ps.end()); int h = 3000; for (const auto &p : ps) { auto pos = ans.begin() + p.first; if (pos > ans.end(...
10
#include <bits/stdc++.h> using namespace std; template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T parse(const string &s) { T x; stringstream ss(s); ss >> x; return x; } const double EPS = 1e-12; const int INF = 1000 * 1000 * 1000; const long long LINF = INF * 1ll * INF; const do...
13
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 32; int ans[N][2], cnt = 0; int G[10][10], rem[10], hd[10], on[10]; int n, d; void RD(int &res) { res = 0; char c; while (c = getchar(), c != '?') ; do { ++res; } while (c = getchar(), c == '?'); } bool HALL() { for (int S = 0; S < (1...
24
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, k, minn = INT_MAX, maxx = INT_MIN; cin >> n >> k; int pile[n]; for (int i = 0; i < n; i++) { cin >> pile[i]; minn = min(minn, pile[i]); maxx = max(maxx, pile[i]); } if ((maxx - minn) > k) co...
5
#include <bits/stdc++.h> using namespace std; const int N = 500005; int sz[N * 8]; int v1[N * 8]; int v2[N * 8]; int n; void pushup(int k) { sz[k] = sz[k * 2] + sz[k * 2 + 1]; v1[k] = max(v1[k * 2], sz[k * 2] + v1[k * 2 + 1]); v2[k] = min(v2[k * 2] + sz[k * 2 + 1], v2[k * 2 + 1]); } void build(int k, int l, int r...
22
#include <bits/stdc++.h> class Task { uint64_t *dp; public: Task(); uint64_t solve(int32_t x); uint64_t *count; static const int32_t MAX; }; Task::Task() { dp = new uint64_t[Task::MAX]; count = new uint64_t[Task::MAX]; std::fill(dp, dp + Task::MAX, 0); std::fill(count, count + Task::MAX, 0); dp[0]...
7
#include <bits/stdc++.h> using namespace std; long long ways[1001][1001]; long long len, ans = 0; char a[1001]; long long getWays(int pos, int dig) { if (pos == len - 1) return 1; if (!ways[pos][dig]) { int sum = dig + a[pos + 1] - '0'; if (sum % 2) ways[pos][dig] += getWays(pos + 1, sum / 2 + 1); ways[...
9
#include <bits/stdc++.h> using namespace std; void swap(long long &a, long long &b) { long long t = a; a = b; b = t; } int main() { long long a, b, ans = 0; cin >> a >> b; if (a > b) swap(a, b); while (a) { ans += b / a; b %= a; swap(a, b); } cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long N = 2 * 10000000 + 1; long long c[N]; int main() { long long a, n, ans = 0; cin >> a >> n; for (int i = 0; i < N; i++) c[i] = 1; for (int i = 2; i * i <= N; i++) for (int j = i * i; j <= N; j += i * i) c[j] = i * i; for (int i = a; i < a + n; i...
7
#include <bits/stdc++.h> using namespace std; long long n, m; int a[100005], b[100005]; long long fastPow(long long b, long long e) { long long ans = 1; while (e > 0) { if (e & 1) ans = (ans * b) % 1000000007; b = (b * b) % 1000000007; e >>= 1; } return ans; } int memo[100005][3]; int DP(int pos, in...
11
#include <bits/stdc++.h> using namespace std; const int N = 1500 * 1500 + 5; map<int, vector<pair<int, int>>> maping; int Next[N]; int dp[N]; int arr[1505]; int range[1505]; int size; void solve() { for (int i = size - 1; i >= 0; i--) { dp[i] = 0; if (i + 1 < size) dp[i] = max(dp[i], dp[i + 1]); if (Next[...
11
#include<bits/stdc++.h> using namespace std; #define max(a, b) (a < b ? b : a) #define min(a, b) ((a > b) ? b : a) #define mod 1e9 + 7 #define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++) #define FORL(a, b, c) for (int(a) = (b); (a) < (c); (a)++) #define FORR(a, b, c) for (int(a) = (b); (a) > (c); (a)--) #define...
0
#include <bits/stdc++.h> using namespace std; int a[5010]; int n; int main() { memset(a, 0, sizeof(a)); scanf("%d", &n); int x, y, z; printf("? 1 2\n"); fflush(stdout); scanf("%d", &x); printf("? 2 3\n"); fflush(stdout); scanf("%d", &y); printf("? 1 3\n"); fflush(stdout); scanf("%d", &z); a[2]...
6
#include <bits/stdc++.h> using namespace std; int n, a[200005], ans = 1; int main() { scanf("%d", &n); if (n % 2) cout << "YES" << endl; else { cout << "NO"; return 0; } for (int i = 1; i <= n; i++) { if (i % 2) { a[i] = ans++; a[i + n] = ans++; } else { a[i + n] = ans++;...
4
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); string second; cin >> second; int n = second.size(); vector<vector<int>> dp(n, vector<int>(n, -1)); vector<int> ans(n, 0); for (int i = 0; i < n; i++) { dp[i][i] = 1; for (int j = 1; i - j >= 0 && i + j <= ...
11
#include <bits/stdc++.h> using namespace std; map<long long, long long> mp; vector<long long> v, prs; long long lg(long long a) { long long res = 0; while (a > 1) { a = (a + 1) / 2; res++; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, x;...
8
#include <iostream> #include <cmath> using namespace std; int main(){ int t; cin>>t; while (t--){ int a, b; cin>>a>>b; b--; cout<<(b+(a%2)*b/(a/2))%a+1<<endl; } }
4
#include <bits/stdc++.h> using namespace std; const long long L = 24; long long A[500009]; vector<pair<long long, long long> > G[500009]; long long mat[25], te[25]; long long ans[500009]; void add(long long v, long long t) { long long i; for (i = L - 1; i >= 0; i--) { if (1 & (v >> i)) { if (mat[i]) { ...
17
#include <bits/stdc++.h> using namespace std; long long int C[300001]; vector<long long int> A; vector<pair<long long int, long long int> > B; int main() { int m; cin >> m; for (int i = 0; i < m; i++) { long long int x; cin >> x; A.push_back(x); } for (int i = 0; i < m; i++) { long long int x;...
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<pair<int, int>> a; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a.push_back(make_pair(y, x)); } int c = 1, ans = 0; sort(a.begin(), a.end(), greater...
3
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 1e6 + 7; int main() { int t, n; string s; cin >> s; map<string, int> q; int len = s.size(); int ans = 0; for (int i = 0; i < len; i++) { string str; for (int j = i; j < len; j++) str += s[j]; for (int j = 0...
1
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int a, b, p; cin >> a >> b >> p; string str; cin >> str; long long int n = str.size(); if (a > p && b > p) { cout << n << endl; continue; } long long int i; ...
5
#include <bits/stdc++.h> using namespace std; const int N = 2e3; const int INF = 1e9 + 9; const int B = 1e9 + 7; int n, m; long long z; struct DSU { int p[N * N], sz[N * N], n, cmp; void init(int _n) { n = _n; for (int i = int(1); i <= int(n); ++i) p[i] = i, sz[i] = 1; cmp = n; } int f(int x) { retu...
12