Codeforces Round #734 (Div. 3) A ~C题解
A. Polycarp and Coins
-
题意
有面值为 1 1 1和 2 2 2的硬币,需要你凑成 n n n的金额。要求使用的两者硬币数量相差最小。 -
解题思路
我们假设用了 x x x个面值为 1 1 1的硬币, y y y个面值为 2 2 2的硬币。这样,就满足 x + 2 y = n x + 2y = n x+2y=n,那我们假设 x = y x = y x=y,那么这个时候是最优的,需要满足 n % 3 = 0 n \%3 =0 n%3=0。那么实际上我们发现,当 n % 3 = 1 n \% 3 = 1 n%3=1时,即说明 x + 2 x − 2 = n x +2x -2=n x+2x−2=n,当 n % 3 = 2 n\%3=2 n%3=2时,即说明 x + 2 x + 2 = n x + 2x+2=n x+2x+2=n,这个时候我们把 n n n对 3 3 3取余的情况都考虑完了,则此题易解。 -
AC代码
/**
*@filename:A_Polycarp_and_Coins
*@author: pursuit
*@csdn:unique_pursuit
*@email: 2825841950@qq.com
*@created: 2021-07-23 22:35
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000 + 5;
const int P = 1e9+7;
int t,n;
void solve(){
if(n % 3 == 0){
cout << n / 3 << " " << n / 3 << endl;
}
else if(n % 3 == 1){
cout << (n + 2) / 3 << " " << (n + 2) / 3 - 1 << endl;
}
else{
cout << (n - 2) / 3 << " " << (n - 2) / 3 + 1 << endl;
}
}
int main(){
cin >> t;
while(t -- ){
cin >> n;
solve();
}
return 0;
}
B1. Wonderful Coloring - 1
-
题意
给你一个序列,你有两种颜色的染料。需要你给这个序列染色成 Wonderful Coloring序列。 Wonderful Coloring序列满足以下条件:- 序列上的元素要么只涂上一种颜色(红色或绿色),要么不涂;
- 涂上相同颜色的两个字母各不相同;
- 涂红色字母的数量等于涂绿色字母的数量;
- 在满足前三个条件的序列中的所有颜色中,尽可能多的涂颜色。
现需要你输出使其成为Wonderful Coloring序列使用红颜料的数量。
-
解题思路
不然知道,不同元素的颜色数量是不受限制的,而相同元素的最多使用红绿一次。所以我们只需要分开统计只出现一次的元素数量和出现了一次以上的元素数量,这样出现一次以上的必定会使用红颜料,而只出现一次的元素数量必定是分均使用颜料。故此题得解。 -
AC代码
/**
*@filename:B1_Wonderful_Coloring_1
*@author: pursuit
*@csdn:unique_pursuit
*@email: 2825841950@qq.com
*@created: 2021-07-23 22:50
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000 + 5;
const int P = 1e9+7;
int t;
string s;
int cnt[27];
void solve(){
memset(cnt,0,sizeof(cnt));
for(auto &x : s){
cnt[x - 'a'] ++;
}
int temp = 0,ans = 0;
for(int i = 0; i < 27; ++ i){
if(cnt[i] > 1)ans++;
else if(cnt[i] == 1)temp ++;
}
cout << ans + temp / 2 << endl;
}
int main(){
cin >> t;
while(t -- ){
cin >> s;
solve();
}
return 0;
}
B2. Wonderful Coloring - 2
-
题意
同 B 1 B1 B1,只不过颜料有 k k k种,需要你输出染完色的序列。 -
解题思路
和 B 1 B1 B1相同的想法,我们是本着能涂则涂的态度,那么对于出现了小于等于 k k k次的元素是一定能填的。我们可以用一个 p o s pos pos数组纪录能填的位置。由于要满足颜料的使用数量是相同的,所以所填元素数量需要整除 k k k,这里我们需要做一下处理即可。最后不要忘记排序,这可以使得相同的元素染不同的色。 -
AC代码
/**
*@filename:Wonderful_Coloring_2
*@author: pursuit
*@csdn:unique_pursuit
*@email: 2825841950@qq.com
*@created: 2021-07-23 23:01
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200000 + 5;
const int P = 1e9+7;
int t,n,k,a[N],cnt[N],ans[N];
vector<int> pos;
bool cmp(int i,int j){
return a[i] < a[j];
}
void solve(){
sort(pos.begin(),pos.end(),cmp);
int len = pos.size();
//cout << len << endl;
while(len % k)len --;
for(int i = 0; i < len; ++ i){
//cout << pos[i] << endl;
ans[pos[i]] = i % k + 1;
}
for(int i = 1; i <= n; ++ i){
cout << ans[i] << " ";
}
cout << endl;
}
int main(){
scanf("%d", &t);
while(t -- ){
scanf("%d%d", &n, &k);
pos.clear();
for(int i = 1; i <= n; ++ i){
cnt[i] = ans[i] = 0;
}
for(int i = 1; i <= n; ++ i){
scanf("%d", &a[i]);
cnt[a[i]] ++;
if(cnt[a[i]] <= k){
pos.push_back(i);
}
}
solve();
}
return 0;
}
C. Interesting Story
-
题意
给你 n n n个仅由'a','b','c','d','e'
组成的字符串。需要你尽可能的选出多的字符串使得其中一个字符的数量必其他字符的总数量多。 -
解题思路
我们首先要知道一个字符串的贡献,对于字符 a a a,如果 c n t a × 2 < = l e n g h t cnt_a \times2<=lenght cnta×2<=lenght。这样,我们可以统计每个字符的贡献,然后按照其排序即可。注意这里有一个技巧,由于我们排序需要根据字符串的贡献,所以我们结构体里需要内置一个值来存储贡献,这个值在我们需要用到的时候计算出来即可。具体看AC代码。 -
AC代码
/**
*@filename:C_Interesting_Story
*@author: pursuit
*@csdn:unique_pursuit
*@email: 2825841950@qq.com
*@created: 2021-07-23 23:22
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200000 + 5;
const int P = 1e9+7;
int t,n;
string s;
struct node{
int cnt[5];
int len,num;//num为我们要判断的字符的长度。
bool operator < (const node & A){
return num > A.num;
}
}temp[N],ans[N];
void solve(){
int maxx = 0;
for(int c = 0; c < 5; ++ c){
for(int i = 1; i <= n; ++ i){
ans[i] = temp[i];
ans[i].num = temp[i].cnt[c] * 2 - temp[i].len;
}
sort(ans + 1,ans + 1 + n);
int sum = 0;
for(int i = 1; i <= n + 1; ++ i){
if(i == n + 1){
maxx = n;
break;
}
//cout << ans[i].num << " ";
sum += ans[i].num;
if(sum <= 0){
maxx = max(maxx, i - 1);
break;
}
}
//cout << endl;
}
for(int i = 1; i <= n; ++ i){
memset(temp[i].cnt,0,sizeof(temp[i].cnt));
}
cout << maxx << endl;
}
int main(){
cin >> t;
while(t -- ){
cin >> n;
for(int i = 1; i <= n; ++ i){
cin >> s;
temp[i].len = s.size();
for(int j = 0; j < s.size(); ++ j){
temp[i].cnt[s[j] - 'a'] ++;
}
}
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!