#include<bits/stdc++.h> usingnamespacestd; using ll = longlong;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
intmain(){ int n = rd(), m = rd(), x = rd(), t = rd(), d = rd(); int st = t - x * d; if (m > x) printf("%d\n", t); elseprintf("%d\n", st + m * d); return0; }
#include<bits/stdc++.h> usingnamespacestd; using ll = longlong; using ld = longdouble;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
const ld PI = 3.1415926535; structvec { ld x, y; vec turn(ld ang){ // 逆时针旋转 ang 角度 ld cosa = cos(ang), sina = sin(ang); return (vec){x * cosa - y * sina, x * sina + y * cosa}; } };
考虑双指针 \(\left(p t r_s, p t
r_t\right)\), 从头对齐往后扫描,每次先不考虑扩张,
能不能匹配上。
如果不能匹配上,即 \(S\left[p t r_s\right]
\neq T\left[p t r_t\right]\), 那么 \(T\left[p t r_t\right]\) 只能往回看,尝试用
\(S\left[p t r_s-1\right]\) 和 \(S\left[p t r_s-2\right]\) 来扩张,
否则无解。
#include<bits/stdc++.h> usingnamespacestd; using ll = longlong;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 3007
ll x[N], y[N], r[N];
int f[N];
intfind(int x){ return x == f[x] ? x : (f[x] = find(f[x])); }
inline ll sqr(ll x){return x * x;}
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) f[i] = i; int sx = rd(), sy = rd(); int tx = rd(), ty = rd(); for (int i = 1; i <= n; ++i) { x[i] = rd(); y[i] = rd(); r[i] = rd(); } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if (sqr(x[i] - x[j]) + sqr(y[i] - y[j]) > sqr(r[i] + r[j])) continue; if (sqr(x[i] - x[j]) + sqr(y[i] - y[j]) < sqr(r[i] - r[j])) continue; int u = find(i), v = find(j); f[u] = v; } int px, py; for (int i = 1; i <= n; ++i) { if (sqr(sx - x[i]) + sqr(sy - y[i]) == sqr(r[i])) px = i; if (sqr(tx - x[i]) + sqr(ty - y[i]) == sqr(r[i])) py = i; } puts(find(px) == find(py) ? "Yes" : "No"); return0; }
#include<bits/stdc++.h> usingnamespacestd; using ll = longlong; using ld = longdouble;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007 #define fr first #define sc second #define pb push_back #define mp make_pair #define pii pair<int, int>
unordered_map<int, int> mx, cnt;
vector<pii> s[N];
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) { int m = rd(); for (int j = 1; j <= m; ++j) { int a = rd(), b = rd(); s[i].pb(mp(a, b)); if (mx[a] < b) {mx[a] = b; cnt[a] = 1;} elseif (mx[a] == b) ++cnt[a]; } } bool fl = 0; int ans = 0; for (int i = 1; i <= n; ++i) { bool tag = 0; for (auto [a, b] : s[i]) { if (mx[a] == b && cnt[a] == 1) {tag = 1; break;} } if (tag) ++ans; else fl = 1; } ans += fl; printf("%d\n", ans); return0; }
#include<bits/stdc++.h> usingnamespacestd; using ll = longlong; using ld = longdouble;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 300007 #define fr first #define sc second #define pb push_back #define mp make_pair #define pii pair<int, int>
vector<pii> e[N]; vector<ll> tmp;
int d[N];
ll f[N][2];
voiddfs(int u, int fa){ ll sum = 0; for (auto [v, w] : e[u]) if (v != fa) dfs(v, u); tmp.clear(); for (auto [v, w] : e[u]) if (v != fa) { sum += f[v][0]; tmp.push_back(f[v][1] + w - f[v][0]); } sort(tmp.begin(), tmp.end()); reverse(tmp.begin(), tmp.end()); if (d[u] == 0) { f[u][0] = sum; f[u][1] = -1e18; } else { f[u][0] = f[u][1] = sum; int len = tmp.size(); for (int i = 0; i < len; ++i) { if (tmp[i] < 0) break; if (i < d[u]) f[u][0] += tmp[i]; if (i < d[u] - 1) f[u][1] += tmp[i]; } } }
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) d[i] = rd(); for (int i = 1; i < n; ++i) { int u = rd(), v = rd(), w = max(0, rd()); e[u].pb(mp(v, w)); e[v].pb(mp(u, w)); } dfs(1, 1); printf("%lld\n", max(f[1][0], f[1][1])); return0; }
给定一个矩阵 \(A_{n \times n}(1 \leq n \leq
400)\), 从某个格子出发,
每次可以向右或向下走。问起点终点的数字相同的路径有多少条?
首先枚举数字是多少,然后考虑计算这个数字对应的所有点之间的贡献。根据每种数字出现次数讨论:
如果出现次数不超过 \(n\),
那么直接暴力枚举任意两个位置算贡献, 答案是 \(\left(\begin{array}{c}\Delta x+\Delta y \\ \Delta
x\end{array}\right)\) 。 复杂度是 \(O\left(\sum_{\sum c n t_i=n^2, c n t_i \leq n} c n
t_i^2\right) \leq O\left(n \times n^2\right)=O\left(n^3\right)\)
,用 \(a^2+b^2 \leq(a+b)^2\)
放缩一下。
#include<bits/stdc++.h> usingnamespacestd; using ll = longlong; using ld = longdouble;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 1007 #define NN 407 #define M 160007 #define mod 998244353 #define pb push_back #define mp make_pair #define pii pair<int, int>
int n, c[N][N], a[NN][NN], f[NN][NN], ans;
vector<pii> pos[M];
inlinevoidcalc(int col){ memset(f, 0, sizeof(f)); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { f[i][j] = (f[i - 1][j] + f[i][j - 1]) % mod; if (a[i][j] == col) { f[i][j] = (f[i][j] + 1) % mod; ans = (ans + f[i][j]) % mod; } } }
intmain(){ c[0][0] = 1; for (int i = 1; i < N; ++i) { c[i][0] = 1; for (int j = 1; j <= i; ++j) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; } n = rd(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { a[i][j] = rd(); pos[a[i][j]].push_back(mp(i, j)); } for (int i = n * n; i; --i) { if (pos[i].size() > n) calc(i); else { int sz = pos[i].size(); for (int j = 0; j < sz; ++j) for (int k = 0; k < sz; ++k) { auto [sx, sy] = pos[i][j]; auto [tx, ty] = pos[i][k]; if (tx < sx || ty < sy) continue; ans = (ans + c[tx - sx + ty - sy][tx - sx]) % mod; } } } printf("%d\n", ans); return0; }