Codeforces Round 1045 (Div. 2) 题解
A
一如既往的 A 题人类判断题,直接根据题目的要求分类判断 \(n, a, b\) 的奇偶性即可。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
int n, a, b;
void solve () {
cin >> n >> a >> b;
if (a <= b) {
if ((b&1) == (n&1)) { cout << "YES\n"; return; }
else { cout << "NO\n"; return; }
} else {
if (((a&1) == (n&1)) && ((b&1) == (n&1))) { cout << "YES\n"; return; }
else { cout << "NO\n"; return; }
}
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int _ = 1; cin >> _;
while (_--) solve();
return 0;
}
B
绷不住了,能被一道数学题硬控 1h 也是没谁了。
这里通过把玩样例以及数学推理,不难发现如下的推理:
\[\text{设} \,x = n(k+1)+m,\text{则有}x \mod (k+1) = m,\text{那么使}x +(k+1)m \text{则能得到} (k+1)(n+m)
\]
此时不难发现所有数字都已是 \((k+1)\) 的倍数。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
const int N = 1e5+100;
ll n, k, a[N];
void solve () {
cin >> n >> k;
for (int i = 1;i <= n;i++) {
cin >> a[i];
cout << a[i] + (a[i]%(k+1))*k << " ";
}
cout << "\n";
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int _ = 1; cin >> _;
while (_--) solve();
return 0;
}
C
我个人觉得 C 比 B 简单。
玩一下样例就不难发现每一次贪心都应该以长度为 \(3\) 的区间为基准,然后每一次都尽量减最少的并达到要求,而且因为区间有覆盖性,所以我们要尽量将修改对后一个区间生效。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
const int N = 2e5+100;
int n, a[N];
void solve() {
cin >> n;
memset(a, 0, sizeof(a));
for (int i = 1;i <= n;i++) cin >> a[i];
ll ans = 0;
for (int i = 2;i <= n;i+=2) {
if (a[i-1]+a[i+1] > a[i]) {
if (a[i-1] > a[i]) {
ans += (a[i-1]-a[i]);
a[i-1] = a[i];
}
if (a[i+1] > a[i]) {
ans += (a[i+1]-a[i]);
a[i+1] = a[i];
}
if (a[i-1]+a[i+1] > a[i]) {
ll sum = a[i-1]+a[i+1], minu = sum-a[i];
ans += minu;
if (a[i+1] >= minu) a[i+1] -= minu;
else {
minu -= a[i+1];
a[i+1] = 0;
a[i-1] -= minu;
}
}
}
}
cout << ans << "\n";
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int _ = 1; cin >> _;
while (_--) solve();
return 0;
}
浙公网安备 33010602011771号