Codeforces Round #605 (Div. 3) 题解

Codeforces Round #605 (Div. 3)

【A. Three Friends】

【题目大意】
有三个人在一维坐标轴x上,坐标依次为a,b,c,每个人可以移动一次或者不移动,问abs(a - b) + abs(a - c) + abs(b - c)的最小值为多少

【解题思路】
不妨设a > b > c
考虑让a左移一格,让c右移一格
上式 = ((a - 1) - b) + ((a - 1) - (c + 1)) + (b - (c + 1)) = 2 * a - 2 * c - 4与b无关
那么找到abc中最大值和最小值即可求得答案

注意有可能a = b || a = c || b = c,那么答案有可能为负,为负时答案最小为0

【AC代码】

#include <bits/stdc++.h>
using namespace std;
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		int x = min(min(a, b), c);
		int y = max(max(a, b), c);
		printf("%d\n", max(2 * (y - x) - 4, 0));
	}
	return 0;
}

【B.Snow Walking Robot】

【题目大意】
给定由L,R,U,D组成的字符串,其分别代表向左,向右,向上,向下移动一格,起始格为(0, 0),地图大小为无限,问尽量少的删去字符串中的一些字母并重新排列剩下的字母后能否使其从(0, 0)运动至(0, 0),除起点外其余点仅能访问一次,输出最大移动次数,并输出最大移动字符串

【解题思路】
统计字符串中个字母出现次数,要想回到起点,那么肯定L = R && U = D,那么我们只要找到L和R中的最小值,U和D中的最小值,画一个矩形就好,如果最小值均为0,那么肯定无法到达,否则如果有一个为0,那么最多只能走两步

【AC代码】

#include <bits/stdc++.h>
using namespace std;
int main() {
	int t;
	scanf("%d", &t);
	string s;
	while (t--) {
		cin >> s;
		int L = 0, R = 0, U = 0, D = 0;
		for (int i = 0; s[i] != '\0'; ++i) {
			if (s[i] == 'L') ++L;
			else if (s[i] == 'R') ++R;
			else if (s[i] == 'U') ++U;
			else ++D;
		}
		int a = min(L, R), b = min(U, D);
		if (!a && !b) {
			puts("0");
			continue;
		}
		if (!a) b = 1;
		else if (!b) a = 1;
		printf("%d\n", 2 * (a + b));
		for (int i = 1; i <= a; ++i) {
			putchar('R');
		}
		for (int i = 1; i <= b; ++i) {
			putchar('U');
		}
		for (int i = 1; i <= a; ++i) {
			putchar('L');
		}
		for (int i = 1; i <= b; ++i) {
			putchar('D');
		}
		puts("");
	}
	return 0;
}

【C.Yet Another Broken Keyboard】

【题目大意】
有一个长度为n的字符串,问仅用给出的k个字符能组成多少个该串的子串

【解题思路】
记录所有能用k个字符表示的子串的长度,那么这些子串的子串肯定也能用那k个字符表示

【AC代码】

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
bool vis[30];
ll sum[maxn];
int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	string s;
	cin >> s;
	while (k--) {
		char c;
		scanf("%c", &c);
		while (c == ' ' || c == '\n') scanf("%c", &c);
		vis[c - 'a'] = true;
	}
	int l = s.length();
	int i = 0, cnt = 0;
	while (i < l) {
		bool flag = false;
		while (vis[s[i] - 'a']) {
			flag = true;
			++sum[cnt];
			++i;
		}
		if (flag) ++cnt;
		++i;
	}
	ll ans = 0;
	for (i = 0; i <= cnt; ++i) {
		ans += sum[i] * (sum[i] + 1) / 2;
	}
	printf("%lld\n", ans);
	return 0;
}

【D.Remove One Element】

【题目大意】
给定长度为n的数组,你可以删除一个数或不删除,问最长的连续递增子序列长度是多少

【解题思路】
dp1代表该以数字结尾的最长连续递增子序列长度
dp2代表以该数字开头的最长连续递增子序列长度
那么考虑a[i],如果不合并,那么ans = max(ans, dp1[i])
考虑合并
如果a[i + 1] > a[i - 1]那么以i - 1结尾的序列和以i + 1开头的序列显然可以合并,那么ans = max(ans, dp1[i - 1] + dp2[i + 1])

【AC代码】

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
ll a[maxn];
ll dp1[maxn];
ll dp2[maxn];
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
	}
	dp1[1] = 1;
	for (int i = 2; i <= n; ++i) {
		if (a[i] > a[i - 1])
			dp1[i] = dp1[i - 1] + 1;
		else dp1[i] = 1;
	}
	dp2[n] = 1;
	for (int i = n - 1; i >= 1; --i) {
		if (a[i + 1] > a[i])
			dp2[i] = dp2[i + 1] + 1;
		else dp2[i] = 1;
	}
	ll ans = 1;
	for (int i = 2; i <= n; ++i) {
		if (a[i + 1] > a[i - 1])
			ans = max(ans, dp1[i - 1] + dp2[i + 1]);
		ans = max(ans, dp1[i]);
	}
	printf("%lld\n", ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值