solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int maxn = 15e2 + 20; int l[maxn], r[maxn], a[maxn], n, s, m, k; int dp[maxn][maxn]; bool check(int x) { memset(dp, 0, sizeof dp); for (int i = 1; i <= n; i++) { int last = i + 5; for (int j = 0; j < s; j++) if (l[j] <= i && i <= r[j]) last = min(last, l[j]); int sum = 0; for (int j = last; j <= i; j++) if (a[j] <= x) sum++; for (int j = 1; j <= m; j++) dp[i][j] = max(dp[i - 1][j], dp[last - 1][j - 1] + sum); } return (dp[n][m] >= k); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> s >> m >> k; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i < s; i++) cin >> l[i] >> r[i]; int l = 0, r = 1e9 + 20; while (r - l > 1) { int mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid; } if (!check(r)) r = -1; cout << r << endl; }
8
#include <bits/stdc++.h> using namespace std; const int MaxN = int(2e6) + 256; const long long INF = (long long)(1e18); const int mod = 10056; const double pi = 3.1415926535897932384626433832795; long long n, m, ans, a[MaxN], b[MaxN], d[MaxN]; bool used[MaxN]; vector<int> g[MaxN]; void dfs(int v, int c) { d[v] = c; used[v] = 1; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!used[to]) { dfs(to, (c + 1) % 2); } } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } for (int i = 1; i <= 2 * n; i += 2) { g[i].push_back(i + 1); g[i + 1].push_back(i); } for (int i = 1; i <= 2 * n; i++) { if (!used[i]) { dfs(i, 0); } } for (int i = 1; i <= n; i++) { cout << d[a[i]] + 1 << " " << d[b[i]] + 1 << "\n"; } return 0; }
9
#include <bits/stdc++.h> using namespace std; void solve() { int n, x, m; cin >> n >> x >> m; x--; int i, j; int start = x, end = x; for (i = 0; i < m; i++) { int l, r; cin >> l >> r; l--; r--; if (max(l, start) <= min(r, end)) { start = min(l, start); end = max(r, end); } } cout << end - start + 1 << endl; } int main() { int t; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct vertex { long long sum, dist; int cnt; vertex(long long sum, long long dist, int cnt) { this->sum = sum; this->dist = dist; this->cnt = cnt; } vertex() { sum = dist = cnt = 0; } }; int x[100100]; int b[100100]; int ty[100100]; int l[100100]; int r[100100]; vector<int> a; vertex t[2000010]; int k; inline vertex merge(vertex a, vertex b) { vertex res; res.sum = a.sum + b.sum; res.cnt = a.cnt + b.cnt; res.dist = b.sum * a.cnt - a.sum * b.cnt + a.dist + b.dist; return res; } inline void mod(int p, vertex v) { p += k - 1; t[p] = v; p >>= 1; while (p) { t[p] = merge(t[p + p], t[p + p + 1]); p >>= 1; } } inline vertex find(int l, int r, int L, int R, int v) { if ((l == L) && (r == R)) return t[v]; int m = (L + R) / 2; if (r <= m) return find(l, r, L, m, v + v); if (l > m) return find(l, r, m + 1, R, v + v + 1); return merge(find(l, m, L, m, v + v), find(m + 1, r, m + 1, R, v + v + 1)); } int main() { int n; scanf("%d", &n); for (int i = (0); i < (n); ++i) { scanf("%d", &x[i]); b[i] = x[i]; a.push_back(x[i]); } int m; scanf("%d", &m); for (int i = (0); i < (m); ++i) { scanf("%d%d%d", &ty[i], &l[i], &r[i]); if (ty[i] == 1) { b[l[i] - 1] += r[i]; a.push_back(b[l[i] - 1]); } else { a.push_back(l[i]); a.push_back(r[i]); } } sort(a.begin(), a.end()); a.resize(unique(a.begin(), a.end()) - a.begin()); k = 1; while (k < a.size()) k += k; for (int i = (0); i < (n); ++i) { int v = lower_bound(a.begin(), a.end(), x[i]) - a.begin(); vertex ver = *new vertex(x[i], 0, 1); mod(v + 1, ver); } for (int i = (0); i < (m); ++i) { if (ty[i] == 1) { int v = lower_bound(a.begin(), a.end(), x[l[i] - 1]) - a.begin(); mod(v + 1, *new vertex()); x[l[i] - 1] += r[i]; v = lower_bound(a.begin(), a.end(), x[l[i] - 1]) - a.begin(); vertex ver = *new vertex(x[l[i] - 1], 0, 1); mod(v + 1, ver); } else { int l1 = lower_bound(a.begin(), a.end(), l[i]) - a.begin(); int r1 = lower_bound(a.begin(), a.end(), r[i]) - a.begin(); vertex v = find(l1 + 1, r1 + 1, 1, k, 1); printf("%I64d\n", v.dist); } } return 0; }
8
#include <bits/stdc++.h> using namespace std; vector<int> adj[10001]; vector<bool> visit(10001, false); void dfs(int n) { visit[n] = true; int i, s = adj[n].size(); for (i = 0; i <= s - 1; i++) if (!visit[adj[n][i]]) dfs(adj[n][i]); } int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, i, j, c = 0; cin >> n; for (i = 1; i <= n; i++) { cin >> j; adj[i].push_back(j); adj[j].push_back(i); } for (i = 1; i <= n; i++) { if (!visit[i]) { c++; dfs(i); } } cout << c; return 0; }
2
#include <bits/stdc++.h> using namespace std; int di[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dj[] = {1, -1, 0, 0, 1, -1, 1, -1}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; if (N % 4 > 1) { cout << -1 << '\n'; return 0; } if (N == 1) { cout << 1 << '\n'; return 0; } int a = 2, b = N; int SZ = N / 4; vector<int> ans; while (SZ--) { ans.push_back(a); ans.push_back(b); a += 2, b -= 2; } a = 1, b = N - 1; SZ = N / 4; while (SZ--) { ans.push_back(b); ans.push_back(a); a += 2; b -= 2; } if (N % 2) ans.push_back(N / 2 + 1); reverse(ans.begin() + N / 2, ans.end()); for (int i = 0; i < ((int)ans.size()); i++) { cout << ans[i] << " \n"[i == ((int)ans.size()) - 1]; } return 0; }
3
#include<bits/stdc++.h> using namespace std; int main() { int t,n,m,i,j; cin>>t; while(t--) { cin>>n>>m; int dp[n+1][m+1]; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { char c; cin>>c; if(c=='*') dp[i][j]=1; } } for(i=n-1;i>=1;i--) { for(j=m-1;j>=1;j--) { if(dp[i][j]) { dp[i][j]+=min(dp[i+1][j-1],min(dp[i+1][j],dp[i+1][j+1])); } } } int sum=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { sum+=dp[i][j]; } } cout<<sum<<"\n"; } }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, l = 0, r = 0; cin >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; if (x < 0) l++; else r++; } if (l <= 1 || r <= 1) cout << "YES"; else cout << "NO"; }
0
#include <bits/stdc++.h> using namespace std; static const int maxn = 100010; int n; int digit[10]; int main() { while (~scanf("%d", &n)) { memset(digit, false, sizeof digit); int sum = 0; for (int i = 0; i < n; ++i) { int t; scanf("%d", &t); sum += t; digit[t]++; } if (digit[0] == 0) { printf("-1\n"); continue; } if (sum == 0) { printf("0\n"); continue; } int r = sum % 3; if (!r) { for (int i = 9; i >= 0; --i) { for (int j = 0; j < digit[i]; ++j) { printf("%d", i); } } printf("\n"); continue; } bool solved = false; int ind = -1; for (int i = 0; i < 9; ++i) { if (i % 3 == r && digit[i]) { digit[i]--; sum -= i; ind = i; solved = true; break; } } if (solved) { if (sum == 0) { printf("0\n"); } else { for (int i = 9; i >= 0; --i) { for (int j = 0; j < digit[i]; ++j) { printf("%d", i); } } printf("\n"); } continue; } r = r == 1 ? 2 : 1; int cnt = 0; for (int i = 0; i < 9; ++i) { if (i % 3 == r && digit[i]) { digit[i]--; sum -= i; ind = i; cnt++; break; } } for (int i = 0; i < 9; ++i) { if (i % 3 == r && digit[i]) { digit[i]--; sum -= i; ind = i; cnt++; break; } } if (cnt == 2) { solved = true; } if (solved) { if (sum == 0) { printf("0\n"); } else { for (int i = 9; i >= 0; --i) { for (int j = 0; j < digit[i]; ++j) { printf("%d", i); } } printf("\n"); } } else { printf("0\n"); } } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct node { long long flag; long long number; long long len; long long pre_len; } vis[100000 + 10]; int Solve(long long repos, long long n) { int l, r, pos, x; l = 0; r = n - 1; while (l <= r) { x = (l + r) / 2; if (vis[x].len <= repos) { pos = x; l = x + 1; } else r = x - 1; } if (vis[pos].flag == 1) return vis[pos].number; else return Solve((repos - vis[pos].len) % vis[pos].pre_len + 1, n); } int main() { long long n, m, a, b, c, pos, length; cin >> n; length = 1; for (int i = 0; i < n; i++) { cin >> a >> b; if (a == 1) { vis[i].flag = 1; vis[i].number = b; vis[i].len = length; length += 1; } else { cin >> c; vis[i].flag = 2; vis[i].len = length; vis[i].pre_len = b; length += b * c; } } cin >> m; for (int i = 0; i < m; i++) { cin >> pos; cout << Solve(pos, n) << ' '; } cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<int> E[300010]; int ans[300010]; int p[300010]; inline int find(int a) { if (p[a] == a) return a; p[a] = find(p[a]); return p[a]; } inline void _union(int a, int b) { p[find(a)] = find(b); find(a); } int depth; int v; inline void dfs(int u, int last, int d) { if (d > depth) { depth = d; v = u; } for (int i = 0; i < E[u].size(); i++) { if (E[u][i] == last) continue; dfs(E[u][i], u, d + 1); } } int main() { int n, m, q; while (cin >> n >> m >> q) { for (int i = 1; i <= n; i++) p[i] = i; for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); E[u].push_back(v); E[v].push_back(u); if (find(u) != find(v)) _union(u, v); } for (int i = 1; i <= n; i++) { if (!ans[find(i)]) { v = i; depth = 0; dfs(i, -1, 0); depth = 0; dfs(v, -1, 0); ans[find(i)] = depth; } } for (int i = 1; i <= q; i++) { int type; scanf("%d", &type); if (type == 2) { int u, v; scanf("%d%d", &u, &v); if (find(u) != find(v)) { int tmp = max((ans[find(u)] + 1) / 2 + (ans[find(v)] + 1) / 2 + 1, max(ans[find(u)], ans[find(v)])); _union(u, v); ans[find(u)] = tmp; } } else { int u; scanf("%d", &u); printf("%d\n", ans[find(u)]); } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, p, q, a, b; bool visited[10001][10001]; int d[1001][1001]; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; queue<int> qx, qy; inline double sqr(double z) { return z * z; } bool in(double x1, double y1, double x2, double y2, double x3, double y3) { double ax = sqrt(sqr(x1 - x3) + sqr(y1 - y3)); double bx = sqrt(sqr(x2 - x3) + sqr(y2 - y3)); double ab = sqrt(sqr(x2 - x1) + sqr(y2 - y1)); return abs(ax + bx - ab) <= 0.000000001; } int main() { cin >> n >> a >> b >> p >> q; visited[a][b] = 1; qx.push(a); qy.push(b); double nn = n; while (!qx.empty() && !qy.empty()) { int x = qx.front(); int y = qy.front(); qx.pop(); qy.pop(); for (int j = 0; j < 4; j++) { int u = x + dx[j]; int v = y + dy[j]; double du = u; double dv = v; bool in1 = in(0, 0, 0, nn, du, dv); bool in2 = in(0, nn, nn, nn, du, dv); bool in3 = in(n, 0, nn, nn, du, dv); bool in4 = in(0, 0, nn, 0, du, dv); bool edge = in1 || in2 || in3 || in4; if (visited[u][v] == 0 && edge) { visited[u][v] = 1; qx.push(u); qy.push(v); d[u][v] = d[x][y] + 1; } } } cout << d[p][q] << endl; }
2
#include <bits/stdc++.h> using namespace std; double a[310], b[310], c[310]; struct POINT { double x; double y; POINT(double a = 0, double b = 0) { x = a; y = b; } }; struct LINE { double a; double b; double c; LINE(double d1 = 1, double d2 = -1, double d3 = 0) { a = d1; b = d2; c = d3; } }; struct LINESEG { POINT s; POINT e; LINESEG(POINT a, POINT b) { s = a; e = b; } LINESEG() {} }; double dotmultiply(POINT p1, POINT p2, POINT p0) { return ((p1.x - p0.x) * (p2.x - p0.x) + (p1.y - p0.y) * (p2.y - p0.y)); } double dist(POINT p1, POINT p2) { return (sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y))); } bool lineintersect(LINE l1, LINE l2, POINT &p) { double d = l1.a * l2.b - l2.a * l1.b; if (abs(d) < 1e-8) return false; p.x = (l2.c * l1.b - l1.c * l2.b) / d; p.y = (l2.a * l1.c - l1.a * l2.c) / d; return true; } LINE makeline(POINT p1, POINT p2) { LINE tl; int sign = 1; tl.a = p2.y - p1.y; if (tl.a < 0) { sign = -1; tl.a = sign * tl.a; } tl.b = sign * (p1.x - p2.x); tl.c = sign * (p1.y * p2.x - p1.x * p2.y); return tl; } bool OnSeg(LINESEG a, POINT b) { POINT p1 = POINT(a.s.x - b.x, a.s.y - b.y); POINT p2 = POINT(a.e.x - b.x, a.e.y - b.y); return (p1.x * p2.y - p1.y * p2.x == 0 && (b.x - a.s.x) * (b.x - a.e.x) <= 0 && (b.y - a.s.y) * (b.y - a.e.y) <= 0); } int main() { double x1, y1, x2, y2; int n; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lf%lf%lf", a + i, b + i, c + i); int ans = 0; for (int i = 1; i <= n; i++) { if ((a[i] * x1 + b[i] * y1 + c[i]) * (a[i] * x2 + b[i] * y2 + c[i]) < 0) ans++; } cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1000000 + 10, MOD = 1000 * 1000 * 1000 + 7; int main() { int n, m, i; cin >> n >> m; int a, one, minus; one = minus = 0; for (i = 1; i <= n; i++) { cin >> a; if (a == 1) one++; else minus++; } int l, r, M; for (i = 0; i < m; i++) { cin >> l >> r; M = r - l + 1; if (M % 2 == 0 && one >= M / 2 && minus >= M / 2) cout << 1 << endl; else cout << 0 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct point { double x; double y; bool operator<(const point& pt) const { if (fabs(x - pt.x) > 1e-9) return (x < pt.x); if (fabs(y - pt.y) > 1e-9) return (y < pt.y); return false; } }; struct circle { point p; double r; }; double dist(point p1, point p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } int CircleIntersection(circle c1, circle c2, point* p1, point* p2) { double d = dist(c1.p, c2.p); if (d == 0 && c1.r == c2.r) return false; if (d > c1.r + c2.r) return false; if (d < fabs(c1.r - c2.r)) return false; double a = (c1.r * c1.r - c2.r * c2.r + d * d) / (2.0 * d); double h = sqrt(c1.r * c1.r - a * a); point p; p.x = c1.p.x + a * (c2.p.x - c1.p.x) / d; p.y = c1.p.y + a * (c2.p.y - c1.p.y) / d; p1->x = p.x + h * (c2.p.y - c1.p.y) / d; p1->y = p.y - h * (c2.p.x - c1.p.x) / d; p2->x = p.x - h * (c2.p.y - c1.p.y) / d; p2->y = p.y + h * (c2.p.x - c1.p.x) / d; return true; } const int MAXN = 1010; int visited[MAXN]; int edges[MAXN][MAXN]; void dfs(int i, int n) { visited[i] = 1; for (int j = (0); j < (n); j++) if (!visited[j] && edges[i][j]) { dfs(j, n); } } int main() { point p1, p2; int n, V = 0, E = 0, K = 0; vector<circle> v; set<point> points; set<point> circlepoints; cin >> n; for (int i = (0); i < (n); i++) { circle c; cin >> c.p.x >> c.p.y >> c.r; v.push_back(c); } for (int i = (0); i < (n); i++) { for (int j = (0); j < (n); j++) { if (CircleIntersection(v[i], v[j], &p1, &p2) == true) { points.insert(p1); points.insert(p2); circlepoints.insert(p1); circlepoints.insert(p2); edges[i][j] = 1; edges[j][i] = 1; } } E += circlepoints.size(); circlepoints.clear(); } V = points.size(); for (int i = (0); i < (n); i++) if (!visited[i]) { dfs(i, n); K++; } cout << (K + 1 + E - V); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int hash[3002] = {}; int a; for (int i = 0; i < n + 1; i++) { cin >> a; hash[a]++; } for (int i = 1; i < 3002; i++) { if (hash[i] == 0) { cout << i << "\n"; break; } } }
2
#include <bits/stdc++.h> using namespace std; int main() { int len, cnt; int t; cin >> t; while (t) { cin >> len >> cnt; while (len) { int index = 97; while (index < cnt + 97 && len) { if (len > 0) { cout << char(index); len--; index++; } } } cout << endl; t--; } }
0
#include <bits/stdc++.h> using namespace std; int a[(int)1e5 + 10]; int line[(int)1e5 + 10]; int cnt[(int)1e5 + 10]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int p = 1; for (int i = 0; i < n; i++) { if (n % 2) { if (i < n / 2) line[i] = p++; else line[i] = p--; continue; } if (i < n / 2) line[i] = p++; else line[i] = --p; } for (int i = 0; i < n; i++) a[i] -= line[i]; for (int i = 0; i < n; i++) if (a[i] >= 0) cnt[a[i]]++; int mx = 0; for (int i = 0; i < (int)1e5 + 10; i++) mx = max<int>(mx, cnt[i]); cout << n - mx << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int c[1024][1024]; int a[1024]; int sqr(int i) { return i * i; } int main() { int n, m; int i, j; int li, lj; long long ans = 0, ans1; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) for (j = 0; j < m; j++) scanf("%d", &c[i][j]); for (i = 0; i < n; i++) { a[i] = 0; for (j = 0; j < m; j++) a[i] += c[i][j]; } ans1 = 1LL << 60; for (i = 0; i <= n; i++) { long long t = 0; for (j = 0; j < n; j++) t += (long long)sqr(j * 2 + 1 - i * 2) * a[j]; if (t < ans1) { ans1 = t; li = i; } } ans += ans1; for (i = 0; i < m; i++) { a[i] = 0; for (j = 0; j < n; j++) a[i] += c[j][i]; } ans1 = 1LL << 60; for (i = 0; i <= m; i++) { long long t = 0; for (j = 0; j < m; j++) t += (long long)sqr(j * 2 + 1 - i * 2) * a[j]; if (t < ans1) { ans1 = t; lj = i; } } ans += ans1; printf("%I64d\n", ans * 4); printf("%d %d\n", li, lj); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m, f[1010], vis[100020], used[100020]; struct edge { int a, b, w, id; bool operator<(const edge& b) const { return w < b.w; } edge(int x = 0, int y = 0, int ok = 0, int z = 0) : a(x), b(y), w(ok), id(z) {} } e[100020]; int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } bool chk(int x, int y) { int xi = find(x), yi = find(y); if (xi == yi) return false; f[xi] = yi; return true; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) f[i] = i; if (n % 2 == 0) return printf("-1"), 0; if (n == 1 || n == 0) return printf("0"), 0; char s[5]; for (int a, b, i = 1; i <= m; i++) { scanf("%d%d%s", &a, &b, &s); if (s[0] == 'S') e[i] = edge(a, b, 1, i); else e[i] = edge(a, b, 0, i); } sort(e + 1, e + 1 + m); int tol = 0; for (int i = 1; i <= m; i++) { int a = e[i].a, b = e[i].b; if (chk(a, b) && e[i].w == 1) tol++, used[e[i].id] = 1; } if (tol * 2 + 1 > n) return printf("-1"), 0; for (int i = 1; i <= n; i++) f[i] = i; for (int i = 1; i <= m; i++) if (used[e[i].id]) chk(e[i].a, e[i].b); for (int i = 1; i <= m; i++) { if (e[i].w == 1 && !used[e[i].id]) if (chk(e[i].a, e[i].b)) tol++, used[e[i].id] = 1; if (tol * 2 + 1 == n) break; } for (int i = 1; i <= m; i++) if (!used[e[i].id] && e[i].w == 0) if (chk(e[i].a, e[i].b)) used[e[i].id] = 1; if (tol * 2 + 1 < n) return printf("-1"), 0; printf("%d\n", n - 1); for (int i = 1; i <= m; i++) if (used[i]) printf("%d ", i); return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long SZ = 1e5 + 7; const long long inf = 1e18; const long long mod = 1e9 + 7; const long long MOD = 998244353; vector<long long> pre(SZ); void init() { for (long long i = 1; i < SZ; ++i) { pre[i] = pre[i - 1] + 3 * i - 1; } } void solve() { long long n; cin >> n; long long cnt = 0; while (n > 1) { long long l = 0; long long r = SZ; while (l + 1 < r) { long long mid = l + (r - l) / 2; if (pre[mid] > n) { r = mid; } else { l = mid; } } n -= pre[l]; cnt++; } cout << cnt << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); init(); long long t = 1; cin >> t; while (t--) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int f(long long int b, long long int c) { long long int c1 = (c / b) * b; long long int c2 = (c / b + 1) * b; if (!c1) c1 = c2; if (abs(c1 - c) < abs(c2 - c)) return c1; else return c2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(20); long long int tt; cin >> tt; while (tt--) { long long int a, b, c; cin >> a >> b >> c; long long int a1, b1, c1; long long int ans = 1000000007; long long int A, B, C; long long int cnt = 0; for (auto i = 1; i <= 10000; i++) { a1 = i; cnt = abs(a - a1); for (long long int j = a1; j <= (10000 + a1); j += a1) { b1 = j; c1 = f(b1, c); cnt += (abs(b - b1) + abs(c - c1)); if (cnt < ans) { ans = cnt; A = a1; B = b1; C = c1; } cnt -= (abs(b - b1) + abs(c - c1)); } } cout << ans << '\n'; cout << A << ' ' << B << ' ' << C << '\n'; }; return 0; }
6
#include <bits/stdc++.h> int cnt, head[200005], col[200005]; int link[200005], vis[200005]; struct cup { int a, b; } cp[100005]; struct node { int to, nxt; }; node edge[200005 * 4]; void addedge(int u, int v) { edge[cnt].to = v, edge[cnt].nxt = head[u], head[u] = cnt++; } void init() { cnt = 0; memset(col, 0, sizeof(col)); memset(head, -1, sizeof(head)); } bool color(int u) { vis[u] = 1; for (int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].to; if (!col[v]) { col[v] = !col[u]; if (!color(v)) return 0; } else if (col[v] == col[u]) { return 0; } } return 1; } int main() { int n; while (scanf("%d", &n) != EOF) { init(); for (int i = 1; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); cp[i].a = a; cp[i].b = b; addedge(a, b); addedge(b, a); } for (int i = 1; i <= n; i++) { addedge(2 * i - 1, 2 * i); addedge(2 * i, 2 * i - 1); } for (int i = 1; i <= 2 * n; i++) { if (!vis[i]) { col[i] = 1; color(i); } } for (int i = 1; i <= n; i++) printf("%d %d\n", col[cp[i].a] + 1, col[cp[i].b] + 1); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; string s; cin >> n >> s; vector<int> groups; int pt = 0; for (int i = 1; i < n; ++i) { if (s[i] != s[pt]) { groups.push_back(i - pt); pt = i; } } groups.push_back(n - pt); n = groups.size(); pt = 0; int ans = 0; for (int i = 0; i < n; ++i) { while (pt < n) { if (groups[pt] > 1) { --groups[pt]; break; } ++pt; } if (pt == n) ++i; if (pt <= i) pt = i + 1; ++ans; } cout << ans << endl; } }
4
#include <bits/stdc++.h> using namespace std; vector<int> deg(16); vector<vector<int>> dist(16, vector<int>(16, 1000000000)); vector<int> temp(16); int timer = 1; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; int x, y, w; long long res = 0; for (int i = 1; i <= n; i += 1) { dist[i][i] = 0; } for (int i = 1; i <= m; i += 1) { cin >> x >> y >> w; res += w; deg[x] += 1; deg[y] += 1; dist[x][y] = dist[y][x] = min(dist[x][y], w); } for (int k = 1; k <= n; k += 1) { for (int i = 1; i <= n; i += 1) { for (int j = 1; j <= n; j += 1) { if (dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } for (int i = 2; i <= n; i += 1) { if (deg[i] && dist[1][i] == 1000000000) { cout << -1; return 0; } } for (int i = 1; i <= n; i += 1) { if (deg[i] & 1) { temp[timer++] = i; } } timer -= 1; vector<int> dp((1 << timer), 1000000000); dp[0] = 0; for (int mask = 0; mask < (1 << timer); mask += 1) { for (int i = 1; i <= timer; i += 1) { for (int j = 1; j <= timer; j += 1) { if (i == j || ((1 << (i - 1)) | mask) == mask || ((1 << (j - 1)) | mask) == mask) { continue; } dp[mask | (1 << (i - 1)) | (1 << (j - 1))] = min(dp[mask | (1 << (i - 1)) | (1 << (j - 1))], dp[mask] + dist[temp[i]][temp[j]]); } } } cout << res + dp[(1 << timer) - 1]; }
8
#include <bits/stdc++.h> using namespace std; bool check(int mid, int n, int m) { int x = (n + m) / 2; n = abs(x - n); m = abs(x - m); x = (n * (n + 1)) / 2 + (m * (m + 1)) / 2; if (mid >= x) return true; return false; } int bs(int n, int m) { int l = 0, h = 1000000000, mid = 0, ans; while (l <= h) { mid = l + (h - l) / 2; if (check(mid, n, m)) { ans = mid; h = mid - 1; } else l = mid + 1; } return ans; } int main() { int n, m; cin >> n >> m; int ans = bs(n, m); cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> std::vector<std::pair<int, int>> node[131072]; int par[131072], dir[131072], deep[131072]; long long int dis[131072]; void preprocess(int u, long long int cur_dis = 0, int cur_deep = 0) { dis[u] = cur_dis, deep[u] = cur_deep; for (const auto &it : node[u]) preprocess(it.first, cur_dis + it.second, cur_deep + 1); } struct Treap { Treap *l, *r, *p; int k, v, w, m; long long int tm, lz; Treap() {} Treap(int _k, int _v, long long int _tm) : l(0), r(0), p(0), k(_k), v(_v), w(1), m(_v), tm(_tm), lz(-1e18) {} } tp_node[131072]; Treap *get_root(Treap *p) { while (p->p) p = p->p; return p; } void pull(Treap *p) { p->w = 1 + (p->l ? p->l->w : 0) + (p->r ? p->r->w : 0); p->m = p->l ? p->l->m : p->v; } void push(Treap *p) { if (p->l) p->l->lz = std::max(p->l->lz, p->lz); if (p->r) p->r->lz = std::max(p->r->lz, p->lz); p->tm = std::max(p->tm, p->lz), p->lz = -1e18; } void cut(Treap *p) { if (p) p->p = 0; } Treap *merge(Treap *p, Treap *q) { if (!p || !q) return p ? p : q; return rand() % (p->w + q->w) < p->w ? (push(p), cut(p->r), p->r = merge(p->r, q), p->r->p = p, pull(p), p) : (push(q), cut(q->l), q->l = merge(p, q->l), q->l->p = q, pull(q), q); } std::pair<Treap *, Treap *> split(Treap *p, int k) { if (!p) return {0, 0}; Treap *l, *m, *r; push(p); if (p->k < k) cut(p->r), std::tie(m, r) = split(p->r, k), p->r = 0, pull(p), l = merge(p, m); else cut(p->l), std::tie(l, m) = split(p->l, k), p->l = 0, pull(p), r = merge(m, p); return {l, r}; } long long int get_tm(Treap *p) { long long int res = p->tm; while (p) res = std::max(res, p->lz), p = p->p; return res; } std::vector<std::pair<long long int, long long int>> segs; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i < n; ++i) { int u, v, d; scanf("%d%d%d", &u, &v, &d); node[u].push_back({v, d}); par[v] = u; dir[u] = v; } preprocess(1); for (int u = 1; u <= n; ++u) tp_node[u] = Treap(deep[u], u, -dis[u]); for (int u = 1; u <= n; ++u) { int v = dir[u]; if (!v) continue; merge(get_root(&tp_node[u]), get_root(&tp_node[v])); } for (int i = 1; i <= n; ++i) { 0; 0; } for (int i = 0; i < m; ++i) { int u, t; scanf("%d%d", &u, &t); Treap *rt = get_root(&tp_node[u]); while (par[rt->m]) { int v = par[rt->m]; 0; 0; 0; segs.push_back({get_tm(&tp_node[v]) + dis[v] + 1, t + dis[v] + 1}); Treap *l, *r; std::tie(l, r) = split(get_root(&tp_node[v]), tp_node[v].k + 1); rt = merge(l, rt); } Treap *l, *r; std::tie(l, r) = split(rt, tp_node[u].k); if (l) l->lz = t; rt = merge(l, r); } for (auto it : segs) 0; long long int t = 1, ans = -1; int i = 0; std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int>> pq; std::sort(segs.begin(), segs.end()); while (i < (int)segs.size() || !pq.empty()) { if (i < (int)segs.size() && t < segs[i].first && pq.empty()) t = segs[i].first; while (i < (int)segs.size() && segs[i].first <= t) pq.push(segs[i++].second); if (pq.top() <= t) { ans = pq.top() - 1; for (int j = i - 1; j >= 0; --j) if (ans < segs[j].second) { 0; --i; } break; } pq.pop(); ++t; } printf("%lld %d\n", ans, i); return 0; }
11
#include <bits/stdc++.h> using lint = long long; void solve() { int n; lint k; std::cin >> n >> k; lint l1, r1, l2, r2; std::cin >> l1 >> r1 >> l2 >> r2; if (l1 > l2) { std::swap(l1, l2); std::swap(r1, r2); } lint ans = 0; if (l2 <= r1) { lint len = std::max(r1, r2) - std::min(l1, l2), com = std::min(r1, r2) - std::max(l1, l2); lint gain = len - com; k -= com * n; k = std::max(k, 0LL); while (n--) { lint step = std::min(k, gain); ans += step; k -= step; } ans += k * 2; } else { lint gain = r2 - l1, cost = l2 - r1; { ans += cost; lint step = std::min(gain, k); ans += step; k -= step; --n; } lint pcost = k * 2; lint ncost = 0; while (n--) { ncost += cost; lint step = std::min(gain, k); ncost += step; k -= step; pcost = std::min(pcost, ncost + k * 2); } ans += pcost; } std::cout << ans << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int q; std::cin >> q; while (q--) solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 100010, T = 1000; int n, m, tot, cnt, tp, ans, p[N], head[N], to[N], nxt[N], siz[N], son[N], topx[N], dep[N], f[N], t[N], dfn[N], rnk[N], q[N], w[N], clr[N], a[N], stk[N], ff[N], fhead[N], fnxt[N], fto[N], idx[N], e[N], ad[T * 2 + 10], ct[T * 2 + 10], pt[T * 2 + 10]; vector<pair<int, int> > fp[T * 2 + 10]; vector<int> tt[N * 2]; void add_edge(int u, int v) { nxt[++tot] = head[u]; to[tot] = v; head[u] = tot; return; } void dfs1(int u) { siz[u] = 1; for (int i = head[u]; ~i; i = nxt[i]) { int v = to[i]; dep[v] = dep[u] + 1; dfs1(v); siz[u] += siz[v]; if (siz[v] > siz[son[u]]) son[u] = v; } return; } void dfs2(int u, int tpx) { rnk[dfn[u] = ++cnt] = u; topx[u] = tpx; if (!son[u]) return; dfs2(son[u], tpx); for (int i = head[u]; ~i; i = nxt[i]) if (to[i] != son[u]) dfs2(to[i], to[i]); return; } int LCA(int x, int y) { while (topx[x] != topx[y]) { if (dep[topx[x]] < dep[topx[y]]) y = f[topx[y]]; else x = f[topx[x]]; } return dep[x] < dep[y] ? x : y; } void dfs(int u) { w[u] = clr[u]; for (int i = head[u]; ~i; i = nxt[i]) { int v = to[i]; dfs(v); w[u] += w[v]; } return; } bool cmp(int x, int y) { return dfn[x] < dfn[y]; } void fadd_edge(int u, int v) { fnxt[++tot] = head[u]; fto[tot] = v; fhead[u] = tot; ff[v] = u; e[v] = tot; for (int i = f[v]; i != u; i = f[i]) idx[i] = tot; return; } void build(int l, int r) { cnt = tp = tot = 0; for (int i = l; i <= r; i++) a[++cnt] = abs(q[i]); memset(idx, 0, sizeof(idx)); a[++cnt] = 1; sort(a + 1, a + cnt + 1, cmp); cnt = unique(a + 1, a + cnt + 1) - a - 1; for (int i = 1; i <= cnt; i++) { if (tp <= 1) stk[++tp] = a[i]; else { int lca = LCA(a[i], stk[tp]); if (lca == stk[tp]) stk[++tp] = a[i]; else { while (dfn[stk[tp]] > dfn[lca]) { if (dfn[stk[tp - 1]] > dfn[lca]) { fadd_edge(stk[tp - 1], stk[tp]); tp--; } else { fadd_edge(lca, stk[tp]); if (dfn[stk[tp - 1]] == dfn[lca]) tp--; else stk[tp] = lca; stk[++tp] = a[i]; break; } } } } } while (tp > 1) { fadd_edge(stk[tp - 1], stk[tp]); tp--; } return; } int main() { memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for (int i = 2; i <= n; i++) { scanf("%d", f + i); add_edge(f[i], i); } for (int i = 1; i <= n; i++) scanf("%d", t + i); dfs1(1); dfs2(1, 1); for (int i = 1; i <= m; i++) scanf("%d", q + i); for (int l = 1; l <= m; l += T) { int r = min(m, l + T - 1); dfs(1); build(l, r); for (int i = 1; i <= tot; i++) { ct[i] = -1; fp[i].clear(); } int mini = 1e9, maxn = 0; for (int i = 1; i <= n; i++) { w[i] = t[i] - w[i]; mini = min(mini, w[i]); } for (int i = 1; i <= n; i++) { tt[w[i] - mini].push_back(i); maxn = max(maxn, w[i] - mini); } ans = 0; for (int i = 0; i <= maxn; i++) { for (auto x : tt[i]) { if (!clr[x] && idx[x]) { if (ct[idx[x]] == -1 || fp[idx[x]][ct[idx[x]]].first != w[x]) { fp[idx[x]].push_back(make_pair(w[x], 0)); ct[idx[x]]++; } fp[idx[x]][ct[idx[x]]].second++; } else { if (w[x] < 0 && !clr[x]) ans++; } } } for (int i = 1; i <= tot; i++) { pt[i] = -1; ad[i] = 0; while (pt[i] < ct[i] && fp[i][pt[i] + 1].first < 0) { pt[i]++; ans += fp[i][pt[i]].second; } } for (int i = l; i <= r; i++) { int type = 1; if (q[i] < 0) { type = 0; q[i] = -q[i]; } if (type) { clr[q[i]] ^= 1; if (w[q[i]] < 0) ans--; for (int x = q[i]; x; x = ff[x]) { if (e[x]) { ad[e[x]]--; if (pt[e[x]] < ct[e[x]] && fp[e[x]][pt[e[x]] + 1].first + ad[e[x]] < 0) { pt[e[x]]++; ans += fp[e[x]][pt[e[x]]].second; } } w[x]--; if (!clr[x] && w[x] == -1) ans++; } } else { clr[q[i]] ^= 1; if (w[q[i]] < 0) ans++; for (int x = q[i]; x; x = ff[x]) { if (e[x]) { ad[e[x]]++; if (pt[e[x]] > -1 && fp[e[x]][pt[e[x]]].first + ad[e[x]] >= 0) { ans -= fp[e[x]][pt[e[x]]].second; pt[e[x]]--; } } w[x]++; if (!clr[x] && w[x] == 0) ans--; } } printf("%d ", ans); } for (int i = 0; i <= maxn; i++) tt[i].clear(); } return 0; }
10
#include <bits/stdc++.h> const int N = 100; const int moder = 1e9 + 7; int power[N]; int test; int powermod(int a, int exp) { int ret = 1; for (; exp; exp >>= 1) { if (exp & 1) { ret = 1ll * ret * a % moder; } a = 1ll * a * a % moder; } return ret; } int sum(int x) { return 1ll * x * (x + 1) / 2 % moder; } int solve(int x, int y, int k) { if (!x || !y) { return 0; } int ret = 0; for (int i = 0; i < 30; ++i) { for (int j = 0; j < 30; ++j) { if ((x >> i & 1) && (y >> j & 1)) { int x1 = (x >> i) - 1, y1 = (y >> j) - 1; if (i < j) { x1 >>= j - i; } else { y1 >>= i - j; } int z1 = x1 ^ y1, z2 = z1, max = std::max(i, j); for (int u = 0; u < max; ++u) { z1 = z1 << 1; z2 = z2 << 1 | 1; } if (k < z1) { continue; } int kk = std::min(z2, k); ret = (ret + 1ll * power[std::min(i, j)] * (sum(kk) - sum(z1 - 1) + moder)) % moder; ret = (ret + 1ll * power[std::min(i, j)] * (kk - z1 + 1 + moder)) % moder; } } } return ret; } int main() { power[0] = 1; for (int i = 1; i < N; ++i) { power[i] = 2 * power[i - 1] % moder; } scanf("%d", &test); while (test--) { int x1, x2, y1, y2, k; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k); --k; int ans = (solve(x2, y2, k) + solve(x1 - 1, y1 - 1, k)) % moder; ans = (ans - solve(x2, y1 - 1, k) + moder) % moder; ans = (ans - solve(x1 - 1, y2, k) + moder) % moder; printf("%d\n", ans); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b; cin >> a >> b; int n1, n2; n1 = int(sqrt(a)) + 1; n2 = int((-1 + sqrt(1 + 4 * b)) / 2.0) + 1; if (n1 <= n2) { cout << "Vladik" << endl; } else { cout << "Valera" << endl; } return 0; }
0
#include <bits/stdc++.h> int n, d, q; int main() { scanf("%d%d%d", &n, &d, &q); while (q--) { static int x, y; scanf("%d%d", &x, &y); if (y >= x - d && y <= x + d && y >= -x + d && y <= -x + 2 * n - d) puts("YES"); else puts("NO"); } }
1
#include <bits/stdc++.h> using namespace std; vector<long long> add[2000000], del[2000000]; struct pp { long long size, v; } tr[4000000]; struct aa { long long l, r, c, p; bool operator<(const aa b) const { return c < b.c; } } edge[300000]; long long n, m, k; void upd(long long rt, long long l, long long r, long long pos, long long c) { if (l == r) { tr[rt].size += c; tr[rt].v += pos * c; return; } long long mid = l + r >> 1; if (pos <= mid) upd(rt << 1, l, mid, pos, c); else upd(rt << 1 | 1, mid + 1, r, pos, c); tr[rt].size = tr[rt << 1].size + tr[rt << 1 | 1].size; tr[rt].v = tr[rt << 1].v + tr[rt << 1 | 1].v; } long long que(long long rt, long long l, long long r, long long k) { if (l == r) { return min(tr[rt].size, k) * l; } long long mid = l + r >> 1; if (k <= tr[rt << 1].size) return que(rt << 1, l, mid, k); else return tr[rt << 1].v + que(rt << 1 | 1, mid + 1, r, k - tr[rt << 1].size); } signed main() { ios::sync_with_stdio(0); cin >> n >> k >> m; long long maxn = 1000; for (long long i = 1; i <= m; ++i) { cin >> edge[i].l >> edge[i].r >> edge[i].c >> edge[i].p; add[edge[i].l].push_back(i); maxn = max(maxn, edge[i].p); del[edge[i].r].push_back(i); } long long ans = 0; for (long long i = 1; i <= n; i++) { vector<long long>::iterator it; for (it = add[i].begin(); it != add[i].end(); it++) upd(1, 1, maxn, edge[*it].p, edge[*it].c); ans += que(1, 1, maxn, k); for (it = del[i].begin(); it != del[i].end(); it++) upd(1, 1, maxn, edge[*it].p, -edge[*it].c); } cout << ans; }
6
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); vector<int> b(n); for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } int c = 0; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { c++; } } if (c == n - 1) cout << "NO\n"; else cout << "YES\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; bool judge(int num) { int k = sqrt(num); if (k * k == num) return true; else return false; } int main() { int n, m; while (~scanf("%d %d", &n, &m)) { int a, b, c, d; int flag = 0; for (a = 1; a <= 100; a++) { for (b = 1; b <= 100; b++) { for (c = 1; c <= 100; c++) { for (d = 1; d <= 100; d++) { if (judge(a * a * (m - 1) + b * b) && judge(a * a * (n - 1) + c * c) && judge(c * c * (m - 1) + d * d) && judge(b * b * (n - 1) + d * d)) { flag = 1; break; } } if (flag) break; } if (flag) break; } if (flag) break; } for (int i = 1; i <= n - 1; i++) { for (int j = 1; j <= m - 1; j++) printf("%d ", a); printf("%d\n", b); } for (int i = 1; i <= m - 1; i++) printf("%d ", c); printf("%d\n", d); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } int main() { int a, b, n, krok = 0; cin >> a >> b >> n; bool podm = false; int g; while (n > 0) { if (krok % 2 == 0) { g = gcd(a, n); n -= g; podm = true; } else { g = gcd(b, n); n -= g; podm = false; } krok++; } if (podm) cout << "0" << endl; else cout << "1" << endl; }
0
#include <bits/stdc++.h> using namespace std; string tostr(int x) { stringstream ss; ss << x; string s; ss >> s; return s; } string tostr(vector<string> vs) { string s = ""; for (int _n(((int)((vs).end() - (vs).begin())) - 1), i(0); i <= _n; i++) s += vs[i]; s += "$" + tostr((int)((vs).end() - (vs).begin())) + "$" + tostr((int)((vs[0]).end() - (vs[0]).begin())); return s; } vector<string> rot(vector<string> vs) { vector<string> w((int)((vs[0]).end() - (vs[0]).begin())); for (int _n(((int)((w).end() - (w).begin())) - 1), i(0); i <= _n; i++) w[i].resize((int)((vs).end() - (vs).begin())); for (int _n(((int)((vs).end() - (vs).begin())) - 1), i(0); i <= _n; i++) for (int _n(((int)((vs[i]).end() - (vs[i]).begin())) - 1), j(0); j <= _n; j++) w[j][i] = vs[(int)((vs).end() - (vs).begin()) - 1 - i][j]; return w; } int main() { int n, m; cin >> n >> m; vector<string> f(n); for (int _n((n)-1), i(0); i <= _n; i++) cin >> f[i]; vector<pair<int, pair<int, int> > > vp; for (int _n((n)), i(1); i <= _n; i++) if (n % i == 0) for (int _n((m)), j(1); j <= _n; j++) if (m % j == 0) { map<vector<string>, int> w; for (int _n((n / i) - 1), k(0); k <= _n; k++) for (int _n((m / j) - 1), t(0); t <= _n; t++) { vector<string> vs(i); for (int _n((i)-1), p(0); p <= _n; p++) vs[p].resize(j); for (int _n((i)-1), p(0); p <= _n; p++) for (int _n((j)-1), r(0); r <= _n; r++) vs[p][r] = f[i * k + p][j * t + r]; vector<string> bst = vs; for (int _n((3) - 1), e(0); e <= _n; e++) { vs = rot(vs); if (tostr(vs) < tostr(bst)) bst = vs; } w[bst]++; } int r = 0; for (map<vector<string>, int>::iterator it = w.begin(); it != w.end(); it++) r = max(r, it->second); if (r < 2) vp.push_back(make_pair(i * j, make_pair(i, j))); } cout << vp.size() << endl; sort((vp).begin(), (vp).end()); cout << vp[0].second.first << " " << vp[0].second.second << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; inline int ge() { int r = 0, f = 1; char c = getchar(); while (!isdigit(c)) f = c == '-' ? -f : f, c = getchar(); while (isdigit(c)) r = (r << 3) + (r << 1) + c - 48, c = getchar(); return r * f; } int main() { int i = -1, a0[100] = {0}, a2[100] = {0}, a5[100] = {0}, a7[100] = {0}; char a[100]; memset(a, '\0', sizeof(a)); while ((a[++i] = getchar()) != '\n' && a[i] != EOF) switch (a[i]) { case '0': a0[++a0[0]] = i; break; case '2': a2[++a2[0]] = i; break; case '5': a5[++a5[0]] = i; break; case '7': a7[++a7[0]] = i; break; } long long s = 0x7fffffff, len = strlen(a) - 2; if (a0[0] > 1) s = min(s, len - a0[a0[0]] + len - 1 - a0[a0[0] - 1]); if (a0[0] > 0 && a5[0] > 0) { int y = 0; if (a5[a5[0]] == 0 && a[1] == '0' && a0[a0[0]] != 1) y = 1; if (a0[a0[0]] < a5[a5[0]]) s = min(s, len - a0[a0[0]] + len - 1 - a5[a5[0]] + 1 + y); else s = min(s, len - a0[a0[0]] + len - 1 - a5[a5[0]] + y); } if (a2[0] > 0 && a5[0] > 0 && (a0[0] != len - 1 || a0[0] == 0)) { int y = 0, hh = 1; if (a5[a5[0]] == 0 || a2[a2[0]] == 0) while (a[hh] == '0') hh++, y++; if (a5[a5[0]] > a2[a2[0]]) s = min(s, len - a5[a5[0]] + len - 1 - a2[a2[0]] + y); else s = min(s, len - a5[a5[0]] + len - 1 - a2[a2[0]] + y + 1); } if (a7[0] > 0 && a5[0] > 0 && (a0[0] != len - 1 || a0[0] == 0)) { int y = 0, hh = 1; if (a5[a5[0]] == 0 || a7[a7[0]] == 0) while (a[hh] == '0') hh++, y++; if (a5[a5[0]] > a7[a7[0]]) s = min(s, len - a5[a5[0]] + len - 1 - a7[a7[0]] + y); else s = min(s, len - a5[a5[0]] + len - 1 - a7[a7[0]] + y + 1); } if (s == 0x7fffffff) printf("-1\n"); else printf("%lld\n", s); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, s1 = 0, s2 = 0, s3 = 0; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 3 == 0) s1 += a[i]; else if (i % 3 == 1) s2 += a[i]; else s3 += a[i]; } if (s1 >= s2 && s1 >= s3) cout << "chest\n"; else if (s2 >= s3 && s2 >= s1) cout << "biceps\n"; else cout << "back\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int sz = 3e5 + 5; long long int a[sz], dp[sz], k; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int i, j, l, z, m, n; long long int fina = 1e18, bg = 0, fre = 1; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long int s, f; cin >> s >> f; long long int dp[n]; dp[0] = a[0]; for (i = 1; i < n; i++) dp[i] = dp[i - 1] + a[i]; long long int ans = 0; vector<pair<long long int, long long int>> der; long long int sum = 0; for (i = 0; i < n; i++) { long long int t = f - s - 1; long long int u = 0; if (i >= 1) u = dp[i - 1]; if (i + f - s - 1 >= n) { sum = dp[i + f - s - 1 - n] + dp[n - 1] - u; } else sum = dp[i + f - s - 1] - u; if (sum >= ans) { ans = sum; der.push_back({i, sum}); } } long long int prt = n + 1; for (auto x : der) { if (x.second == ans) { long long int t = s + n - 1 - x.first + 1; if (t % n == 0) t = n; else t %= n; prt = min(t, prt); } } cout << prt; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<long, long>; using vi = vector<int>; using vl = vector<long long>; void fastIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const ll mod = 1e9 + 7; ll x[1001][1001]; void solve() { int n; cin >> n; string ans(2e6, ' '); int next[(int)2e6]; iota(next, next + (int)2e6, 1); while (n--) { string t; cin >> t; int k; cin >> k; while (k--) { int x; cin >> x; x--; for (int i = x; i < x + int((t).size());) { ans[i] = t[i - x]; int j = next[i]; next[i] = x + int((t).size()); i = j; } } } int i = 2e6 - 1; for (; i >= 0; i--) { if (ans[i] != ' ') { break; } } for (int j = i; j >= 0; j--) { if (ans[j] == ' ') ans[j] = 'a'; } cout << ans.substr(0, i + 1); } int main() { fastIO(); cout << setprecision(15) << fixed; int t = 1; while (t--) solve(); return 0; }
4
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f; const int maxn = 2e5 + 10; using namespace std; int n, k, ans[maxn], f[maxn]; int a[maxn], b[maxn], pre[maxn], aft[maxn]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) f[i] = 1; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[a[i]] = i; pre[i] = i - 1; aft[i] = i + 1; } if (n == 1) { printf("1\n"); return 0; } int r = 1; for (int i = n; i >= 1; i--) { if (f[i] == 0) continue; f[i] = 0; ans[b[i]] = r; int p = b[i], q = b[i]; int pp, qq; for (int j = 1; j <= k; j++) { q = aft[q]; if (q > n) break; f[a[q]] = 0; ans[q] = r; } if (q > n) qq = n + 1; else qq = aft[q]; q = b[i]; for (int j = 1; j <= k; j++) { q = pre[q]; if (q < 1) break; f[a[q]] = 0; ans[q] = r; } if (q < 1) pp = 0; else pp = pre[q]; aft[pp] = qq; pre[qq] = pp; r = 3 - r; } for (int i = 1; i <= n; i++) printf("%d", ans[i]); printf("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 10; typedef long long ll; typedef pair<ll,int> P; vector<pair<int,int> > g[N]; void push(int u,int v,int w){ g[u<<2|0].push_back({v<<2|0,w}); g[u<<2|1].push_back({v<<2|1,w}); g[u<<2|2].push_back({v<<2|2,w}); g[u<<2|3].push_back({v<<2|3,w}); g[u<<2|0].push_back({v<<2|1,0}); g[u<<2|0].push_back({v<<2|2,2*w}); g[u<<2|0].push_back({v<<2|3,w}); g[u<<2|1].push_back({v<<2|3,2*w}); g[u<<2|2].push_back({v<<2|3,0}); } ll dis[N]; int main(){ int n,m; cin>>n>>m; while(m--){ int u,v,w; cin>>u>>v>>w; u--, v--; push(u,v,w), push(v,u,w); } priority_queue<P,vector<P>,greater<P> > q; q.push({0,0}); memset(dis,-1,sizeof(dis)); while(!q.empty()){ int u = q.top().second; ll d = q.top().first; q.pop(); if(dis[u]!=-1) continue; dis[u] = d; for(auto i: g[u]){ int v = i.first, w = i.second; if(dis[v]==-1) q.push({d+w,v}); } } for(int i=1;i<n;i++) printf("%lld ",dis[i<<2|3]); printf("\n"); }
8
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } inline long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } const int MAXN = 50005; vector<int> v[MAXN]; int pos[MAXN]; unordered_map<int, int> usado; unordered_map<int, int> pertence; set<pair<int, int> > q; bool espreme(int i) { if (pos[i] == 0) return false; if (usado[v[i][pos[i] - 1]] && !espreme(pertence[v[i][pos[i] - 1]])) return false; usado[v[i][pos[i]]] = 0; q.erase(q.find(make_pair(-v[i][pos[i]], i))); pos[i]--; usado[v[i][pos[i]]] = 1; pertence[v[i][pos[i]]] = i; q.insert(make_pair(-v[i][pos[i]], i)); return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { int x = a[i]; while (x) { v[i].push_back(x); x /= 2; } reverse(v[i].begin(), v[i].end()); } for (int i = 0; i < n; i++) { usado[a[i]] = 1; pertence[a[i]] = i; pos[i] = v[i].size() - 1; q.insert(make_pair(-a[i], i)); } while (!q.empty()) { int i = q.begin()->second; if (!espreme(i)) break; } for (int i = 0; i < n; i++) cout << v[i][pos[i]] << " "; cout << endl; }
5
#include <bits/stdc++.h> using namespace std; long long MOD = 999999937; long long MD(long long a, long long b) { return (b + (a % b)) % b; } signed main() { cout << "YES\n"; int n; cin >> n; for (int i = 0; i < n; ++i) { long long a, b, c, d; cin >> a >> b >> c >> d; cout << ((100000 - (a % 2) - 2 * (b % 2)) % 4 + 1) << endl; } }
6
#include <bits/stdc++.h> using namespace std; const int nax = 1e6; int mem[nax]; long long e; int f(long long n, int win) { if (n > e) return win; if (!mem[n]) { mem[n] = (!f(n + 1, win) || !f(n * 2, win)) + 1; } return mem[n] - 1; } struct Range { long long l, r; int w[2]; int f(long long p) { return w[p % 2]; } }; int f3(long long s, int win) { Range cur = {e + 1, e * 2, win, win}; while (s < cur.l) { Range nxt = {(cur.l + 1) / 2, cur.r / 2}; assert(nxt.r + 1 == cur.l); nxt.w[0] = nxt.w[1] = !cur.f(0); nxt.w[nxt.r % 2] |= !cur.f(cur.l % 2); nxt.w[!(nxt.r % 2)] |= !nxt.w[nxt.r % 2]; cur = nxt; } return cur.f(s); } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; int lose = 1, win = 0; while (t--) { long long s; cin >> s >> e; int nlose = (lose && f3(s, 0)) || (win && !f3(s, 0)); int nwin = (lose && f3(s, 1)) || (win && !f3(s, 1)); lose = nlose; win = nwin; } cout << win << ' ' << lose << endl; }
9
#include <bits/stdc++.h> using namespace std; int n, ans; int a[1001], last[10]; int f[1001][1 << 9]; vector<int> g[10]; int check(int len) { memset(last, 0, sizeof(last)); memset(f, -1, sizeof(f)); f[0][0] = 0; for (int i = 0; i < n; i++) { for (int s = 0; s < (1 << 8); s++) if (f[i][s] >= 0) { for (int j = 0; j < 8; j++) if (!((1 << j) & s)) { int now = last[j] + len - 1; if (now < g[j].size()) f[g[j][now]][s | (1 << j)] = max(f[g[j][now]][s | (1 << j)], f[i][s]); if (now + 1 < g[j].size()) f[g[j][now + 1]][s | (1 << j)] = max(f[g[j][now + 1]][s | (1 << j)], f[i][s] + 1); } } last[a[i] - 1]++; } int tot = -1e9; for (int i = 0; i <= n; i++) tot = max(tot, f[i][(1 << 8) - 1]); if (tot < 0) return 0; ans = max(ans, 8 * len + tot); return 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); g[a[i] - 1].push_back(i); } int l = 0, r = n / 8 + 1; for (int i = 0; i < 8; i++) r = min(r, (int)g[i].size()); if (!r) { for (int i = 0; i < 8; i++) if (g[i].size()) ans++; printf("%d\n", ans); return 0; } while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) l = mid + 1; else r = mid - 1; } printf("%d\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; void fileio() {} vector<vector<long long int>> g; long long int cube_root(long long int x) { long long int lo = 0, hi = 1e6 + 10; while (lo <= hi) { long long int m = (lo + hi) / 2; long long int val = m * m * m; if (val == x) return m; if (val > x) { hi = m - 1; } else { lo = m + 1; } } return -1; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); fileio(); long long int t; cin >> t; while (t-- > 0) { long long int a, b; cin >> a >> b; long long int x = a * b; long long int c = cube_root(x); if (c == -1) { cout << "NO"; cout << '\n'; continue; } if (a % c != 0 or b % c != 0) { cout << "NO"; cout << '\n'; continue; } cout << "YES"; cout << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000 + 10; int c, d, n, m, k; int main() { ios_base::sync_with_stdio(false); cin >> c >> d >> n >> m >> k; int p = n * m; p -= k; int MIN = 9999999; if (p <= 0) { cout << "0"; return 0; } for (int i = 0; i <= p; i++) { int ans = 0; ans += i * d; int q = p - i; int s = 0; s = q / n; if (q % n != 0) s++; ans += s * c; MIN = min(MIN, ans); } cout << MIN; }
3
#include <bits/stdc++.h> using namespace std; inline long long read() { char ch = getchar(); long long s = 0, w = 1; while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * w; } long long n, m, c[300010]; set<long long> s[300010]; set<long long>::iterator it, it1, it2; struct Node1 { long long c[300010]; inline void add(long long x, long long v) { for (; x <= n; x += x & (-x)) c[x] += v; } inline long long que(long long x) { long long s = 0; for (; x; x -= x & (-x)) s += c[x]; return s; } inline long long QUERY(long long L, long long R) { return que(R) - que(L - 1); } } A; struct Node2 { long long root[300010], tot; long long sum[300010 << 5], ls[300010 << 5], rs[300010 << 5]; Node2() { tot = 0; } inline void add(long long &x, long long l, long long r, long long pos, long long val) { if (!x) x = ++tot; sum[x] += val; if (l == r) return; if (pos <= ((l + r) >> 1)) add(ls[x], l, ((l + r) >> 1), pos, val); else add(rs[x], ((l + r) >> 1) + 1, r, pos, val); } inline long long query(long long x, long long l, long long r, long long left, long long right) { if (l == left && right == r) return sum[x]; if (!x) return 0; if (right <= ((l + r) >> 1)) return query(ls[x], l, ((l + r) >> 1), left, right); else if (left > ((l + r) >> 1)) return query(rs[x], ((l + r) >> 1) + 1, r, left, right); else return query(ls[x], l, ((l + r) >> 1), left, ((l + r) >> 1)) + query(rs[x], ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1, right); } inline long long QUERY(long long L, long long R) { long long ans = 0; for (register long long x = R; x; x -= x & (-x)) ans += query(root[x], 0, n, 0, L - 1); for (register long long x = L - 1; x; x -= x & (-x)) ans -= query(root[x], 0, n, 0, L - 1); return ans; } inline void ADD(long long x, long long t, long long tag) { long long p = x; for (; x <= n; x += x & (-x)) add(root[x], 0, n, t, tag * (p - t)); } } B; int main() { n = read(), m = read(); for (register long long i = 1; i <= n; i++) s[i].insert(0); for (register long long i = 1; i <= n; i++) { c[i] = read(); s[c[i]].insert(i); } for (register long long i = 1; i <= n; i++) { it = s[c[i]].lower_bound(i); it--; long long x = (*it); B.ADD(i, x, 1); A.add(i, i - x); } while (m--) { long long opt = read(), x = read(), y = read(); if (opt == 1) { long long t = c[x]; it1 = s[t].upper_bound(x), it2 = --s[t].lower_bound(x); if (it1 != s[t].end()) { A.add((*it1), x - (*it2)); B.ADD((*it1), x, -1); B.ADD((*it1), (*it2), 1); } A.add(x, (*it2)); B.ADD(x, (*it2), -1); s[t].erase(x); c[x] = y; t = c[x]; s[t].insert(x); it1 = s[t].upper_bound(x), it2 = --s[t].lower_bound(x); if (it1 != s[t].end()) { A.add((*it1), (*it2) - x); B.ADD((*it1), x, 1); B.ADD((*it1), (*it2), -1); } A.add(x, -(*it2)); B.ADD(x, (*it2), 1); } if (opt == 2) { printf("%I64d\n", A.QUERY(x, y) - B.QUERY(x, y)); } } return 0; }
9
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long powmod(long long a, long long b, long long MOD) { if (a == 0ll) { return 0ll; } a %= MOD; long long ans = 1; while (b) { if (b & 1) { ans = ans * a % MOD; } a = a * a % MOD; b >>= 1; } return ans; } void Pv(const vector<int>& V) { int Len = int(V.size()); for (int i = 0; i < Len; ++i) { printf("%d", V[i]); if (i != Len - 1) { printf(" "); } else { printf("\n"); } } } void Pvl(const vector<long long>& V) { int Len = int(V.size()); for (int i = 0; i < Len; ++i) { printf("%lld", V[i]); if (i != Len - 1) { printf(" "); } else { printf("\n"); } } } inline void getInt(int* p); const int maxn = 1000010; const int inf = 0x3f3f3f3f; int n, m; int a[300010][9]; unordered_set<int> st; int t1, t2; bool check(int x) { st.clear(); int t; for (int i = 1; i <= n; i++) { t = 0; for (int j = 1; j <= m; j++) { if (a[i][j] >= x) { t |= (1 << (j - 1)); } } st.insert(t); } for (auto y : st) { for (auto z : st) { if ((y | z) == (1 << m) - 1) { t1 = y; t2 = z; return 1; } } } } int main() { scanf("%d %d", &(n), &(m)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%d", &(a[i][j])); ; } } int l = 0; int r = 1e9; int mid; int ans; while (l <= r) { mid = (l + r) >> 1; if (check(mid)) { l = mid + 1; ans = mid; } else { r = mid - 1; } } int ans1, ans2; for (int i = 1; i <= n; i++) { int t = 0; for (int j = 1; j <= m; j++) { if (a[i][j] >= ans) { t |= (1 << (j - 1)); } } if (t == t1) { ans1 = i; } if (t == t2) { ans2 = i; } } printf("%d %d\n", ans1, ans2); return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == '\n'); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }
6
#include <bits/stdc++.h> int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1}; using namespace std; class pa3 { public: int x; int y, z; pa3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: int x; int y, z, w; pa4(int x = 0, int y = 0, int z = 0, int w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; } bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v == 0) return b; if (b == 0) return v; if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } pair<int, int> operator+(const pair<int, int> &l, const pair<int, int> &r) { return {l.first + r.first, l.second + r.second}; } pair<int, int> operator-(const pair<int, int> &l, const pair<int, int> &r) { return {l.first - r.first, l.second - r.second}; } int pr[10000100]; int inv[10000010]; int beki(int wa, int rr, int warukazu) { if (rr == 0) return 1 % warukazu; if (rr == 1) return wa % warukazu; wa %= warukazu; if (rr % 2 == 1) return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu; long long zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } int comb(int nn, int rr) { if (rr < 0 || rr > nn || nn < 0) return 0; int r = pr[nn] * inv[rr]; r %= mod; r *= inv[nn - rr]; r %= mod; return r; } void gya(int ert) { pr[0] = 1; for (int i = 1; i <= ert; i++) { pr[i] = ((long long)pr[i - 1] * i) % mod; } inv[ert] = beki(pr[ert], mod - 2, mod); for (int i = ert - 1; i >= 0; i--) { inv[i] = (long long)inv[i + 1] * (i + 1) % mod; } } struct seg_addsum_getsum { private: public: int cor = (1 << 17); vector<int> vec; vector<int> lazy; void shoki1() { vec.resize(2 * cor + 3, 0); lazy.resize(2 * cor + 3, 0); } void rangeadd(int a, int b, int w, int k = 1, int l = 0, int r = -3) { if (r < 0) r = cor; if (a <= l && r <= b) { lazy[k] += w; if (lazy[k] >= 1000000007) lazy[k] -= 1000000007; vec[k] += (long long)(r - l) * w % 1000000007; if (vec[k] >= 1000000007) vec[k] -= 1000000007; return; } if (r <= a || b <= l) { return; } rangeadd(a, b, w, k * 2, l, (l + r) / 2); rangeadd(a, b, w, k * 2 + 1, (l + r) / 2, r); vec[k] = vec[2 * k] + vec[2 * k + 1]; if (vec[k] >= 1000000007) vec[k] -= 1000000007; vec[k] += (long long)lazy[k] * (r - l) % 1000000007; if (vec[k] >= 1000000007) vec[k] -= 1000000007; return; } int getsum(int a, int b, int k = 1, int l = 0, int r = -3) { if (r < 0) r = cor; if (a <= l && r <= b) { return vec[k]; } if (r <= a || b <= l) { return 0; } int wa = getsum(a, b, k * 2, l, (l + r) / 2) + getsum(a, b, k * 2 + 1, (l + r) / 2, r); if (wa >= 1000000007) wa -= 1000000007; wa += (long long)(min(r, b) - max(l, a)) * lazy[k] % 1000000007; return (wa >= 1000000007 ? wa - 1000000007 : wa); } void pre() { for (int i = 1; i < 2 * cor; i++) { cout << vec[i] << "/" << lazy[i] << " "; if (((i + 1) & (-(i + 1))) == i + 1) cout << endl; } } }; seg_addsum_getsum ss_ko, ss_wa; int L[100020], R[100020]; vector<int> G2[100020]; int sz[100020]; int dep[100020]; vector<int> oi; int oi_in[100020], oi_en[100020]; int ans = 0; int a[100020]; int P = 0; int dfs_f(int r, int p, int d = 0) { oi_in[r] = P; oi[P] = r; P++; dep[r] = d; sz[r] = 1; for (auto v : G2[r]) if (v != p) { sz[r] += dfs_f(v, r, d + 1); } oi_en[r] = P; return sz[r]; } void add_ans(int l, int r) { int fb = a[r]; int fbdb = (long long)a[r] * (dep[r] + (long long)(1000000007 - 2) * dep[l] % 1000000007) % 1000000007; ans += (long long)ss_wa.getsum(L[r], R[r]) * fb % 1000000007; if (ans >= 1000000007) ans -= 1000000007; ans += (long long)ss_ko.getsum(L[r], R[r]) * fbdb % 1000000007; if (ans >= 1000000007) ans -= 1000000007; } void change(int r, bool f = true) { int val = a[r]; if (!f) val = 1000000007 - val; ss_ko.rangeadd(L[r], R[r], val); ss_wa.rangeadd(L[r], R[r], (long long)val * dep[r] % 1000000007); } void dfs(int r, int p, bool keep = true) { int hv = -1, ma = -1; for (auto v : G2[r]) if (v != p) { if (ma < sz[v]) hv = v, ma = sz[v]; } for (auto v : G2[r]) if (v != hv && v != p) { dfs(v, r, false); } if (hv != -1) { dfs(hv, r, true); } add_ans(r, r); change(r); for (auto v : G2[r]) if (v != hv && v != p) { for (int i = oi_in[v]; i < oi_en[v]; i++) add_ans(r, oi[i]); for (int i = oi_in[v]; i < oi_en[v]; i++) change(oi[i]); } if (!keep) { for (int i = oi_in[r]; i < oi_en[r]; i++) change(oi[i], false); } } signed main() { cin.tie(0); ios::sync_with_stdio(false); mod = 1000000007; gya(100005); int n; cin >> n; oi.resize(n); int zen = 1; for (int i = 1; i <= n; i++) { cin >> L[i] >> R[i]; R[i]++; a[i] = (long long)inv[R[i] - L[i]] * pr[R[i] - L[i] - 1] % 1000000007; zen = (long long)zen * (R[i] - L[i]) % 1000000007; } for (int i = 0; i < n - 1; i++) { int y, yy; cin >> y >> yy; G2[y].push_back(yy); G2[yy].push_back(y); } dfs_f(1, 1); ss_ko.shoki1(); ss_wa.shoki1(); dfs(1, 1); cout << (long long)ans * zen % 1000000007 << endl; return 0; }
9
#include <bits/stdc++.h> int n, print; char map[101][101]; int main() { int i, j, k; bool flag1 = 0, flag2 = 0; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%s", map[i]); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { for (k = 0; k < n; k++) { if (map[i][k] == '.') break; } if (k == n) flag1 = 1; for (k = 0; k < n; k++) { if (map[k][j] == '.') break; } if (k == n) flag2 = 1; } } if (flag1 && flag2) printf("-1\n"); else { if (!flag1) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (map[i][j] == '.') { printf("%d %d\n", i + 1, j + 1); break; } } } } else { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (map[j][i] == '.') { printf("%d %d\n", j + 1, i + 1); break; } } } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 100010, M = 1010; int T; int n, x; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> T; while (T--) { cin >> n >> x; int sum = 0; bool same = true, flag = false; for (int i = 0; i < n; i++) { cin >> a[i]; sum += x - a[i]; if (a[i] != x) same = false; else flag = true; } if (same) cout << "0\n"; else if (flag || !sum) cout << "1\n"; else cout << "2\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a[4], b[4], coord = 0; cin >> n >> m; char c[n][m]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> c[i][j]; if (c[i][j] == '*') { a[coord] = i; b[coord++] = j; } } } if (a[0] == a[1]) a[3] = a[2] + 1; else if (a[1] == a[2]) a[3] = a[0] + 1; else a[3] = a[1] + 1; if (b[0] == b[1]) b[3] = b[2] + 1; else if (b[1] == b[2]) b[3] = b[0] + 1; else b[3] = b[1] + 1; cout << a[3] << " " << b[3] << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long double pi = acos((long double)-1); long long num_generator() { return rand() % 1000; } void array_generator(long long a[], long long n) { for (long long i = 0; i < n; i++) a[i] = rand() % 100; } long long update(long long n) { while (n % 10 == 0) { if (n % 10 == 0) n /= 10; else break; } return n; } void solve() { string s; cin >> s; long long n = s.length(); string p; map<char, long long> M, N; for (long long i = 0; i < n; i++) { M[s[i]]++; if (s[i] != 'a') p += s[i]; } if (M.size() == 1 && s[0] == 'a') { cout << s; return; } for (long long i = n - 1; i >= 0; i--) { if (s[i] == 'a') break; else { N[s[i]]++; M[s[i]]--; p.pop_back(); long long flg = 0; for (char ch = 'b'; ch <= 'z'; ch++) { if (M[ch] != N[ch]) { flg = 1; break; } } if (!flg) { long long k = 0; for (long long j = i; j < n; j++) { if (s[j] == p[k]) { k++; } else { flg = 1; break; } } if (!flg) { for (long long j = 0; j < i; j++) cout << s[j]; return; } } } } cout << ":("; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, d[N], a[N], f[N]; int cmp(int x, int y) { return a[x] > a[y]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), f[i] = i; sort(f + 1, f + n + 1, cmp); for (int i = 2; i <= n; i++) printf("%d %d\n", f[i] * 2 - 1, f[i - 1] * 2 - 1); for (int i = 1; i <= n; i++) d[i] = 2 * f[i] - 1; for (int i = 1; i <= n; i++) { printf("%d %d\n", d[i + a[f[i]] - 1], f[i] * 2); if (!d[i + a[f[i]]]) d[i + a[f[i]]] = f[i] * 2; } }
6
#include <bits/stdc++.h> using namespace std; const int N = 2505; int n, a[N][N]; struct node { int x, y, v; node() {} node(int _x, int _y, int _v) : x(_x), y(_y), v(_v) {} bool operator<(const node k) const { return v < k.v; } }; vector<node> p; bitset<N> r[N], c[N], t; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (a[i][j] != a[j][i]) { puts("NOT MAGIC"); return 0; } if (i == j && a[i][j]) { puts("NOT MAGIC"); return 0; } p.push_back(node(i, j, a[i][j])); } } sort(p.begin(), p.end()); for (int i = 0, j; i < p.size(); i = j) { j = i; while (j < p.size() && p[j].v == p[i].v) j++; for (int k = i; k < j; k++) { int x = p[k].x, y = p[k].y; t = r[x] & c[y]; if (t.count()) { puts("NOT MAGIC"); return 0; } } for (int k = i; k < j; k++) { int x = p[k].x, y = p[k].y; r[x][y] = c[y][x] = 1; } } puts("MAGIC"); return 0; }
8
#include <bits/stdc++.h> using namespace std; bitset<16 * 2002> dp[1 << 16]; int n, k; int a[16]; int weight[16]; int calc(int &x) { int c = 0; while (x % k == 0) x /= k, c++; return c; } int _calc(int x) { int c = 0; while (x % k == 0) x /= k, c++; return c; } void dfs(int s, int x) { if (__builtin_popcount(s) == 1) return; int y = x, z = x; while (y < 16 * 2002) { if (dp[s][y]) z = y; y = y * k; } if (z != x) { for (int i = 0; i < n; i++) if ((1 << i) & s) weight[i] += _calc(z / x); dfs(s, z); } else { for (int i = 0; i < n; i++) if ((1 << i) & s and x >= a[i] and dp[s ^ (1 << i)][x - a[i]]) { dfs(s ^ (1 << i), x - a[i]); break; } } } signed main() { dp[0][0] = 1; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < n; j++) if (i & (1 << j)) dp[i] |= (dp[i - (1 << j)] << a[j]); for (int j = 16 * 2000 / k; j >= 0; j--) if (dp[i][j * k]) dp[i][j] = 1; } if (!dp[(1 << n) - 1][1]) return puts("NO") * 0; puts("YES"); dfs((1 << n) - 1, 1); int cnt = n - 1; while (cnt--) { int f1 = -1, f2 = -1; for (int i = 0; i < n; i++) { if (f1 == -1 or weight[i] > weight[f1]) f2 = f1, f1 = i; else if (f2 == -1 or weight[i] > weight[f2]) f2 = i; } printf("%d %d\n", a[f1], a[f2]); a[f1] += a[f2]; weight[f2] = -1e9; weight[f1] -= calc(a[f1]); } return 0; }
11
#include <bits/stdc++.h> using namespace std; int get() { char ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; if (ch == '-') { int s = 0; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return -s; } int s = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } const int N = 1.2e5 + 5; const int INF = 1e9; int n, q; int a[N]; long long ans[N]; int be[N], B; struct QUERY { int l, r, id; QUERY(const int l_ = 0, const int r_ = 0, const int id_ = 0) { l = l_; r = r_; id = id_; } friend bool operator<(QUERY a, QUERY b) { return a.id < b.id; } }; set<QUERY> s[N]; struct block { int mv, tot, ad, tim; long long pv; } blk[N]; int val[N]; int vt[N]; int lef[N], rig[N]; void rebuild(int x) { for (int i = lef[x]; i <= rig[x]; i++) val[i] = val[i] + blk[x].ad; if (blk[x].tim) for (int i = lef[x]; i <= rig[x]; i++) if (val[i] == blk[x].mv) vt[i] += blk[x].tim; blk[x].ad = blk[x].tot = blk[x].tim = 0; blk[x].mv = INF; for (int i = lef[x]; i <= rig[x]; i++) { if (blk[x].mv > val[i]) blk[x].mv = val[i], blk[x].tot = 0; if (blk[x].mv == val[i]) blk[x].tot++; } } void add(int l, int r, int v) { if (be[l] == be[r]) { rebuild(be[l]); for (int i = l; i <= r; i++) val[i] = val[i] + v; rebuild(be[l]); return; } for (int i = be[l] + 1; i <= be[r] - 1; i++) { blk[i].ad = blk[i].ad + v; blk[i].mv = blk[i].mv + v; } rebuild(be[l]); for (int i = l; i <= rig[be[l]]; i++) val[i] = val[i] + v; rebuild(be[l]); rebuild(be[r]); for (int i = lef[be[r]]; i <= r; i++) val[i] = val[i] + v; rebuild(be[r]); } long long query(int l, int r) { long long ret = 0; if (be[l] == be[r]) { rebuild(be[l]); for (int i = l; i <= r; i++) ret += vt[i]; return ret; } for (int i = be[l] + 1; i <= be[r] - 1; i++) ret = ret + blk[i].pv; rebuild(be[l]); rebuild(be[r]); for (int i = l; i <= rig[be[l]]; i++) ret = ret + vt[i]; for (int i = lef[be[r]]; i <= r; i++) ret = ret + vt[i]; return ret; } void update() { for (int i = 1; i <= be[n]; i++) if (blk[i].mv == 0) blk[i].pv = blk[i].pv + blk[i].tot, blk[i].tim++; } struct Decreasing_Stack { int len, a[N], w[N]; void init() { len = 0; } void pushback(int x, int p) { while (len && a[len] < x) { add(w[len - 1] + 1, w[len], -a[len]); len--; } add(w[len] + 1, p, x); a[++len] = x; w[len] = p; } } Max, Min; int main() { n = get(); for (int i = 1; i <= n; i++) a[i] = get(); q = get(); for (int i = 1; i <= q; i++) { int l = get(), r = get(); s[r].insert(QUERY(l, r, i)); } B = sqrt(n) + 1; for (int i = 1; i <= n; i++) be[i] = (i - 1) / B + 1; for (int i = 1; i <= n; i++) rig[be[i]] = i; for (int i = n; i >= 1; i--) lef[be[i]] = i; for (int i = 1; i <= n; i++) val[i] = INF; Max.init(); Min.init(); for (int i = 1; i <= be[n]; i++) rebuild(i); for (int i = 1; i <= n; i++) { Max.pushback(a[i], i); Min.pushback(-a[i], i); add(1, i, -1); rebuild(be[i]); val[i] = 0; rebuild(be[i]); update(); for (set<QUERY>::iterator h = s[i].begin(); h != s[i].end(); h++) ans[(*h).id] = query((*h).l, (*h).r); } for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]); return 0; }
11
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") using namespace std; const int inf = 0x3f3f3f3f; const int N = 1e4 + 10; char a[N]; int nxt[N]; void kmp(char* a, int n) { int i, j; for (nxt[0] = j = -1, i = 1; i < n; nxt[i++] = j) { while (~j && a[j + 1] != a[i]) j = nxt[j]; if (a[j + 1] == a[i]) j++; } } int f[N]; void down(int& x, int y) { if (y < x) x = y; } int get(int x) { int res = 0; while (x) { res++, x /= 10; } return res; } int main() { std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> (a + 1); int len = strlen(a + 1); for (int i = 1; i <= len; i++) f[i] = i + 1; for (int i = 1; i <= len; i++) { int l = len - i + 1; kmp(a + i, l); for (int j = 0; j < l; j++) { int now = j - nxt[j]; if ((j + 1) % now == 0) down(f[i + j], f[i - 1] + get((j + 1) / now) + now); else down(f[i + j], f[i - 1] + j + 1 + 1); } } cout << f[len] << '\n'; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int Mn = 2 * 100000 + 10; const int Me = 2 * Mn + 10; int hd[Mn], nxt[Me], to[Me], cnt; int hd1[Mn], nxt1[Me], to1[Me], cnt1; int n, m; int st, ed; int S, T, len; int dfn[Mn], low[Mn], ecomp[Mn], vcomp[Me]; int ecompnum, vcompnum, num; int par[Mn], incycle[Mn], path[Mn]; int vvis[Mn], evis[Me], cycle[Mn]; stack<int> edge, vert; vector<int> comp[Me]; inline void add(int x, int y) { to[cnt] = y; nxt[cnt] = hd[x]; hd[x] = cnt++; } inline void add1(int x, int y) { to1[cnt1] = y; nxt1[cnt1] = hd1[x]; hd1[x] = cnt1++; } inline void init() { cnt = cnt1 = 0; memset(hd, -1, sizeof hd); memset(hd1, -1, sizeof hd1); memset(incycle, -1, sizeof incycle); memset(evis, false, sizeof evis); } void findcomp(int x, int p) { low[x] = dfn[x] = ++num; vert.push(x); int children(0); for (int i = hd[x]; ~i; i = nxt[i]) if (!dfn[to[i]]) children++; for (int i = hd[x]; ~i; i = nxt[i]) { if (!dfn[to[i]]) { edge.push(i); findcomp(to[i], i ^ 1); low[x] = min(low[x], low[to[i]]); if ((children > 1 && p == -1) || (p != -1 && dfn[x] <= low[to[i]])) { vcompnum++; int tmp; do { tmp = edge.top(); edge.pop(); vcomp[tmp] = vcomp[1 ^ tmp] = vcompnum; } while (tmp != i && tmp != (1 ^ i)); } } else if (i != p && dfn[to[i]] < dfn[x]) { low[x] = min(low[x], dfn[to[i]]); edge.push(i); } } if (dfn[x] <= low[x]) { ecompnum++; int tmp; do { tmp = vert.top(); vert.pop(); ecomp[tmp] = ecompnum; } while (tmp != x); } } inline void bicomp() { num = ecompnum = vcompnum = 0; memset(dfn, 0, sizeof dfn); memset(low, 0, sizeof low); memset(ecomp, 0, sizeof ecomp); memset(vcomp, 0, sizeof vcomp); while (!edge.empty()) edge.pop(); while (!vert.empty()) vert.pop(); for (int i = 1; i <= n; ++i) { if (!dfn[i]) { findcomp(i, -1); } } } inline void build(int k) { for (int i = 0; i < comp[k].size(); ++i) { add1(to[comp[k][i] ^ 1], to[comp[k][i]]); } } inline void findcycle(int x, int p, int rank) { if (incycle[x] != -1) { st = incycle[x]; ed = rank; return; } incycle[x] = rank; cycle[rank] = x; if (nxt1[nxt1[hd1[x]]] != -1) S = incycle[x]; for (int i = hd1[x]; ~i; i = nxt1[i]) { if (to1[i] != p) { findcycle(to1[i], x, rank + 1); if (S == -1) incycle[x] = -1; else if (rank >= st) { evis[i] = evis[i ^ 1] = true; } return; } } } inline void findway(int x, int rank) { vvis[x] = -2; path[rank] = x; if (incycle[x] >= st && incycle[x] != S) { len = rank; T = incycle[x]; return; } for (int i = hd1[x]; ~i && T == -1; i = nxt1[i]) { if (vvis[to1[i]] != -2 && !evis[i]) { findway(to1[i], rank + 1); } } } int main() { init(); scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int x, y; scanf("%d%d", &x, &y); add(x, y); add(y, x); } bicomp(); for (int i = 0; i < cnt; ++i) { comp[vcomp[i]].push_back(i); } S = T = -1; for (int i = 1; i <= vcompnum; ++i) { if (comp[i].size() > 2) { build(i); findcycle(to[comp[i][0]], -1, 0); if (S != -1) break; memset(hd1, -1, sizeof hd1); cnt1 = 0; } } if (S == -1) cout << "NO" << endl; else { findway(cycle[S], 0); int l1, l2; if (S < T) l1 = T - S, l2 = ed - st + S - T; else l2 = S - T, l1 = ed - st + T - S; cout << "YES" << endl; cout << len + 1; for (int i = 0; i <= len; ++i) cout << ' ' << path[i]; cout << endl; cout << (l1 + 1) << ' ' << cycle[S]; for (int i = S; i != T;) { if (++i == ed) i = st; cout << ' ' << cycle[i]; } cout << endl; cout << (l2 + 1) << ' ' << cycle[S]; for (int i = S; i != T;) { if (i == st) i = ed; cout << ' ' << cycle[--i]; } cout << endl; } return 0; }
11
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5, Mod = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, m; cin >> n >> m; long long ans = 0; if (n == 2 || m == 2) { if (m == 2) swap(n, m); cout << 2 * (2 * (m / 4) + min(m % 4, 2LL)); return 0; } cout << max(n, max(m, (n * m + 1) / 2)); return 0; }
5
#include <bits/stdc++.h> using namespace std; static const int maxn = 200000 + 5; char s[maxn]; int Z[maxn]; void Z_ALGO() { int len = strlen(s); Z[0] = len; int lt = 0, rt = 0; for (int k = 1; k < len; k++) { if (k > rt) { int n = 0; while (n + k < len && s[n] == s[n + k]) n++; Z[k] = n; if (n > 0) { lt = k; rt = k + n - 1; } } else { int p = k - lt; int right_part_len = rt - k + 1; if (Z[p] < right_part_len) Z[k] = Z[p]; else { int i = rt + 1; while (i < len && s[i] == s[i - k]) i++; Z[k] = i - k; lt = k; rt = i - 1; } } } } int data[maxn]; int SUM[maxn]; void SOLVE() { int len = strlen(s); for (int i = 0; i < len; i++) data[Z[i]]++; for (int i = 1; i <= len; i++) SUM[i] = SUM[i - 1] + data[i]; } vector<pair<int, int> > ans; void SOLUTION_PRINT() { int len = strlen(s); for (int i = 0; i < len; i++) { int j = len - i - 1; if (Z[j] == i + 1) { int OCC = SUM[len] - SUM[Z[j] - 1]; ans.push_back({i + 1, OCC}); } } printf("%d\n", ans.size()); for (auto it : ans) printf("%d %d\n", it.first, it.second); } int main() { scanf("%s", s); Z_ALGO(); SOLVE(); SOLUTION_PRINT(); }
6
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e12; const long long int mod = 1e9 + 7; vector<long long int> adj[100003]; long long int vis[100003]; void dfs(long long int n) { vis[n] = 1; for (long long int child : adj[n]) { if (vis[child] == 0) { dfs(child); } } } void solve() { long long int n; cin >> n; vector<pair<long long int, long long int>> a; vector<pair<long long int, long long int>> b; long long int mx = (-1 * inf), mxi; long long int aa; for (long long int i = 1; i <= n; i++) { cin >> aa; a.push_back({aa, i}); if (aa > mx) { mx = aa; mxi = i; } } for (long long int i = 1; i <= n; i++) { cin >> aa; b.push_back({aa, i}); if (aa > mx) { mx = aa; mxi = i; } } if (n == 1) { cout << "1\n"; return; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); for (long long int i = 0; i <= n - 2; i++) { adj[a[i + 1].second].push_back(a[i].second); } for (long long int i = 0; i <= n - 2; i++) { adj[b[i + 1].second].push_back(b[i].second); } dfs(mxi); string st = ""; for (long long int i = 1; i <= n; i++) { if (vis[i] == 1) { st.push_back('1'); } else { st.push_back('0'); } } cout << st << "\n"; for (long long int i = 0; i <= 100001; i++) { vis[i] = 0; adj[i].clear(); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; vector<vector<long long>> board(n + 1, vector<long long>(n + 1)); vector<vector<bool>> board2(n + 1, vector<bool>(n + 1)); for (int i = 2 * n; i >= 2; i--) { bool tmp = true; for (int j = max(1, i - n); j <= min(n, i - 1); j++) { tmp = (!tmp); if (tmp) { board[j][i - j] = (1LL << (i - 2 * n + 51)); board2[j][i - j] = true; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << board[i][j] << " "; } cout << endl; } fflush(stdout); int q; cin >> q; for (int i = 0; i < q; i++) { long long sum; cin >> sum; vector<pair<int, int>> ans; queue<pair<int, int>> Q; Q.push({n, n}); int tmp = 51; while (!Q.empty()) { tmp--; auto f = Q.front(); Q.pop(); ans.push_back(f); int r = f.first, c = f.second; if ((sum >> tmp) & 1) { if (r > 1 && board2[r - 1][c]) { Q.push({r - 1, c}); continue; } else if (c > 1 && board2[r][c - 1]) { Q.push({r, c - 1}); continue; } } else { if (r > 1 && !board2[r - 1][c]) { Q.push({r - 1, c}); continue; } else if (c > 1 && !board2[r][c - 1]) { Q.push({r, c - 1}); continue; } } } int m = ans.size(); for (int i = m - 1; i >= 0; i--) { cout << ans[i].first << " " << ans[i].second << endl; } fflush(stdout); } cout << (1LL << 50) << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const long long INF = 0x3f3f3f3f3f3f3f3f; const long long _INF = 0xc0c0c0c0c0c0c0c0; const long long mod = (int)1e9 + 7; const int N = 1e4 + 100; int cnt = 0, tot, n; int head[N], to[N], nt[N], link[N]; bool vis[N]; void add_edge(int u, int v) { to[tot] = v; nt[tot] = head[u]; head[u] = tot++; to[tot] = u; nt[tot] = head[v]; head[v] = tot++; } bool hun(int u) { for (int i = head[u]; ~i; i = nt[i]) { if (!vis[to[i]]) { vis[to[i]] = 1; if (-1 == link[to[i]] || hun(link[to[i]])) { link[u] = to[i]; link[to[i]] = u; return 1; } } } return 0; } int p[N], c[N]; int ans[N], ban[N], fvis[N]; int m; void Ac() { tot = 0; memset(head, -1, sizeof(head)); memset(link, -1, sizeof(link)); scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &p[i]); for (int i = 1; i <= n; ++i) scanf("%d", &c[i]); int d; scanf("%d", &d); for (int i = 1; i <= d; ++i) { scanf("%d", &ban[i]); fvis[ban[i]] = 1; } for (int i = 1; i <= n; ++i) { if (fvis[i]) continue; add_edge(p[i], 5000 + c[i]); } for (int i = d; i >= 1; --i) { for (int j = ans[i + 1];; ++j) { memset(vis, 0, sizeof(vis)); if (hun(j)) ; else { ans[i] = j; break; } } add_edge(p[ban[i]], 5000 + c[ban[i]]); } for (int i = 1; i <= d; ++i) printf("%d\n", ans[i]); } int main() { Ac(); return 0; }
8
#include <bits/stdc++.h> using namespace std; void dout() { cerr << endl; } template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << H << ' '; dout(T...); } using ll = long long; using pii = pair<ll, ll>; using ld = long double; template <typename T> void do_uniq(vector<T> &vec) { sort((vec).begin(), (vec).end()); vec.resize(unique((vec).begin(), (vec).end()) - vec.begin()); } int mod = 1e9 + 7; int ml(int a, int b) { return (a * 1ll * b) % mod; } int add(int a, int b) { return (a + b) % mod; } int sub(int a, int b) { return add(a, mod - b); } int sq(int a) { return ml(a, a); } int b_p(int b, int p) { if (p == 0) return 1; if (p & 1) return ml(b, b_p(b, p - 1)); return sq(b_p(b, p >> 1)); } ld INF = 1000; const int MAXN = 1e5 + 12; ll mx[8 * MAXN]; ll sm[8 * MAXN]; void update(int v, int l, int r, int tl, int tr, int mod) { if (tl > tr) return; if (mx[v] < mod) return; if (l == r) { sm[v] %= mod; mx[v] %= mod; return; } int mid = (l + r) >> 1; update(2 * v, l, mid, tl, min(mid, tr), mod); update(1 + 2 * v, mid + 1, r, max(mid + 1, tl), tr, mod); mx[v] = max(mx[2 * v], mx[2 * v + 1]); sm[v] = sm[2 * v] + sm[2 * v + 1]; } ll sum(int v, int l, int r, int tl, int tr) { if (tl > tr) return 0; if (tl == l && tr == r) return sm[v]; int mid = (l + r) >> 1; return sum(2 * v, l, mid, tl, min(mid, tr)) + sum(1 + 2 * v, mid + 1, r, max(mid + 1, tl), tr); } void sett(int v, int l, int r, int pos, int x) { if (l == r) { mx[v] = sm[v] = x; return; } int mid = (l + r) >> 1; if (pos <= mid) sett(v * 2, l, mid, pos, x); else sett(1 + 2 * v, mid + 1, r, pos, x); mx[v] = max(mx[2 * v], mx[2 * v + 1]); sm[v] = sm[2 * v] + sm[2 * v + 1]; } void solve() { int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { int curr; cin >> curr; sett(1, 0, n - 1, i, curr); } for (int i = 0; i < m; ++i) { int t; cin >> t; if (t == 1) { int l, r; cin >> l >> r; --l; --r; cout << sum(1, 0, n - 1, l, r) << "\n"; } else if (t == 2) { int l, r, x; cin >> l >> r >> x; --l; --r; update(1, 0, n - 1, l, r, x); } else { int k, x; cin >> k >> x; --k; sett(1, 0, n - 1, k, x); } } } void multisolve() { int t; cin >> t; int i = 1; while (t--) { solve(); i++; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cout << fixed << setprecision(20); solve(); }
7
#include <bits/stdc++.h> using namespace std; string ch[10] = {"", "", "2", "3", "223", "5", "53", "7", "7222", "7332"}, str, an; int n; int main() { cin >> n; cin >> str; for (int i = 0; i < (str.size()); i++) an += ch[str[i] - '0']; sort((an).begin(), (an).end()); reverse((an).begin(), (an).end()); cout << an << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; long long k; struct Tp { long long x; int i; } a[111111]; bool ByX(Tp A, Tp B) { return A.x < B.x || A.x == B.x && A.i < B.i; } bool ByI(Tp A, Tp B) { return A.i < B.i; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i].x; a[i].i = i; } sort(a, a + n, ByX); int j = 0; long long last = 0, mi = 0; while (j < n && (a[j].x - last) * ((long long)n - j) <= k) { k -= (a[j].x - last) * ((long long)n - j); mi += (a[j].x - last); last = a[j].x; j++; } for (int i = 0; i < n; i++) a[i].x -= mi; if (j == n) { if (k > 0) cout << -1 << endl; else cout << endl; return 0; } long long ad = k / (n - j); k %= n - j; for (int i = j; i < n; i++) a[i].x -= ad; sort(a, a + n, ByI); for (int i = 0; i < k; i++) if (a[i].x > 0) a[i].x--; else k++; for (int i = k; i < n; i++) if (a[i].x > 0) cout << i + 1 << " "; for (int i = 0; i < k; i++) if (a[i].x > 0) cout << i + 1 << " "; cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int abs(int a) { return a < 0 ? -1 * a : a; } void solve() { int n, m, bx, by; cin >> n >> bx; int x[n]; for (int i = 0; i < n; i++) cin >> x[i]; cin >> m >> by; int y[m]; for (int i = 0; i < m; i++) cin >> y[i]; long long X = 0, Y = 0; for (int i = n - 1; i >= 0; i--) { X = X + x[i] * pow(bx, n - i - 1); } for (int i = m - 1; i >= 0; i--) { Y = Y + y[i] * pow(by, m - i - 1); } if (X == Y) { cout << "="; return; } else if (X > Y) { cout << ">"; return; } cout << "<"; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int t = 1; solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; template <class T> inline void rread(T& num) { num = 0; T f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') num = num * 10 + ch - '0', ch = getchar(); num *= f; } const int inf = 2e9, mod = 1e9 + 7; const int maxn = 3e5 + 10; const long double PI = acos(-1.0); long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long power(long long a, long long n, long long mod) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } int n, q; int a[maxn], qu[maxn], last[maxn], l[maxn]; int main() { while (cin >> n >> q) { int sum = 0; memset(a, 0, sizeof a); memset(last, 0, sizeof last); int be = 0, cun = 0; for (int i = 1; i <= q; i++) { int t, x; scanf("%d%d", &t, &x); if (t == 1) { sum++; a[x]++; qu[++cun] = x; l[x] = cun; } else if (t == 2) { last[x] = l[x]; sum -= a[x]; a[x] = 0; } else if (x > be) { for (int j = be + 1; j <= x; j++) if (last[qu[j]] < j) a[qu[j]]--, sum--, last[qu[j]] = j; be = x; } printf("%d\n", sum); } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long N = 2e7 + 5; long long mind(long long x, long long y) { if (x > y) { return y; } else { return x; } } long long poww(long long a, long long b) { if (b < 0 || a <= 0) return 0; long long ans = 1LL; while (b) { if (b & 1) ans = ans * a; a = a * a; b >>= 1; } return ans; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<pair<long long, long long> > d; while (n--) { long long a, b; cin >> a >> b; d.push_back(make_pair(a, b)); } sort(d.begin(), d.end()); long long ans = d[0].second; for (long long i = 0; i < d.size(); i++) { if (d[i].second >= ans) { ans = d[i].second; } else { ans = d[i].first; } } cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int N, MAX = INT_MIN, MIN = INT_MAX, sayi, a, b, f; void read() { cin >> N; for (int i = 1; i <= N; i++) { cin >> f; if (f > MAX) MAX = f, a = 1; else if (f == MAX) a++; if (f < MIN) MIN = f, b = 1; else if (f == MIN) b++; } } int main() { read(); sayi = a * b; cout << MAX - MIN << ' '; if (MAX == MIN) cout << (a * (a - 1)) / 2 << endl; else cout << sayi << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, i, j; cin >> n; int arr[n + 1]; vector<int> ans(n + 10, 0), tmp(n + 10, 0); for (i = 1; i <= n; i++) cin >> arr[i]; for (i = 1; i <= n; i++) { ans[arr[i]] = max(ans[arr[i]], i - tmp[arr[i]]); tmp[arr[i]] = i; } vector<int> anss(n + 15, n + 5); for (i = 1; i <= n; i++) { ans[i] = max(ans[i], n + 1 - tmp[i]); } for (i = 1; i <= n; i++) { anss[ans[i]] = min(i, anss[ans[i]]); } for (i = 1; i <= n; i++) { anss[i] = min(anss[i], anss[i - 1]); if (anss[i] > n) cout << "-1 "; else cout << anss[i] << " "; } printf("\n"); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e9; int main() { string a, b; cin >> a >> b; int k1 = a.size(), k2 = b.size(), ans; ans = k1 + k2; k1--, k2--; for (int i = 0; i <= min(k1, k2); i++) { if (a[k1 - i] == b[k2 - i]) ans -= 2; else break; } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; const long long int M = 1e9 + 7; const long long int N = 2e5; long long int power(long long int x, long long int y, long long int M = ::M) { x %= M; long long int ans = 1; while (y) { if (y & 1) ans = (ans * x) % M; y >>= 1LL; x = (x * x) % M; } return ans; } long long int add(long long int a, long long int b) { return ((a % M + b % M) % M); } long long int sub(long long int a, long long int b) { return ((a % M - b % M + M) % M); } long long int mul(long long int a, long long int b) { return (((a % M) * (b % M)) % M); } long long int divi(long long int a, long long int b) { return (mul(a, power(b, M - 2)) % M); } int vis[1001]; int lev[1001]; vector<int> v[1001]; int mx; void dfs(int val, int par, int l) { lev[l]++; mx = max(mx, l); for (auto ele : v[val]) { if (ele != par) { dfs(ele, val, l + 1); } } } inline void solve() { int n, x; cin >> n >> x; for (int i = 0; i <= 1000; i++) { vis[i] = 0; lev[i] = 0; v[i].clear(); } mx = 0; for (int i = 0, a, b; i < n - 1; i++) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } if (n == 1) { cout << "Ayush\n"; return; } if (v[x].size() == 1) { cout << "Ayush\n"; return; } dfs(x, x, 0); int st = 0; int prev = 0; for (int i = mx; i >= 1; i--) { if (lev[i] % 2 == 0) st = st ^ 1; prev = st; st = st ^ 1; } if (prev == 0) cout << "Ayush\n"; else cout << "Ashish\n"; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int t; cin >> t; while (t--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 10; const long long M = 2111; const long long MX = 1e6 + 55; const long long INF = 1e18; const long long inf = -1e18; const long long MOD = 1e9 + 7; const long long xesh = 31; long long n, m, k, ans, v[N], cnt[N]; pair<long long, long long> a[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + n + 1); reverse(a + 1, a + n + 1); for (int i = 1; i <= m * k; i++) { ans += a[i].first; v[i] = a[i].second; } sort(v + 1, v + m * k + 1); for (int i = 1; i < k; i++) { cnt[i] = v[(i)*m]; } cout << ans << "\n"; for (int i = 1; i < k; i++) { cout << cnt[i] << " "; } } }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m, i, a[5]; cin >> n; for (i = 0; i < 5; i++) { a[i] = n % 10; n /= 10; } n = a[3] + 10 * a[1] + 100 * a[0] + 1000 * a[2] + 10000 * a[4]; m = n; m = n * n; m %= 100000; m = m * n; m %= 100000; m = m * n; m %= 100000; m = m * n; m %= 100000; if (m < 10000) { cout << "0"; } if (m < 1000) { cout << "0"; } if (m < 100) { cout << "0"; } if (m < 10) { cout << "0"; } cout << m; return 0; }
3
#include <bits/stdc++.h> double a[1001], b[1001]; double max(double x, double y) { return x < y ? y : x; } int main() { int n, r; scanf("%d %d", &n, &r); for (int i = 0; i < n; i++) scanf("%lf", &a[i]); for (int i = 0; i < n; i++) { double center = r; for (int j = 0; j < i; j++) { center = max(center, b[j] + sqrt(4 * r * r - (a[i] - a[j]) * (a[i] - a[j]))); } b[i] = center; printf("%.10lf ", b[i]); } }
3
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TC, i, j; cin >> TC; while (TC--) { long long int n, k, m, q, r; cin >> n >> k; string s; cin >> s; long long int cW = count(s.begin(), s.end(), 'W'); int ans = 0, fst = 0, lst = 0; if (cW == 0) { cout << max(0LL, 2 * k - 1) << '\n'; } else { vector<pair<int, int> > v; for (i = 0; i < n; i++) { bool fl = 0; if (s[i] == 'L') { if (i == 0) fl = 1; int cnt = 1; while (i < n && s[i + 1] == 'L') { i++; cnt++; } if (i == n - 1) fl = 1; if (fl && i != n - 1) { fst = cnt; } if (fl && i == n - 1) { lst = cnt; } if (fl == 0) v.push_back({cnt, fl}); } else if (s[i] == 'W') { int cnt = 1; while (i < n && s[i + 1] == 'W') { i++; cnt++; } ans += 2 * cnt - 1; } } sort(v.begin(), v.end()); int mn = min(fst, lst); int mx = max(fst, lst); v.push_back({mn, 1}); v.push_back({mx, 1}); int sum = 0; for (i = 0; i < (long long int)v.size(); i++) { if (sum >= k) break; if (sum + v[i].first <= k) { ans += 2 * v[i].first + (v[i].second != 1); sum += v[i].first; } else { ans += 2 * (k - sum); sum += (k - sum); } } cout << ans << '\n'; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000010; int val[MAXN], q[MAXN]; int N, T; int Stack[MAXN], top; int main() { while (scanf("%d", &N) != EOF) { for (int i = 1; i <= N; i++) scanf("%d", &val[i]); memset(Stack, 0, sizeof(Stack)); top = 0; scanf("%d", &T); for (int i = 1; i <= T; i++) { int x; scanf("%d", &x); val[x] = -val[x]; } for (int i = N; i > 0; i--) { if (Stack[top] != val[i]) { Stack[++top] = abs(val[i]); val[i] = -abs(val[i]); } else top--; } if (top == 0) { puts("YES"); for (int i = 1; i <= N; i++) printf("%d ", val[i]); puts(""); } else puts("NO"); } return 0; }
6
#include <bits/stdc++.h> using namespace std; vector<long long> Peaks_count(81), a_to_first(81), a_to_last(81), b_to_first(81), b_to_last(81); void Init(long long x, int n, vector<long long> &first, vector<long long> &last) { if (n == 0) { first[0] = 0; last[0] = 0; return; } if (n == 1) { if (x == 2) first[1] = 1; else first[1] = 0; if (x == 1) last[1] = 1; else last[1] = 0; return; } if (x <= Peaks_count[n - 1]) { Init(x, n - 1, first, last); first[n] = min(first[n - 1], last[n - 1] + 2); last[n] = min(first[n - 1], last[n - 1]) + 1 + (n - 2 + 1) / 2; } else { Init(x - Peaks_count[n - 1], n - 2, first, last); first[n] = first[n - 2] + 1; last[n] = last[n - 2]; } } long long dist(long long a, long long b, int n) { if (a == b) return 0; if (n <= 2) return 1; if (a <= Peaks_count[n - 1]) { if (b >= Peaks_count[n - 1] + 1) return min(a_to_first[n - 1], a_to_last[n - 1]) + 1 + b_to_first[n - 2]; else return min(dist(a, b, n - 1), min(a_to_first[n - 1] + 2 + b_to_last[n - 1], a_to_last[n - 1] + 2 + b_to_first[n - 1])); } else return dist(a - Peaks_count[n - 1], b - Peaks_count[n - 1], n - 2); } int main() { int t; int rank; cin >> t >> rank; Peaks_count[0] = 1; Peaks_count[1] = 2; for (int i = 2; i <= 80; i++) Peaks_count[i] = Peaks_count[i - 1] + Peaks_count[i - 2]; for (int ii = 1; ii <= t; ii++) { long long a, b; cin >> a >> b; if (a > b) swap(a, b); a_to_first.resize(81, 0); a_to_last.resize(81, 0); b_to_first.resize(81, 0); b_to_last.resize(81, 0); Init(a, min(rank, 80), a_to_first, a_to_last); Init(b, min(rank, 80), b_to_first, b_to_last); cout << dist(a, b, min(rank, 80)) << endl; } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, INF = 1e9; pair<int, int> t[N * 4]; int t_2[N * 4], a[N]; void build(int v, int l, int r) { if (l == r) { t[v] = make_pair(a[l], l); t_2[v] = 1; } else { int m = (l + r) / 2; build(v * 2, l, m); build(v * 2 + 1, m + 1, r); t[v] = min(t[v * 2], t[v * 2 + 1]); t_2[v] = t_2[v * 2] + t_2[v * 2 + 1]; } } pair<int, int> min_seg(int v, int t_l, int t_r, int l, int r) { if (l > r) return make_pair(INF, 0); if (l == t_l && r == t_r) return t[v]; int t_m = (t_l + t_r) / 2; return min(min_seg(v * 2, t_l, t_m, l, min(r, t_m)), min_seg(v * 2 + 1, t_m + 1, r, max(l, t_m + 1), r)); } int sum(int v, int t_l, int t_r, int l, int r) { if (l > r) return 0; if (l == t_l && r == t_r) return t_2[v]; int t_m = (t_l + t_r) / 2; return sum(v * 2, t_l, t_m, l, min(r, t_m)) + sum(v * 2 + 1, t_m + 1, t_r, max(l, t_m + 1), r); } int find_pr(int v, int l, int r, int x) { if (l == r) return l; if (x <= t_2[v * 2]) return find_pr(v * 2, l, (l + r) / 2, x); else return find_pr(v * 2 + 1, (l + r) / 2 + 1, r, x - t_2[v * 2]); } void update(int v, int l, int r, int pos) { if (l == r) { t[v] = make_pair(INF, 0); t_2[v] = 0; } else { int m = (l + r) / 2; if (pos <= m) update(v * 2, l, m, pos); else update(v * 2 + 1, m + 1, r, pos); t[v] = min(t[v * 2], t[v * 2 + 1]); t_2[v] = t_2[v * 2] + t_2[v * 2 + 1]; } } int main() { ios::sync_with_stdio(0); int n, pt = 0, where_new; long long ans = 0; pair<int, int> suf; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; build(1, 0, n - 1); while (t_2[1]) { suf = min_seg(1, 0, n - 1, pt, n - 1); if (suf.first != t[1].first) { suf = t[1]; ans += sum(1, 0, n - 1, pt, n - 1) + sum(1, 0, n - 1, 0, t[1].second); } else { ans += sum(1, 0, n - 1, pt, suf.second); } update(1, 0, n - 1, suf.second); where_new = sum(1, 0, n - 1, 0, suf.second); if (where_new != t_2[1]) pt = find_pr(1, 0, n - 1, where_new + 1); else pt = find_pr(1, 0, n - 1, 1); } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.length(), i = 0; if (s[0] == '9') { i++; } while (i < n) { if (s[i] >= '5') { s[i] = '9' - s[i] + '0'; } i++; } cout << s; return 0; }
2
#include <bits/stdc++.h> using namespace std; inline int read() { static int r; static char c; r = 0, c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') r = r * 10 + (c - '0'), c = getchar(); return r; } template <typename T> inline void print(T *a, int n) { for (int i = 1; i < n; ++i) cout << a[i] << " "; cout << a[n] << endl; } const int mod = (int)1e9; struct matrix { int m[2][2]; inline matrix operator*(const matrix &x) const { static matrix r; for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { r.m[i][j] = 0; for (int k = 0; k < 2; ++k) r.m[i][j] += (long long)m[i][k] * x.m[k][j] % mod; r.m[i][j] %= mod; } return r; } inline void print() { for (int i = 0; i < 2; ++i) { for (int __i = 0; __i != 2; ++__i) cout << m[i][__i] << " "; cout << endl; }; cout << endl; } } t[200000 + 1]; int f[200000 + 1], fs[200000 + 1], a[200000 + 1]; struct node { int w, w2, s, d; } tree[200000 * 5]; inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline node update(node l, node r) { static node ret; ret.s = l.s + r.s, ret.d = 0; ret.w = (l.w + (long long)r.w * t[l.s].m[0][0] + (long long)r.w2 * t[l.s].m[1][0]) % mod; ret.w2 = (l.w2 + (long long)r.w * t[l.s - 1].m[0][0] + (long long)r.w2 * t[l.s - 1].m[1][0]) % mod; return ret; } inline void applyDelta(int x, int v) { tree[x].d += v; tree[x].w = add(tree[x].w, (long long)v * fs[tree[x].s - 1] % mod); if (tree[x].s > 1) tree[x].w2 = add(tree[x].w2, (long long)v * fs[tree[x].s - 2] % mod); } inline void push(int x) { if (!tree[x].d) return; applyDelta(((x) << 1), tree[x].d); applyDelta(((x) << 1 | 1), tree[x].d); tree[x].d = 0; } void build(int x, int l, int r) { if (l == r) { tree[x].s = 1; tree[x].w = a[l], tree[x].w2 = 0; return; } int mid = l + r >> 1; build(((x) << 1), l, mid); build(((x) << 1 | 1), mid + 1, r); tree[x] = update(tree[((x) << 1)], tree[((x) << 1 | 1)]); } node query(int x, int l, int r, int s, int t) { if (l == s && r == t) return tree[x]; push(x); int mid = l + r >> 1; if (t <= mid) return query(((x) << 1), l, mid, s, t); if (s > mid) return query(((x) << 1 | 1), mid + 1, r, s, t); return update(query(((x) << 1), l, mid, s, mid), query(((x) << 1 | 1), mid + 1, r, mid + 1, t)); } void modify(int x, int l, int r, int s, int t, int v) { if (l == s && r == t) { applyDelta(x, v); return; } push(x); int mid = l + r >> 1; if (t <= mid) modify(((x) << 1), l, mid, s, t, v); else if (s > mid) modify(((x) << 1 | 1), mid + 1, r, s, t, v); else { modify(((x) << 1), l, mid, s, mid, v); modify(((x) << 1 | 1), mid + 1, r, mid + 1, t, v); } tree[x] = update(tree[((x) << 1)], tree[((x) << 1 | 1)]); } int n, m; int main(int argc, char *argv[]) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", a + i); f[0] = f[1] = fs[0] = 1, fs[1] = 2; for (int i = 2; i <= n; ++i) { f[i] = add(f[i - 1], f[i - 2]); fs[i] = add(fs[i - 1], f[i]); } t[1].m[0][0] = t[1].m[0][1] = t[1].m[1][0] = 1, t[1].m[1][1] = 0; t[0] = t[1]; for (int i = 2; i <= n; ++i) t[i] = t[i - 1] * t[1]; build(1, 1, n); while (m--) { int l, r, p, v; scanf("%d", &p); if (p == 1) { scanf("%d%d", &p, &v); modify(1, 1, n, p, p, v - query(1, 1, n, p, p).w); } else if (p == 2) { scanf("%d%d", &l, &r); printf("%d\n", query(1, 1, n, l, r).w); } else { scanf("%d%d%d", &l, &r, &v); modify(1, 1, n, l, r, v); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1007; int n, m; int c[MAX_N][MAX_N]; long long sx[MAX_N], sy[MAX_N]; long long cal(int i, int j) { if (i < j) return (j - i) * 4 - 2; return (i - j) * 4 + 2; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { scanf("%d", &c[i][j]); } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { sx[i] += c[i][j]; sy[j] += c[i][j]; } } long long sumx = 0, sumy = 0; int x = -1, y = -1; for (int i = 0; i <= n; ++i) { long long s = 0; for (int j = 1; j <= n; ++j) { s += sx[j] * cal(i, j) * cal(i, j); } if (x == -1 || s < sumx) { x = i; sumx = s; } } for (int i = 0; i <= m; ++i) { long long s = 0; for (int j = 1; j <= m; ++j) { s += sy[j] * cal(i, j) * cal(i, j); } if (y == -1 || s < sumy) { y = i; sumy = s; } } long long sum = sumx + sumy; printf("%I64d\n", sum); printf("%d %d\n", x, y); return 0; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:60000000") using namespace std; const long double eps = 1e-9; const int inf = (1 << 30) - 1; const long long inf64 = ((long long)1 << 62) - 1; const long double pi = 3.1415926535897932384626433832795; const string task = ""; template <class T> T sqr(T x) { return x * x; } long long pl, pr; long long vl, vr; int k; vector<long long> A; void go(long long v) { if (v > 1000000000LL) return; A.push_back(v); go(v * 10 + 4); go(v * 10 + 7); } long long calc(long long l1, long long r1, long long vl, long long vr) { long long l = max(l1, vl); long long r = min(r1, vr); if (l <= r) return r - l + 1; return 0; } int main() { cin >> pl >> pr >> vl >> vr >> k; A.clear(); go(4); go(7); A.push_back(0); A.push_back(1000000001); sort(A.begin(), A.end()); int l = 1; int r = l + k; long long res = 0; long long sum = (pr - pl + 1) * (vr - vl + 1); while (r < (int)A.size()) { int l1 = A[l - 1] + 1; int r1 = A[l]; int l2 = A[r - 1]; int r2 = A[r] - 1; res += calc(l1, r1, pl, pr) * calc(l2, r2, vl, vr); res += calc(l2, r2, pl, pr) * calc(l1, r1, vl, vr); if (r1 == l2 && (pl <= r1 && r1 <= pr && vl <= r1 && r1 <= vr)) res--; l++; r++; } printf("%.10lf\n", (double)res / (double)sum); return 0; }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vb = vector<bool>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vvvpii = vector<vvpii>; template <typename T> T read() { T t; cin >> t; return t; } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { o << '(' << p.first << ", " << p.second << ')'; return o; } void panic() { cout << -1 << endl; exit(0); } void Main() { int n = read<int>(); int m = read<int>(); vector<int> A(n); for (int i = int(0); i < int(n); ++i) A[i] = read<int>(); vi O, E; map<int, int> co; int no = 0, ne = 0; for (auto &&a : A) { co[a]++; if (a % 2) { O.emplace_back(a); no++; } else { E.emplace_back(a); ne++; } } sort((O).begin(), (O).end()); sort((E).begin(), (E).end()); O.erase(unique(O.begin(), O.end()), O.end()); E.erase(unique(E.begin(), E.end()), E.end()); vpii re; for (auto &&an : co) { re.emplace_back(an.second, an.first); } sort((re).begin(), (re).end(), greater<decltype(re)::value_type>()); int io = 0, ie = 0; int uo = 1, ue = 2; vpii ans; auto use = [&m](int &i, const vi &V, int &u) { while (i < ((int)(V).size()) && V[i] == u) { i++; u += 2; } if (u > m) panic(); }; for (auto &&ca : re) { int c = ca.first; int a = ca.second; int ap = a % 2; for (int i = int(0); i < int(c - 1); ++i) { bool to_odd; if (no == ne) to_odd = ap != 0; else if (no > ne) to_odd = false; else to_odd = true; (ap ? no : ne)--; if (to_odd) { use(io, O, uo); ans.emplace_back(a, uo); no++; uo += 2; } else { use(ie, E, ue); ans.emplace_back(a, ue); ne++; ue += 2; } } } int size = ((int)(ans).size()); map<int, vi> conv; for (auto &&ft : ans) { conv[ft.first].push_back(ft.second); } for (int i = int(0); i < int(n); ++i) { int a = A[i]; if (conv.count(a) && ((int)(conv[a]).size())) { int t = *conv[a].rbegin(); A[i] = t; conv[a].pop_back(); } else { if (no != ne) { if (no > ne && (a % 2 == 1)) { use(ie, E, ue); A[i] = ue; size++; no--; ne++; ue += 2; } else if (ne > no && (a % 2 == 0)) { use(io, O, uo); A[i] = uo; size++; ne--; no++; uo += 2; } } } } cout << size << endl; for (int i = int(0); i < int(n); ++i) { (void)printf(i + 1 < n ? "%d " : "%d\n", A[i]); } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; set<string> A, B; vector<string> C(max(n, m)); string tt; for (int i = 0; i < n; i++) cin >> tt, A.emplace(tt); for (int i = 0; i < m; i++) cin >> tt, B.emplace(tt); auto it = set_intersection(A.begin(), A.end(), B.begin(), B.end(), C.begin()); if ((int)(it - C.begin()) % 2) n++; cout << (n > m ? "YES" : "NO"); }
1
#include <bits/stdc++.h> using namespace std; bool mark[100000000]; set<int> ooxx; int main() { int n, limit; int s; scanf("%d %d", &n, &limit); bool flag = false; for (int i = 0; i < n; i++) { scanf("%d", &s); ooxx.insert(s); if (s == 1) flag = true; } if (limit == 2) { printf("0\n"); return 0; } if (flag) { printf("1\n"); return 0; } int ans = 0; for (int i = 2; i < limit; i++) { if (!mark[i]) { ans++; if (ooxx.find(i) == ooxx.end()) { printf("-1\n"); return 0; } for (int j = 1; i * j < limit; j++) mark[i * j] = true; } } printf("%d\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; int arr[2 * 100005]; int main() { vector<int> ans; int n, i, num; double ave = 0; cin >> n; for (i = 0; i < n; i++) cin >> num, ave += num, arr[i] = num; ave /= (double)n; for (i = 0; i < n; i++) if (arr[i] == ave) ans.push_back(i + 1); cout << ans.size() << endl; for (i = 0; i < ans.size(); i++) cout << ans[i] << ' '; return 0; }
2
#include <bits/stdc++.h> using namespace std; void testCase() { long long n; cin >> n; vector<string> grid(n); vector<pair<long long, long long>> ans; for (string &s : grid) cin >> s; if (grid[0][1] == grid[1][0]) { if (grid[n - 1][n - 2] == grid[0][1]) ans.push_back({n - 1, n - 2}); if (grid[n - 2][n - 1] == grid[0][1]) ans.push_back({n - 2, n - 1}); } else if (grid[n - 1][n - 2] == grid[n - 2][n - 1]) { if (grid[n - 1][n - 2] == grid[0][1]) ans.push_back({0, 1}); if (grid[n - 1][n - 2] == grid[1][0]) ans.push_back({1, 0}); } else { if (grid[0][1] == '1') ans.push_back({0, 1}); if (grid[1][0] == '1') ans.push_back({1, 0}); if (grid[n - 1][n - 2] == '0') ans.push_back({n - 1, n - 2}); if (grid[n - 2][n - 1] == '0') ans.push_back({n - 2, n - 1}); } cout << (long long)(ans.size()) << endl; for (auto &it : ans) cout << it.first + 1 << " " << it.second + 1 << endl; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cin.tie(nullptr); long long t_c = 1; cin >> t_c; while (t_c--) testCase(); }
1
#include <bits/stdc++.h> using namespace std; int main() { int num, k; cin >> num >> k; int arr[num + 1]; arr[0] = 1; for (int i = 1; i <= num; i++) cin >> arr[i]; sort(arr, arr + num + 1); if (arr[k] == arr[k + 1]) cout << "-1" << endl; else cout << arr[k] << endl; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 55; const int MOD = 998244353; long long modpow(long long x, long long p) { if (p == 0) return 1; if (p % 2 == 1) return x * modpow(x, p - 1) % MOD; long long r = modpow(x, p / 2); return r * r % MOD; } long long modinv(long long x) { return modpow(x, MOD - 2); } int n, l[MAXN], r[MAXN], x[MAXN], y[MAXN]; long long dp[MAXN][2 * MAXN], inv[MAXN]; long long cnt(long long s, int n) { s += n; long long ret = 1; for (int i = 1; i <= n; i++) ret = ret * (s - i + 1) % MOD * inv[i] % MOD; return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d %d", &l[n - i - 1], &r[n - i - 1]); vector<int> vx; for (int i = 0; i < n; i++) { vx.push_back(l[i]); vx.push_back(r[i] + 1); } sort(vx.begin(), vx.end()); vx.resize(unique(vx.begin(), vx.end()) - vx.begin()); long long denom = 1; for (int i = 0; i < n; i++) { x[i] = lower_bound(vx.begin(), vx.end(), l[i]) - vx.begin(); y[i] = lower_bound(vx.begin(), vx.end(), r[i] + 1) - vx.begin(); denom = denom * (r[i] - l[i] + 1) % MOD; } denom = modinv(denom); for (int i = 1; i <= n; i++) inv[i] = modinv(i); for (int i = n; i >= 0; i--) for (int j = (int)vx.size(); j >= 0; j--) { if (i == n) { dp[i][j] = 1; } else { if (j == (int)vx.size()) { dp[i][j] = 0; continue; } dp[i][j] = dp[i][j + 1]; for (int k = i; k < n; k++) { if (j < x[k] || j >= y[k]) break; dp[i][j] = (dp[i][j] + cnt(vx[j + 1] - vx[j] - 1, k - i + 1) * dp[k + 1][j + 1]) % MOD; } } } printf("%lld\n", dp[0][0] * denom % MOD); return 0; }
9
#include <bits/stdc++.h> using namespace std; int d[4005][4005]; int x[4005], y[4005]; int deg[4005]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= m; ++i) { scanf("%d %d", &x[i], &y[i]); d[x[i]][y[i]] = 1; d[y[i]][x[i]] = 1; ++deg[x[i]]; ++deg[y[i]]; } int ans = 1000000001; for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (j == x[i] || j == y[i]) continue; if (!d[x[i]][j] || !d[y[i]][j]) continue; ans = min(ans, deg[x[i]] + deg[y[i]] + deg[j] - 6); } } if (ans == 1000000001) ans = -1; printf("%d", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long n, m, v; int main() { cin >> n >> m; if (n < m * 2) cout << 0; else cout << n - (2 * m); cout << " "; for (long long i = 0; i <= n; i++) if (i * (i - 1) / 2 >= m) { v = i; break; } cout << n - v << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; signed main() { long long t; cin >> t; while (t--) { long long a, b; cin >> a >> b; string s; cin >> s; long long cnt = 0, sum = 0; vector<long long> v; long long f = 0; char c = '?'; for (long long i = 0; i < s.length(); i++) { if (s[i] != c) { if (cnt) { if (c == '1') sum++, f = 1; else if (f) v.push_back(cnt); } c = s[i]; cnt = 1; } else cnt++; } if (cnt && c == '1') sum++; long long ans = 0; for (auto u : v) { if (u * b < a) ans += u * b, sum--; } cout << ans + sum * a << endl; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int k, d, i, j; cin >> k >> d; if (d != 0) { for (i = 1; i <= k; i++) { if (i == 1) cout << d; else cout << 0; } } if (d == 0) { if (k == 1) cout << 0; else cout << "No solution"; } }
1
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (auto &x : v) cin >> x; map<int, int> mp; map<int, int> past; for (int i = 0; i < n; i++) { past[v[i]] = -1; mp[v[i]] = 0; } for (int i = 0; i < n; i++) { mp[v[i]] = max(mp[v[i]], i - past[v[i]] - 1); past[v[i]] = i; } for (int i = 0; i < n; i++) { mp[v[i]] = max(mp[v[i]], n - past[v[i]] - 1); } vector<int> a(n, -1); for (auto &x : mp) { int w = x.second; if (a[w] != -1) { a[w] = min(a[w], x.first); } else { a[w] = x.first; } } bool flag = true; int minval = INT_MAX; for (auto &x : a) { if (x != -1) { minval = min(minval, x); } if (minval != INT_MAX) { if (x == -1) { x = minval; } x = min(x, minval); minval = x; } } for (auto &x : a) cout << x << " "; cout << '\n'; } return 0; }
3