Codeforces Round #782 (Div. 2)A,B题解
A. Red Versus Blue
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of nn matches.
In the end, it turned out Team Red won rr times and Team Blue won bb times. Team Blue was less skilled than Team Red, so bb was strictly less than rr.
You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length nn where the ii-th character denotes who won the ii-th match — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won 33 times in a row, which is the maximum.
You must find a string satisfying the above conditions. If there are multiple answers, print any.
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
Each test case has a single line containing three integers nn, rr, and bb (3≤n≤1003≤n≤100; 1≤b<r≤n1≤b<r≤n, r+b=nr+b=n).
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
3
7 4 3
6 5 1
19 13 6
RBRBRBR
RRRBRR
RRBRRBRRBRRBRRBRRBR
题目大意:共n个字符,其中r个为‘R’,b个为‘B’,求出一种排列方式使R相邻的个数最少。
思路:字符B将整个序列分为b+1个子序列,依次输出即可。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int t,n,r,b;
int a[1000];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--)
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>r>>b;
int op=r/(b+1);
int num=r-(b+1)*op;
for(int i=1;i<=b+1;i++)
{
a[i]=op;
if(num>0)
{
a[i]++;
num--;
}
}
for(int h=1;h<=b;h++)
{
for(int i=1;i<=a[h];i++)
cout<<'R';
cout<<'B';
}
for(int i=1;i<=a[b+1];i++)
cout<<'R';
cout<<'\n';
}
return 0;
}
B. Bit Flipping
You are given a binary string of length nn. You have exactly kk moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped (00 becomes 11, 11 becomes 00). You need to output the lexicographically largest string that you can get after using all kk moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.
A binary string aa is lexicographically larger than a binary string bb of the same length, if and only if the following holds:
- in the first position where aa and bb differ, the string aa contains a 11, and the string bb contains a 00.
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
Each test case has two lines. The first line has two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105; 0≤k≤1090≤k≤109).
The second line has a binary string of length nn, each character is either 00 or 11.
The sum of nn over all test cases does not exceed 2⋅1052⋅105.
For each test case, output two lines.
The first line should contain the lexicographically largest string you can obtain.
The second line should contain nn integers f1,f2,…,fnf1,f2,…,fn, where fifi is the number of times the ii-th bit is selected. The sum of all the integers must be equal to kk.
6
6 3
100001
6 4
100011
6 0
000000
6 1
111001
6 11
101100
6 12
001110
111110
1 0 0 2 0 0
111110
0 1 1 1 0 1
000000
0 0 0 0 0 0
100110
1 0 0 0 0 0
111111
1 2 1 3 0 4
111110
1 1 4 2 0 4
题目大意:一个二进制数,每次固定一位翻转其他所有位上的数,经过k次翻转,输出最小的二进制数和每个位置固定了几次。
思路:一个数翻转偶数次不变,翻转奇数次则位n^1。
如果为奇数,则从左到右优先翻转本来就为1的数,为偶数的话则从左到右优先翻转本来为0的数,剩余的k值则全部用来翻转最后一位。
#include<bits/stdc++.h>
using namespace std;
int n,k,t;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>t;
while(t--)
{
cin>>n>>k;
string s;
vector<int>v,ans;
cin>>s;
int sum=k;
if(k&1)
{
for(int i=0;i<s.size()-1;i++)
{
if(s[i]=='1'&&sum>0)
{
v.push_back(1);
sum--;
}
else
v.push_back(0);
}
v.push_back(sum);
for(int i=0;i<s.size();i++)
{
if((s[i]-'0')%2==v[i]%2)
ans.push_back(1);
else
ans.push_back(0);
}
for(int i=0;i<ans.size();i++)
cout<<ans[i];
cout<<'\n';
for(int i=0;i<v.size();i++)
cout<<v[i]<<' ';
cout<<'\n';
}
else
{
for(int i=0;i<s.size()-1;i++)
{
if(s[i]=='0'&&sum>0)
{
v.push_back(1);
sum--;
}
else
v.push_back(0);
}
v.push_back(sum);
for(int i=0;i<s.size();i++)
{
if((s[i]-'0')%2!=v[i]%2)
ans.push_back(1);
else
ans.push_back(0);
}
for(int i=0;i<ans.size();i++)
cout<<ans[i];
cout<<'\n';
for(int i=0;i<v.size();i++)
cout<<v[i]<<' ';
cout<<'\n';
}
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析