Codeforces Round #327 (Div. 2) 591ABC题解

46 篇文章 0 订阅
34 篇文章 0 订阅

题目链接:点击打开链接


A: 两个人在两端相向而行, 第一次碰面后掉头, 到端点后又掉头, 问第二次碰面时候距离左端点的距离.

脑洞题目, 第二次和第一次碰面在同一个地方, 要求精度在1e-4以内, 输出5位小数即可.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
int l, p, q;
int main(int argc, char const *argv[])
{
    scanf("%d%d%d", &l, &p, &q);
    printf("%.5f\n", (1.0 * l * p) / (p + q));
    return 0;
}


B: 给定长度为n的字符串, 进行m次操作. 每次操作形如a b, 就是把字符串中所有的字母a变为字母b.

模拟题目, 定义一个存储26个字母的数组, 每次操作对数组进行操作即可, 输出的时候运用该数组输出.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 2e5 + 5;
int n, m;
char s[MAXN], word[30];
int main(int argc, char const *argv[])
{
	cin >> n >> m >> s;
	for(int i = 0; i < 26; ++i)
		word[i] = i + 'a';
	for(int i = 0; i < m; ++i) {
		char x, y;
		cin >> x >> y;
		for(int j = 0; j < 26; ++j)
			if(word[j] == x) word[j] = y;
			else if(word[j] == y) word[j] = x;
	}
	for(int i = 0; i < n; ++i)
		cout << word[s[i] - 'a'];
	cout << endl;
	return 0;
}


C: 给定一个长度为n的数组, 问你经过多少次变换可以使该数组stable, 不能则输出-1. 每次变换要求i由2至n - 1, a[i]变为a[i - 1], a[i], 

a[i + 1]的中位数.

模拟题. 列举可能出现的情况, 可以发现101, 010会发生变化. 定义一个dp数组求出经过变换的次数, 然后输出变换后的数组即可.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 5e5 + 5;
int n, ans, a[MAXN], dp[MAXN];
int main(int argc, char const *argv[])
{
	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for(int i = 1; i < n; ++i)
		if(a[i] != a[i - 1]) dp[i] = dp[i - 1] + 1;
		else dp[i] = 0;
	dp[n - 1] = 0;
	for(int i = n - 1; i > 0; --i)
		if(a[i] != a[i + 1]) dp[i] = min(dp[i], dp[i + 1] + 1);
		else dp[i] = 0;
	for(int i = 0; i < n; ++i)
		ans = max(ans, dp[i]);
	printf("%d\n", ans);
	for(int i = 0; i < n; ++i)
		if(i == 0 || i == n - 1) printf("%d ", a[i]);
		else printf("%d ", a[i] ^ (dp[i] & 1));
    printf("\n");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值