【CF】Educational Codeforces Round 132 (Rated for Div. 2) C++代码

A. Three Doors

There are three doors in front of you, numbered from 11 to 33 from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.

There are three keys — one for each door. Two of them are hidden behind the doors, so that there is no more than one key behind each door. So two doors have one key behind them, one door doesn't have a key behind it. To obtain a key hidden behind a door, you should first unlock that door. The remaining key is in your hands.

Can you open all the doors?

做一个小深搜?

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 1e5 + 5;
void solve()
{
	int x, a[5];
	cin >> x;
	cin >> a[1] >> a[2] >> a[3];
	int k = x;
	int cnt = 0, c = 0;
	bool b[4];
	b[1]=b[2]=b[3]=0;
	while (k && c <= 4)
	{
		if (b[k] == 0)
		{
			cnt++;
			b[k] = 1;
		}
		k = a[k];
		c++;
	}
	if (cnt == 3)cout << "YES\n";
	else cout << "NO\n";
	return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}

B. Also Try Minecraft

You are beta testing the new secret Terraria update. This update will add quests to the game!

Simply, the world map can be represented as an array of length nn, where the ii-th column of the world has height aiai.

There are mm quests you have to test. The jj-th of them is represented by two integers sjsj and tjtj. In this quest, you have to go from the column sjsj to the column tjtj. At the start of the quest, you are appearing at the column sjsj.

In one move, you can go from the column xx to the column x−1x−1 or to the column x+1x+1. In this version, you have Spectre Boots, which allow you to fly. Since it is a beta version, they are bugged, so they only allow you to fly when you are going up and have infinite fly duration. When you are moving from the column with the height pp to the column with the height qq, then you get some amount of fall damage. If the height pp is greater than the height qq, you get p−qp−q fall damage, otherwise you fly up and get 00 damage.

For each of the given quests, determine the minimum amount of fall damage you can get during this quest.

从前扫一遍前缀和,再从后做一遍前缀和

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 1e5 + 5;
ll sum[N], lsum[N];
ll a[N], n;
void solve()
{
	int l, r;
	cin >> l >> r;
	if (l < r)cout << sum[r] - sum[l] << '\n';
	else cout << lsum[r] - lsum[l] << '\n';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> n;
	cin >> t;
	cin >> a[1];
	sum[0] = sum[1] = 0;
	for (int i = 2; i <= n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + max(0ll, a[i - 1] - a[i]);
	}
	for (int i = n - 1; i >= 1; i--)
	{
		lsum[i] = lsum[i + 1] + max(0ll, a[i + 1] - a[i]);
		//cout << lsum[i] << " ";
	}
	while (t--)
	{
		solve();
	}
	return 0;
}

 C. Recover an RBS

A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example:

  • bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");
  • bracket sequences ")(", "(" and ")" are not.

There was an RBS. Some brackets have been replaced with question marks. Is it true that there is a unique way to replace question marks with brackets, so that the resulting sequence is an RBS?

第一步,看看问号?数量和缺少的“)”或“(”是否一样,一样就只有一种选择,不用再考虑。

第二步,填写。先把右括号填满,当仅需要1个右括号时,先填写一个左括号,然后填右括号,剩下的都是右括号,因为如果可以交换(有其他可能),首先会交换最中间的两个问号的左右括号。

为什么要换中间两个:因为题目给的数据是必然有解的,如果对于问号处,先填写左括号(,再填写右括号),必然是答案的解。我们要找第二组解,肯定是交换中间的两个问号?产生的影响最小,如果有多组解,如?(??)?,看中间两个交换能否成立。

如果改中间两个?不能成立,那就不可能有多组解了。

#include<bits/stdc++.h>
using namespace std;
void solve()
{
	string s = "";
	cin >> s;
	int n = s.size();
	int cnt = 0, cnt1 = 0, cnt2 = 0, ct = 0;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == '?')
		{
			cnt++;
		}
		if (s[i] == '(')
		{
			cnt1++;
		}
		if (s[i] == ')')
		{
			cnt1--;
		}
	}
	if (cnt == abs(cnt1))
	{
		cout << "YES\n";
		return;
	}

	int cntpos = (cnt - cnt1) / 2; //需要的(数量
//	cout << cntpos << endl;
	bool flag = false;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == '?')
		{
			if (cntpos > 1)
			{
				s[i] = '(';
				cntpos--;
			}
			else if (cntpos == 1)
			{
				if (flag)
				{
					s[i] = '(';
					cntpos--;
				}
				else
				{
					s[i] = ')';
					flag = true;
				}
			}
			else
			{
				s[i] = ')';
			}
		}
	}
	cnt2=0;
	for (int i = 0; i < n; i++)
	{
		if (s[i] == '(')
			cnt2++;
		else
			cnt2--;
		if (cnt2 < 0)
		{
			cout << "YES\n";
			return;
		}
	}
	cout << "NO\n";
	return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}
方法二
int main() {
#ifdef local
	freopen("1.in", "r", stdin);
#endif
	T = read();
	while (T--) {
		scanf("%s", c + 1);
		n = strlen(c + 1);
		int cnt[2] = { 0, 0 };
		len = 0;
		for (int i = 1; i <= n; i++) {
			if (c[i] == '(') ++cnt[0];
			if (c[i] == ')') ++cnt[1];
			if (c[i] == '?') seq[++len] = i;
		}
		if (cnt[0] == n / 2 || cnt[1] == n / 2) {
			printf("YES\n");
			continue;
		}
		for (int i = 1; i <= n / 2 - cnt[0]; i++) c[seq[i]] = '(';
		for (int i = n / 2 - cnt[0] + 1; i <= len; i++) c[seq[i]] = ')';
		swap(c[seq[n / 2 - cnt[0]]], c[seq[n / 2 - cnt[0] + 1]]);
		int sum = 0, ans = 0;
		rep(i, 1, n) {
			sum += (c[i] == '(' ? 1 : -1);
			if (sum < 0) {
				ans = 1;
				break;
			}
		}
		printf("%s\n", ans ? "YES" : "NO");
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值