Educational Codeforces Round 149 (Rated for Div. 2)A~D

Grasshopper on a Line

题意:

给出n和k,求从0到n最少走几步,以及步长,要求步长不能整除k。

思路:

从n往下找到%k不等于0的数,输出该数和n-该数即可,如果n%k!=0,那就只需要一步。

代码:

/*************************************************************************
    > File Name: a.cpp
    > Author: Beans
    > Mail: 3112748286@qq.com 
    > Created Time: 2023/5/26 10:17:22
 ************************************************************************/
#include <iostream>
#include <algorithm>
#define int long long
#define endl '\n'

using namespace std;

int n, k;

void solve(){
	cin >> n >> k;
	int ans = n;
	while(ans % k == 0)	ans -- ;
	if(ans == n){
		cout << 1 << endl << ans << endl;
	}
	else {
		cout << 2 << endl << ans << ' ' << n - ans << endl;
	}
}

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

Comparison String

题意:

给一个只包含<和>的字符串,长度为n,有一个隐藏数组,如果s[i]是<就说明a[i]<a[i+1],如果是>则相反,问a数组中最少有多少个不同的数。

思路:

因为<升高和>降低可以相互抵消,所以不难发现,最少的不同的数即为字符串中最长的连续的<或>数,输出即可。

代码:

/*************************************************************************
    > File Name: b.cpp
    > Author: Beans
    > Mail: 3112748286@qq.com 
    > Created Time: 2023/5/26 14:03:24
 ************************************************************************/
#include <iostream>
#include <algorithm>
#define int long long
#define endl '\n'

using namespace std;

int n;
string s;

void solve(){
	cin >> n;cin >> s;
	int ans = 0, tem = 1;
	for(int i = 1; i < n; i ++ )
		if(s[i] == s[i - 1])	tem ++ ;
		else	ans = max(ans, tem), tem = 1;
	cout << max(ans, tem) + 1 << endl;
}

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

Best Binary String

题意:

给一个包含0,1,?的字符串,?可以换成0或1,要求换完之后使得成本最小,二进制字符串的成本定义为按非降序对字符串进行排序所需的“反转字符串的任意连续子字符串”形式的最小操作数。

思路:

因为每次操作是反转连续子字符串,即反转一个区间,所以一个1或0与连续的一块1或0的操作数都是一样的,所以只需要把?替换成与前面一样的即可。

代码:

/*************************************************************************
    > File Name: c.cpp
    > Author: Beans
    > Mail: 3112748286@qq.com 
    > Created Time: 2023/5/26 14:09:09
 ************************************************************************/
#include <iostream>
#include <algorithm>
#define int long long
#define endl '\n'

using namespace std;

string s;

void solve(){
	cin >> s;char a = '0';
	for(auto& c : s){
		if(c == '?')	c = a;
		a = c;
	}
	cout << s << endl;
}

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

Bracket Coloring

题意:

给出一个括号序列,定义漂亮序列为匹配括号序列或者反转之后是匹配括号序列的序列,现在要求染色,使得相同颜色的括号组成漂亮序列,问最少需要多少种颜色即每个括号染的颜色。

思路:

这里可以用栈来匹配括号序列,因为可以反转,而且要颜色最少。首先排除不合法的序列,直接输出-1。对于合法序列,不难发现,最多只需要花费两种颜色即可,先全部初始化为颜色2,对于正向匹配的括号,染成颜色1,最后判断,如果存在颜色2,说明存在逆向匹配的括号,这时候再重新初始化为2,将逆向匹配的括号染成1即可,因为正向匹配的括号不兼容逆向匹配的括号,而逆向匹配的括号兼容正向匹配的括号。
例如:)()( 这个序列,如果按照正向匹配则需要1,2两种颜色, 而逆向匹配可以全部染色成颜色1.

代码:

/*************************************************************************
    > File Name: d.cpp
    > Author: Beans
    > Mail: 3112748286@qq.com 
    > Created Time: 2023/5/26 11:37:04
 ************************************************************************/
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#define int long long
#define endl '\n'

using namespace std;

int n;
string s;

void solve(){
	cin >> n;cin >> s;
	s = " " + s;
	if((int)count(s.begin(), s.end(), '(') * 2 != n){cout << -1 << endl;return;}
	vector<int> ans(n + 1, 2);ans[0] = 0;
	stack<int> st;
	for(int i = 1; i <= n; i ++ )
		if(s[i] == '(')	st.push(i);
		else if (st.size())	ans[i] = 1, ans[st.top()] = 1, st.pop();
	if(count(ans.begin(), ans.end(), 2)){
		ans.assign(n + 1, 2), stack<int>().swap(st), ans[0] = 0;
		for(int i = 1; i <= n; i ++ )
			if(s[i] == ')')	st.push(i);
			else if(st.size())	ans[i] = 1, ans[st.top()] = 1, st.pop();
	}
	cout << *max_element(ans.begin(), ans.end()) << endl;
	for(int i = 1; i <= n; i ++ )	cout << ans[i] << " \n"[i == n];
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方哲Beans

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值