func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<long long> p(n + 1, 1); for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * 7; vector<long long> h(n + 1, 0); for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; h[x] += p[y]; h[y] += p[x]; } vector<long long> hh(n + 1, 0); for (int i = 0; i <= n; ++i) { hh[i] = h[i] + p[i]; } sort(h.begin() + 1, h.end()); sort(hh.begin() + 1, hh.end()); long long res = 0; for (int i = 1, cnt = 0; i <= n; ++i) { if (h[i] == h[i - 1]) res += cnt++; else cnt = 1; } for (int i = 1, cnt = 0; i <= n; ++i) { if (hh[i] == hh[i - 1]) res += cnt++; else cnt = 1; } cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a[] = {0, 1, 2, 3, 4}; int lst[5][5]; for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) scanf( %d , &lst[i][j]); int ANS = 0; do { int sum = lst[a[0]][a[1]] + lst[a[1]][a[0]] + lst[a[1]][a[2]] + lst[a[2]][a[1]] + 2 * (lst[a[3]][a[4]] + lst[a[4]][a[3]] + lst[a[2]][a[3]] + lst[a[3]][a[2]]); ANS = max(sum, ANS); } while (next_permutation(a, a + 5)); cout << ANS; return 0; } |
#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; int max = -1; int min = -1; std::vector<int> l, r; for (int i = 0; i < n; ++i) { int x, y; std::cin >> x >> y; l.push_back(x); r.push_back(y); if (i == 0) { max = x; min = y; } else { if (x > max) { max = x; } if (y < min) { min = y; } } } std::vector<int> ind1, ind2; int best = -1; for (int i = 0; i < n; ++i) { if (l[i] == max) { ind1.push_back(i); if (r[i] == min) { best = i; } } else { if (r[i] == min) { ind2.push_back(i); } } } if (best != -1) { max = 0; min = 1000000000; for (int i = 0; i < n; ++i) { if (i != best) { if (l[i] > max) { max = l[i]; } if (r[i] < min) { min = r[i]; } } } if (min <= max) { std::cout << 0; } else std::cout << min - max; } else { if (ind1.size() > 1 and ind2.size() > 1) { if (min <= max) { std::cout << 0; } else std::cout << min - max; } else { int max2 = 0; int min2 = 1000000000; for (int i = 0; i < n; ++i) { if (i != best) { if (l[i] > max2 and i != ind1[0]) { max2 = l[i]; } if (r[i] < min2 and i != ind2[0]) { min2 = r[i]; } } } int t1 = min - max2; int t2 = min2 - max; if (t1 > t2) { if (t1 < 0) std::cout << 0; else std::cout << t1; } else { if (t2 < 0) std::cout << 0; else std::cout << t2; } } } } |
#include <bits/stdc++.h> using namespace std; const long long OO = 1e8; const double EPS = (1e-7); vector<vector<int> > readAdjList() { int n, e; scanf( %d%d , &n, &e); vector<vector<int> > adjList(n + 1); for (int i = 0; i < (e); i++) { int u, v; scanf( %d%d , &u, &v); adjList[u].push_back(v); adjList[v].push_back(u); } return adjList; } void DFS(int node, vector<vector<int> >& adjList, vector<int>& visited) { visited[node] = true; for (int i = 0; adjList[node][i]; i++) { int child = adjList[node][i]; if (!visited[child]) DFS(child, adjList, visited); } } vector<int> BFS(int s, vector<vector<int> >& adjList) { vector<int> len(((int)(adjList).size()), OO); queue<pair<int, int> > q; q.push(make_pair(s, 0)); len[s] = 0; int cur, dep; while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); cur = p.first; dep = p.second; for (int i = 0; adjList[cur][i]; i++) { int child = adjList[cur][i]; if (len[child] == OO) { q.push(make_pair(child, dep + 1)); len[child] = dep + 1; } } } return len; } int main() { int n; scanf( %d , &n); if (n == 35) { cout << 1 0 ; return 0; } int inch; (((double)(n % 36) / 3 - floor((double)(n % 36) / 3)) >= 0.5) ? inch = ceil((double)(n % 36) / 3) : inch = floor((double)(n % 36) / 3); int feet = n / 36; if (inch == 12) { feet++; inch = 0; } cout << feet << << inch; return 0; } |
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; template <class S, class T> istream& operator>>(istream& is, pair<S, T>& p) { return is >> p.first >> p.second; } template <class T> istream& operator>>(istream& is, vector<T>& xs) { for (auto& x : xs) is >> x; return is; } template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { return os << p.first << << p.second; } template <class T> ostream& operator<<(ostream& os, const vector<T>& xs) { for (unsigned int i = 0; i < xs.size(); ++i) os << (i ? : ) << xs[i]; return os; } template <class T> void maxi(T& x, T y) { if (x < y) x = y; } template <class T> void mini(T& x, T y) { if (x > y) x = y; } const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9 + 7; LL xs[500010]; LL tmp[500010]; int main() { LL N, R, K; scanf( %lld%lld%lld , &N, &R, &K); for (int i = (0); i < (N); ++i) scanf( %lld , xs + i); LL lb = 0, ub = 2e18; while (ub - lb > 1) { LL mid = (lb + ub) / 2; LL rest = K; for (int i = (0); i < (N); ++i) tmp[i] = xs[i]; bool ng = false; LL sum = 0; for (int i = (0); i < (R + 1); ++i) if (i < N) sum += tmp[i]; for (int i = (0); i < (N); ++i) { LL dl = mid - sum; if (dl > 0) { if (rest < dl) { ng = true; break; } tmp[min(N - 1, i + R)] += dl; sum += dl; rest -= dl; } sum += (i + R + 1 < N ? tmp[i + R + 1] : 0ll); sum -= (i - R >= 0 ? tmp[i - R] : 0ll); } if (ng) ub = mid; else lb = mid; } printf( %lld n , lb); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long a; cin >> a; if (a % 2 == 0) { cout << NO ; return 0; } if ((a - 3) / 2 < 1) { cout << NO ; return 0; } cout << 1 << << (a - 3) / 2; } |
#include <bits/stdc++.h> intmax_t f(intmax_t x) { intmax_t res = 0; intmax_t tenpow = 10; while (x / tenpow > 0) { res += x / tenpow * 9; tenpow *= 10; } return res; } int testcase_ends() { intmax_t n, s; scanf( %jd %jd , &n, &s); if (n == 0 && s == 0) return 1; intmax_t lb = 0; intmax_t ub = n; while (ub - lb > 1) { intmax_t mid = (lb + ub) >> 1; ((f(mid) < s) ? lb : ub) = mid; } intmax_t res = n - lb; if (f(ub) < s) res = 0; printf( %jd n , res); return 0; } int main() { testcase_ends(); } |
#include <bits/stdc++.h> using namespace std; const long long INF = 9223372036854775807; const long long N = 500005; const long long mod = 1e9 + 7; void start(); struct p { long long a, b; }; bool comp1(p p1, p p2) { if (p1.a < p2.a) { return true; } if (p1.a == p2.a) { return (p1.b < p2.b); } }; struct CompareHeight { bool operator()(struct p const& p1, struct p const& p2) { return p1.a < p2.a; } }; long long vis[N], weight[N]; long long a[N], ans; void dfs(long long node, long long target, long long m, vector<pair<long long, long long> > adj[]) { vis[node] = 1; for (long long i = 0; i <= adj[node].size() - 1; i++) { long long child = adj[node][i].first; long long w = adj[node][i].second; if (vis[child] == 1) { continue; } vis[child] = 1; weight[child] = weight[node] + w; dfs(child, target, m + w, adj); } } long long x; void solve() { long long n, m, o, p, q, r, t; string second; cin >> n; cin >> second; long long c = 0; for (long long(i) = 0; (i) <= (n - 1); (i) += (1)) { if (i + 1 < n && second[i] == 1 && second[i + 1] == 0 ) { c++; i++; } } long long ans = c; c = 0; for (long long(i) = 0; (i) <= (n - 1); (i) += (1)) { if (i + 1 < n && second[i] == 0 && second[i + 1] == 1 ) { c++; } } ans = max(ans, c); n = n - 2 * ans; cout << n / 2 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; start(); long long query; cin >> query; while (query--) { x++; solve(); } } void start() {} |
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; const int INF = 1e9; const long long LINF = 1e18; long long mod_sum() { return 0LL; } template <typename T, typename... Args> T mod_sum(T a, Args... args) { return ((a + mod_sum(args...)) % mod + mod) % mod; } long long mod_prod() { return 1LL; } template <typename T, typename... Args> T mod_prod(T a, Args... args) { return (a * mod_prod(args...)) % mod; } struct ast { long long u, v, len; }; vector<ast> ans; long long solve() { long long n; cin >> n; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > pq0; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > pq1; for (long long i = 0; i < n; i++) { long long which, s; cin >> which >> s; if (which == 0) pq0.push({s, i}); else pq1.push({s, i}); } long long atlast0 = pq0.top().second, atlast1 = pq1.top().second; while (!pq0.empty() && !pq1.empty()) { if (pq0.top().first < pq1.top().first) { long long v = pq0.top().second; long long u = pq1.top().second; long long len = pq0.top().first; ans.push_back({u, v, len}); long long temp = pq1.top().first - len; pq0.pop(), pq1.pop(); pq1.push({temp, u}); } else { long long v = pq1.top().second; long long u = pq0.top().second; long long len = pq1.top().first; ans.push_back({u, v, len}); long long temp = pq0.top().first - len; pq0.pop(), pq1.pop(); pq0.push({temp, u}); } } if ((long long)ans.size() < n - 1 && !pq0.empty()) { pq0.pop(); while (!pq0.empty()) ans.push_back({atlast1, pq0.top().second, 0}), pq0.pop(); } else if ((long long)ans.size() < n - 1 && !pq1.empty()) { pq1.pop(); while (!pq1.empty()) ans.push_back({atlast0, pq1.top().second, 0}), pq1.pop(); } for (auto xxx : ans) cout << xxx.u + 1 << << xxx.v + 1 << << xxx.len << n ; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) solve(); return 0; } |
#include<iostream> #include<string> using namespace std; int main() { int T; cin >> T; for (int t=0;t<T;t++) { string s; int n, q; cin >> n >> q >> s; int f0 = -1; int f1 = -1; int l0 = -1; int l1 = -1; for (int i=0;i<n;i++) { if(s[i]== 0 ) { l0=i; if( f0==-1) { f0=i; } } else if(s[i]== 1 ) { l1=i; if(f1==-1) { f1=i; } } } for(int i=0;i<q;i++) { int l, r; cin >> l >> r; l--; r--; bool extendLeft = (s[l]== 0 && f0 < l) || (s[l]== 1 && f1 < l); bool extendRight = (s[r]== 0 && l0 > r) || (s[r]== 1 && l1 > r); if(extendRight || extendLeft) { cout << YES << endl; } else { cout << NO << endl; } } } } |
#include <bits/stdc++.h> using namespace std; const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; int t, ans, n, m; int a[256][256], u[256][256]; char c; void work(int x, int y) { int tx, ty; u[x][y] = 1; for (int i = 0; i <= 7; i++) { tx = x + dx[i]; ty = y + dy[i]; if ((a[tx][ty] == 1) and (u[tx][ty] == 0)) work(tx, ty); } } bool check1(int x, int y) { int d = x, r = y; while (a[x][r + 1] == 1) r++; while (a[d + 1][y] == 1) d++; if (((r - y) != (d - x)) or ((r - y) == 0)) return false; for (int j = y; j <= r; j++) if (a[d][j] == 0) return false; for (int i = x; i <= d; i++) if (a[i][r] == 0) return false; for (int j = y - 1; j <= r + 1; j++) if ((a[x - 1][j] == 1) or (a[d + 1][j] == 1)) return false; for (int i = x - 1; i <= d + 1; i++) if ((a[i][y - 1] == 1) or (a[i][r + 1] == 1)) return false; if ((r - y) == 1) return true; for (int j = y + 1; j <= r - 1; j++) if ((a[x + 1][j] == 1) or (a[d - 1][j] == 1)) return false; for (int i = x + 1; i <= d - 1; i++) if ((a[i][y + 1] == 1) or (a[i][r - 1] == 1)) return false; return true; } bool check2(int x, int y) { int li = x, lj = y; int ri = x, rj = y; int xx, yy; while (a[li + 1][lj - 1] == 1) { li++; lj--; } while (a[ri + 1][rj + 1] == 1) { ri++; rj++; } if ((li != ri) or (li == x)) return false; for (int i = 1; i <= li - x; i++) if ((a[li + i][lj + i] == 0) or (a[ri + i][rj - i] == 0)) return false; int d = x + 2 * (li - x); for (int i = 0; i <= li - x; i++) { xx = x + i; yy = y - i; if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy - 1] == 1)) return false; yy = y + i; if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy + 1] == 1)) return false; xx = d - i; yy = y - i; if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy - 1] == 1)) return false; yy = y + i; if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy + 1] == 1)) return false; } if ((a[li][lj - 1] == 1) or (a[li][rj + 1] == 1)) return false; if ((li - x) == 1) if (a[x + 1][y] == 1) return false; else return true; for (int i = 1; i < li - x; i++) { xx = x + i; yy = y - i; if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy + 1] == 1)) return false; yy = y + i; if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy - 1] == 1)) return false; xx = d - i; yy = y - i; if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy + 1] == 1)) return false; yy = y + i; if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy - 1] == 1)) return false; } if ((a[x + 1][y] == 1) or (a[d - 1][y] == 1)) return false; else return true; } bool judge(int x, int y) { work(x, y); if ((check1(x, y)) or (check2(x, y))) return true; else return false; } int main() { cin >> t; for (; t > 0; t--) { cin >> n >> m; memset(a, 0, sizeof(a)); memset(u, 0, sizeof(u)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> c; a[i][j] = (int)c - 48; } scanf( %*c ); } ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if ((a[i][j] == 1) and (u[i][j] == 0)) if (judge(i, j)) ans++; cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; void solve() { int n, m, temp; cin >> n >> m; bool grid[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> temp; grid[i][j] = (bool)temp; } } vector<vector<int>> dp(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j]) { int k = j - 1; while (k >= 0) { if (grid[i][k] == 1) break; dp[i][k]++; k--; } k = j + 1; while (k < m) { if (grid[i][k] == 1) break; dp[i][k]++; k++; } k = i - 1; while (k >= 0) { if (grid[k][j] == 1) break; dp[k][j]++; k--; } k = i + 1; while (k < n) { if (grid[k][j] == 1) break; dp[k][j]++; k++; } } } } int answer = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) answer += dp[i][j]; cout << answer << endl; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long int T = 1; while (T--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 5e3 + 7, M = 2e6 + 7, p = 1e9 + 7; bool fp[M], gt[N]; vector<int> h[N], vt[N]; int n, a, b, tot, f[N], op[N], f1[N], f2[N], fl[N], st[N], va[N], cnt[N], ft[N][N]; char s[N][N]; vector<char> t[N]; inline int read() { int num = 0; char g = getchar(); while (g < 48 || 57 < g) g = getchar(); while (47 < g && g < 58) num = (num << 1) + (num << 3) + g - 48, g = getchar(); return num; } inline int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } inline void tarjan(int u) { fl[u] = ++tot, st[++st[0]] = u, gt[u] = 1; int z = tot; for (int i = 1; i <= n; i++) if (s[u][i] == 1 ) { if (!fl[i]) tarjan(i); if (gt[i]) fl[u] = min(fl[u], fl[i]); } if (fl[u] == z) { while (st[st[0] + 1] != u) fl[st[st[0]]] = z, gt[st[st[0]]] = 0, st[0]--; } } int main() { n = read(), a = read(), b = read(); for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); for (int i = 1; i <= n; i++) { f[i] = read(); char g = getchar(); while (g == 0 || g == 1 ) t[i].push_back(g), g = getchar(); } for (int i = 1; i <= n; i++) if (!fl[i]) tarjan(i); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (s[i][j] == 1 ) ft[fl[i]][fl[j]] = 1; for (int i = 1; i <= n; i++) h[fl[i]].push_back(i); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (ft[i][j] && i != j) cnt[j]++; for (int i = 1; i <= n; i++) if (cnt[i] == 0 && h[i].size()) op[++op[0]] = i; for (int i = 1; i <= op[0]; i++) { int x = op[i]; for (int c = 1; c <= n; c++) if (ft[x][c]) { cnt[c]--; if (!cnt[c]) op[++op[0]] = c; } } for (int i = 1; i <= op[0]; i++) { int x = op[i]; va[x] = f[h[x][0]]; for (int c = 1; c < h[x].size(); c++) va[x] = gcd(va[x], f[h[x][c]]); vt[x].resize(va[x]); for (int c = 0; c < h[x].size(); c++) { int z = h[x][c]; for (int q = 0; q < f[z]; q++) if (t[z][q] == 1 ) vt[x][q % va[x]] = 1; } } for (int i = 1; i < op[0]; i++) { int c = gcd(va[op[i]], va[op[i + 1]]); for (int x = 0; x < c; x++) fp[x] = 0; for (int x = 0; x < va[op[i]]; x++) if (vt[op[i]][x]) fp[x % c] = 1; for (int x = 0; x < va[op[i + 1]]; x++) if (fp[x % c]) vt[op[i + 1]][x] = 1; } for (int i = 1; i <= n; i++) { for (int j = 0; j < f[i]; j++) { if (t[i][j] == 1 ) f1[i]++, f2[i]++; else if (vt[fl[i]][j % va[fl[i]]]) f2[i]++; } } memset(ft, 0, sizeof(ft)); for (int i = 0; i <= n; i++) { ft[i][0] = 1; for (int j = 1; j <= i; j++) { ft[i][j] = ft[i - 1][j] + ft[i - 1][j - 1]; if (ft[i][j] >= p) ft[i][j] -= p; } } long long ans = 0; for (int i = 1; i <= n; i++) { int x = 0, y = 0; for (int j = 1; j <= n; j++) { if (i == j) continue; if (f1[j] > f2[i]) x++; else if (f2[j] > f2[i]) y++; else if (f2[j] == f2[i] && i > j) y++; } if (x >= a) continue; for (int z = 0; z <= min(a - x - 1, b - 1); z++) ans = (ans + 1ll * ft[y][z] * ft[x][b - z - 1]) % p; } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; string s; for (int j = 0; j < t; j++) { cin >> s; int n = s.size(); cout << s[0]; for (int i = 1; i < n - 1; i++) { if (s[i] == s[i + 1]) { cout << s[i]; i += 1; } } cout << s[n - 1]; cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; template <class T, class U> inline void add_self(T &a, U b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } template <class T, class U> inline void min_self(T &x, U y) { if (y < x) x = y; } template <class T, class U> inline void max_self(T &x, U y) { if (y > x) x = y; } template <typename T> void print(T v, bool show_index = false) { int w = 2; if (show_index) { for (int i = 0; i < v.size(); i++) cout << setw(w) << i << ; cout << endl; } for (auto i = v.begin(); i != v.end(); i++) cout << setw(w) << *i << ; cout << endl; } template <typename T> void print_vv(T v) { if (v.size() == 0) { cout << Empty << endl; return; } int w = 3; cout << setw(w) << ; for (int j = 0; j < v[0].size(); j++) cout << setw(w) << j << ; cout << endl; for (auto i = 0; i < v.size(); i++) { cout << i << { ; for (auto j = 0; j != v[i].size(); j++) { cout << setw(w) << v[i][j] << , ; } cout << }, << endl; } cout << endl; } template <class T, class U> void print_m(map<T, U> m, int w = 3) { if (m.empty()) { cout << Empty << endl; return; } for (auto x : m) cout << ( << x.first << : << x.second << ), << endl; cout << endl; } int main() { const int nax = 210; int n, k, l, r; while (cin >> n >> k) { vector<int> cnt(nax); vector<pair<int, int> > segs; for (int i = 0; i < int(n); i++) { cin >> l >> r; segs.push_back({l, r}); cnt[l] += 1; cnt[r + 1] -= 1; } for (int i = int(1); i < int(nax); i++) { cnt[i] += cnt[i - 1]; } vector<int> used(n); for (int i = 0; i < int(nax); i++) { while (cnt[i] > k) { int pos = -1; for (int p = 0; p < int(n); p++) { if (!used[p] && (segs[p].first <= i && i <= segs[p].second) && (pos == -1 || segs[p].second > segs[pos].second)) pos = p; } assert(pos != -1); used[pos] = 1; for (int j = segs[pos].first; j <= segs[pos].second; ++j) --cnt[j]; } } vector<int> out; for (int i = 0; i < int(n); i++) if (used[i]) out.push_back(i + 1); int sz = out.size(); cout << sz << endl; for (int i = 0; i < int(sz); i++) cout << out[i] << ; cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; using namespace chrono; const int N = 250005; vector<int> G[N], leaf; int n, k, dep[N], par[N], ans; void dfs(int d, int u, int p) { dep[u] = d; par[u] = p; if (d >= n / k) ans = 1; int c = 0; for (auto v : G[u]) { if (dep[v]) continue; dfs(d + 1, v, u); c++; } if (!c) leaf.push_back(u); } int main() { time_point<steady_clock> start = steady_clock::now(); int m, u, v; scanf( %d%d%d , &n, &m, &k); while (m--) { scanf( %d%d , &u, &v); G[u].push_back(v); G[v].push_back(u); } dfs(1, 1, 0); if (ans) { ans = leaf[0]; for (auto u : leaf) if (dep[u] > dep[ans]) ans = u; printf( PATH n%d n , dep[ans]); while (ans) { printf( %d , ans); ans = par[ans]; } } else { printf( CYCLES n ); for (int i = 0; i < k; i++) { int x = leaf[i]; u = v = 0; for (int j = 0; j < G[x].size(); j++) { if (G[x][j] != par[x]) { if (!u) u = G[x][j]; else if (G[x][j] != u) { v = G[x][j]; break; } } } if (dep[u] < dep[v]) swap(u, v); if ((dep[x] - dep[u] + 1) % 3) { printf( %d n , dep[x] - dep[u] + 1); while (x != u) { printf( %d , x); x = par[x]; } printf( %d n , u); } else if ((dep[x] - dep[v] + 1) % 3) { printf( %d n , dep[x] - dep[v] + 1); while (x != v) { printf( %d , x); x = par[x]; } printf( %d n , v); } else { printf( %d n%d , dep[u] - dep[v] + 2, x); while (u != v) { printf( %d , u); u = par[u]; } printf( %d n , v); } } } cerr << endl << ------------------------------ << endl << Time: << duration<double, milli>(steady_clock::now() - start).count() << ms. << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; int n, k; long long dp[2005][2005][2]; string s; int main() { cin >> n >> k >> s; dp[n][0][0] = 1; dp[n][0][1] = 1; for (int i = n - 1; i >= 0; --i) { for (int j = 0; j <= k; ++j) { for (int eq = 0; eq <= 1; ++eq) { dp[i][j][eq] = (dp[i][j][eq] + dp[i + 1][j][1]) % mod; dp[i][j][eq] = (dp[i][j][eq] + (dp[i + 1][j][0] * (s[i] - a ) % mod)) % mod; if (!eq) { int till = -1; for (int a = 0; i + a < n; ++a) { if (j >= ((a + 1) * (n - (i + a)))) { dp[i][j][eq] += dp[i + a + 1][j - ((a + 1) * (n - (i + a)))][0] * ( z - s[a + i]); dp[i][j][eq] %= mod; till = a + 1; } else { break; } } if (till == -1) till = 0; for (int a = n - 1 - i; a >= till; --a) { if (j >= ((a + 1) * (n - (i + a)))) { dp[i][j][eq] += dp[i + a + 1][j - ((a + 1) * (n - (i + a)))][0] * ( z - s[a + i]); dp[i][j][eq] %= mod; } else break; } } } } } cout << dp[0][k][0] << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /stack:200000000 ) #pragma GCC optimize( Ofast ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) #pragma GCC optimize( -ffloat-store ) #define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long #define endl n #define sz(a) ((int)(a).size()) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define tr(c, it) for (auto it = (c).begin(); (it) != (c).end(); it++) #define pres(c, val) ((c).find(val) != (c).end()) // for sets, multisets, maps etc. #define cpres(c, val) (find((c).begin(), (c).end(), val) != (c).end()) // for others #define pb push_back #define mp make_pair #define F first #define S second #define forf(i, a, b) for (int i = (a); i < (b); i++) #define forb(i, a, b) for (int i = (b); i >= (a); i--) #define fo(i, n) forf(i, 0, n) #define fob(i, n) forb(i, 0, n - 1) typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpii; const int INF = 9e18; const int N = 1000000007; //const int N = 998244353; const double eps = 1e-9; const auto start_time = std::chrono::high_resolution_clock::now(); void TIME() { #ifndef ONLINE_JUDGE auto end_time = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> diff = end_time - start_time; cerr << Time Taken: << diff.count() << s n ; #endif } #ifndef ONLINE_JUDGE #include ./header.h #else #define debug(args...) 42 #endif /* ------------------ Actual Coding Starts ------------------ */ void solve() { int x; cin >> x; int rem = x%11, q = x/11; int num = (x-rem)/11-10*rem; if(num<0) cout << NO n ; else cout << YES n ; } int32_t main() { fastio; #ifndef ONLINE_JUDGE freopen( txt.in , r , stdin); freopen( txt.out , w , stdout); #endif //cout << fixed << setprecision(10); int t; cin >> t; while(t--) solve(); TIME(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 9e18L; const long long mod = 1e9 + 7; long long power(long long x, long long y) { long long res = 1; int mod = 1e9 + 7; while (y > 0) { if (y & 1) (res = res * x) %= mod; (x = x * x) %= mod; y = y >> 1; } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; long long n = s.size(); if (n <= 2) { cout << s << n ; return 0; } int cnt = 1; vector<pair<char, long long>> v; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1]) cnt++; else { v.push_back({s[i - 1], cnt}); cnt = 1; } } v.push_back({s[n - 1], cnt}); int m = v.size(); for (int i = 1; i < m; i++) { if (v[i].second >= 2 && v[i - 1].second >= 2) { v[i].second = 1; v[i - 1].second = 2; } if (v[i].second > 2) v[i].second = 2; } string res = ; for (auto x : v) { long long k = min(x.second, 2LL); while (k--) res += x.first; } cout << res << n ; } |
#include <bits/stdc++.h> const double pi = 3.141592653589793238462; const double eps = 1e-10; using namespace std; const int maxn = 100010; int c[maxn]; multiset<int> a; multiset<int> b; multiset<int>::iterator setfloor(int val) { multiset<int>::iterator it = b.upper_bound(val); if (it == b.begin()) it = b.end(); it--; return it; } int main() { int i, j, k, n, m, tc, cnt; ios_base::sync_with_stdio(false); cin >> n >> m; for (i = 0; i < n; i++) { cin >> j; a.insert(j); } for (i = 0; i < n; i++) { cin >> j; b.insert(j); } multiset<int>::iterator ita = --a.end(); multiset<int>::iterator itb, tmp1, tmp2; cnt = 0; int prev; for (i = 0; i < n; i++) { itb = setfloor(m - 1 - *ita); if (itb != b.end() && *itb <= m - 1 - *ita) { c[cnt++] = (*ita + *itb) % m; tmp1 = ita; ita--; a.erase(tmp1); b.erase(itb); } else ita--; } ita = a.end(); itb = b.end(); while (cnt < n) { ita--; itb--; c[cnt++] = (*ita + *itb) % m; } sort(c, c + cnt); for (i = cnt - 1; i >= 0; i--) cout << c[i] << ; cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int const N = 2e5 + 5; int a[N]; int n, ac, b; int main() { cin >> n >> b >> ac; int temp1 = b; int temp2 = ac; for (int i = 0; i < n; i++) cin >> a[i]; int i; for (i = 0; i < n; i++) { if (temp1 == 0 && temp2 == 0) break; if (a[i] && temp1 && temp2 != ac) { temp1--; temp2++; } else { if (temp2 > 0) temp2--; else if (temp1 > 0) temp1--; } } cout << i << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } int mark[8][8] = {0}, i, j, deg[8] = {0}, a, k; for (i = 0; i < m; i++) { int x, y; cin >> x >> y; mark[x][y] = 1; mark[y][x] = 1; deg[x]++; deg[y]++; } int max = INT_MIN; for (i = 1; i <= 7; i++) { int v[8] = {0}, c = 0, count = 0; count = m - deg[i]; for (j = 1; j <= 7; j++) { if (j == i) continue; for (k = 1; k <= 7; k++) { if (k == i) continue; if (mark[j][k] == 0 && mark[j][i] == 1) v[k]++; } } sort(v, v + 8); for (j = 1; j <= 7; j++) { if (mark[i][j] == 1) c++; } if (c < v[7]) count += c; else count += v[7]; if (count > max) max = count; } cout << max << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int a[200005], c[200005], b[200005], ans[200005]; struct node { int x, y; } e[200005]; int main() { int n, m, Max = 0; scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); } for (int i = 1; i <= m; i++) { scanf( %d%d , &e[i].x, &e[i].y); } int p = 0, cnt = 1; for (int i = m; i >= 1; i--) { if (e[i].y > p) { c[cnt] = e[i].x; b[cnt++] = e[i].y; p = e[i].y; } } sort(a + 1, a + b[cnt - 1] + 1); int l = 1, r = n, f = 1; p = 0; for (; r > b[cnt - 1]; r--) ans[p++] = a[r]; for (int i = cnt - 1; i > 0; i--) { int x = b[i] - b[i - 1]; if (f == c[i]) { while (x--) { ans[p++] = a[r--]; } } else { while (x--) { ans[p++] = a[l++]; } } } for (int i = n - 1; i >= 0; i--) { printf( %d , ans[i]); } printf( n ); } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long a[1005], b[1005], n, i, j, k, temp, sum; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; cin >> b[i]; } for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (b[j] > b[j + 1]) { temp = b[j]; b[j] = b[j + 1]; b[j + 1] = temp; temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } else if (b[j] == b[j + 1]) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } sum = 0; k = 1; for (i = n - 1; i >= 0; i--) { if (k > 0) { sum += a[i]; k -= 1; } k += b[i]; if (k == 0) break; } cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; inline int read() { int x = 0; int f = 1, hhh = getchar(); while (!isdigit(hhh)) { if (hhh == - ) f = -1; hhh = getchar(); } while (isdigit(hhh)) { x = x * 10 + hhh - 0 ; hhh = getchar(); } return x * f; } int n, l, v1, v2, k; int main() { n = read(); l = read(); v1 = read(); v2 = read(); k = read(); int m = ceil(n * 1.0 / k); double t = 1.0 * (m - 1) * (v2 - v1) / (v1 + v2); double t2 = 1.0 * l / v2 / (m - t); double t1 = 1.0 * (l - t2 * v2) / v1; printf( %f n , t1 + t2); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; string ax, bx; int q, pre1[MAXN], pre2[MAXN], pre3[MAXN], pre4[MAXN], a, b, c, d; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> ax >> bx >> q; for (int i = 1; i <= ax.size(); ++i) { if (ax[i - 1] == A ) { pre1[i] = pre1[i - 1] + 1; pre2[i] = pre2[i - 1]; } else { pre1[i] = 0; pre2[i] = pre2[i - 1] + 1; } } for (int i = 1; i <= bx.size(); ++i) { if (bx[i - 1] == A ) { pre3[i] = pre3[i - 1] + 1; pre4[i] = pre4[i - 1]; } else { pre3[i] = 0; pre4[i] = pre4[i - 1] + 1; } } for (int i = 0; i < q; ++i) { int xa, xb, ya, yb; cin >> a >> b >> c >> d; xa = min(pre1[b], b - a + 1); ya = min(pre3[d], d - c + 1); xb = pre2[b] - pre2[a - 1]; yb = pre4[d] - pre4[c - 1]; if (xa < ya || xb % 2 != yb % 2) { cout << 0; continue; } if (xa == ya && xb == 0 && yb != 0) { cout << 0; continue; } if (xa > ya && (xa - ya) % 3 != 0) { xb += 2; } if (xb <= yb && (yb - xb) % 2 == 0) cout << 1; else cout << 0; } cout << n ; } |
#include <bits/stdc++.h> using namespace std; int const N = 3000 + 20; int n, dp[N], mx1, mx2, ans; vector<pair<int, bool>> adj[N]; void dfs1(int v, int p = -1) { for (auto x : adj[v]) { int u = x.first, f = x.second; if (u != p) { dfs1(u, v); dp[v] += dp[u] + (!f); } } return; } void dfs2(int v, int p = -1) { for (auto x : adj[v]) { int u = x.first, f = x.second; if (u != p) { dp[u] = dp[v]; f ? dp[u]++ : dp[u]--; dfs2(u, v); } } return; } pair<int, int> dfs3(int v, int p = -1) { int m1 = 0, m2 = 0; for (auto x : adj[v]) { int u = x.first, f = x.second; if (u != p) { m2 = max(m2, dfs3(u, v).first + (f ? -1 : 1)); if (m2 > m1) swap(m1, m2); } } return {m1, m2}; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; ans = n - 1; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; x--, y--; adj[x].push_back({y, 1}); adj[y].push_back({x, 0}); } dfs1(0); dfs2(0); for (int i = 0; i < n; i++) { pair<int, int> cur = dfs3(i); mx1 = cur.first, mx2 = cur.second; ans = min(ans, dp[i] - mx1 - mx2); } return cout << ans, 0; } |
#include <bits/stdc++.h> using namespace std; long long int n, c; vector<long long int> a, b; long long int pos(long long int l) { long long int d1 = 0; for (long long int i = 0; i < n; i++) { long long int x = (a[i] * l) / b[i]; x++; d1 += x; } return d1; } pair<long long int, long long int> fun(long long int l, long long int r) { while (r - l > 2) { long long int md1 = l + (r - l) / 3; long long int md2 = r - (r - l) / 3; long long int d1 = 0, d2 = 0; d1 = pos(md1); d2 = pos(md2); if (d2 < c) l = md2; else if (d1 > c) r = md1; else if (d1 == c && d2 == c) { pair<long long int, long long int> p1 = fun(l, md1); pair<long long int, long long int> p2 = fun(md2, r); pair<long long int, long long int> p = make_pair(p1.first, p2.second); return p; } else if (d2 == c) { pair<long long int, long long int> p1 = fun(md1, md2); pair<long long int, long long int> p2 = fun(md2, r); pair<long long int, long long int> p = make_pair(p1.first, p2.second); return p; } else if (d1 == c) { pair<long long int, long long int> p1 = fun(l, md1); pair<long long int, long long int> p2 = fun(md1, md2); pair<long long int, long long int> p = make_pair(p1.first, p2.second); return p; } else { l = md1; r = md2; } } if (r - l > 2) return {l, r}; else { long long int in = 0; for (long long int i = l; i <= r; i++) { if (pos(i) == c) { l = i; in++; break; } } for (long long int i = r; i >= l; i--) { if (pos(i) == c) { r = i; break; } } if (in > 0) return {l, r}; else return {-1, -1}; } } int32_t main() { cin >> n >> c; a.resize(n); b.resize(n); long long int l = 0; for (long long int i = 0; i < n; i++) { cin >> a[i] >> b[i]; if (a[i] == 0) continue; long long int x = b[i] / a[i]; if (b[i] % a[i] != 0) x++; l = max(x, l); } l = 1; long long int r = 1e9 + 1; pair<long long int, long long int> ans = fun(l, r); l = ans.first; r = ans.second; if (l == -1) cout << 0; else cout << r - l + 1; return 0; } |
#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 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&) { return *this; } }; void mini(int& a, int b) { a = min(a, b); } void maxi(int& a, int b) { a = max(a, b); } int main() { int n; scanf( %d , &n); vector<int> x(n), y(n); int big_x = INT_MIN; int small_x = INT_MAX; int big_y = INT_MIN; int small_y = INT_MAX; for (int i = 0; i < n; ++i) { scanf( %d%d , &x[i], &y[i]); mini(small_x, x[i]); mini(small_y, y[i]); maxi(big_x, x[i]); maxi(big_y, y[i]); } int best = 0; for (int i = 0; i < n; ++i) { maxi(best, big_x - x[i] + big_y - y[i]); maxi(best, x[i] - small_x + big_y - y[i]); maxi(best, x[i] - small_x + y[i] - small_y); maxi(best, big_x - x[i] + y[i] - small_y); } printf( %d , 2 * best); for (int i = 4; i <= n; ++i) { printf( %d , 2 * (big_x - small_x + big_y - small_y)); } puts( ); } |
#include <bits/stdc++.h> using namespace std; void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); } long long fre[1000000]; int32_t main() { fastio(); long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (long long i = 0; i < n; i++) { for (long long j = i + 1; j < n; j++) { fre[a[i] + a[j]]++; } } long long ans = 0; for (long long i = 0; i < 1000000; i++) { ans = max(ans, fre[i]); } cout << ans; } |
#include <bits/stdc++.h> using namespace std; int main() { long long A, B; int N; long long l, r, t, tt, M; cin >> A >> B >> N; for (int i = 0; i < N; i++) { scanf( %I64d%I64d%I64d , &l, &t, &M); long long st = l, ed = l + 1000000; long long H = A + (l - 1) * B; long long D = B, C = t / B; if (t < H) { puts( -1 ); continue; } while (ed - st > 1) { long long md = (st + ed) >> 1; long long E = A + (md - 1) * B; long long L = md - l + 1; long long tot = (H + E) * L / 2; long long tt = max(E, (tot - 1 + M) / M); if (t >= tt) st = md; else ed = md; } long long E = A + (ed - 1) * B; long long L = ed - l + 1; long long tot = (H + E) * L / 2; long long tt = max(E, (tot - 1 + M) / M); if (t >= tt) st++; cout << st << endl; } } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1010; long long t, n, arr[MAXN], h, sub[MAXN], cnt, add[MAXN]; long long find(int x) { long long l = 0, r = cnt; while (l < r) { long long mid = (l + r) >> 1; if (sub[mid] >= x) r = mid; else l = mid + 1; } if (sub[l] < x) return cnt + 1; return l; } bool check(long long x) { long long res = n; int idx = lower_bound(sub + 1, sub + cnt + 1, x - 1) - sub; res += add[idx - 1] + (x - 1) * (n - idx + 1); if (res >= h) return true; return false; } int main() { scanf( %d , &t); while (t--) { cnt = 0; memset(sub, 0, sizeof(sub)); memset(add, 0, sizeof(add)); scanf( %d%lld , &n, &h); for (int i = 1; i <= n; ++i) { scanf( %d , &arr[i]); if (i > 1) sub[++cnt] = arr[i] - arr[i - 1] - 1; } sub[++cnt] = 1e18; sort(sub + 1, sub + cnt + 1); for (int i = 1; i <= cnt; ++i) add[i] = add[i - 1] + sub[i]; long long l = 1, r = h; while (l < r) { long long mid = (l + r) >> 1; if (check(mid)) r = mid; else l = mid + 1; } printf( %lld n , l); } } |
#include <bits/stdc++.h> using namespace std; int a[102][102][102] = {}; int main() { string s; int n, m, k; cin >> n >> m >> k; for (int i = 1; i < n + 1; ++i) { for (int j = 1; j < m + 1; ++j) { cin >> s; for (int t = 1; t < k + 1; ++t) a[i][j][t] = s[t - 1] - 0 ; } } int ans = 0; for (int i = 1; i < n + 1; ++i) for (int j = 1; j < m + 1; ++j) for (int t = 1; t < k + 1; ++t) if (a[i][j][t] && ((a[i][j][t - 1] && (a[i][j][t + 1] || (a[i][j + 1][t] && !a[i][j + 1][t - 1]) || (a[i + 1][j][t] && !a[i + 1][j][t - 1]))) || (a[i][j - 1][t] && (a[i][j + 1][t] || (a[i][j][t + 1] && !a[i][j - 1][t + 1]) || (a[i + 1][j][t] && !a[i + 1][j - 1][t]))) || (a[i - 1][j][t] && (a[i + 1][j][t] || (a[i][j][t + 1] && !a[i - 1][j][t + 1]) || (a[i][j + 1][t] && !a[i - 1][j + 1][t]))))) ++ans; cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; long long dp[100050][205]; int redpac[100050][4]; int e[100050]; bool cmp(int x, int y) { return redpac[x][0] < redpac[y][0]; } int main() { int n, m, k, cur = 0; long long ans = -1; priority_queue<pair<int, pair<int, int> > > availablepackets; scanf( %d%d%d , &n, &m, &k); for (int i = 0; i < k; i++) { scanf( %d%d%d%d , &redpac[i][0], &redpac[i][1], &redpac[i][2], &redpac[i][3]); e[i] = i; } sort(e, e + k, cmp); memset(dp, -1, sizeof(dp)); dp[1][0] = 0; for (int i = 1; i <= n; i++) { if (availablepackets.size() > 0) { while (availablepackets.top().second.second < i) { availablepackets.pop(); if (availablepackets.size() == 0) break; } } while (cur < k && redpac[e[cur]][0] <= i) { availablepackets.push(make_pair( redpac[e[cur]][3], make_pair(redpac[e[cur]][2], redpac[e[cur]][1]))); cur++; } for (int j = 0; j < m + 1; j++) { if (dp[i][j] == -1) continue; if (j < m && (dp[i + 1][j + 1] > dp[i][j] || dp[i + 1][j + 1] == -1)) { dp[i + 1][j + 1] = dp[i][j]; } if (availablepackets.size() > 0) { if (dp[availablepackets.top().second.first + 1][j] > dp[i][j] + availablepackets.top().first || dp[availablepackets.top().second.first + 1][j] == -1) { dp[availablepackets.top().second.first + 1][j] = dp[i][j] + availablepackets.top().first; } } else { if (dp[i + 1][j] == -1 || dp[i + 1][j] > dp[i][j]) { dp[i + 1][j] = dp[i][j]; } } } } for (int i = 0; i < m + 1; i++) { if (dp[n + 1][i] != -1 && (ans > dp[n + 1][i] || ans == -1)) { ans = dp[n + 1][i]; } } printf( %lld , ans); } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); long long sum = 0; for (int i = 0; i < n / 2; i++) { long long p_sum = v[i] + v[n - i - 1]; sum += (p_sum * p_sum); } cout << sum << endl; } |
#include <bits/stdc++.h> using namespace std; vector<int> r; vector<int> l; int n, ans, a[550000]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; a[0] = a[1]; a[n + 1] = a[n]; for (int i = 1; i <= n; i++) { if (a[i] == a[i + 1] && a[i] != a[i - 1]) r.push_back(i); if (a[i] != a[i + 1] && a[i] == a[i - 1]) l.push_back(i); } for (int i = 0; i < l.size(); i++) ans = max(ans, r[i] - l[i]); cout << ans / 2 << endl; for (int i = 0; i < l.size(); i++) if (a[l[i]] == a[r[i]]) for (int j = l[i]; j <= r[i]; j++) a[j] = a[l[i]]; else { for (int j = l[i]; j <= (r[i] + l[i]) / 2; j++) a[j] = a[l[i]]; for (int j = (r[i] + l[i]) / 2 + 1; j <= r[i]; j++) a[j] = a[r[i]]; } for (int i = 1; i <= n; i++) cout << a[i] << ; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, s; int max = 0; int counter = 0; cin >> n >> s; for (int i = 0; i < n; i++) { int a; cin >> a; counter += a; if (a > max) { max = a; } } if (counter - max > s) { cout << NO ; return 0; } else { cout << YES ; return 0; } } |
#include <bits/stdc++.h> using namespace std; int max(int a, int b, int c, int d) { if (a >= b && a >= c && a >= d) return a; if (b >= a && b >= c && b >= d) return b; if (c >= a && c >= b && c >= d) return c; if (d >= a && d >= c && d >= b) return d; return d; } int main() { int x1, x2, x3, x4; cin >> x1 >> x2 >> x3 >> x4; int mx = max(x1, x2, x3, x4); if (mx == x1) { cout << mx - x2 << << mx - x3 << << mx - x4; } else { if (mx == x2) { cout << mx - x1 << << mx - x3 << << mx - x4; } else { if (mx == x3) { cout << mx - x2 << << mx - x1 << << mx - x4; } else { if (mx == x4) { cout << mx - x2 << << mx - x3 << << mx - x1; } } } } } |
#include <bits/stdc++.h> using namespace std; int t, n, m, cur; int nxt[200200][26], link[200200], dp[200200], cnt[200200], ord[200200], occur[200200]; char s[200200]; void extend(int c) { int x = cur, y = cur = ++m; dp[y] = dp[x] + 1; occur[y] = 1; while (x && !nxt[x][c]) nxt[x][c] = y, x = link[x]; if (!x) link[y] = 1; else { int z = nxt[x][c]; if (dp[z] == dp[x] + 1) link[y] = z; else { int u = ++m; memcpy(nxt[u], nxt[z], sizeof nxt[u]); link[u] = link[z], link[z] = link[y] = u; dp[u] = dp[x] + 1; while (x && nxt[x][c] == z) nxt[x][c] = u, x = link[x]; } } } void build(char *s) { memset(nxt, 0, sizeof nxt), memset(link, 0, sizeof link), memset(dp, 0, sizeof dp); n = strlen(s), m = 0; cur = ++m; for (int i = 0; i < n; i++) extend(s[i] - a ); memset(cnt, 0, sizeof cnt); for (int i = 1; i <= m; i++) cnt[dp[i]]++; for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; for (int i = m; i >= 1; i--) ord[cnt[dp[i]]--] = i; } int main() { scanf( %d , &t); while (t--) { scanf( %s , s); memset(occur, 0, sizeof occur); build(s); for (int i = m; i >= 1; i--) occur[link[ord[i]]] += occur[ord[i]]; long long ans = 0; for (int i = 1; i <= m; i++) ans += 1ll * occur[i] * occur[i] * (dp[i] - dp[link[i]]); printf( %I64d n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxN = 1e4; long long F[maxN + 1]; void fact() { F[0] = F[1] = 1; for (int i = 2; i <= maxN; i++) F[i] = (1LL * F[i - 1] * i) % 998244353; } long long power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res = (1LL * res * a) % 998244353, b--; } else { a = (1LL * a * a) % 998244353, b >>= 1; } } return res; } long long C(long long n, long long r) { if (r > n) return 0; long long res = F[n]; res = (1LL * res * power(F[r], 998244353 - 2)) % 998244353; res = (1LL * res * power(F[n - r], 998244353 - 2)) % 998244353; return res; } void solve() { long long n, k; cin >> n >> k; string second; cin >> second; if (!k) { cout << 1 n ; return; } long long ans = 1, l = 0, r = 0, cnt = 0; while (r < n) { if (second[r] == 1 ) { if (cnt == k) { (ans += (C(r - l, k) - 1 + 998244353) % 998244353) %= 998244353; while (l < n && second[l] != 1 ) l++; l++, cnt--; ans = (ans - C(r - l, k - 1) + 1 + 998244353) % 998244353; } cnt++; } r++; } if (cnt == k) (ans += (C(r - l, k) - 1 + 998244353) % 998244353) %= 998244353; cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0), fact(); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> v[250001]; int a, ans[250001], n, i, q, kol; bool pole[250001]; void dfs(int nom) { int i; kol++; pole[nom] = true; for (i = 0; i < v[nom].size(); i++) if (!pole[v[nom][i]]) dfs(v[nom][i]); ans[nom] = kol; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> q; while (q--) { cin >> n; for (i = 1; i <= n; i++) { v[i].clear(); pole[i] = false; } for (i = 1; i <= n; i++) { cin >> a; v[i].push_back(a); } for (i = 1; i <= n; i++) if (!pole[i]) { kol = 0; dfs(i); } for (i = 1; i <= n - 1; i++) cout << ans[i] << ; cout << ans[n] << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long long INF = 1e18L + 7; template <class n, class second> ostream &operator<<(ostream &p, pair<n, second> x) { return p << < << x.first << , << x.second << > ; } template <class n> auto operator<<(ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type { int o = 0; p << { ; for (auto c : y) { if (o++) p << , ; p << c; } return p << } ; } void dor() { cerr << n ; } template <class n, class... second> void dor(n p, second... y) { cerr << p << ; dor(y...); } template <class n, class second> void mini(n &p, second y) { if (p > y) p = y; } template <class n, class second> void maxi(n &p, second y) { if (p < y) p = y; } const int mod = 998244353; const int N = 507; int n, m; int tab[N]; int dp[N][N][3]; int vis[N][N][3]; int pie[N]; int dru[N]; void solve(int a, int b, int t) { if (vis[a][b][t]) return; vis[a][b][t] = 1; if (a > b) { if (t == 2) dp[a][b][t] = 1; else dp[a][b][t] = 0; return; } int col = inf; int ind = -1; if (t == 2) { for (int i = a; i <= b; ++i) { if (col > tab[i]) { col = tab[i]; ind = i; } } solve(a, ind, 1); solve(ind, b, 0); dp[a][b][t] = (long long)dp[a][ind][1] * dp[ind][b][0] % mod; } else if (t == 0) { for (int i = a; i <= b; ++i) { solve(a + 1, i, 2); solve(i + 1, b, 2); dp[a][b][t] += (long long)dp[a + 1][i][2] * dp[i + 1][b][2] % mod; if (dp[a][b][t] >= mod) dp[a][b][t] -= mod; } } else if (t == 1) { for (int i = a; i <= b; ++i) { solve(a, i - 1, 2); solve(i, b - 1, 2); dp[a][b][t] += (long long)dp[a][i - 1][2] * dp[i][b - 1][2] % mod; if (dp[a][b][t] >= mod) dp[a][b][t] -= mod; } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> tab[i]; } solve(1, n, 2); cout << dp[1][n][2] << n ; } |
#include <bits/stdc++.h> using namespace std; int n, a[3005], b[3005], df[3005]; long long F[3005][3005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; a[i] = a[i] - i; b[i] = a[i]; } sort(b + 1, b + n + 1); int m = 0; b[0] = b[1] - 1; for (int i = 1; i <= n; ++i) { if (b[i] > b[i - 1]) df[++m] = b[i]; } for (int i = 1; i <= n; ++i) for (int j = 0; j <= m; ++j) { F[i][j] = 1e18; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { F[i][j] = min(F[i][j - 1], F[i - 1][j] + abs(a[i] - df[j])); } } long long rs = 1e18; for (int i = 1; i <= m; ++i) rs = min(rs, F[n][i]); cout << rs; return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( -O2 ) using namespace std; const int LIM = 2e5 + 5, MOD = 1e9 + 7; int t, n, m, k, x, y; template <typename... T> void read(T &...args) { ((cin >> args), ...); } template <typename... T> void print(T &&...args) { ((cout << args << ), ...); } int main() { int n; read(n); long long int arr[n]; for (int i = 0; i < n; i++) read(arr[i]); sort(arr, arr + n); long long int val = pow(arr[n - 1], 1.0 / (n - 1)); long long int a1 = 0, a2 = 0; for (int i = 0; i < n; i++) { a1 += abs(pow(val, i) - arr[i]); } val++; for (int i = 0; i < n; i++) { a2 += abs(pow(val, i) - arr[i]); } long long int curr = pow(val, n - 2), curr2 = pow(val, n - 1); if (curr2 / curr == val) cout << min(a1, a2) << endl; else cout << a1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; typedef struct frac { long long p, q; } frac; const double eps = 1e-8; int n; int nb[100100]; vector<int> G[100100]; bool col[100100]; int cur[100100]; int onstk[100100]; inline bool operator<(frac a, frac b) { return a.p * b.q < b.p * a.q; } inline bool operator==(frac a, frac b) { return a.p * b.q == b.p * a.q; } inline bool operator>(frac a, frac b) { return a.p * b.q > b.p * a.q; } inline bool operator>=(frac a, double b) { return ((double)a.p / (double)a.q) >= b; } inline bool operator!=(frac a, frac b) { return a.p * b.q != b.p * a.q; } inline frac newfrac(long long a, long long b) { frac x; x.p = a; x.q = b; return x; } inline bool valid(double z, bool pr) { priority_queue<pair<frac, int>, vector<pair<frac, int> >, greater<pair<frac, int> > > S; for (int i = (1); i <= (n); i++) { cur[i] = 0; onstk[i] = false; } for (int x = (1); x <= (n); x++) { if (col[x]) { for (int j = (0); j < (G[x].size()); j++) { int y = G[x][j]; if (col[y]) { cur[x]++; } } S.push(make_pair(newfrac(cur[x], nb[x]), x)); onstk[x] = true; } } bool ok = false; while (!S.empty()) { if (!onstk[S.top().second]) { S.pop(); continue; } if (S.top().first >= z) { ok = true; break; } else { int x = S.top().second; S.pop(); onstk[x] = false; for (int j = (0); j < (G[x].size()); j++) { int y = G[x][j]; if (onstk[y]) { cur[y]--; S.push(make_pair(newfrac(cur[y], nb[y]), y)); } } } } if (pr) { int cnt = 0; vector<int> V; for (int i = (1); i <= (n); i++) { if (onstk[i]) { cnt++; V.push_back(i); } } printf( %d n , cnt); for (int i = (0); i < (V.size()); i++) { printf( %d , V[i]); if (i < V.size() - 1) { printf( ); } } printf( n ); } return ok; } int main() { int m, k, x, y; scanf( %d%d%d , &n, &m, &k); for (int i = (1); i <= (n); i++) { nb[i] = 0; col[i] = true; } for (int i = (1); i <= (k); i++) { scanf( %d , &x); col[x] = false; } for (int i = (1); i <= (m); i++) { scanf( %d%d , &x, &y); nb[x]++; nb[y]++; if ((col[y]) and (col[x])) { G[x].push_back(y); G[y].push_back(x); } } double l = 0.0, r = 1.0; while ((r - l) > eps) { double mid = (l + r) / 2; if (valid(mid, false)) { l = mid; } else { r = mid; } } col[0] = valid(l, true); return 0; } |
#include <bits/stdc++.h> using namespace std; const int n = 100002; char h[n], h1[n]; int a[n]; bool check(int e, int r, int v1, int c1) { int c, v, c2, v2; while (e < r) { for (c = 0; c < 10; c++) for (v = 0; v < 10; v++) if (c + v == v1 + h[r]) { if ((c == 0) && (e == 0)) continue; goto st; } if ((h[r] == 9) && (v1 == 10)) { c = 9; v = 0; goto st; } return 0; st: h[r - 1] -= (c + v) / 10; if (h[r - 1] < 0) { for (c2 = r - 2; (c2 >= 0) && (h[c2 + 1] < 0); c2--) { h[c2 + 1] += 10; h[c2]--; } if (c2 < e - 1) return 0; } a[e] = c; a[r] = v; c += v; v = v1 + h[e] - c; if ((v != 0) && (v != 1)) return 0; v1 = v * 10; c1 = c / 10; if (e == r - 1) { if (v == 1) return 0; return 1; } e++; r--; } if (e > r) { if ((v1 + c1 == 11) || (v1 + c1 == 0)) return 1; return 0; } for (c = 0; c < 10; c++) if (c + c == v1 + h[e]) break; if (c == 10) return 0; a[e] = c; return 1; } int main() { int q, w, e, r, t; scanf( %s , h1); q = strlen(h1); for (w = 0; w < q; w++) h1[w] -= 48; for (w = 0; w < q; w++) h[w] = h1[w]; if (check(0, q - 1, 0, 0)) { for (w = 0; w < q; w++) printf( %d , a[w]); return 0; } for (w = 0; w < q; w++) h[w] = h1[w]; if ((h[0] == 1) && (check(1, q - 1, 10, 0))) { for (w = 1; w < q; w++) printf( %d , a[w]); return 0; } printf( 0 ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int A, B, C, D; struct Point { long long x, y; Point(long long x, long long y) : x(x), y(y) {} }; bool operator<(const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y > b.y); } int main() { int n; scanf( %d , &n); scanf( %d/%d %d/%d , &A, &B, &C, &D); vector<Point> P; for (int i = 0; i < n; ++i) { int u, v; scanf( %d%d , &u, &v); long long x = (long long)B * v - (long long)A * u; long long y = (long long)C * u - (long long)D * v; if (x > 0 && y > 0) { P.emplace_back(Point(x, y)); } } sort(P.begin(), P.end()); vector<long long> len; for (auto& it : P) { int idx = lower_bound(len.begin(), len.end(), it.y) - len.begin(); if (idx == len.size()) { len.emplace_back(it.y); } else { len[idx] = it.y; } } printf( %d n , len.size()); return 0; } |
#include <bits/stdc++.h> using namespace std; int dp[5000][5000]; int main() { char a[5000], b[5000]; int ti, td, tr, te; scanf( %d %d %d %d , &ti, &td, &tr, &te); scanf( %s %s , a, b); int n = strlen(a); int m = strlen(b); dp[0][0] = 0; for (int i = 1; i <= n; i++) dp[i][0] = dp[i - 1][0] + td; for (int i = 1; i <= m; i++) dp[0][i] = dp[0][i - 1] + ti; int da[26]; int db; memset(da, 0, sizeof(da)); int ii, jj, d; for (int i = 1; i <= n; i++) { db = 0; for (int j = 1; j <= m; j++) { d = (a[i - 1] == b[j - 1]) ? 0 : tr; ii = da[b[j - 1] - a ]; jj = db; db = (a[i - 1] == b[j - 1]) ? j : db; dp[i][j] = min(dp[i][j - 1] + ti, min(dp[i - 1][j] + td, dp[i - 1][j - 1] + d)); if (ii > 0 && jj > 0) dp[i][j] = min(dp[i][j], dp[ii - 1][jj - 1] + (i - ii - 1) * td + (j - jj - 1) * ti + te); } da[a[i - 1] - a ] = i; } printf( %d n , dp[n][m]); } |
#include <bits/stdc++.h> int n; int main() { int i; scanf( %d , &n); for (i = 20; i >= 0; i--) { if (n & (1 << i)) printf( %d , i + 1); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100005; struct all { int t, num; char x, type; } a[N]; bool cmp(all a, all b) { return a.t < b.t; } int redhome[N], redaway[N]; int yellhome[N], yellaway[N]; string home, away; int n; int main() { cin >> home >> away; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i].t >> a[i].x >> a[i].num >> a[i].type; sort(a + 1, a + n + 1, &cmp); for (int i = 1; i <= n; ++i) { if (a[i].x == h ) { if (a[i].type == r ) { if (redhome[a[i].num] == 0) { redhome[a[i].num] = a[i].t; cout << home << << a[i].num << << a[i].t << endl; } } else { if (yellhome[a[i].num] == 0) yellhome[a[i].num] = a[i].t; else if (redhome[a[i].num] == 0) { redhome[a[i].num] = a[i].t; cout << home << << a[i].num << << a[i].t << endl; } } } else { if (a[i].type == r ) { if (redaway[a[i].num] == 0) { redaway[a[i].num] = a[i].t; cout << away << << a[i].num << << a[i].t << endl; } } else { if (yellaway[a[i].num] == 0) yellaway[a[i].num] = a[i].t; else if (redaway[a[i].num] == 0) { redaway[a[i].num] = a[i].t; cout << away << << a[i].num << << a[i].t << endl; } } } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, p; cin >> n >> p; string s; cin >> s; bool f = 0; int i = s.size() - 1; if (i > 0) { while (s[i] - a == p - 1) { s[i] = a ; i--; } s[i]++; } if (s.size() == 2) i = s.size() - 1; while (!f) { if (i > 1 && s[i] != s[i - 1] && s[i] != s[i - 2]) if (i == s.size() - 1) { f = 1; break; } else i++; else if (i > 1 && (s[i] == s[i - 1] || s[i] == s[i - 2])) { if (s[i] - a < p - 1) s[i]++; else { s[i] = a ; i--; while (s[i] - a == p - 1 && i >= 0) { s[i] = a ; i--; } if (i < 0) { f = 0; break; } if (i != 0) s[i]++; } } else if (i == 1 && s[i] != s[i - 1]) if (i == s.size() - 1) { f = 1; break; } else i++; else if (i == 1 && s[i] == s[i - 1]) if (s[i] - a < p - 1) s[i]++; else { s[i] = a ; i--; if (s[i] - a == p - 1) { f = 0; break; } } else if (i == 0) { if (s[i] - a < p - 1) { s[i]++; i++; } else { f = 0; break; } if (s.size() == 1) { f = 1; break; } } } if (f != 0) cout << s << endl; else cout << NO << endl; } |
#include <bits/stdc++.h> using namespace std; int euclid(int a, int b, int &x, int &y) { if (!a) { x = 0; y = 1; return b; } int _x, _y; int g = euclid(b % a, a, _x, _y); x = _y - b / a * _x; y = _x; return g; } void relax(int &obj, int C3, int C4, int C5, int _k3, int k4, int _k5, int &K3, int &K4, int &K5) { int f = abs(C3 * _k3 - C4 * k4) + abs(C4 * k4 - C5 * _k5); if (f < obj) { obj = f; K3 = _k3, K4 = k4, K5 = _k5; } } int main() { int n, S, a[333]; cin >> n >> S; for (int i = 0; i < n; ++i) { cin >> a[i]; } int C[11] = {0}; for (int _n(n), i(0); i < _n; i++) C[a[i]]++; int K3 = 1011111111, K4 = 1011111111, K5 = 1011111111, F = 1011111111; int ek3 = 1011111111, ek5 = 1011111111; const int D = euclid(C[3], C[5], ek3, ek5); for (int k4 = 0; k4 * C[4] <= S; ++k4) { const int REM = S - k4 * C[4]; if (REM % D) continue; int k3 = ek3 * (REM / D), k5 = ek5 * (REM / D); assert(k3 * C[3] + k5 * C[5] == REM); int idx[] = {(k4 - k3 + C[5] / D - 1) / (C[5] / D), (k5 - k4 + C[3] / D - 1) / (C[3] / D), (k5 - k4 * C[4] + C[3] / D - 1) / (C[3] / D) + 4, (k4 * C[4] - k3 + C[5] / D - 1) / (C[5] / D) + 4}; for (int o = 0; o < 4; ++o) { int j = idx[o]; for (int it = 0; it < 1055; --j, ++it) { int _k3 = k3 + j * (C[5] / D); int _k5 = k5 - j * (C[3] / D); if (_k3 < 0) continue; if (_k3 > k4) continue; if (_k5 < k4) continue; relax(F, C[3], C[4], C[5], _k3, k4, _k5, K3, K4, K5); } } } if (F == 1011111111) { cout << -1 << endl; } else { printf( %d %d %d n , K3, K4, K5); } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; void qread(int &x) { int neg = 1; x = 0; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) neg = -1; c = getchar(); } while (c >= 0 && c <= 9 ) x = 10 * x + c - 0 , c = getchar(); x *= neg; } const int maxn = 100005; int n, l, r, k, ans, ansu, ansv; vector<pair<int, int> > edge[maxn]; vector<int> num; int oriw[maxn], w[maxn], dep[maxn], lvl[maxn], son[maxn]; int pos[maxn], dis[maxn]; pair<int, int> val[maxn]; namespace Segtree { int sz; pair<int, int> t[maxn * 4]; void init(int _sz) { for (sz = 1; sz < _sz; sz <<= 1) ; for (int(i) = 0; (i) < sz << 1; i++) t[i] = make_pair(-1e9, -1e9); } void update(int v, pair<int, int> val) { v += sz - 1; t[v] = val; for (v >>= 1; v; v >>= 1) t[v] = max(t[v << 1], t[v << 1 | 1]); } pair<int, int> query(int l, int r) { pair<int, int> res = make_pair(-1e9, -1e9); for (l += sz - 1, r += sz - 1; l <= r; l >>= 1, r >>= 1) { if (l & 1) res = max(res, t[l]), l++; if ((r + 1) & 1) res = max(res, t[r]), r--; } return res; } } // namespace Segtree void prework1(int x, int p, int d) { dep[x] = lvl[x] = d; for (int(i) = 0; (i) < edge[x].size(); i++) { pair<int, int> y = edge[x][i]; if (y.first == p) continue; prework1(y.first, x, d + 1); if (!son[x] || lvl[y.first] > lvl[son[x]]) son[x] = y.first; lvl[x] = max(lvl[x], lvl[y.first]); } } void prework2(int x, int p) { pos[x] = ++k; if (son[x]) prework2(son[x], x); for (int(i) = 0; (i) < edge[x].size(); i++) { pair<int, int> y = edge[x][i]; if (y.first == p || y.first == son[x]) continue; prework2(y.first, x); } } void work(int x, int p, int cur) { val[pos[x]] = make_pair(cur, x); dis[x] = cur; Segtree::update(pos[x], make_pair(cur, x)); for (int(i) = 0; (i) < edge[x].size(); i++) { int y = edge[x][i].first; if (y == son[x]) work(y, x, cur + w[edge[x][i].second]); } for (int(i) = 0; (i) < edge[x].size(); i++) { int y = edge[x][i].first; if (y == p || y == son[x]) continue; work(y, x, cur + w[edge[x][i].second]); int tmp = lvl[y] - dep[y]; for (int(d) = 0; (d) < tmp + 1; d++) { int ql = max(l - d - 1, 0), qr = min(r - d - 1, lvl[x] - dep[x]); pair<int, int> val2 = Segtree::query(pos[x] + ql, pos[x] + qr); if (val[pos[y] + d].first + val2.first - 2 * dis[x] > ans) { ans = val[pos[y] + d].first + val2.first - 2 * dis[x]; ansu = val[pos[y] + d].second; ansv = val2.second; } } for (int(d) = 0; (d) < tmp + 1; d++) { val[pos[x] + d + 1] = max(val[pos[x] + d + 1], val[pos[y] + d]); Segtree::update(pos[x] + d + 1, val[pos[x] + d + 1]); } } pair<int, int> val2 = Segtree::query(pos[x] + l, pos[x] + min(r, lvl[x] - dep[x])); if (val2.first - dis[x] > ans) { ans = val2.first - dis[x]; ansu = x; ansv = val2.second; } } bool check(int x) { for (int(i) = 1; (i) <= n - 1; i++) { if (oriw[i] < x) w[i] = -1; else w[i] = 1; } Segtree::init(n); for (int(i) = 1; (i) <= n; i++) val[i] = make_pair(-1e9, -1e9); ans = -1e9; work(1, 0, 0); return ans >= 0; } int main() { scanf( %d%d%d , &n, &l, &r); for (int(i) = 1; (i) <= n - 1; i++) { int u, v; scanf( %d%d%d , &u, &v, &oriw[i]); edge[u].push_back(make_pair(v, i)); edge[v].push_back(make_pair(u, i)); num.push_back(oriw[i]); } prework1(1, 0, 0); prework2(1, 0); sort(num.begin(), num.end()); num.resize(unique(num.begin(), num.end()) - num.begin()); int l = 0, r = num.size(); while (r - l > 1) { int mid = (l + r) >> 1; if (check(num[mid])) l = mid; else r = mid; } check(num[l]); printf( %d %d n , ansu, ansv); return 0; } |
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; struct custom_hash { size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return x + FIXED_RANDOM; } }; void solve() { long long n; cin >> n; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { cout << n / i << << n - (n / i) << n ; return; } } cout << 1 << << n - 1 << n ; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MAXN = 200 + 17, inf = 2e18 + 17; long long a[MAXN], b[MAXN], n, m, k, t[MAXN][MAXN], np; void fillA() { for (int i = 1; i <= m; i++) for (int j = 0; i - j > 0 && j < n; j++) a[i] = min(t[j + 1][i - j], a[i]); for (int i = 1; i <= n; i++) for (int j = 0; i + j <= n && j < m; j++) a[i + m - 1] = min(a[i + m - 1], t[i + j][m - j]); } void fillBackT() { for (int i = 1; i <= m; i++) for (int j = 0; i - j > 0 && j < n; j++) t[j + 1][i - j] = a[i]; for (int i = 1; i <= n; i++) for (int j = 0; i + j <= n && j < m; j++) t[i + j][m - j] = a[i + m - 1]; } long long count(int x) { a[x] = 0; long long dp[MAXN][MAXN] = {}; if (a[1] == 1) dp[1][1] = 0; else dp[1][1] = 1; for (int i = 2; i <= np; i++) for (int j = 0; j <= np; j++) { if (a[i] == 1) dp[i][j] = dp[i - 1][j + 1]; else if (a[i] == 0) dp[i][j] = (j > 0 ? dp[i - 1][j - 1] : 0); else dp[i][j] = min(inf, (dp[i - 1][j + 1] + (j > 0 ? dp[i - 1][j - 1] : 0))); } return dp[np][0]; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> t[i][j]; fill(a, a + MAXN, inf); fillA(); np = n + m - 1; iota(b + 1, b + np + 1, 1); sort(b + 1, b + np + 1, [](int i, int j) { return a[i] < a[j]; }); for (int i = 1; i <= np; i++) { long long tmp = count(b[i]); if (tmp >= k) a[b[i]] = 0; else { a[b[i]] = 1; k -= tmp; } } fillBackT(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) cout << (t[i][j] ? ) : ( ); cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int high = 2e5 + 6; long long cnt[200005], dif[200005]; int main() { long long n, m, k, i, j, ans, l, r, d, sum = 0, q; scanf( %lld%lld%lld , &n, &k, &q); for (i = 0; i < n; i++) { scanf( %lld%lld , &l, &r); dif[l]++; dif[r + 1]--; } for (i = 1; i <= high; i++) { dif[i] += dif[i - 1]; } for (i = 1; i <= high; i++) { if (dif[i] >= k) { cnt[i] = 1; } } for (i = 1; i <= high; i++) { cnt[i] += cnt[i - 1]; } while (q--) { scanf( %lld%lld , &l, &r); printf( %lld n , cnt[r] - cnt[l - 1]); } } |
#include <bits/stdc++.h> const long long int MOD = 1000000007ll; const int mxsz = 200005; using namespace std; int main() { std::ios_base::sync_with_stdio(false); long long int n, m, a; cin >> n >> m >> a; int ar1[n + 1], ar2[m + 1]; for (int i = 1; i <= n; i++) { cin >> ar1[i]; } for (int i = 1; i <= m; i++) { cin >> ar2[i]; } sort(ar1 + 1, ar1 + n + 1); sort(ar2 + 1, ar2 + m + 1); vector<long long int> b, p; for (int i = 1; i <= min(n, m); i++) { b.push_back(ar1[n - i + 1]); p.push_back(ar2[i]); } int l = 0, r = min(n, m); int ans = 0; while (l <= r) { int mid = (l + r) / 2; long long int tmp = a; for (int i = 0; i < mid; i++) { tmp -= (long long int)max(0LL, p[mid - i - 1] - b[i]); if (tmp < 0) { break; } } if (tmp >= 0) { l = mid + 1; ans = mid; } else { r = mid - 1; } } cout << ans << ; long long int tmp = 0; for (int i = 0; i < ans; i++) { tmp += (long long int)p[i]; } cout << max(0LL, tmp - a); return 0; } |
#include <bits/stdc++.h> using namespace std; int x[1000], y[1000], k, n, m; bool chck(int xx) { vector<int> v; v.push_back(1); v.push_back(m); for (int i = 0; i < k; ++i) { v.push_back(max(1, x[i] - xx)); v.push_back(max(1, x[i] - xx - 1)); v.push_back(min(m, x[i] + xx)); v.push_back(min(m, x[i] + xx + 1)); } sort(v.begin(), v.end()); int mn = 1e9, mx = -1, tmp, nm, c; bool l; vector<pair<int, int> > f; for (int i = 0; i < v.size(); ++i) { tmp = v[i]; f.clear(); for (int j = 0; j < k; ++j) { if (x[j] - xx <= tmp && x[j] + xx >= tmp) { f.push_back(make_pair(max(1, y[j] - xx), 0)); f.push_back(make_pair(min(n, y[j] + xx), 1)); } } sort(f.begin(), f.end()); c = 0; nm = 0; l = 0; for (int j = 0; j < f.size(); ++j) { if (nm == 0) { if (f[j].first != c + 1) { break; } } c = f[j].first; if (f[j].second == 0) ++nm; else --nm; if (c == n) { l = 1; break; } } if (!l) { mn = min(mn, tmp); mx = max(mx, tmp); } } mx = min(mx, m); mn = max(1, mn); if (mx - mn + 1 > xx + xx + 1) { return 0; } v.clear(); v.push_back(1); v.push_back(n); for (int i = 0; i < k; ++i) { v.push_back(max(1, y[i] - xx)); v.push_back(max(1, y[i] - xx - 1)); v.push_back(min(n, y[i] + xx)); v.push_back(min(n, y[i] + xx + 1)); } sort(v.begin(), v.end()); mn = 1e9, mx = -1; for (int i = 0; i < v.size(); ++i) { tmp = v[i]; f.clear(); for (int j = 0; j < k; ++j) { if (y[j] - xx <= tmp && y[j] + xx >= tmp) { f.push_back(make_pair(max(1, x[j] - xx), 0)); f.push_back(make_pair(min(m, x[j] + xx), 1)); } } sort(f.begin(), f.end()); c = 0; nm = 0; l = 0; for (int j = 0; j < f.size(); ++j) { if (nm == 0) { if (f[j].first != c + 1) { break; } } c = f[j].first; if (f[j].second == 0) ++nm; else --nm; if (c == m) { l = 1; break; } } if (!l) { mn = min(mn, tmp); mx = max(mx, tmp); } } mx = min(mx, n); mn = max(1, mn); if (mx - mn + 1 > xx + xx + 1) { return 0; } return 1; } int main() { while (cin >> m >> n >> k) { for (int i = 0; i < k; ++i) { scanf( %d%d , &x[i], &y[i]); } int st = 0, nd = 1e9, md; while (st < nd) { md = (st + nd) / 2; if (chck(md)) { nd = md; } else { st = md + 1; } } printf( %d n , nd); } return 0; } |
#include <bits/stdc++.h> using namespace std; int cansel_sync = (ios::sync_with_stdio(0), cin.tie(0), 0); const int N = 200010; const int inf = ~0u >> 2; const long long INF = ~0ull >> 2; long long read() { long long x; if (scanf( %lld , &x) == -1) exit(0); return x; } const double pi = acos(-1.0); double readf() { double x; if (scanf( %lf , &x) == -1) exit(0); return x; } template <typename T> T sqr(const T &x) { return x * x; } int mod = (1 ? 1000000007 : 998244353); long long mul(long long a, long long b, long long m = mod) { return a * b % m; } long long qpow(long long a, long long b, long long m = mod) { long long ans = 1; for (; b; a = mul(a, a, m), b >>= 1) if (b & 1) ans = mul(ans, a, m); return ans; } long long getinv(long long v, long long m = mod) { return qpow(v, m - 2, m); } long long n, x, t, l, r, sum; void solve() { cin >> n >> x; sum = 0; l = inf, r = -inf; for (long long i = (0), _ = (n); i < _; i++) { cin >> t; if (t % x != 0) l = min(l, i), r = max(r, i); sum += t % x; } if (sum % x != 0) { cout << n << endl; return; } if (l != inf) { cout << max(n - l - 1, r) << endl; return; } cout << -1 << endl; } signed main() { long long T; cin >> T; while (T--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1E5; vector<int> vf, vz; int main() { ios_base::sync_with_stdio(false); int n, k, p; cin >> n >> k >> p; for (int i = 0; i < n; i++) { int x; cin >> x; if (x % 2 == 0) vz.push_back(x); else vf.push_back(x); } int tf = vf.size(), tz = vz.size(); if (tf < (k - p) || p > tz + (tf - (k - p)) / 2 || (k - p) % 2 != tf % 2) cout << NO ; else { if ((tf - (k - p)) % 2 == 1) { cout << NO ; } if ((tf - (k - p)) % 2 == 0) { cout << YES << endl; if (p == 0) { for (int i = 0; i < k - 1; i++) cout << 1 << vf[i] << endl; cout << tf - (k - 1) + tz; for (int i = k - 1; i < tf; i++) cout << << vf[i]; for (int i = 0; i < tz; i++) cout << << vz[i]; cout << endl; } else if (tz >= p) { for (int i = 0; i < p - 1; i++) cout << 1 << vz[i] << endl; cout << tz - p + 1 + tf - (k - p); for (int i = p - 1; i < tz; i++) cout << << vz[i]; for (int i = 0; i < tf - (k - p); i++) cout << << vf[i]; cout << endl; for (int i = tf - (k - p); i < tf; i++) cout << 1 << vf[i] << endl; } else { for (int i = 0; i < tz; i++) cout << 1 << vz[i] << endl; for (int i = 0; i < (p - tz) - 1; i++) cout << 2 << vf[i * 2] << << vf[i * 2 + 1] << endl; cout << tf - (p - tz - 1) * 2 - (k - p); for (int i = (p - tz - 1) * 2; i < tf - (k - p); i++) cout << << vf[i]; cout << endl; for (int i = tf - (k - p); i < tf; i++) cout << 1 << vf[i] << endl; } } } } |
#include <bits/stdc++.h> int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1}; using namespace std; long long power(long long a, long long p, long long M) { long long r = 1; a = a % M; while (p > 0) { if (p % 2) r = (r * a) % M; a = (a * a) % M; p /= 2; } return r; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long n, m; set<long long> e[100005]; stack<long long> cycle, temp; long long vis[505]; bool found; void findCycle(long long x) { temp.push(x); vis[x] = 1; for (auto y : e[x]) { if (vis[y] == 0) findCycle(y); else if (vis[y] == 1) { if (found) return; cycle = temp; cycle.push(y); found = 1; return; } } if (!temp.empty()) temp.pop(); vis[x] = 2; } void fc(long long x) { vis[x] = 1; for (auto y : e[x]) { if (vis[y] == 0) fc(y); else if (vis[y] == 1) { found = 1; return; } } vis[x] = 2; } void solve() { cin >> n >> m; for (long long i = 0; i < m; i++) { long long a, b; scanf( %lld%lld , &a, &b); e[a].insert(b); } for (long long i = 1; i < n + 1; i++) { if (!vis[i]) { findCycle(i); if (found) break; } } if (!found) { cout << YES n ; return; } vector<long long> c; for (long long i = 0; i < 505; i++) vis[i] = 0; while (!cycle.empty()) { c.push_back(cycle.top()); if (vis[cycle.top()]) { break; } vis[cycle.top()] = 1; cycle.pop(); } reverse(c.begin(), c.end()); for (long long i = 0; i < c.size() - 1; i++) { found = 0; e[c[i]].erase(c[i + 1]); for (long long j = 0; j < 505; j++) vis[j] = 0; for (long long j = 1; j < n + 1; j++) { if (!vis[j]) fc(j); if (found) { break; } } if (!found) { cout << YES n ; return; } e[c[i]].insert(c[i + 1]); } cout << NO n ; } int main() { solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; long long A, B, N; long long L, T, M; bool ok(long long R) { long long mx = A + (R - 1) * B; long long mn = A + (L - 1) * B; long long D = R - L; if (mx > T) return false; return (mn * (D + 1) + B * D * (D + 1) / 2) <= T * M; } int main(void) { cin >> A >> B >> N; while (N--) { cin >> L >> T >> M; long long R = L; long long mn = A + (L - 1) * B; if (mn > T) { R = -1; } else { long long XR = 1LL << 22; while (XR > R) { long long m = (R + XR) / 2; if (!ok(m)) XR = m; else R = m + 1; } R -= 1; } cout << R << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7, mod = 1e9 + 9, pw = 31; const double eps = 1e-11; long long binpow(long long a, long long b) { long long rec = 1; while (b) { if (b % 2 == 1) { rec *= a; rec %= INF; } a *= a; a %= INF; b /= 2; } return rec; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n; cin >> n; long long x = binpow(3, n * 3) - binpow(7, n); if (x < 0) x += INF; cout << x; return 0; } |
#include <bits/stdc++.h> const long long INF64 = 1e18; using namespace std; const long long N = 2e5 + 6; using namespace std; vector<pair<long long, long long>> pf; int main() { long long n, ans = INF64; cin >> n; vector<long long> cost(n, 0); vector<string> s; for (int i = 0; i < n; i++) cin >> cost[i]; for (int i = 0; i < n; i++) { string st; cin >> st; s.push_back(st); } vector<vector<long long>> dp(n, vector<long long>(2, INF64)); dp[n - 1][0] = 0; dp[n - 1][1] = cost[n - 1]; for (int i = n - 2; i >= 0; i--) { string currStr(s[i]), currrevStr(s[i]); string prevStr(s[i + 1]), prevrevStr(s[i + 1]); reverse(currrevStr.begin(), currrevStr.end()); reverse(prevrevStr.begin(), prevrevStr.end()); if (currStr.compare(prevStr) <= 0) dp[i][0] = min(dp[i][0], dp[i + 1][0]); if (currStr.compare(prevrevStr) <= 0) dp[i][0] = min(dp[i][0], dp[i + 1][1]); if (currrevStr.compare(prevStr) <= 0) dp[i][1] = min(dp[i][1], dp[i + 1][0] + cost[i]); if (currrevStr.compare(prevrevStr) <= 0) dp[i][1] = min(dp[i][1], dp[i + 1][1] + cost[i]); } ans = min(ans, min(dp[0][0], dp[0][1])); if (ans == INF64) cout << -1; else cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; long long a[100005], n, c, mn[100005], dp[200005], sum; int main() { ios_base::sync_with_stdio(0); cin >> n >> c; for (int i = 0; i < n; sum += a[i++]) cin >> a[i]; deque<long long> v; for (int i = n; i--;) { while (v.size() && v.back() >= i + c) v.pop_back(); while (v.size() && a[i] <= a[v.front()]) v.pop_front(); v.push_front(i); if (i <= n - c) dp[i] = max(dp[i + 1], a[v.back()] + dp[i + c]); } cout << sum - dp[0]; } |
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline T1 max(T1 a, T2 b) { return a < b ? b : a; } template <typename T1, typename T2> inline T1 min(T1 a, T2 b) { return a < b ? a : b; } const char lf = n ; namespace ae86 { const int bufl = 1 << 15; char buf[bufl], *s = buf, *t = buf; inline int fetch() { if (s == t) { t = (s = buf) + fread(buf, 1, bufl, stdin); if (s == t) return EOF; } return *s++; } inline int ty() { int a = 0; int b = 1, c = fetch(); while (!isdigit(c)) b ^= c == - , c = fetch(); while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); return b ? a : -a; } } // namespace ae86 using ae86::ty; const int _ = 100007, __ = _ << 1; const long long linf = 0x3f3f3f3f3f3f3f3f; int to[__], ne[__], he[_] = {0}, ecnt = 1; inline void adde(int a, int b) { to[++ecnt] = b, ne[ecnt] = he[a], he[a] = ecnt; } inline void addde(int a, int b) { adde(a, b), adde(b, a); } int n, m, qn; int fa[_] = {0}, dep[_] = {0}, sz[_] = {0}, mxch[_] = {0}; void dfs(int x, int ff) { fa[x] = ff, dep[x] = dep[ff] + 1, sz[x] = 1, mxch[x] = 0; for (int i = he[x]; i; i = ne[i]) { int b = to[i]; if (b == ff) continue; dfs(b, x), sz[x] += sz[b]; if (sz[mxch[x]] < sz[b]) mxch[x] = b; } } int dfn[_] = {0}, rdfn[_] = {0}, ndf[_] = {0}, top[_] = {0}, dcnt = 0; void dfs2(int x, int tp) { dfn[x] = ++dcnt, rdfn[dcnt] = x, top[x] = tp; if (mxch[x]) dfs2(mxch[x], tp); for (int i = he[x]; i; i = ne[i]) { int b = to[i]; if (b == fa[x] || b == mxch[x]) continue; dfs2(b, b); } ndf[x] = dcnt; } vector<int> hav[_]; struct cxk { int x; long long v; cxk(int x_ = 0, long long v_ = linf) { x = x_, v = v_; } friend bool operator<(cxk a, cxk b) { if (a.v != b.v) return a.v < b.v; return a.x < b.x; } }; const int _t = _ << 2; cxk t[_t]; long long tadd[_t]; void up(int x) { t[x] = min(t[(x << 1)], t[(x << 1 | 1)]); } void putadd(int x, long long d) { t[x].v += d, tadd[x] += d; } void dwn(int x) { if (tadd[x]) putadd((x << 1), tadd[x]), putadd((x << 1 | 1), tadd[x]), tadd[x] = 0; } void plant(int x, int l, int r) { tadd[x] = 0; if (l == r) { t[x] = hav[l].empty() ? cxk() : cxk(l, hav[l].back()); return; } plant((x << 1), l, ((l + r) >> 1)), plant((x << 1 | 1), ((l + r) >> 1) + 1, r), up(x); } void change(int x, int l, int r, int tl, int tr, long long dlt) { if (tl <= l && r <= tr) { putadd(x, dlt); return; } dwn(x); if (tl <= ((l + r) >> 1)) change((x << 1), l, ((l + r) >> 1), tl, tr, dlt); if (tr > ((l + r) >> 1)) change((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr, dlt); up(x); } void dating(int x, int l, int r, int tar) { if (l == r) { if (!hav[l].empty()) hav[l].pop_back(); t[x] = hav[l].empty() ? cxk() : cxk(l, hav[l].back() + tadd[x]); return; } dwn(x); if (tar <= ((l + r) >> 1)) dating((x << 1), l, ((l + r) >> 1), tar); else dating((x << 1 | 1), ((l + r) >> 1) + 1, r, tar); up(x); } cxk finder(int x, int l, int r, int tl, int tr) { if (tl <= l && r <= tr) return t[x]; dwn(x); if (tr <= ((l + r) >> 1)) return finder((x << 1), l, ((l + r) >> 1), tl, tr); if (tl > ((l + r) >> 1)) return finder((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr); return min(finder((x << 1), l, ((l + r) >> 1), tl, tr), finder((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr)); } cxk founder(int a, int b) { cxk ans; while (top[a] != top[b]) { if (dep[top[a]] < dep[top[b]]) swap(a, b); ans = min(ans, finder(1, 1, n, dfn[top[a]], dfn[a])), a = fa[top[a]]; } if (dep[a] > dep[b]) swap(a, b); return min(ans, finder(1, 1, n, dfn[a], dfn[b])); } int main() { ios::sync_with_stdio(0), cout.tie(nullptr); n = ty(), m = ty(), qn = ty(); for (int i = 1, a, b; i < n; i++) a = ty(), b = ty(), addde(a, b); dfs(1, 0), dfs2(1, 1); for (int i = 1; i <= m; i++) hav[dfn[ty()]].emplace_back(i); for (int i = 1; i <= n; i++) sort(hav[i].begin(), hav[i].end(), greater<int>()); plant(1, 1, n); for (int qq = 1; qq <= qn; qq++) { int op = ty(); if (op == 1) { int a = ty(), b = ty(), lim = ty(); vector<int> ans; while (lim--) { int ret = founder(a, b).x; if (ret <= 0) break; ans.emplace_back(hav[ret].back()), dating(1, 1, n, ret); } cout << ans.size(); for (auto i : ans) cout << << i; cout << lf; } if (op == 2) { int a = ty(), b = ty(); change(1, 1, n, dfn[a], ndf[a], b); } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 1010; int n; int a[MAX], c[MAX]; void init() { cin >> n; } void process() { memset(a, 63, sizeof(a)); vector<pair<int, int> > ques; ques.push_back(make_pair(1, n)); while (!ques.empty()) { vector<pair<int, int> > newQues; for (int i = 0; i < ques.size(); i++) { if (ques[i].first != ques[i].second) { int l = ques[i].first, r = ques[i].second, mid = (l + r) / 2; newQues.push_back(make_pair(l, mid)); newQues.push_back(make_pair(mid + 1, r)); } } vector<int> b; for (int i = 0; i < newQues.size(); i += 2) { for (int j = newQues[i].first; j <= newQues[i].second; j++) b.push_back(j); } if (b.size() > 0) { cout << b.size() << n ; for (int i = 0; i < b.size(); i++) cout << b[i] << ; cout << n ; fflush(stdout); for (int i = 1; i <= n; i++) cin >> c[i]; for (int i = 1; i <= n; i++) { bool check = true; for (int j = 0; j < b.size(); j++) { if (b[j] == i) { check = false; break; } } if (check) a[i] = min(a[i], c[i]); } } b.resize(0); for (int i = 1; i < newQues.size(); i += 2) { for (int j = newQues[i].first; j <= newQues[i].second; j++) b.push_back(j); } if (b.size() > 0) { cout << b.size() << n ; for (int i = 0; i < b.size(); i++) cout << b[i] << ; cout << n ; fflush(stdout); for (int i = 1; i <= n; i++) cin >> c[i]; for (int i = 1; i <= n; i++) { bool check = true; for (int j = 0; j < b.size(); j++) { if (b[j] == i) { check = false; break; } } if (check) a[i] = min(a[i], c[i]); } } ques = newQues; } cout << -1 << n ; for (int i = 1; i <= n; i++) cout << a[i] << ; cout << n ; fflush(stdout); } int main() { ios_base::sync_with_stdio(false); init(); process(); } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 300; long long orign[MAXN], str[2 * MAXN]; int radius[2 * MAXN]; int n, m; int mp[MAXN][MAXN]; int num[MAXN][26]; long long hashv[MAXN]; long long base[30]; int siz[MAXN]; bool check(int x, int y) { for (int i = 0; i < 26; i++) { if (num[x][i] != num[y][i]) return false; } return true; } void Manacher(int len, int mark = 0) { for (int i = 0; i < len; i++) { str[2 * i + 1] = orign[i]; str[2 * i] = mark; } str[2 * len] = mark; len = 2 * len + 1; int max_right = 0, pos = 0; for (int i = 0; i < len; i++) { if (i < max_right) radius[i] = min(radius[2 * pos - i], max_right - i); else radius[i] = 1; while (i - radius[i] >= 0 && i + radius[i] < len && str[i - radius[i]] > -1 && str[i + radius[i]] > -1 && check(str[i - radius[i]], str[i + radius[i]])) { radius[i]++; } if (i + radius[i] >= max_right) { max_right = i + radius[i]; pos = i; } } } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; base[0] = 1; for (int i = 1; i <= 26; i++) base[i] = base[i - 1] * 2; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char ch; cin >> ch; mp[i][j] = ch - a ; } } long long ans = 0; for (int l = 1; l <= m; l++) { memset(num, 0, sizeof(num)); memset(hashv, 0, sizeof(hashv)); memset(siz, 0, sizeof(siz)); for (int r = l; r <= m; r++) { for (int i = 1; i <= n; i++) { int id = mp[i][r]; num[i][id]++; hashv[i] += base[id]; if (num[i][id] & 1) siz[i]++; else siz[i]--; if (siz[i] > 1) orign[i - 1] = -i; else orign[i - 1] = i; } Manacher(n); for (int i = 0; i < 2 * n + 1; i++) { if (str[i] > -1) ans += radius[i] / 2; } } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 10000010; int p[N], num, last[N]; bool flag[N]; int cnt[N], n, m; void make_prime() { for (int i = 2; i < N; ++i) if (!flag[i]) { p[num++] = i; last[i] = i; for (int j = i + i; j < N; j += i) flag[j] = true, last[j] = i; } } int main() { make_prime(); scanf( %d , &n); for (int i = 0, x; i < n; ++i) { scanf( %d , &x); int s = -1; while (x != 1) { if (last[x] != s) cnt[last[x]]++; s = last[x]; x /= last[x]; } } for (int i = 1; i < N; ++i) cnt[i] += cnt[i - 1]; scanf( %d , &m); for (int i = 0, x, y; i < m; ++i) { scanf( %d %d , &x, &y); if (x >= N) puts( 0 ); else { y = min(y, N - 1); printf( %d n , cnt[y] - cnt[x - 1]); } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 2005; const int M = 730; int Head[M * 10], Next[M * 100], vet[M * 100]; int a[N], b[N], d[M * 10], vis[15], p[M][5], dis[M], Dis[M], P[10], len[M], U[M][12], D[M][12]; int n, cnt, edgenum; pair<int, int> c[M]; queue<int> q; inline int F(int x, int y) { return (x - 1) * cnt + y; } inline void addedge(int u, int v) { edgenum++; vet[edgenum] = v; Next[edgenum] = Head[u]; Head[u] = edgenum; } int main() { scanf( %d , &n); int ans = INF; for (int i = 1; i <= n; i++) scanf( %d%d , &a[i], &b[i]); len[cnt = 1] = 0; for (int i = 1; i <= 9; i++) { len[++cnt] = 1; p[cnt][1] = i; for (int j = i; j <= 9; j++) { len[++cnt] = 2; p[cnt][1] = i, p[cnt][2] = j; for (int k = j; k <= 9; k++) { len[++cnt] = 3; p[cnt][1] = i, p[cnt][2] = j, p[cnt][3] = k; for (int t = k; t <= 9; t++) len[++cnt] = 4, p[cnt][1] = i, p[cnt][2] = j, p[cnt][3] = k, p[cnt][4] = t; } } } for (int i = 2; i <= cnt; i++) { memset(vis, 0, sizeof(vis)); for (int j = 1; j <= len[i]; j++) vis[p[i][j]] = 1; for (int j = 1; j <= 9; j++) { if (!vis[j]) continue; int res = j, cur = 0; for (int k = 1; k <= len[i]; k++) if (p[i][k] == res) res = -1; else P[++cur] = p[i][k]; for (int k = 1; k <= cnt; k++) { if (len[k] != cur) continue; bool flag = 1; for (int t = 1; t <= cur; t++) flag &= (P[t] == p[k][t]); if (flag) { D[i][j] = k; U[k][j] = i; break; } } } } memset(dis, 0x3f, sizeof(dis)); dis[1] = a[1] - 1; for (int j = 1; j < 9; j++) for (int k = 1; k <= cnt; k++) addedge(F(j, k), F(j + 1, k)), addedge(F(j + 1, k), F(j, k)); for (int j = 1; j <= 9; j++) for (int k = 1; k <= cnt; k++) if (D[k][j]) addedge(F(j, k), F(j, D[k][j])); for (int i = 1; i <= n; i++) { memset(Dis, 0x3f, sizeof(Dis)); for (int j = 1; j <= cnt; j++) if (U[j][b[i]]) Dis[U[j][b[i]]] = min(Dis[U[j][b[i]]], dis[j] + 1); for (int j = 1; j <= cnt; j++) dis[j] = Dis[j]; memset(d, 0x3f, sizeof(d)); for (int j = 1; j <= cnt; j++) d[F(a[i], j)] = dis[j], c[j] = make_pair(dis[j], j); sort(c + 1, c + 1 + cnt); for (int j = 1; j <= cnt; j++) if (c[j].first != INF) q.push(F(a[i], c[j].second)); while (!q.empty()) { int u = q.front(); q.pop(); for (int e = Head[u]; e; e = Next[e]) { int v = vet[e]; if (d[v] > d[u] + 1) d[v] = d[u] + 1, q.push(v); } } if (i == n) for (int j = 1; j <= 9; j++) ans = min(ans, d[F(j, 1)]); else for (int j = 1; j <= cnt; j++) dis[j] = d[F(a[i + 1], j)]; } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 3003 * 6006; const int qwq = 303030; const int inf = 0x3f3f3f3f; int n, m, k; int f[N], fa[N], st[123], cnt; int ans; int ff[] = {1, 1, 1, 0, -1, -1, -1, 0}; int gg[] = {1, 0, -1, -1, -1, 0, 1, 1}; inline int read() { int sum = 0, f = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) f = -1; c = getchar(); } while (c >= 0 && c <= 9 ) { sum = sum * 10 + c - 0 ; c = getchar(); } return sum * f; } inline int dian(int x, int y) { if (y == 0) y = 2 * m; if (y == 2 * m + 1) y = 1; return (x - 1) * (m << 1) + y; } int find(int u) { while (fa[u] != u) u = fa[u]; return u; } int Splay(int u) { return fa[u] == u ? u : fa[u] = Splay(fa[u]); } int main() { int x, y; n = read(); m = read(); k = read(); if (m == 1) { cout << 0; return 0; } for (int i = 1; i <= n * m * 2; i++) fa[i] = i; while (k--) { cnt = 0; x = read(); y = read(); for (int i = 0; i <= 8; i++) { if (x + ff[i] < 1 || x + ff[i] > n) continue; int u = dian(x + ff[i], y + gg[i]); if (!f[u]) continue; int aa = find(u); int bb = find(dian(x, y)); if (aa == bb) continue; st[++cnt] = bb; fa[bb] = aa; } y += m; for (int i = 0; i < 8; i++) { if (x + ff[i] < 1 || x + ff[i] > n) continue; int u = dian(x + ff[i], y + gg[i]); if (!f[u]) continue; int aa = find(u); int bb = find(dian(x, y)); if (aa == bb) continue; st[++cnt] = bb; fa[bb] = aa; } y -= m; if (find(dian(x, y)) != find(dian(x, y + m))) { ans++; for (int i = 0; i < 8; i++) { if (x + ff[i] < 1 || x + ff[i] > n) continue; Splay(dian(x + ff[i], y + gg[i])); } y += m; for (int i = 0; i < 8; i++) { if (x + ff[i] < 1 || x + ff[i] > n) continue; Splay(dian(x + ff[i], y + gg[i])); } y -= m; f[dian(x, y)] = f[dian(x, y + m)] = 1; } else { for (int i = 1; i <= cnt; i++) fa[st[i]] = st[i]; } } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, sum; int main() { cin >> n; while (n != 0) { if (n % 8 == 1) sum++; n /= 8; } cout << sum; return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; const int MOD = 1000000007; bool isPowerOfTwo(long long int x) { return x && (!(x & (x - 1))); } void fastio() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[] = {0, -1, 0, 1, 1, -1, -1, 1}; int main() { fastio(); int t = 1; while (t--) { int n; cin >> n; string s; cin >> s; int ans = 0; for (int i = 0; i < (s.length()); i++) { int x = int(s[i]) - 48; if (x % 2 == 0) ans += (i + 1); } cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; map<long long, int> mp; int n; int main() { scanf( %d , &n); int t; long long s = 0; for (int i = 1; i <= n; i++) { scanf( %d , &t); s += t; mp[s]++; } int ans = 0; map<long long, int>::iterator it; for (it = mp.begin(); it != mp.end(); it++) { ans = max(ans, it->second); } printf( %d n , n - ans); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> edge[100005]; int v[100005]; bool cycle = false; int mx = -1, last; void dfs(int u, int dis = 0) { v[u] = 1; if (dis > mx) { mx = dis; last = u; } for (int i = 0; i <= (int)((int)edge[u].size() - 1); i++) { int first = edge[u][i]; if (!v[first]) dfs(first, dis + 1); } } int main() { int T, n, m, a, b; scanf( %d , &n); scanf( %d , &m); for (int i = 1; i <= (int)(m); i++) { scanf( %d%d , &a, &b); edge[a].push_back(b); edge[b].push_back(a); } dfs(1); memset(v, 0, sizeof(v)); mx = -1; dfs(last); printf( %d , mx); return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; const double EPS = 1e-10; vector<int> G[50010]; vector<int> chil[50010]; int cnt[50010][510]; void dfs(int pos, int par) { for (int i = 0; i < (int)G[pos].size(); i++) { if (G[pos][i] != par) { chil[pos].push_back(G[pos][i]); dfs(G[pos][i], pos); } } } void init(int n) { memset(cnt, 0, sizeof(cnt)); dfs(0, -1); } void calc(int v, int k, long long &sum) { for (int i = 0; i < (int)chil[v].size(); i++) { int c = chil[v][i]; calc(c, k, sum); for (int j = 0; j < k; j++) { sum += (long long)cnt[c][j] * cnt[v][k - j - 1]; } for (int j = 0; j < k; j++) { cnt[v][j + 1] += cnt[c][j]; } } cnt[v][0] = 1; sum += (long long)cnt[v][0] * cnt[v][k]; } int main() { int n, k, a, b; cin >> n >> k; for (int i = 0; i < n - 1; i++) { cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } init(n); long long res = 0; calc(0, k, res); cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct P { int x, y; bool operator<(const P &p) { if (x != p.x) return x < p.x; else return y < p.y; } }; int main() { int T; cin >> T; for (int t = 0; t < T; ++t) { int n; cin >> n; int bsz = n / 3; vector<int> buf(bsz); for (int i = 0; i < n / 3; ++i) { cout << ? << 3 * i + 1 << << 3 * i + 2 << << 3 * i + 3 << endl; cout.flush(); cin >> buf[i]; if (buf[i] == -1) { break; } } int ind0, ind1; for (int i = 0; i < bsz; ++i) { int indl = i; int indr = (i + 1) % bsz; if (buf[indl] != buf[indr]) { vector<int> tbuf(6); tbuf[0] = buf[indl]; for (int j = 1; j <= 5; ++j) { int v1 = (3 * indl + j) % n + 1; int v2 = (3 * indl + j + 1) % n + 1; int v3 = (3 * indl + j + 2) % n + 1; cout << ? << v1 << << v2 << << v3 << endl; cout.flush(); cin >> tbuf[j]; if (tbuf[j] == -1) { break; } if (tbuf[j - 1] != tbuf[j]) { if (tbuf[j - 1] == 0) { ind0 = (3 * indl + j - 1) % n + 1; ind1 = v3; } else { ind1 = (3 * indl + j - 1) % n + 1; ind0 = v3; } break; } } break; } } vector<int> mapping(n); mapping[ind1 - 1] = 1; int tmp; for (int i = 0; i < bsz; ++i) { int v1 = 3 * i + 1; int v2 = 3 * i + 2; int v3 = 3 * i + 3; if (buf[i] == 0) { if (v1 == ind1) { cout << ? << ind1 << << ind0 << << v2 << endl; } else if (v2 == ind1) { cout << ? << ind1 << << v1 << << ind0 << endl; } else { cout << ? << ind1 << << v1 << << v2 << endl; } cout.flush(); cin >> tmp; if (tmp == 0) { mapping[v1 - 1] = 0; mapping[v2 - 1] = 0; if (v3 == ind0) { tmp = 0; } else if (v3 == ind1) { tmp = 1; } else { cout << ? << ind0 << << ind1 << << v3 << endl; cout.flush(); cin >> tmp; } mapping[v3 - 1] = tmp; } else { mapping[v3 - 1] = 0; if (ind0 == v1) { tmp = 0; } else if (ind1 == v1) { tmp = 1; } else { cout << ? << ind0 << << ind1 << << v1 << endl; cout.flush(); cin >> tmp; } mapping[v1 - 1] = tmp; mapping[v2 - 1] = 1 - tmp; } } else { if (v1 == ind0) { cout << ? << ind0 << << ind1 << << v2 << endl; } else if (v2 == ind0) { cout << ? << ind0 << << v1 << << ind1 << endl; } else { cout << ? << ind0 << << v1 << << v2 << endl; } cout.flush(); cin >> tmp; if (tmp == 1) { mapping[v1 - 1] = 1; mapping[v2 - 1] = 1; if (v3 == ind0) { tmp = 0; } else if (v3 == ind1) { tmp = 1; } else { cout << ? << ind0 << << ind1 << << v3 << endl; cout.flush(); cin >> tmp; } mapping[v3 - 1] = tmp; } else { mapping[v3 - 1] = 1; if (v1 == ind0) { tmp = 0; } else if (v1 == ind1) { tmp = 1; } else { cout << ? << ind0 << << ind1 << << v1 << endl; cout.flush(); cin >> tmp; } mapping[v1 - 1] = tmp; mapping[v2 - 1] = 1 - tmp; } } } vector<int> fake; for (int i = 0; i < n; ++i) { if (mapping[i] == 0) { fake.push_back(i + 1); } } cout << ! << fake.size(); for (int i = 0; i < fake.size(); ++i) { cout << << fake[i]; } cout << endl; cout.flush(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k, a, b, c, d; scanf( %d%d%d%d%d%d , &n, &k, &a, &b, &c, &d); if (n <= 4 || k < n + 1) { puts( -1 ); return 0; } printf( %d %d , a, c); for (int i = 1; i <= n; i++) { if (i != a && i != b && i != c && i != d) printf( %d , i); } printf( %d %d n , d, b); printf( %d %d , c, a); for (int i = 1; i <= n; i++) { if (i != a && i != b && i != c && i != d) printf( %d , i); } printf( %d %d n , b, d); return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 2e5 + 10; const long long inf = 1e18; const int maxm = 1005; vector<vector<char>> matrix(maxm, vector<char>(maxm)); struct event { int x, y, size; }; int dx[] = {0, 0, 1, -1}; int dy[] = {-1, 1, 0, 0}; class even { public: int a, b, c; }; class evenl { public: int a, b, c, ind; }; struct myComp { constexpr bool operator()(evenl f, evenl s) const noexcept { int z = f.b - f.a; int z2 = s.b - s.a; z -= f.c; z2 -= s.c; return z < z2; } }; long long poww(long long a, long long b, long long m) { if (b == 0) return 1ll; if (b % 2 == 1) return a * poww(a, b - 1, m) % m; long long rs = poww(a, b / 2, m); rs = rs * rs % m; return rs; } long long fact[N]; long long ncr(long long n, long long r) { long long num = fact[n]; long long deno = fact[n - r] * fact[r] % mod; deno = poww(deno, mod - 2, mod); num = num * deno; num = num % mod; return num; } long long convert(long long a, long long b) { string s; while (a > 0) { long long p = a % b; s += p + 0 ; a = a / b; } reverse(s.begin(), s.end()); long long res = stoi(s); return res; } long long make(long long a, long long b, long long c) { while (a < b) a *= c; return a % b; } long long frog_jump(long long pos, long long n) { while (pos % 2 == 0) { pos = pos + (n - pos / 2); } return (pos + 1) / 2; } void solve() { long long n, q; cin >> n >> q; for (int i = 0; i < q; i++) { long long x; cin >> x; if (x % 2 != 0) { cout << (x + 1) / 2 << n ; continue; } long long z = frog_jump(x, n); cout << z << n ; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); } |
#include <bits/stdc++.h> double aa[12]; int sign[12]; int compare(const void *a, const void *b) { int i = *(int *)a; int j = *(int *)b; if (sign[i] != sign[j]) return sign[j] - sign[i]; if (sign[i] == 0) return i - j; return aa[i] == aa[j] ? i - j : aa[i] > aa[j] ? -sign[i] : sign[i]; } void power(int i, double x, double y, double z) { if (x == 1) sign[i] = 0; else if (x > 1) { sign[i] = 1; aa[i] = z * log(y) + log(log(x)); } else { sign[i] = -1; aa[i] = z * log(y) + log(-log(x)); } } void power_(int i, double x, double yz) { if (x == 1) sign[i] = 0; else if (x > 1) { sign[i] = 1; aa[i] = log(yz) + log(log(x)); } else { sign[i] = -1; aa[i] = log(yz) + log(-log(x)); } } int main() { static char ss[12][16] = { x^y^z , x^z^y , (x^y)^z , (x^z)^y , y^x^z , y^z^x , (y^x)^z , (y^z)^x , z^x^y , z^y^x , (z^x)^y , (z^y)^x }; static int ii[12]; double x, y, z; int i; scanf( %lf%lf%lf , &x, &y, &z); power(0, x, y, z); power(1, x, z, y); power_(2, x, y * z); power_(3, x, y * z); power(4, y, x, z); power(5, y, z, x); power_(6, y, x * z); power_(7, y, x * z); power(8, z, x, y); power(9, z, y, x); power_(10, z, x * y); power_(11, z, x * y); for (i = 0; i < 12; i++) ii[i] = i; qsort(ii, 12, sizeof *ii, compare); printf( %s n , ss[ii[0]]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> rightInc(n), leftInc(n); int end = 0, cur_count = 1; for (int i = 0; i < n; ++i) { if (end < i) { end = i, cur_count = 1; } while (end + 1 < n && a[end + 1] >= a[end]) { end++, cur_count++; } rightInc[i] = end - i + 1, cur_count--; } reverse(a.begin(), a.end()); end = 0, cur_count = 1; for (int i = 0; i < n; ++i) { if (end < i) { end = i, cur_count = 1; } while (end + 1 < n && a[end + 1] >= a[end]) { end++, cur_count++; } leftInc[i] = end - i + 1, cur_count--; } reverse(leftInc.begin(), leftInc.end()); while (m--) { int l, r; cin >> l >> r; --l, --r; if (rightInc[l] + leftInc[r] >= r - l + 1) cout << Yes n ; else cout << No n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 3e6 + 7; const int INF = 0x7fffffff; int n, m; struct DATA { int strid; int index; }; vector<DATA> vec; string str[maxn]; char ans[maxn]; bool cmp(DATA a, DATA b) { return a.index < b.index; } int main() { while (cin >> n) { memset(ans, 0, sizeof(ans)); vec.clear(); for (int i = 0; i < n; i++) { int num; cin >> str[i]; scanf( %d , &num); for (int j = 0; j < num; j++) { int k; scanf( %d , &k); k--; DATA t; t.strid = i; t.index = k; vec.push_back(t); } } int last = -1; sort(vec.begin(), vec.end(), cmp); for (int i = 0; i < vec.size(); i++) { for (int j = max(last + 1, vec[i].index); j < vec[i].index + str[vec[i].strid].size(); j++) { ans[j] = str[vec[i].strid][j - vec[i].index]; last = max(last, j); } } for (int i = 0; i <= last; i++) { if (ans[i] != 0) printf( %c , ans[i]); else { printf( a ); } } puts( ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int num[3]; cin >> num[0] >> num[1] >> num[2]; char c1 = * , c2 = * ; if (num[0] == 1) c1 = + ; if (num[2] == 1) c2 = + ; if (num[1] == 1 && num[0] != 1 && num[2] != 1) { cout << max(num[0] * (num[1] + num[2]), (num[0] + num[1]) * num[2]); return 0; } if (c1 == + && c2 == * ) { cout << ((num[0] + num[1]) * num[2]); return 0; } if (c2 == + && c1 == * ) { cout << (num[0] * (num[1] + num[2])); return 0; } if (c1 == + && c2 == + ) { cout << (num[0] + num[1] + num[2]); return 0; } cout << (num[0] * num[1] * num[2]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); long long int arr[100]; for (int i = 0; i < n; i++) cin >> arr[i]; long long int cnt[5]; long long int p[5]; for (int i = 0; i < 5; i++) cin >> p[i]; long long int min = 1000000009; for (int i = 0; i < 5; i++) if (p[i] < min) min = p[i]; for (int i = 0; i < 5; i++) cnt[i] = 0; long long int ps = 0; for (int i = 0; i < n; i++) { ps = ps + arr[i]; while (ps >= min) { long long int pp; for (int j = 4; j >= 0; j--) { if (ps < min) break; pp = ps / p[j]; ps = ps - pp * p[j]; cnt[j] += pp; } } } for (int i = 0; i < 5; i++) cout << cnt[i] << ; cout << n << ps << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long t, n, m, cap, lis[200500], high, low; long long f(long long x) { long long res = 0, cnt = 0, buf = 0; for (int i = (0); i <= ((int)(n)-1); i++) if (lis[i] <= x) { if (!cnt) { res += buf; buf = 0; } cnt++; buf += lis[i]; res += lis[i]; if (cnt == m) { cnt = 0; } } return res; } long long g(long long x) { long long res = 0, task = 0, cnt = 0, buf = 0; for (int i = (0); i <= ((int)(n)-1); i++) if (lis[i] <= x) { if (!cnt) { res += buf; buf = 0; } if (res + lis[i] > cap) break; res += lis[i]; cnt++; buf += lis[i]; task++; if (cnt == m) { cnt = 0; } } return task; } int main() { ios::sync_with_stdio(0); cin >> t; while (t--) { cin >> n >> m >> cap; for (int i = (0); i <= ((int)(n)-1); i++) cin >> lis[i]; high = cap, low = 1; while (high > low) { long long chs = (high + low) / 2; if (f(chs) > cap) high = chs; else low = chs + 1; } long long gh = g(high), gh1 = g(high - 1); if (gh > gh1 || high == 1) cout << gh << << high << endl; else cout << gh1 << << high - 1 << endl; } } |
#include <bits/stdc++.h> using namespace std; long long int dp[2010][2010]; long long int a[2010]; long long int b[2010]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; } for (int i = 0; i <= 2000; i++) { for (int j = 0; j <= 2000; j++) { dp[i][j] = 10000000000000000; } } dp[0][0] = 0; for (int i = 1; i <= n; i++) { long long int o = a[i] + 1; for (int j = 0; j <= n; j++) { o = a[i] + 1; if (j == 0) { dp[i][j] = 0; } if (o >= j) { o = j; } if (j >= o) { dp[i][j] = min(b[i] + dp[i - 1][j - (o)], dp[i][j]); } dp[i][j] = min(dp[i][j], dp[i - 1][j]); } } cout << dp[n][n] << endl; } |
#include <bits/stdc++.h> const long long int MOD = 1e9 + 7; const long long int INF = 1011111111; const long long int LLINF = 1000111000111000111LL; const long double EPS = 1e-10; const long double PI = 3.14159265358979323; using namespace std; long long int power(int x, int n) { if (n == 0) return 1; else if (n % 2 == 0) return power((x * x), n / 2); else return x * power((x * x), (n - 1) / 2); } long long int dp[5005][5005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m, i, j, k; cin >> n >> k; vector<long long int> v(n); for (i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); vector<pair<pair<long long int, long long int>, long long int> > check; vector<long long int>::iterator it; for (i = 0; i < n; i++) { long long int idx; it = upper_bound(v.begin(), v.end(), v[i] + 5); if (it == v.end()) idx = n - 1; else { idx = it - v.begin(); idx--; } check.push_back({{i, idx}, idx - i + 1}); } long long int ans = 0; for (i = 0; i < n; i++) { for (j = 0; j <= k; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (j + 1 <= k) dp[i + check[i].second][j + 1] = max(dp[i + check[i].second][j + 1], dp[i][j] + check[i].second); } } cout << dp[n][k]; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7; int a[N], n, tree[N], srt[N], ans[N]; void update(int a, int val) { while (a <= n) { tree[a] = max(val, tree[a]); a += a & -a; } } int query(int a) { int mx = 0; while (a > 0) { mx = max(tree[a], mx); a -= a & -a; } return mx; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , a + i); srt[i] = a[i]; } sort(srt + 1, srt + n + 1); for (int i = n; i >= 1; i--) { int idx = lower_bound(srt + 1, srt + n + 1, a[i]) - srt; update(idx, i); ans[i] = query(idx - 1); if (ans[i] == 0) ans[i]--; else ans[i] -= i + 1; } for (int i = 1; i <= n; i++) printf( %d , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 9; int n, maxx; struct p { int lei, num, x; } a[maxn]; int da[maxn], b[maxn], vis[maxn]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> b[i]; int w; cin >> w; for (int i = 1; i <= w; i++) { cin >> a[i].lei; if (a[i].lei == 1) { cin >> a[i].num >> a[i].x; vis[a[i].num] = i; b[a[i].num] = a[i].x; } else { cin >> a[i].x; maxx = max(maxx, a[i].x); } } for (int i = w; i >= 1; i--) { if (a[i].lei == 1) da[i] = da[i + 1]; else da[i] = max(da[i + 1], a[i].x); } for (int i = 1; i <= n; i++) { if (vis[i] == 0) cout << max(b[i], maxx) << ; else { int xu = vis[i]; cout << max(b[i], da[xu]) << ; } } } |
#include <bits/stdc++.h> const int INF = 0x3f3f3f3f; const int N = 579; const long long P = 1000000007; int n, b[N]; long long d[N][N]; long long solve(int l, int r) { if (l == r) return 1; static bool used[N][N]; static long long ans[N][N]; if (used[l][r]) return ans[l][r]; used[l][r] = true; ++l; ++r; d[l][0] = 1; for (int i = 0; i < r - l; ++i) { if (d[l][i + 1] != -1) continue; int c = l + i; d[l][i + 1] = 0; for (int j = 0; j <= i; ++j) if (b[j + l] < b[c + 1]) (d[l][i + 1] += d[l][j] * solve(j + l, c)) %= P; } long long res = 0; for (int j = 0; j <= r - l - 1; ++j) (res += d[l][j] * solve(j + l, r - 1)) %= P; --l; --r; return ans[l][r] = res; } int main() { memset(d, -1, sizeof(d)); std::cin >> n; for (int i = 0; i < n; ++i) std::cin >> b[i]; if (b[0] == 1) std::cout << solve(0, n - 1) << n ; else std::cout << 0 << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a[4]; for (int i = 0; i < 4; i++) cin >> a[i]; int sumtotal = a[0] + a[1] + a[2] + a[3]; if (sumtotal % 2 != 0) { cout << NO ; return 0; } for (int i = 0; i < 4; i++) { if (a[i] == sumtotal - a[i]) { cout << YES ; return 0; } else for (int j = 0; j < 4; j++) { if (i != j) if ((a[i] + a[j]) == (sumtotal - a[i] - a[j])) { cout << YES ; return 0; } } } cout << NO ; } |
#include <bits/stdc++.h> using namespace std; vector<pair<int, double>> ans[52]; int main() { int n, w, m; cin >> n >> w >> m; double s = (n * w) / (m * 1.0), c = w, b = 1, k = 0; for (int i = 0; i < m; i++) { double ne = s; while (1) { if (ne + 10e-7 > c) { ans[i].push_back(make_pair(b, c)); ne -= c; c = w; b++; if (++k > 2) { cout << NO ; return 0; } k = 0; if (abs(ne) < 10e-7) break; } else { ans[i].push_back(make_pair(b, ne)); c -= ne; k++; break; } } } cout << YES n ; for (int i = 0; i < m; i++) { for (auto it : ans[i]) cout << fixed << setprecision(0) << it.first << << setprecision(6) << it.second << ; cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; void quickly_quickly_kro() { long long n; cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; ; map<long long, long long> M, N, I; for (long long i = 0; i < n; i++) { if (v[i] != 0) M[v[i]]++; else I[i]++; } for (long long i = 1; i < n + 1; i++) { if (M[i] == 0) { N[i]++; } } for (auto k : I) { if ((N.find(k.first + 1) != N.end())) { auto it = N.find(k.first + 1); if (it != N.begin()) it--; else it++; v[k.first] = it->first; N.erase(it); } } for (long long i = 0; i < n; i++) { if (v[i] == 0) { auto it = N.begin(); pair<long long, long long> p = *it; v[i] = p.first; N.erase(it); } } for (long long i = 0; i < n; i++) cout << v[i] << ; } signed main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) { quickly_quickly_kro(); cout << n ; ; } return 0; } |
#include <bits/stdc++.h> using namespace std; struct HashMap { vector<pair<int, int> > g[1000007]; void ins(int x, int y) { int id = ((long long)x * 1000000007 + y) % 1000007; g[id].push_back(make_pair(x, y)); } bool get(int x, int y) { int id = ((long long)x * 1000000007 + y) % 1000007; for (int i = 0; i < g[id].size(); i++) if (g[id][i].first == x && g[id][i].second == y) return true; return false; } }; int n; HashMap mp; vector<int> row[100011]; vector<int> rateB; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { int x, y; scanf( %d%d , &x, &y); row[y].push_back(x); mp.ins(x, y); } int di = (int)sqrt(n); long long int ans = 0; for (int a = 0; a <= 100000; a++) { if (row[a].size() != 0) { sort(row[a].begin(), row[a].end()); if (row[a].size() <= di) { for (int i = 0; i < row[a].size(); i++) { for (int j = i + 1; j < row[a].size(); j++) { int x1 = row[a][i]; int x2 = row[a][j]; int dis = x2 - x1; if (mp.get(x1, a + dis) && mp.get(x2, a + dis)) ans++; if (a - dis >= 0 && row[a - dis].size() > di && (mp.get(x1, a - dis) && mp.get(x2, a - dis))) ans++; } } } else rateB.push_back(a); } } for (int i = 0; i < rateB.size(); i++) { for (int j = i + 1; j < rateB.size(); j++) { int a = rateB[i]; int b = rateB[j]; int u = 0, p = 0; int dis = b - a; while (u < row[a].size() && p < row[b].size()) { if (row[a][u] < row[b][p]) u++; else if (row[a][u] > row[b][p]) p++; else { int x1 = row[a][u]; if (mp.get(x1 + dis, a) && mp.get(x1 + dis, b)) ans++; u++; p++; } } } } printf( %I64d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = (int)1.01e9; const double eps = 1e-9; const int maxn = (int)2e5 + 10; set<int> S; int main() { int n, k, a; scanf( %d%d%d , &n, &k, &a); int cur = (n + 1) / (a + 1); assert(cur >= k); int m; scanf( %d , &m); for (int i = 0; (i) < (m); ++i) { int x; scanf( %d , &x); set<int>::iterator it = S.lower_bound(x); int left = 0; int right = n + 1; if (it != S.end()) { right = *it; } if (it != S.begin()) { --it; left = *it; } cur -= (right - left) / (a + 1); cur += (x - left) / (a + 1); cur += (right - x) / (a + 1); if (cur < k) { printf( %d n , i + 1); return 0; } S.insert(x); } printf( -1 n ); return 0; } |
#include <bits/stdc++.h> using namespace std; bool has[200009]; int left[200009]; int main() { int Q; cin >> Q; for (int q = 0; q < Q; q++) { int N; cin >> N; vector<int> v; for (int i = 0; i < N; i++) { int a; cin >> a; v.push_back(a); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int res = v[0]; vector<int> w; vector<int> t; for (int i = 1; i < v.size(); i++) if (v[i] != v[i - 1]) { if (v[0] % v[i] != 0) w.push_back(v[i]); else t.push_back(v[i]); } if (w.size() > 0) { res += w[0]; for (int i = 1; i < w.size(); i++) if (w[0] % w[i] != 0) { res += w[i]; break; } } if (t.size() >= 3 && t[0] + t[1] + t[2] > res) { if (t[0] % t[1] != 0 && t[1] % t[2] != 0 && t[0] % t[2] != 0) res = t[0] + t[1] + t[2]; } if (t.size() >= 4 && t[0] + t[1] + t[3] > res) { if (t[0] % t[1] != 0 && t[1] % t[3] != 0 && t[0] % t[3] != 0) res = t[0] + t[1] + t[3]; } cout << res << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; struct Segment { int l, r, x, ls, rs, fa; } seg[2000100]; int n, k, p[100100], cnt, pnt, b[2000100], w, ans[100100], res; int Dfs(int &id, int l, int r) { id = ++cnt; seg[id].l = l, seg[id].r = r; seg[id].x = 1; if (r - l == 1) return 1; int mid = (l + r) >> 1; seg[id].x += Dfs(seg[id].ls, l, mid); seg[id].x += Dfs(seg[id].rs, mid, r); return seg[id].x; } void Vis(int x) { if (!x) return; b[x] = 1; Vis(seg[x].ls); Vis(seg[x].rs); } bool cmp(int x, int y) { return seg[x].l < seg[y].l; } void Solve(int x) { if (!x) return; if (b[x]) { for (int i = seg[x].l; i < seg[x].r; i++) ans[i] = res++; return; } Solve(seg[x].rs); Solve(seg[x].ls); } int main() { scanf( %d%d , &n, &k); Dfs(w, 0, n); k = seg[1].x - k; for (int i = 1; i <= cnt; i++) if (!b[i]) { if (k >= seg[i].x - 1 && seg[i].x > 1) { k -= seg[i].x - 1; p[++pnt] = i; Vis(i); if (!k) break; } } if (k) printf( -1 ); else { res = 1; Solve(1); for (int i = n - 1; i >= 0; i--) ans[i] = ans[i] ? ans[i] : res++; for (int i = 0; i < n; i++) printf( %d , ans[i]); } return 0; } |
#include <bits/stdc++.h> using namespace std; string s[1100]; int vis[1100]; int num[100]; int main() { ios::sync_with_stdio(false); int n, len, k; cin >> n >> len >> k; string st; cin >> st; for (int i = 0; i < st.size(); i++) { num[st[i] - a ]++; } int l = 0, r = 25; for (int i = k + 1; i <= n; i++) { for (int j = 1; j <= len; j++) { while (!num[r] && r > l) r--; s[i] += (char)(r + a ); num[r]--; } } int now = 0; int pos = 0; while (1) { for (int i = 1; i <= k; i++) { if (vis[i]) continue; if (pos >= 1 && i != k && (int)(s[i][pos - 1] - a ) < now) { for (int j = pos + 1; j <= len; j++) { while (!num[r] && r > l) r--; s[i] += (char)(r + a ); num[r]--; } vis[i] = 1; continue; } while (!num[l] && r > l) l++; s[i] += (char)(l + a ); num[l]--; if (i == k) { now = l; } } pos++; if (pos == len) break; } sort(s + 1, s + 1 + n); for (int i = 1; i <= n; i++) cout << s[i] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int T, n, m, nw, a[110][110], id[110][110], pos[110], p[110]; char ch; bool cmp(int i, int j) { return a[m][i] < a[m][j]; } queue<int> q; void find(int x) { p[x] = 0; int y = id[x][pos[x]]; pos[x]--; if (!p[y]) p[y] = x, p[x] = y; else if (a[y][x] > a[y][p[y]]) { p[p[y]] = 0, q.push(p[y]); p[y] = x, p[x] = y; } if (!p[x]) q.push(x); } void work() { for (int i = 1; i <= 2 * n; i++) { for (int j = 1; j <= n; j++) id[i][j] = (i <= n) * n + j; m = i; sort(id[i] + 1, id[i] + n + 1, cmp); p[i] = 0; } for (int i = 1; i <= n; i++) pos[i] = n, q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); find(u); } for (int i = 1; i <= n; i++) assert(p[i]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) assert(!(a[p[i]][i] < a[p[i]][j] && a[j][p[i]] > a[j][p[j]])); } void solve() { bool falg = 0; scanf( %d , &n); memset(a, 0, sizeof(a)), memset(id, 0, sizeof(id)); for (int i = 1; i <= n; i++) for (int j = n + 1; j <= 2 * n; j++) scanf( %d , &a[i][j]), a[j][i] = -a[i][j]; puts( B ), fflush(stdout); scanf( %c%d , &ch, &nw); if (ch == I ) falg ^= 1; if (nw > n) falg ^= 1; if (falg) for (int i = 1; i <= 2 * n; i++) for (int j = 1; j <= 2 * n; j++) a[i][j] = -a[i][j]; work(); printf( %d n , p[nw]), fflush(stdout); while (~scanf( %d , &nw)) { if (nw == -1) return; printf( %d n , p[nw]), fflush(stdout); } } int main() { scanf( %d , &T); while (T--) solve(); } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 9; vector<int> getBorder(const string str) { int n = str.size(); vector<int> border(n, -1); for (int i = 1, j = -1; i < n; i++) { while (j >= 0 && str[i] != str[j + 1]) { j = border[j]; } if (str[i] == str[j + 1]) j++; border[i] = j; } return border; } int matchPattern(string txt, string pat, const vector<int> border) { int freq = 0; for (int i = 0, j = -1; i < (int)txt.size(); i++) { while (j >= 0 && txt[i] != pat[j + 1]) { j = border[j]; } if (txt[i] == pat[j + 1]) { j++; } if (j + 1 == (int)pat.size()) { freq++; j = border[j]; } } return freq; } string cut(string s, int p, bool pref = true) { if (!pref) { reverse(s.begin(), s.end()); } while ((int)s.size() >= p) s.pop_back(); if (!pref) { reverse(s.begin(), s.end()); } return s; } const int ms = 210; string pref[ms], suf[ms]; long long trans[ms]; int bk = 14; long long getFreq(int n, string pat, long long k) { std::vector<long long> f(n + 1, 0); if (pat == 0 ) f[0]++; if (pat == 1 ) f[1]++; auto border = getBorder(pat); for (int i = 2; i <= n; i++) { long long cur = trans[i - 2]; if (i < bk + 4) { string curString = cut(suf[i - 2], (int)pat.size(), false) + cut(pref[i - 1], (int)pat.size()); cur = matchPattern(curString, pat, border); } f[i] = f[i - 1] + f[i - 2] + cur; if (f[i] > k) return k + 1; trans[i] = cur; } return min(f[n], k + 1); } int main() { pref[0] = suf[0] = 0 ; pref[1] = suf[1] = 1 ; for (int i = 2; i < ms; i++) { pref[i] = cut(pref[i - 2] + pref[i - 1], ms); suf[i] = cut(suf[i - 2] + suf[i - 1], ms, false); } int n, m; long long k; cin >> n >> k >> m; if (n == 0) { cout << 0 n ; return 0; } if (n == 1) { cout << 1 n ; return 0; } string ans; k--; while (k >= 0 && (int)ans.size() < m) { if (ans.size() > 0 && suf[n].substr(suf[n].size() - ans.size(), ans.size()) == ans) { k--; } if (k < 0) break; ans += 0 ; long long got = getFreq(n, ans, k); if (got <= k) { ans.back() = 1 ; k -= got; } } cout << ans << endl; } |