A Add Candies
輸出1就行
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n;
cout << n << '\n';
rep (i, 1, n) cout << i << ' ';
cout << '\n';
}
return 0;
}
B Numbers Box
仔細觀察可以發現, 兩個相鄰的減號可以消掉
我們可以將一個減號, 通過操作傳遞到任何一個位置
故, 最后最多剩下一個減號(負數), 貪心選絕對值最小的就行
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n >> m; ll ans = 0;
vector<int> x, y;
rep(i, 1, n)
rep(j, 1, m) {
cin >> a[i][j]; ans += abs(a[i][j]);
if (a[i][j] < 0) x.pb(a[i][j]);
else y.pb(a[i][j]);
}
sort(all(x)); sort(all(y));
if (x.size() & 1) {
if (!y.empty() && y[0] < abs(x.back())) ans -= y[0] << 1;
else ans += x.back() << 1;
}
cout << ans << '\n';
}
return 0;
}
C Numbers Box
貪心, 先選最大的
PLL a[N];
int main() {
IOS;
for (cin >> _; _; --_) {
ll w, m; cin >> n >> w; m = w;
rep (i ,1, n) cin >> a[i].fi, a[i].se = i;
sort(a + 1, a + 1 + n, greater<PLL>());
VI ans;
rep (i, 1, n) if (w >= a[i].fi) ans.pb(a[i].se), w -= a[i].fi;
sort(all(ans));
if ((w << 1) > m) { cout << "-1\n"; continue; }
cout << ans.size() << '\n';
for (auto &i : ans) cout << i << ' ';
cout << '\n';
}
return 0;
}
D Catching Cheaters
這道題一共考察了兩個dp 模型
第一個是最長公共子序列, 一個是連續區間和最大
最長公共子序列就不說了, 題目都告訴你了
我們觀察以下式子, 4 * lcs - lenx- leny = (2 * lcs - lenx) + (2 * lcs - leny)
(2 * lcs - lenx) 就是 lcs長度 減去不是 lcs的長度, 所以 f[i][j] 的轉移公式就成了
if (a[i] == b[i]) f[i][j] = max(f[i - 1][j - 1], 0) + 2; //負數直接扔, 連續區間和一樣
else f[i][j] = max(f[i][j - 1], f[i- 1][j]) - 1;
int main() {
IOS; cin >> n >> m >> a + 1 >> b + 1;
rep (i, 1, n) {
rep (j, 1, m) {
f[i][j] = max(f[i - 1][j], f[i][j - 1]) - 1;
if (a[i] == b[j]) umax(f[i][j], max(f[i - 1][j - 1], 0ll) + 2);
umax(ans, f[i][j]);
}
}
cout << ans;
return 0;
}
E Xor Tree
建 trie樹, 只能留一顆最大的樹和一個分支
struct Trie {
static const int N = 31 * 2e5;
int tr[N][2], tot = 1;
void insert(int x) {
int p = 1;
per (i, 31, 0) {
int ch = x >> i & 1;
if (!tr[p][ch]) tr[p][ch] = ++tot;
p = tr[p][ch];
}
}
int dfs(int p) {
if (!p) return 0;
if (!tr[p][0] && !tr[p][1]) return 1;
return max(dfs(tr[p][0]) + (tr[p][1] > 0), dfs(tr[p][1]) + (tr[p][0] > 0));
}
} T;
int main() {
IOS: cin >> n;
rep (i, 1, n) cin >> m, T.insert(m);
cout << n - T.dfs(1);
return 0;
}