A
分析可得:当 n ≥ 3 n\ge 3 n≥3时就能取得最大值 2 m 2m 2m
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n, m; cin >> n >> m;
if (n >= 3) cout << 2 * m << endl;
else if (n == 2) cout << m << endl;
else cout << 0 << endl;
}
}
B
全部元素按大小排序后,能选k个b序列的就选,否则选a序列的。
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n, k; cin >> n >> k;
vector<int> a(2 * n), vis(35);
for (int i = 0; i < n; ++i) {
cin >> a[i];
vis[a[i]]++;
}
for (int i = 0; i < n; ++i) {
cin >> a[i + n];
}
sort(a.begin(), a.begin() + 2 * n, [&](int x, int y) { return x > y; });
int sum = 0, tmp_n = n;
for (int i = 0; i < 2 * n && tmp_n; ++i) {
if (vis[a[i]]) {
sum += a[i];
tmp_n--;
vis[a[i]]--;
}
else if (k) {
sum += a[i];
tmp_n--;
k--;
}
}
cout << sum << endl;
}
}
C
全部都要回到最中间,然后一圈一圈的发散,每圈的数字个数 8 ∗ i 8 * i 8∗i,乘上该圈的步数 i i i得到 ∑ i = 1 n / 2 8 ∗ i 2 \displaystyle \sum\limits_{i=1}^{n/2}8 *i^2 i=1∑n/28∗i2
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
ll sum = 0;
for (int i = 1; i <= (n - 1) / 2; ++i) {
sum += 8ll * i * i;
}
cout << sum << endl;
}
}
D
由题意可知,排序规则为区间大小,其次索引大小
递归处理出每个元素的区间大小和索引大小,最后处理出次序即可
pair<int, int> a[maxn];
int ans[maxn];
void f(int l, int r) {
if (l == r) {
a[l] = make_pair(1, l);
return;
}
if (l > r) return;
if ((r - l + 1) & 1) {
int idx = (l + r) / 2;
a[idx] = make_pair(r - l + 1, idx);
f(l, idx - 1);
f(idx + 1, r);
}
else {
int idx = (l + r - 1) / 2;
a[idx] = make_pair(r - l + 1, idx);
f(idx + 1, r);
f(l, idx - 1);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
f(1, n);
sort(a + 1, a + n + 1, [&](pair<int, int> x, pair<int, int> y) {
if (x.first != y.first) return x.first > y.first;
return x.second < y.second;
});
for (int i = 1; i <= n; ++i) {
ans[a[i].second] = i;
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << ' ';
}
cout << endl;
}
}
E
处理出下标%k的子序列,答案即为选择一个子序列形成000011…1110000这种状态的改变量 与 将其他子序列变为全0 的操作数和的最小值
对每个子序列,考虑字符为’1’是代价为1,字符为’0’代价为-1,即为求最长上升子序列
string str;
vector<int> a;
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
int t; cin >> t;
while (t--) {
int n, k; cin >> n >> k >> str;
int sum = 0, ans = 1e9;
for (int i = 0; i < n; ++i) sum += str[i] == '1';
for (int i = 0; i < k; ++i) {
a.clear();
int tmp = 0, now = 0;
for (int j = i; j < n; j += k) {
if (str[j] == '1') a.push_back(1);
else a.push_back(-1);
}
for (int p = 0; p < a.size(); ++p) {
tmp += a[p];
if (tmp < 0) tmp = 0;
now = max(now, tmp);
}
ans = min(ans, sum - now);
}
cout << ans << endl;
}
}