codeforces round 149 div2(a,b,c,d)

手速场,可惜我傻逼卡 c c c
题目链接

A

#include<bits/stdc++.h>
 
using namespace std;
 
#define int long long
#define PII pair<int,int>
 
void solve()
{
	int n,k;cin>>n>>k;
	if(n<k){
		cout<<1<<'\n';
		cout<<n<<'\n';
	}
	if(n==k){
		cout<<2<<'\n';
		cout<<n-1<<' '<<1<<'\n';
	}
	if(n>k){
		if(n%k!=0){
			cout<<1<<'\n';
			cout<<n<<'\n';
			return;
		}
		cout<<2<<'\n';
		cout<<n-1<<' '<<1<<'\n';
	}
}
 
signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

B

#include<bits/stdc++.h>
 
using namespace std;
 
#define int long long
#define PII pair<int,int>
 
void solve()
{
	int n;cin>>n;
	string str;cin>>str;
	int cnt=0;
	str=' '+str;
	int ans=INT_MIN;
	for(int i=1;i<=str.size();i++){
		cnt=1;
		while(str[i]==str[i+1]&&i+1<=n)cnt++,i++;
		ans=max(ans,cnt);
	}
	cout<<ans+1<<'\n';
}
 
signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

C

题目大意

给定仅包含 1 , 0 , ? 1,0,? 1,0,?的字符串 s s s,允许把 ? ? ?变为 1 1 1 0 0 0。定义操作为翻转任意连续字串。定义字符串代价为,令整个串呈非降序的操作数量

思路

结论:当 ? ? ?在首位,则变为 0 0 0,若在后面,则等于前面的字符
证明:考虑 ? ? ?的位置,当在首位的时候,令其为 0 0 0对总操作数没有贡献
在后面的时候,若 ? ? ?前面是 1 1 1,则令其为 1 1 1对总操作数没有贡献,前面是 0 0 0 1 1 1同理

ACcode

#include<bits/stdc++.h>

using namespace std;

#define int long long

void solve()
{
	string str;cin>>str;
	if(str[0]=='?')str[0]='0';
	for(int i=1;i<str.size();i++){
		if(str[i]=='?')str[i]=str[i-1];
	}
	cout<<str<<'\n';
}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

D

题目大意

如果一个括号序列符合以下两个条件中的一个,那么它是优美的:

1、序列的任意一个前缀中,左括号的个数不少于右括号的个数,且整个序列中,左括号的个数等于右括号的个数。

2、序列的任意一个前缀中,右括号的个数不少于左括号的个数,且整个序列中,左括号的个数等于右括号的个数。

给定一个括号序列,你需要把它分成若干组,使得每一组的括号构成的序列都是优美的。求最少需要分成多少组,并输出分组方案。如果无解,则输出 − 1 -1 1
——翻译来自洛谷Bracket Coloring

思路

括号匹配
0 , 1 0,1 0,1赋给 ) , ( ),( ),(,统计前缀和
在这里插入图片描述
所有绿色是一种,所有红色是一种
再特判一下括号数量不同和分成一组即可

ACcode

#include<bits/stdc++.h>

using namespace std;

#define int long long

void solve()
{
	int n;cin>>n;
	string str;cin>>str;
	if(n&1){cout<<-1<<'\n';return;}
	if(str[0]==')')for(int i=0;i<n;i++)if(str[i]=='(')str[i]=')';else str[i]='(';
	vector<int>ou(n,2);
	bool ok=1;bool pos=1;int c=0;
	for(int i=0;i<n;i++){
		if(str[i]=='(')c++;
		else c--;
		if(c>0)pos=1,ou[i]=1;
		if(c==0)if(pos)ou[i]=1;
		if(c<0)pos=0,ok=0;
	}
	if(c){cout<<-1<<'\n';return;}
	if(ok){
		cout<<1<<'\n';
		for(int i=0;i<n;i++)cout<<1<<' ';
		cout<<'\n';
		return;		
	}
	cout<<2<<'\n';
	for(int i=0;i<n;i++)cout<<ou[i]<<' ';
	cout<<'\n';
}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;
	while(t--){
		solve();
	}
	return 0;
}
  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值