比赛链接:Here
1551A. Polycarp and Coins (签到)
题意:
我们有任意个面额为 (1) 和 (2) 的硬币去支付 (n) 元账单,
现在请问怎么去分配数额使得 (c_1 +2 * c_2 = n) 并且要最小化 (|c_1-c_2|)
贪心,
很容易想到最小化 (|c_1 - c_2|) 等于 (0) 或 (1)
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n;
cin >> n;
if (n % 3 == 0) cout << n / 3 << " " << n / 3 << "
";
else if (n % 3 == 1) cout << n / 3 + 1 << " " << n / 3 << "
";
else cout << n / 3 << " " << n / 3 + 1 << "
";
}
}
1551B1. Wonderful Coloring - 1(easy)
题意:
给定长度为 (n) 的字符串和 (k = 2) 种颜色,
对于字符串每一个位置要么不涂颜色,要么每种颜色的字母都要不同并且最后 (k) 种颜色涂的个数相同
请输出每种颜色最后的个数
统计 26 个字母出现次数,然后出现超过 2 的+1,仅出现一次的个数 / 2即可(易证明)
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
string s;
cin >> s;
int a[30] = {0};
for (int i = 0; i < int(s.length()); ++i) {
a[s[i] - 'a']++;
}
int a2 = 0, a1 = 0;
for (int i = 0; i < 26; ++i) {
if (a[i] == 1)a1++;
if (a[i] > 1)a2++;
}
cout << a1 / 2 + a2 << "
";
}
}
1551B2. Wonderful Coloring - 2 (hard版本)
题意基本同 B1 一致,
但需输出每个位置数字涂的颜色
写了两罚模拟 (mathcal{O}(n^2)) 不出所料肯定 T了,
转换一下思路,首先和 B1 一样我们需要去统计每种数字的个数,但由于最后需要输出每个位置的颜色编号,所以还得存一下下标 (pos)
- (cnt[a[i]] le k) 的将下标存储
- (cnt[a[i]] > k) 无需使用标 (0)
当然对于多余的元素,可以直接从删除
while (pos.size() % k != 0)pos.pop_back();
此时 pos 中所有元素均为有效下标,我们只要对应原数组标记颜色编号即可
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n, k;
cin >> n >> k;
vector<int>a(n), cnt(n);
vector<int>pos;
for (int i = 0; i < n; ++i) {
cin >> a[i], a[i] -= 1;
cnt[a[i]] += 1;
if (cnt[a[i]] <= k) pos.push_back(i);
}
while (pos.size() % k != 0)pos.pop_back();
sort(pos.begin(), pos.end(), [&](int i, int j) {return a[i] < a[j];});
vector<int>ans(n);
for (int i = 0; i < pos.size(); ++i)
ans[pos[i]] = i % k + 1;
for (int i = 0; i < n; ++i)
cout << ans[i] << "
"[i == n - 1];
}
}
1551C. Interesting Story
题意:
一个作家有 (n) 个单词,每一个单词只会由 a,b,c,d,e
构成,他想用 (m(0le mle n)) 个单词写一个故事
一个故事是否有趣在于:
-
故事中存在一个字母的个数比其他字母个数的总和还更多
比如:
bac
,aaada
,e
三个单词,a
出现的次数比其他 4 种单词出现次数都多
请输出在保证故事有趣的情况下用更多的单词写故事
这里建议直接看代码便于理解
【AC Code】代码参考于比赛 高Rank dalao
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n;
cin >> n;
vector<string>s(n);
for (int i = 0; i < n; ++i) cin >> s[i];
int ans = 0;
for (int c = 0; c < 5; ++c) {
vector<int>f(n);
for (int i = 0; i < n; ++i) {
for (auto x : s[i])
if (x == 'a' + c)f[i]++;
else f[i]--;
}
sort(f.begin(), f.end(), greater<int>());
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += f[i];
if (sum <= 0)break;
ans = max(ans, i + 1);
}
}
cout << ans << "
";
}
}
很好奇自己是怎么读题读成使用连续的单词去写故事的啊?kola
1551D1. Domino (easy version)
题意:待补
多米诺骨牌的排放似乎在ABC的某一场也出现过
如果 (n) 为奇数
- 最少要横放 (m / 2) 个,不然最少 (0) 个
- 最多 (n * m / 2 = (m \% 2 == 1 ? n / 2 : 0)) 个
对于给出的 (k) 只要在 ([mn,mx]) 就输出 YES
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
int n, m, k;
cin >> n >> m >> k;
int mn = (n % 2 == 1 ? m / 2 : 0);
int mx = n * m / 2 - (m % 2 == 1 ? n / 2 : 0);
if (mn <= k and k <= mx and (k - mn) % 2 == 0) cout << "YES
";
else cout << "NO
";
}
}