solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; inline int read() { char c = getchar(); int s = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar(); return s * f; } void write(int x) { if (x < 0) { putchar...
1
#include <bits/stdc++.h> using namespace std; const int N = 610; int V, e, n, m; struct E { int v, w; }; vector<E> G[N]; int a[N], lk[210][N]; int mindis[N][N]; int vis[N], mat[N], T; int dfs(int u) { for (int v = 1; v <= V; ++v) { if (lk[u][v] && vis[v] != T) { vis[v] = T; if (!mat[v] || dfs(mat[v]...
13
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pair<int, int> > a(4 * n + 1); set<int> x; set<int> y; for (auto &i : a) { cin >> i.first >> i.second; x.insert(i.first); y.insert(i.second); } for (...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 200001; int n, m, a[MAXN], c[MAXN], f[MAXN], q[MAXN]; pair<int, int> b[MAXN], d[MAXN + 1]; bool check(int cnt_day) { memset(c, 0, sizeof(c)); memset(f, 0, sizeof(f)); memcpy(q, a, sizeof(a)); for (int i = 0; i <= MAXN; ++i) d[i].first = 0, d[i].seco...
12
#include <bits/stdc++.h> using namespace std; const int MAXN = 300005; struct Edge { int to, id; }; int N, K, D; queue<int> Q; vector<Edge> G[MAXN]; bool vis[MAXN], used[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> K >> D; for (int i = 0; i < K; i++) { int x; cin >> x; ...
13
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, j, k, l, cnt; map<char, long long int> mp; map<char, long long int>::iterator it; cin >> n >> k; string s; cin >> s; char c; char c1 = '0'; for (i = 0; i < s.size(); i++) { mp[s[i]]++; } for (it = mp.begin(); it != ...
4
#include <bits/stdc++.h> using namespace std; string s; string s1[] = {"no", "great", "don t think so", "no", "no", "don t touch me", "not bad", "cool", "no", "no"}; string s2[] = {"no", "don t even", "terrible", "worse", "are you serio...
11
#include<bits/stdc++.h> using namespace std; #define ll long long int main(){ ll t; cin>>t; while(t--){ ll n,d,x,maxa=-1,y,z,flag=0; cin>>n>>d; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; if(d==1) { for(int i=0;i<n;i++) cout<<"YES"<<endl; continue; } if(d==2) { for(int ...
3
#include <bits/stdc++.h> const int N = 8 + 7; using namespace std; int n, k; double A; int p[N], lvl[N]; double maax = 0; vector<int> o; void solve(vector<int> v) { double ans = 0.0; for (int i = 0; i < (1 << n); i++) { double ret = 1, suum = 0, cnt = 0; for (int j = 0; j < n; j++) { if (i & (1 << j))...
10
#include <bits/stdc++.h> using namespace std; int main() { char a[1000001], b[1000001]; cin >> a >> b; if (strcmp(a, b) == 0 || (strlen(a) == strlen(b) && strchr(a, '1') != NULL && strchr(b, '1') != NULL)) { cout << "YES\n"; } else { cout << "NO\n"; } return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; long long n, k, p1, p2, x, q, ans1, ans2, y, l, m, temp, ans; long long minn = INT_MAX, temp1, temp2, maxx = INT_MIN, num1, num2, num, sum; int main() { q = 1; cin >> q; while (q--) { int a, b, c, d; cin >> a >> b >> c >> d; i...
5
#include <bits/stdc++.h> using namespace std; int n, r, l, poz; int main() { cin >> n >> poz >> l >> r; if (l == 1 and r == n) cout << 0; else if (l == 1) cout << abs(r - poz) + 1; else if (r == n) cout << abs(l - poz) + 1; else { cout << min(abs(r - poz), abs(l - poz)) + 2 + r - l; } }
5
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; int main() { int n, k; cin >> n >> k; int cnt2, cnt5; cnt2 = cnt5 = 0; while (n % 2 == 0) { n /= 2; cnt2++; } while (n % 5 == 0) { n /= 5; cnt5++; } while (cnt2 > k) { n *=...
3
#include <bits/stdc++.h> using namespace std; int n, m; int a[100010]; int c[100010]; long long g[100010]; long long f[100010]; void dp(int l, int r, int ll, int rr, long long w) { if (l > r) return; int mid = (l + r) >> 1; int k, p = min(mid, rr); for (int i = l; i <= mid; ++i) { w += c[a[i]]; c[a[i]]+...
17
#include <bits/stdc++.h> using namespace std; inline int gi() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return f * x; } inline long long gl() { long long f = 1, x = 0...
17
#include <bits/stdc++.h> using namespace std; int l[300100], r[300100], prr[300100], prl[300100], sul[300100], sur[300100], n; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d %d", &l[i], &r[i]); prl[0] = sul[n] = 0; prr[0] = sur[n] = 1073741824; for (int i = 0; i < n; ++i) { prl[i + 1]...
8
#include <bits/stdc++.h> using namespace std; int main() { int num[56] = {}, ans[56] = {}, tmp[56]; const int shuf[] = {1, 3, 4, 2, 0}; string s; cin >> s; for (int i = 0; i < 5; ++i) { num[i] = s[shuf[i]] - '0'; } ans[0] = 1; for (int ks = 0; ks < 5; ++ks) { memset(tmp, 0, sizeof(tmp)); for...
6
#include <bits/stdc++.h> using namespace std; template <class T> inline bool getmin(T *a, const T &b) { if (b < *a) { *a = b; return true; } return false; } template <class T> inline bool getmax(T *a, const T &b) { if (b > *a) { *a = b; return true; } return false; } template <class T> inlin...
9
#include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, int b, int n) { long long int rm = 1; while (b) { if (b % 2) { rm = (rm * a) % n; } a = (a * a) % n; b /= 2; } return rm; } long long int h, m, h1, m1, h2, m2; long long int m3; int k, length; int leng(long...
19
#include <bits/stdc++.h> using namespace std; int n; const int maxn = 302; int p[maxn + 20][maxn + 20]; int ans[maxn]; int main(void) { scanf("%d", &n); for (int i = 0; i < n + 1; i++) for (int j = 0; j < n; j++) { int tmp; scanf("%d", &tmp); p[i][tmp] = j; } for (int i = 1; i <= n; i++)...
10
#include <bits/stdc++.h> using namespace std; int a[100005], p[100005]; int dp[100005]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) p[a[i]] = i; dp[1] = 1; for (int i = 2; i <= n; i++) { if (p[i] > p[i - 1]) dp[i] = dp[i - 1] +...
8
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> a; for (int i = 0; i < n; i++) { int h, m; scanf("%d:%d", &h, &m); a.push_back(h * 60 + m); } sort(a.begin(), a.end()); a.push_back(a[0]); int ans = 0; for (int i = 0; i < a.size() - 2; i++) { ...
9
#include <bits/stdc++.h> using namespace std; char s[1000006]; int n, m; long long f[26]; int main() { scanf("%d", &n); for (; n--;) { scanf("%s", s); m = strlen(s); vector<pair<int, int> > G; for (int i = 0, j; i < m; i = j) { for (j = i; j < m && s[i] == s[j]; j++) ; G.push_bac...
15
#include <bits/stdc++.h> using namespace std; const int N = 1234567; const int inf = (int)1e9 + 7; int n; long long m; int c[N], w[N]; int ax[N], ay[N]; set<pair<int, int> > s; int give(int id) { int tot = ax[id] * 100 + ay[id] - c[id]; return ((tot / 100) + (tot % 100)) * w[id]; } int main() { scanf("%d %lld", &...
16
#include <bits/stdc++.h> using namespace std; const int infinito = 1000000000; int hay[1000000]; vector<int> g[1000000]; void eliminapadre(int u, int p) { vector<int> &ar = g[u]; vector<int> nextar; for (int i = 0; i < int(ar.size()); i++) { int v = ar[i]; if (v != p) { eliminapadre(v, u); nex...
12
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, M = 1e6 + 10, inf = 2e9 + 10, mod = 6; const long long INF = 1e18 + 10; long long c[N], w[N]; struct is { long long x; int y; is() {} is(long long a, int b) { x = a, y = b; } bool operator<(const is &b) const { return x > b.x; } }; priority...
16
#include <bits/stdc++.h> const int N = 510; using namespace std; char s[N][N]; int a[N][N]; int top_to_down[N][N]; int left_to_right[N][N]; int main() { int n, m, k; int i, j; scanf("%d%d%d", &n, &m, &k); for (i = 0; i < n; i++) scanf("%s", s[i]); if (n < 3 || m < 3) { printf("0\n"); return 0; } m...
12
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return {i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug...
17
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { if (a.first < b.first) return true; if (a.first == b.first) { if (a.second > b.second) return true; else return false; } if (a.first > b.first) return false; } int main() { int n, a, b, aux; vect...
9
#include <bits/stdc++.h> using namespace std; const int N = 10000 + 11; const int INF = 0x3f3f3f3f; int n, m, s; int dx, dy; int sx, sy; int main() { while (cin >> n >> m) { cin >> sx >> sy; cin >> s; long long ans = 0; for (int i = 0; i < s; ++i) { cin >> dx >> dy; int t = INF; if (...
5
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define cendl cout<<endl #define ll long long int #define vi vector<int> #define vll vector<ll> #define vvi vector < vi > #define pii pair<int,int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 1000000000000000001; #...
0
#include <bits/stdc++.h> using namespace std; struct Node { int x; Node* prev; }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int j = 0, d, k = 1, y = 0, q, i, r, g, z, l, b = 0, c = 0, n, x; string s1, s2, s; cin >> n; Node* w; map<long long int, long long int>...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { int a, b, c; string s; vector<int> v; vector<int> req; vector<int> ans; int freq_1[201] = {0}; int freq_2[2001] = {0}; cin >> a; cin >> s; ...
7
#include <bits/stdc++.h> using namespace std; int used[600][600], x, y, r, c; char com[1000000]; int main() { scanf("%d %d %d %d", &x, &y, &r, &c); scanf("%s", com); int uz = strlen(com); int con = x * y; for (int i = 0; i < uz; i++) { if (!used[r][c]) { used[r][c] = 1; con--; printf("1 ...
8
#include <bits/stdc++.h> using namespace std; void file() { freopen("max.in", "r", stdin); freopen("max.out", "w", stdout); } const long long MOD = (long long)1e9 + 7ll; const long long mod = 998244353; const long long T = (long long)1e18; const long long K = (long long)1e9; const long long N = 3e5 + 100; const lon...
8
#include <bits/stdc++.h> using namespace std; vector<int> v; bool vis[500010]; int main() { int n, escondidos = 0; scanf("%d", &n); v.clear(); for (int i = 0; i < 500010; i++) vis[i] = false; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); v.push_back(x); } sort(v.begin(), v.end()); fo...
8
#include <bits/stdc++.h> using namespace std; int Map[10000]; int a[200], d[4100][4100]; int v[5000][200]; void load() {} bool cmp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); } void trungtt() { int n, m, q; ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> q; ...
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 100; vector<int> pos[MAXN]; int a[MAXN]; inline int get(int l, int r, int c) { return lower_bound(pos[c].begin(), pos[c].end(), r) - lower_bound(pos[c].begin(), pos[c].end(), l); } int main() { ios::sync_with_stdio(false); cout.tie(0); ...
10
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); void fast_in_out() { std::ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); } int dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; int dy[] = {-1, 1, 0, 0, -1, -1, 1, 1, 0}; const long double eps = 1e-9; const int mod = 998244353; con...
10
#include <bits/stdc++.h> using namespace std; int n, h, x[200020], y[200020]; long long ck[200020], sum[200020]; long long ans; set<long long> s; map<long long, long long> k; int main() { scanf("%d%d", &n, &h); for (int i = 1; i <= n; i++) { scanf("%d%d", &x[i], &y[i]); sum[i] = (long long)y[i] - (long long...
9
#include <bits/stdc++.h> using namespace std; const int maxN = 2e5 + 10; string s; int n, lef = 0, rig, ans = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; n = s.size(); rig = n - 1; while (lef < s.length() && s[lef] != '[') lef++; if (lef == s.length()) return cout <<...
5
#include <bits/stdc++.h> using namespace std; int n, m, k; int obj[200000]; int binSearch(int p, int r) { if (p == r) return p; int q = p + r + 1 >> 1, cnt = m, s = 0; for (int i = n - q; i < n && cnt; ++i) { if (s + obj[i] > k) s = 0, cnt--; s += obj[i]; } return (cnt ? binSearch(q, r) : binSearch(p,...
10
#include <bits/stdc++.h> using namespace std; int main() { long long x, y; while (cin >> x >> y) { long long now = 1; long long step = 0; while (1) { if (step & 1) y -= now; else x -= now; now++; if (y < 0 || x < 0) break; step++; } if (y < 0) ...
0
#include <bits/stdc++.h> using namespace std; int n; vector<pair<int, int> > V[100005]; bool use[100005]; long long mx, cnt; void input() { cin >> n; int x, y, z; for (int i = 1; i < n; i++) { cin >> x >> y >> z; V[x].push_back({y, z}); V[y].push_back({x, z}); } return; } void dfs(int u) { use[u...
6
#include <bits/stdc++.h> using namespace std; const long double PI = 3.14159265358979323846; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { string s; cin >> s; int n = s.size(); int ones = 0, zeroes = 0; for (int i = 0; i < n; ++i) { if (s[i] == '1')...
6
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j; string s[1000]; cin >> n; for (i = 0; i < n; i++) { cin >> s[i]; } int xt = 0; for (i = 0; i < n; i++) { if (s[i][0] == 'O' && s[i][1] == 'O') { xt++; s[i][0] = '+'; s[i][1] = '+'; cout << "YES" << endl...
0
#include <bits/stdc++.h> #pragma comment(linker, "/stack:64000000") using namespace std; const long long INF = 1000000001; const long double EPS = 1E-3; const int SIZE = 2100; int a[SIZE][SIZE], c[SIZE][SIZE], used[SIZE][SIZE], d[SIZE][SIZE], b[SIZE][SIZE]; int n; const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}; cons...
11
#include <bits/stdc++.h> #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") using namespace std; const int MAX = 3e5 + 500; const long long MAX2 = 11; const long long MOD = 1000000007; const long long MOD2 = 1000005329; const long long INF = 2e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int ...
11
#include <bits/stdc++.h> using namespace std; int u[100010], cnt[100010]; multiset<int> s; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> u[i]; int ans = 1; for (int i = 1; i <= n; i++) { if (cnt[u[i]]) s.erase(s.find(cnt[u[i]])); s.inser...
7
#include <bits/stdc++.h> long long int A[100001]; long long int B[100001]; long long int C[100001]; long long int M[100001][3]; int main() { int n, m, k; int check = scanf("%d%d%d", &n, &m, &k); while (check == 3) { int i = 0; while (i < n) { scanf("%lld", &A[i]); i++; } for (int i = 0...
6
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s, s1, s2; long long a, b; cin >> s; for (int i = 0; i < s.size(); i++) { if (i % 2) s2 += s[i]; else s1 += s[i]; } a = stoi(s1); if (!s2.empty()) { b = st...
8
#include <bits/stdc++.h> using namespace std; int a[100000]; int n, i, j, s; long long s2, s1; int main() { cin >> n; s = s2 = s1 = 0; for (i = 0; i < n; i++) { cin >> a[i]; s2 += a[i]; } for (i = 0; i < n - 1; i++) { s1 += a[i]; s2 -= a[i]; if (s1 == s2) s++; } cout << s; return 0; ...
4
#include <bits/stdc++.h> using namespace std; const int MAX_N = 20; char tab[MAX_N][MAX_N][MAX_N]; int k, n, m, a, b, c; void dfs(int x, int y, int z) { if (tab[x][y][z] == '#' || x < 0 || x >= k || y < 0 || y >= n || z < 0 || z >= m) return; c++, tab[x][y][z] = '#'; dfs(x + 1, y, z), dfs(x - 1, y, z), ...
6
#include <bits/stdc++.h> const double eps = (1e-10); const double PI = acos(-1.0); using namespace std; int dcmp(double a, double b) { return fabs(a - b) <= eps ? 0 : (a > b) ? 1 : -1; } int getBit(int num, int idx) { return ((num >> idx) & 1) == 1; } int setBit1(int num, int idx) { return num | (1 << idx); } int set...
12
#include <bits/stdc++.h> using namespace std; struct node { int v, pos; node(int a = 0, int b = 0) : v(a), pos(b) {} bool operator<(const node &b) const { return v < b.v; } }; const int MAX = 1e5 + 20; node an[MAX]; int n; int st[MAX], tt[MAX], cur[MAX]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i...
12
#include <bits/stdc++.h> using namespace std; struct Stree { vector<pair<int, int> > data; vector<pair<int, int> > span; int size; Stree(int nsize) { size = 1; while (size < nsize) size *= 2; data.resize(size * 2, make_pair(-1, -1)); span.resize(size * 2); for (int i = 0; i < size; i++) span...
15
#include <bits/stdc++.h> #include<string> //#include<unordered_set> #define ll long long int #define llu long long unsigned int #define fi first #define se second #define m_p make_pair #define endl '\n' #define fast_io ios_base::sync_with_stdio(0); cin.tie(0) #define endl '\n' #...
9
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; string s1 = "hello"; int x = 0, y = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == s1[x]) { x++; y++; } } if (y == 5) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1000005; char str[N]; long long dp[N][5]; long long calc(int i, int s) { if (str[i] == 0) return 0; long long &ret = dp[i][s]; if (ret != -1) return ret; ret = 0; if (s == 0) { ret += calc(i + 1, 0); if (isalpha(str[i])) ret += calc(i + 1, 1)...
11
#include <bits/stdc++.h> using namespace std; int mapx[6] = {32, 1, 4, 8, 2, 16}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int result = 0; int k; cin >> k; for (int i = 0; i < 6; i++) if (k & (1 << i)) result += mapx[5 - i]; cout << result; return 0; }
11
#include <bits/stdc++.h> using namespace std; int n, m; long long dp[100010][8]; char s[100010]; int main() { scanf("%d%d%s", &n, &m, s + 1); for (int i = 1; i <= n; i++) s[i] -= 'a'; for (int i = 0; i < m; i++) { int st = 0; if (i == s[1]) st ^= 2; if (i == s[1] || i == s[2]) st ^= 4; dp[1][st]++...
19
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") long long arr[300005]; long long A[1000005]; long long tree[300005 + 5]; void fun() { for (long long i = 1; ...
12
#include <bits/stdc++.h> using namespace std; const int maxn = 1300; const double eps = 1e-18; const unsigned long long base = 13331; const long long mod = 1e9 + 7; int n, m; int a[maxn][maxn]; int cnt[maxn]; int f[maxn]; int ans = 0; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for...
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector") using namespace std; using lli = long long; using ld = long double; using ii = pair<int, int>; using vi = vector<int>; const ld eps = 1e-9; int sgn(ld a) { return (a > eps) - (a < -eps); } struct Pt { ld x, y; explicit Pt(ld x = ...
13
#include <bits/stdc++.h> using namespace std; int main(void) { int t = 0; cin >> t; while (t-- > 0) { int n = 0; cin >> n; vector<int> a(n); for (auto &x : a) cin >> x; sort(a.begin(), a.end()); vector<int> b; b.push_back(a[0]); for (int i = 1; i < n; ++i) if (b[(int)b.size()...
0
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932385; const long double E = 2.71828182845904523536; const double eps = 1e-9; const long long mod = 1e9 + 7; const long long inf = 1 << 30; const int N = 2020; int frq[555][N]; int ans[555][N]; int n; bool ok(int x, int m, int c) { f...
8
#include <bits/stdc++.h> using namespace std; double x1, yy1, x2, y2, vx, vy, wx, wy, vmax, t; int main() { int c = 500; scanf("%lf%lf%lf%lf", &x1, &yy1, &x2, &y2); scanf("%lf%lf", &vmax, &t); scanf("%lf%lf%lf%lf", &vx, &vy, &wx, &wy); double xx = x2 - x1, yy = y2 - yy1, x = xx - t * vx, y = yy - t * vy; do...
13
#include <bits/stdc++.h> using namespace std; long long ta, tb, t, n, a, m = 1e12 + 7, ans; int main() { cin >> ta >> tb >> t >> n; while (n--) { cin >> a; if (a && a <= tb - t) { if (max(ta, a - 1) <= tb - t && ta - a + 1 < m) m = ta - a + 1, ans = min(a - 1, ta); ta = max(ta, a) + t; ...
13
#include <bits/stdc++.h> using namespace std; const long long N = 3e3 + 8, Mod = 1e9 + 7; long long n, m, C[N][N]; void prepro() { C[1][0] = C[1][1] = 1; for (long long i = 2; i < N; i++) for (long long j = 0; j <= i; j++) C[i][j] = ((C[i - 1][j] % Mod) + (C[i - 1][j - 1] % Mod)) % Mod; } int main() { i...
8
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int N = 200005; const int inf = 1e9 + 7; int n; unordered_map<int, vector<int> > a; unordered_map<int, int> pos; unordered_map<int, bool> vertex; unordered_map<int, bool> vis; string st; int ans = -1; pair<int, int> rs; pair<int, int> dfs(in...
19
#include <bits/stdc++.h> using namespace std; const int MAXN = 10000001; const int inf = 1000000000; int d[MAXN]; int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); for (int i = 0; i < MAXN; ++i) d[i] = i; for (int i = 1; i * i < MAXN; ++i) d[i * i] = 1; for (int i = 2; i < MAXN; ++i) i...
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int four = 0; while (4 * four <= n && (n - 4 * four) % 7) ++four; if (4 * four > n) { cout << -1 << endl; } else { for (int i = 0; i < four; ++i) { printf("4"); } int k = (n - 4 * four) / 7; for (int i = 0;...
2
#include <bits/stdc++.h> using namespace std; struct node { int in, out, w, s, v; }; node a[505]; bool op(node a, node b) { if (a.in == b.in) return a.out > b.out; return a.in < b.in; } int dp[505][1005]; int dp2[1005]; int n, s; int dfs(int i, int s) { if (dp[i][s] != -1) return dp[i][s]; vector<pair<pair<in...
18
#include <bits/stdc++.h> using namespace std; long long cnt[200002 << 1], s[200002 << 1]; long long ans, k; int main() { int n, i, j; scanf("%d", &n); for (int i = 1, x; i <= n; i++) scanf("%d", &x), cnt[x]++; for (i = 1; i <= 2 * 200002; i++) s[i] = s[i - 1] + cnt[i]; for (i = 1; i <= 200002; i++) { if (...
11
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; int a[100010], b[100010]; int cmp(int a, int b) { return a > b; } int main() { int n, i, x, l, r, ans = 0; scanf("%d%d", &n, &x); for (int(i) = (1); (i) <= (n); ++(i)) scanf("%d", &a[i]); for (int(i) = (1); (i) <= (n); ++(i...
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N = 2e5 + 228; const int M = 5e2 + 7; const int B = 1100; const long long p1 = 19; const int add1 = 39; const long long per = 1e10; const long long INF = 1e...
14
#include <bits/stdc++.h> using namespace std; int const MAXN = 2e6 + 9; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int T; cin >> T; while (T--) { int n; cin >> n; if (n == 1) { cout << -1 << '\n'; continue; } if ((n - 1) * 5 % 3) { for (int i = 0; i < n - 1; i++...
2
#include <bits/stdc++.h> using namespace std; long long h, w; long long Map[1010][1010]; long long r[1010], c[1010]; long long PowMod(long long a, long long b, long long c) { long long result = 1; long long base = a % c; while (b) { if (b & 1) result = (result * base) % c; base = (base * base) % c; b ...
6
#include <bits/stdc++.h> using namespace std; bool use[10]; long long cal(int n, int p) { int i, j, k; for (i = 0; i < 8; i++) use[i] = 0; long long rs = 1; for (i = 0; i < p; i++) { k = n - i; for (j = 2; j <= p; j++) { if (k % j == 0 && !use[j]) { use[j] = true; k /= j; } ...
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e3 + 10; const int MAXM = 4e6 + 10; const long long MOD = 1e9 + 7; const long long INF = 0x3f3f3f3f; const unsigned long long BASE = 233; const int base = 5; mt19937_64 mt(time(0)); char rubbish[110]; bool GETDATA; inline long long read() { long long x =...
18
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; void solve() { int n, k; cin >> n >> k; vector<int> A(n); bool f = false; for (int i = 0; i < n; ++i) { cin >> A[i]; if (A[i] == k) { f = true; } } if (!f) { cout << "no" << "\n"; return; } if (n ==...
12
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7, Base = 998244353; const long long N = 1e3 + 7; const long long INF = 1LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7LL; const double pie = acos(-1.0); long long Fact[N], k, a[N], n, Ans; void extended_euclid(long long a, long long b, long lo...
7
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&...
13
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long double PI = 3.141592653589793238; const long long pi = 31415926; const long long inf = 1000000000000000000; const long long small_inf = 100000000000000; long long int modpow(long long int x, long long int n, ...
15
#include <bits/stdc++.h> using namespace std; long double eps = (long double)1 / 1e15; long long inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7; long long sqr(long long a) { return a * a; } long long qb(long long a) { return a * a * a; } long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); } long long...
13
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int len = s.length(); vector<int> ind; for (int i = 0; i < len; i++) if (s[i] == 'a') ind.push_back(i); if (ind.size() == len) { s[len - 1] = 'z'; } else if (ind.size() == 0) { for (int i = 0; i < len; i++) s[i]--; ...
4
#include <bits/stdc++.h> using namespace std; int n, a, tmp; vector<int> ans; int main() { cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; if (tmp == 2 && x < 0) { ans.push_back(i - a); tmp = 1; a = i; } else if (x < 0) { tmp++; } } ans.push_back(n - a); co...
2
#include <bits/stdc++.h> using namespace std; vector<int> g[3010]; int n, m; bool vis[3010]; int dis[3010][3010]; void init() { cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) { queue<int> q; mems...
13
#include <bits/stdc++.h> using namespace std; int k, n; const int maxn = 1e3 + 10; const int mod = 998244353; int a[maxn]; int dp[maxn][maxn]; int solve(int limit) { for (int j = 2; j <= k; ++j) { int now_v = 0, now_i = j - 2; for (int i = j; i <= n; ++i) { while (a[now_i + 1] + limit <= a[i] && now_i +...
17
#include <bits/stdc++.h> int n, m, d, a[1005], c[1005], i, j, k, l, sum; int main() { for (scanf("%d%d%d", &n, &m, &d), i = 0; i < m; i++) scanf("%d", &c[i]), sum += c[i]; if (sum + (d - 1) * (m + 1) < n || sum > n) { printf("NO\n"); return 0; } else printf("YES\n"); for (i = j = 0; i < n; i++) ...
9
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; int count = 0; cin >> a >> b >> c >> d; if (a == b) { count++; } if (a == c) { count++; } if (a == d) { count++; } if (b == c && b != a) { count++; } if (b == d && b != a) { count++; } if (c == d &...
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 55, M = 1e5 + 55; char ss[1 << 21], *A = ss, *B = ss, cc; inline char gc() { return A == B && (B = (A = ss) + fread(ss, 1, 1 << 21, stdin), A == B) ? EOF : *A++; } template <class...
24
#include <bits/stdc++.h> using namespace std; const int MAXN = 300; int n, cnt, nn, i; int a[MAXN]; int main() { cin >> n; nn = 2 * n - 1; for (i = 0; i < nn; i++) { cin >> a[i]; if (a[i] <= 0) cnt++; a[i] = abs(a[i]); } sort(a, a + nn); if ((cnt - n) % 2 == 0) cnt -= n; cnt = cnt & 1; for (...
10
#include <bits/stdc++.h> using namespace std; typedef long long int ll; void solve() { int n, m; cin >> n >> m; vector<int> arr(n); for(auto &x : arr) cin >> x; vector<pair<int,double>> prob(m); for(int i = 0; i < m; i++) cin >> prob[i].first >> prob[i].second; int last = 0; for(int i...
7
#include <bits/stdc++.h> using namespace std; const double eps = 1e-12; const int maxn = 300010; const int mod = 1000000; const int inf = 0x3f3f3f3f; const int INF = 0x7FFFFFFF; void Mod(long long &x) { x %= mod; } long long Min(long long a, long long b) { return a < b ? a : b; } long long Max(long long a, long long b)...
10
#include <bits/stdc++.h> using namespace std; int dp[300][300][300]; int nxt[100111][30], nas[30]; char str[100111]; int n, q; vector<int> v[3]; int fun(int a, int b, int c) { if (a == 0 && b == 0 && c == 0) return 0; int &ret = dp[a][b][c]; if (ret != -1) return ret; ret = (1 << 20); if (a > 0) { int pos...
14
#include <bits/stdc++.h> using namespace std; const int oo = 1e9 + 7; int res, n, k; vector<vector<int> > a, used; void dfs(int L, int R) { used[L][R] = 1; if (R >= n) res = 1; if (R < 3 * n && a[L][R + 1] == 0) { if (!a[L - 1][R + 1] && !a[L - 1][R + 2] && !a[L - 1][R + 3] && L > 1 && !used[L - 1][R ...
9
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; using namespace std; int main() { string s; cin >> s; long long p, l =...
1
#include <bits/stdc++.h> using namespace std; long long n, k; long long num[2]; long long C[110][110]; long long dp[240][60][60]; int main() { scanf("%lld %lld", &n, &k); for (int i = 0; i < 60; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % 1000000007; ...
13
#include <bits/stdc++.h> using namespace std; int n, m, k; map<int, int> a; int main() { scanf("%d%d%d", &n, &m, &k); int t; for (int i = 0; i < m; i++) { scanf("%d", &t); a[t] = 1; } int flag = 1; int u, v; for (int i = 0; i < k; i++) { scanf("%d%d", &u, &v); if (a[u] && u == flag) { ...
5