Codeforces Round #738 (Div. 2)

https://codeforces.com/contest/1559/problem/A

A. Mocha and Math

题意:给你一个长度为n的数组,你可以指定一个范围[l,r],让0<=i<=r-l范围内的数,变成a(l+i)&a[r-i].你可以操作任意次,问你操作完后,这个数组里最小的值是多少。

思路:考察&的性质(两个为1时才为1,否则就是0),那么就考虑如果每个数都&一遍,那么得出的结果一定是最小的。

代码:

#include<bits/stdc++.h>
#define ll long long
#define hh 0x3f3f3f3f
const int maxn=1e5+5;
using namespace std;
inline int read()
{
	ll s=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
	return s*f;
}
void solve()
{
	int n,ans=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		if(i==1)
			ans=x;
		else
			ans=ans&x;
	}
	cout<<ans<<endl;
}
int main()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
	int t;
	cin>>t;
	while(t--)solve();
	return 0;
}

https://codeforces.com/contest/1559/problem/B

B. Mocha and Red and Blue

题意:给你一个长度为n的字符串,里面只含“R”、“?”、“B”,其中?可以变成“R”或者“B”,问你让这个?怎么变,使得这个字符串相邻颜色相同个数最少。比如:BRRRBBR,相同相邻的颜色个数是3(有两次RR,一次BB);

思路:模拟,

第一种情况:字符串全是'?',如果是那么就好办,奇数是'R',偶数是'B'。

第二种情况以字母结尾,比如'????B'、'B???R??B???R'这种,这种的解法就是让一堆问号从后往前依次与后面一个作比较比如'B???R' -> 'B??BR' ->'B?RBR' -> 'BBRBR' 这样。

第三种情况就是以问号结尾,比如:'R??BB?'、'BR???'这种解法就是让这堆问号从从前往后推依次比较,比如:‘BR???’ -> 'BRB??' -> 'BRBR?' -> 'BRBRB' 。

第三种情况可以在处理完第二种情况后,再用一个循环排除一下就行了。

代码:

#include<bits/stdc++.h>
#define ll long long
#define hh 0x3f3f3f3f
const int maxn=1e5+5;
using namespace std;
inline int read()
{
	ll s=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
	return s*f;
}
char s[200];
void solve()
{
	int n,top=0,tp=0;
	cin>>n>>s+1;
	for(int i=1;i<=n;i++)
	{
		if(s[i]=='?')tp++;
	}
	//第一种情况 
	if(tp==n)
	{
		for(int i=1;i<=n;i++)
		{
			if(i%2==0)
				cout<<"R";
			else 
				cout<<"B";
		}
		cout<<endl;return ;
	}
	//第二种情况 
	for(int i=1;i<=n;i++)
	{
		if(s[i]=='?')
			top=i;
		else
		{
			for(int j=top;;j--)
			{
				if(s[j]!='?')break;
				else
				{
					if(s[j+1]=='B')
						s[j]='R';
					else 
						s[j]='B';
				}
			}
		}
	}
	//第三种情况 
	for(int i=1;i<=n;i++) 
	{
		if(s[i]=='?'&&s[i-1]=='R')s[i]='B';
		else if(s[i]=='?'&&s[i-1]=='B')s[i]='R';
	}
	for(int i=1;i<=n;i++)cout<<s[i]; 
	cout<<endl<<endl;
}
int main()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
	int t;
	cin>>t;
	while(t--)solve();
	return 0;
}

https://codeforces.com/contest/1559/problem/C

C. Mocha and Hiking

题意:给你一个长度为n的数组a,表示有n+1个村庄,数组a里只有0和1,ai和a(i+1)之前都存在一条路,现在你要走完n+1个村庄。

有以下限制:当ai=0时,你可以在第i个村庄走向第n+1个村庄;

                      当ai=1时,你可以从第n+1个村庄走向第i个村庄。

问你怎么走,每个村庄都走一次?如果有多种方案就输出任意一种即可。

思路:分三种情况:

1.当a1=1时,那么这个答案就是:n+1 -> 1 -> 2 -> ..... -> n;

2.当an=0时,那么这个答案就是:1 -> 2 -> 3 -> ...... -> n -> n+1;

3.当ai=0且a(i+1)=1时,答案就是:1 -> 2 ->......-> i -> n+1 -> i+1 -> ... ->n。 

代码:

#include<bits/stdc++.h>
#define ll long long
#define hh 0x3f3f3f3f
const int maxn=1e5+5;
using namespace std;
inline int read()
{
	ll s=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=(s<<3)+(s<<1)+c-'0';c=getchar();}
	return s*f;
}
int f[10005],n,pd=1; 
void solve()
{
	pd=1;cin>>n;
	for(int i=1;i<=n;i++)cin>>f[i];
	if(f[1]==1)
	{
		cout<<n+1<<" ";
		for(int i=1;i<=n;i++)cout<<i<<" ";
	}
	else if(f[n]==0)
	{
		for(int i=1;i<=n;i++)cout<<i<<" ";
		cout<<n+1;
	}
	else
	{
		for(int i=1;i<=n;i++)
		{
			if(f[i]==0&&f[i+1]==1&&pd==1)
			{
				cout<<i<<" "<<n+1<<" ";pd=0;
			}
			else cout<<i<<" ";
		}
	}
	cout<<endl;
}
int main()
{
	ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
	int t;
	cin>>t;
	while(t--)solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值