solution
stringlengths
52
181k
difficulty
int64
0
6
#include<cstdio> #include<algorithm> #define LL long long using namespace std; const int maxn = 3e2 + 5; int mod, n, k; LL dp[maxn][maxn][maxn], ans; int main() { scanf("%d%d%d", &n, &k, &mod); dp[0][1][0] = 1; for(register int i = 0; i <= n; ++i) for(register int j = 1; j <= k; ++j) for(register int p = i;...
0
#include <bits/stdc++.h> using namespace std; const int MAX = 123; unordered_map<string, int> mm; struct node { long long w, h, ty, b, s, id; int con[MAX]; node() { memset(con, 0, sizeof(con)); w = h = b = s = 0; } void maintain(); }; node ns[MAX]; int cnt, n; inline void node::maintain() { if (ty =...
2
#include <bits/stdc++.h> using namespace std; int main(){ int n,k; cin>>n>>k; vector<string> s(n); for(auto &a:s){ cin>>a; } sort(s.begin(),s.end()); for(auto a:s){ cout<<a; } }
0
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& num) { num = 0; bool f = true; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = false; ch = getchar(); } while (ch >= '0' && ch <= '9') { num = num * 10 + ch - '0'; ch = getchar(); } ...
3
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; if (a % 2 == 0) cout << "Mahmoud"; else cout << "Ehab"; return 0; }
1
#include <string> #include <iostream> #include <math.h> #include <algorithm> #include <vector> #include <iomanip> #include <map> #include <set> typedef long long ll; const int mod=1e9+7; #define lol(i,n) for(int i=0;i<n;i++) using namespace std; int h,w,x,y; int main() { cin >>h>>w>>x>>y; if((h*w)%2==1&&(x+y)%...
0
#include <bits/stdc++.h> using namespace std; int n; vector<int> g[200000]; long long int a[200000]; long long int m = LLONG_MIN; long long int sz[200000]; long long int mx[200000]; void dfs(int cur, int pr) { sz[cur] = a[cur]; mx[cur] = LLONG_MIN; vector<long long int> q; for (int i = 0; i < g[cur].size(); i++...
4
#include<bits/stdc++.h> #define fi first #define se second #define INF 100000000000000000LL #define ll long long #define debug(x) cout << #x << " "<< x <<endl; #define debug_vector(v) cout << #v << " "; _print(v); const ll inf =1e9+44; const int MAX=3e5+9; const ll MOD= 1e9+7; const double eps=1e-10; double const ...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long int item[n], client[n]; long long int total = 0; vector<pair<long long int, long long int> > extra; for (int i = 0; i < n; i++) { cin >> item[i] >> client[i]; if (item[i] == 0 || client[i] == 0 || client[i]...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; int n, k, d, a[maxn], dp[maxn]; int main() { cin >> n >> k >> d; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a, a + 1 + n); dp[0] = 1; int j = 1; for (int i = 1; i <= n; i++) { while (j < i && (a[i] - a[j] > d || dp[j - 1] == 0)...
5
#include <bits/stdc++.h> using namespace std; struct pt { int x, y; pt(int _x = 0, int _y = 0) : x(_x), y(_y) {} pt operator-(const pt &p2) const { return pt(x - p2.x, y - p2.y); } int dist2() const { return x * x + y * y; } }; const int MAXN = 1000; int todel[MAXN]; int es[MAXN][MAXN]; int ecnt[MAXN]; int n; b...
4
#include <iostream> #include <algorithm> class Matryoshika{ public: int h, r; bool operator<(const Matryoshika& m)const{ return h < m.h; } bool operator>(const Matryoshika& m)const{ return h > m.h; } bool operator<<(const Matryoshika& m)const{ return h < m.h && r < m.r; } bool operator>>(const Mat...
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, x, ans = 1, temp = 0; cin >> n >> x; if (n == 1 || n == 2) { cout << ans << endl; continue; } n = n - 2; if (n < x) { cout << ans + 1 << endl; continue; } ans = ...
1
#include <bits/stdc++.h> using namespace std; int n; string s; long long dp[200011]; const long long Mod = 1e9 + 7; long long f[200011][3]; long long power(long long x, long long n) { if (n < 0) return 0; if (n == 0) return 1; if (n % 2 == 0) return power(x * x % Mod, n / 2); return x * power(x * x % Mod, n / 2...
6
#include <bits/stdc++.h> using namespace std; string str(char c) { return string(1, c); } int main() { string s; cin >> s; int start = 0; if (s.size() % 2 == 0) start = s.size() / 2 - 1; else start = s.size() / 2; int i = start - 1; int j = start + 1; string ans = str(s[start]); while (i >= 0 ...
1
#include <bits/stdc++.h> using namespace std; long long n, a, b; int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> a >> b; cout << min((n + a) - b, min(min(a, b), n)) + 1; }
2
#include <bits/stdc++.h> using namespace std; struct Node { int x; int y; Node(int x = 0, int y = 0) : x(x), y(y) {} int Level() const { return max(x, y); } bool operator<(const Node &ant) const { int l1 = Level(); int l2 = ant.Level(); if (l1 != l2) return l1 < l2; if (x != ant.x) { ret...
6
#include<bits/stdc++.h> using namespace std; int main() { set<int> s; int n; scanf("%d",&n); for(int i=0;i<n;i++) { int t; scanf("%d",&t); s.insert(t); } scanf("%d",&n); int ans=0; for(int i=0;i<n;i++) { int t; scanf("%d",&t); ans+=s.count(t); } printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k, s = 0, a; int sg(int x) { if (x == 0 || x == 1) return x; if (k % 2 == 0) { if (x == 2) return 2; if (x % 2 == 0) return 1; else return 0; } else { if (x == 2) return 0; if (x == 3) return 1; if (x == 4) return 2; if (...
3
#include <bits/stdc++.h> using namespace std; typedef map<int, int>::iterator iMII; int n; vector<int> V; vector<vector<int> > M; vector<int> primers; vector<int> sobren; int main() { cin >> n; V = vector<int>(n + 3, -1); M = vector<vector<int> >(n + 3); for (int i = 2; i <= n / 2; ++i) if (V[i] == -1) { ...
5
#include <bits/stdc++.h> using namespace std; const int inv = numeric_limits<int>::max(); const int minv = numeric_limits<int>::min(); const int max_n = 5010; int n; long long A[max_n]; int P[max_n]; int dp[max_n]; bool cm(int j, int i) { if (A[j] % A[i]) return false; if (i - j <= P[i]) return (P[i] - P[j] == i - ...
4
#include <bits/stdc++.h> using namespace std; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } int main() { map<pair<int, int>, pair<int, int> > ob; int n; scanf("%d", &n); double r1 = -...
4
#include <bits/stdc++.h> const double PI = acosl(-1.0); int bits[1 << 23]; void pre(int n) { int LOG = 0; while (1 << (LOG + 1) < n) { LOG++; } for (int i = 1; i < n; i++) { bits[i] = (bits[i >> 1] >> 1) | ((i & 1) << LOG); } } std::vector<std::complex<double> > fft(std::vector<std::complex<double> > ...
4
#include <bits/stdc++.h> using namespace std; struct s_ix { long long ix; long long val; }; bool operator<(s_ix aa, s_ix bb) { return aa.val - aa.ix < bb.val - bb.ix; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long T; cin >> T; while (T--) { long long n, k, sum, nbw, maxnbw; cin >> n...
3
#include <iostream> #include <climits> #include <algorithm> #include <vector> using ll = long long; int main(){ int t{0}; std::cin >> t; std::vector<ll> results; while(t--){ int n{0}; std::cin >> n; std::vector<int> arr(n); for(auto &val: arr) std::cin >> val; ll maxCost = 0; for(int i{1}; i < n; i++...
2
#include <bits/stdc++.h> const long long mod = 998244353; const int N = 2e5 + 100, LG = 20; std::vector<int> g[2][N]; long long answ = 0; long long dp[N][LG][2]; long long d[N][2]; long long n, m; long long INF; long long get(long long x) { return (1LL << x) - 1; } struct pos { long long cost; int i; int t; fri...
3
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; const double pi = 3.1415926535897932384626433832795; const double eln = 2.718281828459045235360287471352; const long long mod = 1e9 + 7; long long sum[5005]; bool b[27]; long long c[5005]; char s[5005], s1[5005]; long long dp[5005][27]; int i, j, k...
4
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<ll,ll> P; const double EPS = 1e-10; const ll INF = 100000000; const ll MOD = 1000000007; ll n, m, k; ll kai[1000001]; ll p3[1000001]; ll dp[1000001]; void init (int _n) { kai[0] = 1; p...
0
#include <bits/stdc++.h> using namespace std; void solve() { int n, x; cin >> n >> x; vector<int> a(n), b(n); for (auto &x : a) cin >> x; for (auto &x : b) cin >> x; sort(a.begin(), a.end()); sort(b.begin(), b.end(), greater<int>()); for (int i = 0; i < n; i++) { if (a[i] + b[i] > x) { cout <<...
1
#include <bits/stdc++.h> using namespace std; const int OO = (int)1e9; const double eps = 1e-10; const int MX = 1e5 + 5; int n, m, k, s, t, d, w; long long mem[MX][205]; vector<vector<pair<int, int> > > add(MX), del(MX); vector<pair<int, int> > next_move(MX); multiset<pair<int, int> > active; long long dp(int idx, int ...
5
#include <bits/stdc++.h> using namespace std; const int MAXM = 100; int N, K; int sx, sy; bool vis[MAXM][MAXM]; int cal(int x, int yl, int yr) { int i, j = 0; for (i = yl; i <= yr; i++) j += abs(x - sx) + abs(i - sy); return j; } void check(int x) { int i, j, k, pre, mins = -1, l, r, row; for (i = 1; i <= K; ...
2
#include <bits/stdc++.h> using namespace std; int main(){ list<int> l; list<int>::iterator it=l.end(); int q; cin>>q; while(q--){ int y; cin>>y; if(y==0){ int x; cin>>x; it=l.insert(it,x); } else if(y==1){ int d...
0
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 5; const int M = 1e9 + 7; int pa[N << 2], lpa[N << 2]; int a[N]; long long oa[N << 2], loa[N << 2]; int invprime[70]; int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79...
6
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<vector> #include<map> #include<set> #include<string> #include<queue> #include<stack> #include<complex> #include<numeric> using namespace std; #define MOD 1000000007 #define MOD2 998244353 #define PRIM 3 #define INF (1<<29) #define LINF (1L...
0
#include <bits/stdc++.h> #define pb push_back #define F first #define S second #define all(x) x.begin(), x.end() #define debug(x) cerr << #x << " : " << x << '\n' using namespace std; typedef long long ll; typedef long double ld; typedef string str; typedef pair<ll, ll> pll; const ll Mod = 1000000007LL; const int N...
0
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const int INF = 2e9; const long long LINF = 1e18 + 5; const long long MOD = 1e9 + 7; const int MAXN = 1e5 + 5; const int MN = 1e6 + 2; double x[1001]; double y[1001]; vector<pair<pair<double, double>, double> > v; int le[MN]; int ri[MN]; bool eq(...
5
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int maxn = 2e3 + 5; char s[maxn]; int vis[maxn]; vector<pair<int, int>> g[maxn]; void add(int u, int v, int c) { g[u].push_back(make_pair(v, c)); g[v].push_back(make_pair(u, c)); } int check(int u) { if (vis[u] == -1) vis[u] = 0; for...
4
#include <bits/stdc++.h> using namespace std; int n, m, a[110], b[110]; void getans(int n, int a[110]) { if (n == 1) a[1] = 1; else if (n == 2) a[1] = 3, a[2] = 4; else if (n % 2) { a[1] = 2; for (int i = 2; i < n; i++) a[i] = 1; a[n] = (n + 1) / 2; } else { for (int i = 1; i < n; i++) a...
3
#include <bits/stdc++.h> using namespace std; int a[105][105], u[105], v[105]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &a[i][j]); for (int j = 0; j < m; j++) { int cnt[105] = {}; for (int i = 0; i < n; i++) if (...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100 * 1000 + 100; int n, q; long long sum[maxn]; int a[maxn]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n, greater<int>()); for (int i = 1; i < n + 1; ++i) sum[i] = sum[i - 1] + a[i - ...
2
#include <bits/stdc++.h> using namespace std; struct node { int u, v, l, r; } que[105000]; map<pair<int, int>, int> mp; int n, q; int fa[105000]; int val[105000]; int dep[105000]; int tot, cnt; int save[105000 << 1]; int getfa(int x) { if (x == fa[x]) return x; return getfa(fa[x]); } int getval(int x) { int ans...
6
#include <bits/stdc++.h> using namespace std; int n, ans; int cnt[100001] = {1}, dep[100001]; int h[100001], nxt[100001], to[100001], tot; inline void ins(int x, int y) { nxt[++tot] = h[x]; to[tot] = y; h[x] = tot; } void DFS(int u) { for (int i = h[u]; i; i = nxt[i]) { dep[to[i]] = dep[u] + 1; ++cnt[de...
1
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long ans[4][410000]; long long mul_inv(long long a, long long b = mod) { long long b0 = b, t, q; long long x0 = 0, x1 = 1; if (b == 1) return 1; while (a > 1) { q = a / b; t = b, b = a % b, a = t; t = x0, x0 = x1 - q * x0...
2
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 28; const double INF = 1e12, EPS = 1e-9; const int mod = (int)1e9 + 7; int dp[701][701][3][3]; string in; int n, p[701]; int rec(int l, int r, int fl, int fr) { int &res = dp[l][r][fl][fr]; if (res >= 0) return res; if (l >= r) return res = 1; r...
4
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using ld = long double; const int N = 100228; const int V = 7001; using BS = bitset<V>; BS a[N]; BS b[V]; BS c[V]; bool bad[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(io...
6
#include <bits/stdc++.h> using namespace std; struct node { int l, r, pos, id; bool operator<(const node &a) const { if (pos == a.pos) return r < a.r; return pos < a.pos; } } ask[200005]; int a[200005], s[200005 << 3]; long long res, ans[200005]; int n, m, B; void cha(int p, int k) { res = res + 1ll * 2...
4
#include <bits/stdc++.h> #pragma GCC optimize("O3") const long long MOD = 1e9 + 7; using namespace std; long long n_bits(long long n) { long long x = __builtin_popcount(n); return x; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); double x, l, h, n; cin >> h >> l; long long tem...
2
#include <bits/stdc++.h> using namespace std; const int N = 30; int root[N], sz[N], n; vector<int> ans[30]; string a, b; int getroot(int u) { if (u == root[u]) return u; return root[u] = getroot(root[u]); } void mrg(int u, int v) { int ru = getroot(u); int rv = getroot(v); if (ru == rv) return; if (sz[rv] >...
4
#include <bits/stdc++.h> using namespace std; void task1() { int n; string s, t; cin >> n >> s >> t; int pos = n - 1; auto flip = [&](int r) { for (int i = 0; i <= r; ++i) { if (s[i] == '1') s[i] = '0'; else s[i] = '1'; } reverse(s.begin(), s.begin() + r + 1); }; ve...
3
#include <bits/stdc++.h> using namespace std; int parent(int x, vector<int> &v) { return v[x] = (v[x] == x ? x : parent(v[x], v)); } void u(int x, int y, vector<int> &v, vector<int> &r) { x = parent(x, v); y = parent(y, v); if (r[x] == r[y]) { r[x]++; } if (r[x] > r[y]) { v[y] = x; } else { v[...
4
#include <cstdio> #include <algorithm> #include <vector> #include <set> #include <cstdlib> #include <utility> #include <cmath> #include <queue> #include <stack> #include <cstring> using namespace std; #define ll long long #ifndef ONLINE_JUDGE #define debug(format, ...) fprintf(stderr, \ "%s:%d: " format "\n", __...
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; int prime[MAXN + 1]; void getPrime() { memset(prime, 0, sizeof(prime)); for (int i = 2; i <= MAXN; i++) { if (!prime[i]) prime[++prime[0]] = i; for (int j = 1; j <= prime[0] && prime[j] <= MAXN / i; j++) { prime[prime[j] * i] = 1;...
2
#include <bits/stdc++.h> using namespace std; int n; int righ[300010], Left[300010]; struct node { int x, y; } ddd[300010]; void zser() { int l = righ[n - 1], r = Left[0]; int ans = r - l; if (ans < 0) ans = 0; for (int i = 0; i < n; i++) { l = righ[n - 1], r = Left[0]; if (ddd[i].x == l && ddd[i].y =...
3
#include <bits/stdc++.h> using namespace std; template <typename T> T mymax(T a, T b) { return (a > b) ? a : b; } template <typename T> T mymin(T a, T b) { return (a < b) ? a : b; } template <typename T> T myabs(T a) { return (a < 0) ? (-1 * a) : a; } template <typename T> T mysqrt(T x) { if (x == 0 | x == 1) r...
2
#include <bits/stdc++.h> using namespace std; long long a[1000001]; map<long long, long long> M1; map<long long, long long> M2; map<long long, long long>::iterator it; long long F1[1000001]; long long F2[1000001]; long long BIT[1000001]; void update(int u, long long k, int n) { while (u <= n) { BIT[u] += k; u...
4
#include <bits/stdc++.h> using namespace std; const int inv = numeric_limits<int>::max(); const int minv = numeric_limits<int>::min(); const int max_n = 100; int n; long long a[max_n]; int nsol[2]; long long sol[2][max_n]; void solve(long long p, long long q) { sol[0][nsol[0]++] = p / q; if (q != 1) solve(q, p % q)...
2
#include <bits/stdc++.h> using namespace std; int n, a[100005], mil = -1000000000, mir = -1000000000, mal = 1000000000, mar = 1000000000, mini, maxi; string b; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cin >> b; for (int i = 4; i < n; i++) {...
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int m, n; cin >> m >> n; vector<vector<bool>> st(m, vector<bool>(n)); for (int i = 0; i < m; ++i) { int t; cin >> t; while (t--) { int s; cin >> s; st[i][s - 1] = true; ...
5
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, k; int vis[N]; int pos[N]; long long a[N]; long long ABS(long long x) { return x < 0 ? -x : x; } int get(int len, long long down, long long up, int t) { if (t == 1) { long long be = -len / 2 + len; if (be < up) return -len / 2; ...
5
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_bac...
0
#include<iostream> using namespace std; int main(){ int m,d; int M[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; while(cin>>m>>d){ if(m==0)break; int day=2; for(int i=m-1;i>0;i--){ day+=M[i]; } day+=d; if(day%7==0)cout<<"Monday"<<endl; if(day%7==1)cout<<"Tuesday"<<endl; if(day...
0
#include <bits/stdc++.h> using namespace std; const int N = 1010; int n, a[N], A[N][N]; string s; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { cin >> s; for (int j = 0; j < n; ++j) { if (s[j] == '1') A[i][j + 1] = 1; else A[i][...
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; for (int i = 0; i < s.size(); ++i) if (s[i] - '0' == 8 && s.size() - i >= 11) { cout << "YES\n"; ...
1
#include <bits/stdc++.h> using namespace std; int n; long double fat[51], memo[51]; long double num_good_permutations(int x) { if (x > n) return 1; if (memo[x] != -1) return memo[x]; long double ret = 0; for (int len = 1; x + len - 1 <= n; len++) { if (len == 1) ret += num_good_permutations(x + len); ...
5
#include <bits/stdc++.h> const int N = 505; int n, k, a[N][N], cnt, Sum; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) for (int j = 1; j < k; j++) a[i][j] = ++cnt; for (int i = 1; i <= n; i++) for (int j = k; j <= n; j++) a[i][j] = ++cnt; for (int i = 1; i <= n; i++) Sum += a[i][k]; ...
3
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> bool Max(T1 &a, T2 b) { return a < b ? a = b, 1 : 0; } template <typename T1, typename T2> bool Min(T1 &a, T2 b) { return a > b ? a = b, 1 : 0; } template <int MOD> struct ModInt { unsigned x; ModInt() : x(0) {} ModInt(signed si...
4
#include <stdio.h> #include <algorithm> #include <map> using namespace std; int n , k; int a[120000] , b[120000] , c[120000] , tot; void work () { int i , j , l , ti; scanf ( "%d%d" , &n , &k ); if ( 2 * k - 1 > n ) { printf ( "-1\n" ); return ; } for ( i = 1 , j = k ; j <= k + n - 1 ; i++ , j += 2 ) { a[i...
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 10; struct node { int x = 0; int more = 0; int l = -1; int r = -1; node() {} }; node pool[4 * MAXN]; int pt = 0; int build(int L, int R) { node A; if (L == R - 1) { pool[pt] = A; pt++; return pt - 1; } int M = (L + R) / 2...
5
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } char ch[10]; int main()...
6
#include <bits/stdc++.h> using namespace std; int n; int a[312345]; map<int, int> last; long long cumSum[312345]; int main() { ios_base::sync_with_stdio(0); cin >> n; vector<int> toRemove; long long totalSum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; last[a[i]] = i; totalSum += (a[i] >= 0) *...
1
#include <bits/stdc++.h> using namespace std; queue<int> que; struct str { int v, nex, cap, cost; } edge[101000]; int e, n, m, s, t; int f[210], vis[210], fst[210], h[210], dis[210], mark[210]; int ans, flow; void make_edge(int a, int b, int cap, int cost) { edge[++e].nex = fst[a]; fst[a] = e; edge[e].v = b; ...
4
#include <bits/stdc++.h> using namespace std; const int N = 500100; int a[N]; int b[N]; int c[N]; bool vis[N]; int mabs(int x) { return x >= 0 ? x : -x; } int main() { int n; int cnt = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] != 0) cnt++; } if (cnt == 0) { p...
4
#include <bits/stdc++.h> using namespace std; struct fenvick { vector<bool> got; vector<long long> fen; fenvick() {} fenvick(int n) { fen.resize(n); got.resize(n); } void upd(int pos) { if (got[pos] == true) { return; } got[pos] = true; while (pos < fen.size()) { fen[pos]...
6
#include <bits/stdc++.h> using namespace std; int check(int k) { int ans; if (k <= 9) { ans = k; } else if (k < 190) { k = k - 9; int g = (k - 1) / 2 + 10; if (k % 2 == 0) ans = g % 10; else ans = g / 10; } else if (k < 2890) { k = k - 189; int g = (k - 1) / 3 + 100; ...
1
#include <bits/stdc++.h> using namespace std; long long n, k, A, B; const int maxn = 1e5 + 5; long long a[maxn]; long long rec(long long l, long long r) { long long x = upper_bound(a + 1, a + k + 1, r) - lower_bound(a + 1, a + k + 1, l), mid = (l + r) / 2; if (l == r) return x == 0 ? A :...
3
#include<bits/stdc++.h> using namespace std; #define li long int #define lli long long int #define ld long double void solve(){ li n; cin>>n; string s; cin>>s; if(s[n-4]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){ cout<<"YES\n"; return; } if(s[0]=='2'&&s[n-3]=='0'&&s[n-2]=...
2
#include <bits/stdc++.h> using namespace std; char ch[20]; int n; bool found; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return ...
4
#include <bits/stdc++.h> using namespace std; template <class T, class second> ostream& operator<<(ostream& s, pair<T, second>& p) { s << '{' << p.first << ", " << p.second << '}'; return s; } template <class T> ostream& operator<<(ostream& s, vector<T>& v) { if (v.empty()) { s << "[]"; return s; } s ...
6
#include <iostream> #include <set> using namespace std; int main() { int n; cin >> n; set<int> a_set; for (int i = 0; i < n; i++) { int a; cin >> a; a_set.insert(a); } cout << (a_set.size() - (a_set.size() % 2 == 0 ? 1 : 0)) << endl; }
0
#include <iostream> #include <string> #include <vector> #include <tuple> using namespace std; typedef long long ll; #define rep(i, n) for(int i=0; i<(n); ++i) const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; int H, W; ll L; vector<string> maze; vector<vector<vector<ll> > > vis; inline bool inrect(int x, int y)...
0
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = ge...
4
#include <bits/stdc++.h> using namespace std; const int nax = 1e6 + 5; const int inf = 1e9 + 5; int t[nax], res[nax]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &t[i]); int worst = 0; for (int i = 1; i <= n; ++i) { worst = min(worst, t[i] - i); res[i] = i + worst; ...
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; int n, k; long long a[1000010]; vector<vector<int>> inds(1000010); long long modvals[1000010]; multiset<long long> curin; long long myans[1000010]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for (in...
6
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; ll n, m; vector<vector<bool>> ok; bool dfs(ll i = 0, ll j = 0) { if (i >= n || j >= m || !ok[i][j]) return false; if (i == n - 1 && j == m - 1) return true; ok[i][j] = false; if (dfs(i + 1, j)) return true; if (dfs(i, ...
4
#include<iostream> #include<algorithm> int ans; int x, y; int main() { std::cin >> x >> y; if (y < x)y = -y,ans++; ans += std::min(std::abs(y - x), std::abs(x + y) + 1); std::cout << ans << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; priority_queue<long long> v[64]; int findSize(long long a) { int ret = 0; while (a != 0) { a /= 2; ret++; } return ret; } int first0(long long a) { int size = findSize(a); for (int i = size - 1; i >= 0; i--) { if ((((long long)1 << i) & a) == ...
3
#include <bits/stdc++.h> const int maxn = 2005; using namespace std; int dp[maxn][maxn], n; int gcd(int a, int b) { if (a > b) return b ? gcd(b, a % b) : a; else return a ? gcd(b % a, a) : b; } int main() { scanf("%d", &n); int ceng = 0, sum = 0; for (int i = 1; i <= n; i++) { scanf("%d", &dp[n][i...
1
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } sort(a.begin(), a.end()); int cur = 1; for (int x : a) { if (x >= cur) { ++cur; } } printf("%d\n", cur); return 0; }
2
#include <bits/stdc++.h> using namespace std; void RAJ_MEHTA_IS_PRO() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int x, first; cin >> x >> first; long long int sum = 0; for (int i = 0; i < n; i++) sum += (a[i] + first - 1) / (x + first); cout << sum * first << "\n"; } int ma...
6
#include <bits/stdc++.h> using namespace std; int const MAXN = 505; int g[MAXN][MAXN], n, m, q, I, J; int main() { scanf("%d %d %d", &n, &m, &q); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &g[i][j]); for (int i = 0; i < q; i++) { scanf(" %d %d", &I, &J); if (g[I][J] == 1) ...
2
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j = 0, x = 0, y = 0; cin >> n; int a[n]; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { x = i; break; } } for (i = x; i < n - 1; i++) { if (a[i] < a[i + 1]) { y = i...
2
#include<bits/stdc++.h> using namespace std; #define int long long int a[2000]; signed main(){ int n,k; cin >> n >> k; for(int i = 1; i <= n; i++){ cin >> a[i]; a[i] += a[i-1]; } vector<int> vec; for(int i = 0; i < n; i++){ for(int j = i+1; j <= n; j++){ vec.push_back(a[j] - a[i]); ...
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, i; cin >> n; long long A[n + 5], max[n]; for (i = 0; i < n; i++) cin >> A[i]; if (A[0] != 0) cout << 1; else { max[0] = A[0]; for (i = 1; i < n; i++) { if (A[i] > max[i - 1]) max[i] = A[i]; else m...
2
#include <bits/stdc++.h> using namespace std; int arr[100], per[100], i, j, ac, n, tot; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); tot += arr[i]; } if (tot % 2 == 0) { sort(arr, arr + n); for (i = 0; i < n; i++) { if (arr[i] % 2 != 0) { tot -= arr...
2
#include <algorithm> #include <cstring> #include <deque> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; using ll = long long; struct Trie { Trie *child[2]; Trie() { child[0] = child[1] = nullptr; } void insert(const string &s) { Tr...
0
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline int read() { int x = 0; char ch = getchar(); bool positive = 1; for ...
4
#include <bits/stdc++.h> using namespace std; const int N = 100005; const long long inf = 0x7fffffffffffffff; long long cost[N][3]; vector<int> G[N]; int fa[N]; int son[N]; int degree[N]; void dfs(int v, int f) { fa[v] = f; int len = G[v].size(); for (int i = 0; i < len; i++) { int u = G[v][i]; if (u != f...
4
#include <bits/stdc++.h> using namespace std; struct tr { int x; struct tr* left; struct tr* right; }; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; set<int> s; map<int, int> left; map<int, int> right; int x; cin >> x; s.insert(x); for (int i = 1; i < n; ++i) { ci...
4
#include <bits/stdc++.h> namespace ZDY { #pragma GCC optimize(3) template <class T> __inline__ __attribute__((always_inline)) T ABS(T x) { return x > 0 ? x : -x; } template <class T> __inline__ __attribute__((always_inline)) T MAX(T x, T y) { return x > y ? x : y; } template <class T> __inline__ __attribute__((alwa...
5
#include<iostream> #include<string> using namespace std; int main() { string s; cin >> s; int c = s[0] - '0'; for (int i = 1; i < s.size(); i++) { if (s[i] != '9') { c--; break; } } cout << c + 9 * (s.size() - 1) << endl; return 0; }
0