inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
int l, r;
intmain(){ int T = read(); while (T --> 0) { l = read(); r = read(); puts(l * 2 <= r ? "NO" : "YES"); } return0; }
inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
constint MAXN = 200 + 10;
int n; int opm[MAXN], dp[MAXN][MAXN << 1];
intmain(){ int T = read(); while (T --> 0) { n = read(); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= (n << 1); ++j) { dp[i][j] = 0x7f7f7f7f; } opm[i] = read(); } std::sort(opm + 1, opm + 1 + n); for (int j = 1; j <= (n << 1); ++j) { dp[1][j] = std::abs(opm[1] - j); } for (int i = 2; i <= n; ++i) { for (int j = 1; j <= (n << 1); ++j) { for (int k = 1; k < j; ++k) { // 垃圾 vscode 这里把 std::abs 返回值报成 <error-type> // 于是就只能分开写 int newt = (int) (dp[i - 1][k] + int(std::abs(opm[i] - j))); dp[i][j] = std::min(dp[i][j], newt); } } } printf("%d\n", *std::min_element(dp[n] + 1, dp[n] + 1 + (n << 1))); } return0; }
D. Minimal Height Tree
解题思路
题目里的一个重要信息:
g[k] is the list of all children of vertex k, sorted in ascending order
inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
constint MAXN = 2e5 + 10;
int n, aa[MAXN]; std::vector<int> fa[MAXN]; // fa[i] 表示深度为 i 的(可用的)所有点 int dep;
voidsolve(){ int p = 2, fap = 0; while (p <= n) { fa[dep + 1].push_back(p); ++p; while (p <= n && aa[p - 1] < aa[p]) { fa[dep + 1].push_back(p); ++p; } if (fap < (int) fa[dep].size() - 1) ++fap; else { ++dep; fap = 0; } } if (fap) ++dep; }
intmain(){ int T = read(); while (T --> 0) { n = read(); dep = 0; for (int i = 1; i <= n; ++i) aa[i] = read(); fa[0].push_back(1); solve(); printf("%d\n", dep); for (int i = 0; i <= dep + 1; ++i) fa[i].clear(); } return0; }
inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
constint MAXN = 500000 + 10;
int n, k, aa[MAXN], ban[MAXN];
intmain(){ n = read(); k = read(); for (int i = 1; i <= n; ++i) aa[i] = read() - i; for (int i = 1; i <= k; ++i) ban[i] = read(); ban[0] = 0, ban[k + 1] = n + 1; aa[0] = -2147482333, aa[n + 1] = 2147482333; int ans = 0; for (int i = 0; i <= k; ++i) { int l = ban[i], r = ban[i + 1]; if (aa[l] > aa[r]) return (puts("-1") & 0); std::vector<int> lis; for (int j = l + 1; j <= r - 1; ++j) if (aa[l] <= aa[j] && aa[j] <= aa[r]) { auto pos = std::upper_bound(lis.begin(), lis.end(), aa[j]); if (pos == lis.end()) lis.push_back(aa[j]); else *pos = aa[j]; } ans += r - l - 1 - lis.size(); } printf("%d\n", ans); return0; }