A. Dual Trigger (思维)
题意:
有 n n n 盏灯,编号为 1 1 1 至 n n n ,排成一排,一开始全部都是灭的。可以执行以下任意次数的操作(可能为零):
- 选择两个不相邻的
†
{}^\dagger
† 没开的灯,将它们打开。
确定是否能达到配置 s s s ,其中 s i = 1 s_i = 1 si=1 表示 i i i个灯泡已打开,否则为 s i = 0 s_i = 0 si=0 。
分析:
灯的数量为偶数时,需要特判如果只有两盏灯亮,那么要求不能相邻,相邻不成立,其他成立。灯的数量为奇数时不成立,
代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n;
string s;
cin >> n >> s;
vector<int> tmp;
for (int i = 0; i < s.size(); i++)
if (s[i] == '1')
tmp.push_back(i);
if (tmp.size() % 2) {
cout << "NO" << endl;
continue;
}
if (tmp.size() == 2 && tmp[0] + 1 == tmp[1])
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
B.Battle Cows (贪心)
题意:
有 n n n 头奶牛参加编码比赛。奶牛 i i i 的 C o w d e f o r c e s Cowdeforces Cowdeforces 值为 a i a_i ai (全部不同),最初位于位置 i i i 。比赛由以下 n − 1 n-1 n−1 场比赛组成:
- 第一场比赛在位置 1 1 1 的奶牛和位置 2 2 2 的奶牛之间进行。
- 随后,每场比赛 i i i 都在位置 i + 1 i+1 i+1 的奶牛和比赛 i − 1 i-1 i−1 的获胜者之间进行。
- 在每场比赛中, C o w d e f o r c e s Cowdeforces Cowdeforces 等级较高的奶牛获胜并进入下一场比赛。
您是奶牛 k k k 的主人。对您来说,赢得比赛并不重要;相反,您希望您的奶牛在尽可能多的比赛中获胜。作为比赛组织者的熟人,您可以要求他们将您的奶牛与另一头奶牛交换一次位置,或者您也可以选择什么都不做。
找出您的奶牛能够赢得的最大场次。
分析:
通过观察发现,对于第 k k k头牛,如果前面有一个下标 i i i满足 a i > a k a_i > a_k ai>ak ,对于第 k k k头牛来说只有两种情况,一种是击败从 1 1 1到 i − 1 i-1 i−1的牛,另一种是和第 i i i头牛交换,击败从 i i i开始的到下一个大于 a k a_k ak的中间所有的牛。
代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
int pos = 0;
for (int i = 1; i < k; i++) {
if (a[i] > a[k]) {
pos = i;
break;
}
}
int ans = 0;
if (pos != 0) {
if (pos != 1)
ans = 1;
for (int i = 1; i < pos; i++)
if (a[i] > a[k])
ans = 0;
for (int i = pos + 1; i <= n; i++) {
if (a[k] > a[i])
ans++;
else
break;
}
}
int tmp = 0;
if (k != 1)
swap(a[1], a[k]);
for (int i = 2; i <= n; i++)
if (a[i] > a[1])
break;
else
tmp++;
cout << max(tmp, ans) << endl;
}
return 0;
}
C.Ticket Hoarding (贪心)
题意:
作为一家创业公司的首席执行官,你想奖励 k k k位员工每人一张即将到来的音乐会门票。门票将在 n n n 天内发售,你预测在 i i i 天每张门票的价格将是 a i a_i ai 。但是,为了防止囤积门票,演唱会主办方采取了以下措施:
- 每人每天购票不得超过 m m m 张。
- 如果某人在 i i i 日购买了 x x x 张门票,则其后各日(即从 i + 1 i+1 i+1 日开始)的每张门票价格将提高 x x x 。
例如,如果 a = [ 1 , 3 , 8 , 4 , 5 ] a = [1, 3, 8, 4, 5] a=[1,3,8,4,5] 你在 1 1 1 日购买了 2 2 2 张门票,那么这些门票的总价为 2 2 2 ,而从 2 2 2 日开始的价格将变为 [ 5 , 10 , 6 , 7 ] [5, 10, 6, 7] [5,10,6,7] 。如果您在 2 2 2 日又购买了 3 3 3 张门票,那么这些门票的总费用将增加 15 15 15 ,从 3 3 3 日起的价格将变为 [ 13 , 9 , 10 ] [13, 9, 10] [13,9,10] 。
求购买 k k k 张车票的最低消费。
分析:
通过观察发现,如果票的价格和每种票买的数量是确定的,那么最终价格和购买顺序没有任何关系,所以我们只需要贪心地进行排序,每次买价格最低的即可。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
int T;
cin >> T;
while (T--) {
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n + 1), id(n);
for (int i = 1; i <= n; i++) {
cin >> a[i];
id[i - 1] = i;
}
sort(id.begin(), id.end(), [&](int x, int y) {
if (a[x] != a[y]) return a[x] < a[y];
return x < y;
});
LL ans = 0;
int num = 0;
for (auto tmp: id) {
int t = min(m, k);
ans += 1LL * t * (a[tmp] + num);
num += t;
k -= t;
}
cout << ans << endl;
}
return 0;
}
D.Buying Jewels (思维)
题意:
爱丽丝有 n n n 枚硬币,她想去鲍勃的珠宝店购物。今天,虽然鲍勃还没有开店,但他想确保爱丽丝会买到准确的 k k k 颗珠宝。为了开设珠宝店,鲍勃最多可以设置 60 60 60 个摊位(每个摊位上的珠宝数量不限),并将每个摊位上每件珠宝的价格设置为介于 1 1 1 和 1 0 18 10^{18} 1018 之间的整数个硬币。
幸运的是,鲍勃知道爱丽丝会贪婪地购买:她会去 1 1 1 号摊位,尽可能多地购买珠宝,然后去 2 2 2 号摊位,尽可能多地购买珠宝,以此类推直到最后一个摊位。知道这一点后,鲍勃就可以选择摆摊的数量,并设定每个摊位的价格,这样爱丽丝就能买到恰好 k k k 件珠宝。请帮助鲍勃完成任务,或者判断是否不可能完成任务。
请注意,爱丽丝不需要花光所有金币。
分析:
首先处理特判, n = k , k = 1 n=k,k=1 n=k,k=1时分别 1 , 1 1,1 1,1和 1 , n 1,n 1,n。通过打表或其他方法发现当 2 × k ≤ n + 1 2 \times k \le n+1 2×k≤n+1 时,可以两步解决, n − k + 1 , 1 n-k+1,1 n−k+1,1,首先取 k − 1 k-1 k−1个 1 1 1,剩下的一块取即可。其他都是无解的。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
int main() {
int T;
cin >> T;
while (T--) {
LL n, k;
cin >> n >> k;
if (n == k) {
cout << "YES" << endl;
cout << 1 << endl;
cout << 1 << endl;
continue;
}
if (k == 1) {
cout << "YES" << endl;
cout << 1 << endl;
cout << n << endl;
continue;
}
if (2 * k <= n + 1) {
cout << "YES" << endl;
cout << 2 << endl;
cout << n - k + 1 << " " << 1 << endl;
continue;
}
cout << "NO" << endl;
}
return 0;
}
E. No Palindromes (字符串)
题意:
给出一个由小写拉丁字符组成的字符串 s s s 。需要将其分割成若干子串,使得每个子串都不是回文子串。
分析:
首先特判本身就是回文串的情况,本题有个结论:除了某些特殊情况, s s s最多分成两个字符串,在保证 s s s是回文串的情况下,只有以下情况不能被分割:
-
n < 4 n<4 n<4时,无法分割
-
s s s是只由同一个字母构成的字符串
-
s s s的形状是 a a a a a b a a a a a aaaaabaaaaa aaaaabaaaaa,其中一个字母有 n − 1 n-1 n−1个
- s s s的形状是 a b a b a b a b a ababababa ababababa 的循环子串。
其他情况都能被分成两个非回文串,通过双哈希进行回文串的判断。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct PIIhash {
const LL base[2] = {29, 31};
const LL hashmod[2] = {(LL) 1e9 + 9, 998244353};
array<vector<LL>, 2> hash1;
array<vector<LL>, 2> pwMod;
void init(string S) {
int n = S.size();
S = ' ' + S;
hash1[0].resize(n + 1), hash1[1].resize(n + 1);
pwMod[0].resize(n + 1), pwMod[1].resize(n + 1);
for (int i = 0; i < 2; ++i) {
pwMod[i][0] = 1;
for (int j = 1; j <= n; ++j) {
pwMod[i][j] = pwMod[i][j - 1] * base[i] % hashmod[i];
hash1[i][j] = (hash1[i][j - 1] * base[i] + S[j]) % hashmod[i];
}
}
}
pair<LL, LL> get(int l, int r) {
pair<LL, LL> ans;
ans.first = (hash1[0][r] - hash1[0][l - 1] * pwMod[0][r - l + 1]) % hashmod[0];
ans.second = (hash1[1][r] - hash1[1][l - 1] * pwMod[1][r - l + 1]) % hashmod[1];
ans.first = (ans.first + hashmod[0]) % hashmod[0];
ans.second = (ans.second + hashmod[1]) % hashmod[1];
return ans;
}
};
bool check(PIIhash &a, int la, int ra, PIIhash &b, int lb, int rb) {
return a.get(la, ra) == b.get(lb, rb);
}
int main() {
int T;
cin >> T;
while (T--) {
string s;
cin >> s;
int n = s.size();
string tmp = s;
reverse(tmp.begin(), tmp.end());
PIIhash a, b;
a.init(s), b.init(tmp);
if (!check(a, 1, n, b, 1, n)) {
cout << "YES" << endl;
cout << 1 << endl;
cout << s << endl;
continue;
}
int cnt = count(s.begin(), s.end(), s[0]);
if (n != 1)
count(s.begin(), s.end(), s[1]);
if (cnt >= n - 1) {
cout << "NO" << endl;
continue;
}
bool flag = 0;
for (int i = 2; i < n; i++) {
if (s[i] != s[i - 2])
flag = 1;
}
if (n < 4 || (!flag) && (n & 1)) {
cout << "NO" << endl;
continue;
}
for (int i = 1; i <= n; i++) {
if (!check(a, 1, i, b, n - i + 1, n) && !check(a, i + 1, n, b, 1, n - i)) {
cout << "YES" << endl;
cout << 2 << endl;
cout << s.substr(0, i) << ' ' << s.substr(i, n - i) << endl;
break;
}
}
}
return 0;
}
赛后交流
在比赛结束后,会在交流群中给出比赛题解,同学们可以在赛后查看题解进行补题。
群号: 704572101,赛后大家可以一起交流做题思路,分享做题技巧,欢迎大家的加入。