Codeforces Round #832 (Div. 2)(A、B、C)

文章提供了三道编程题目,分别涉及数组的分割以最大化绝对差值,将字符串BAN调整以消除子串,以及Alice和Bob的游戏策略问题。解题思路包括将正数和负数分开处理,以及分析字符串变换和游戏轮流操作的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A题

Problem - A - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1747/problem/A题意:

        本题是要求将题目所给的数组分成两组,分为s1和s2,使得|sum(s1)|−|sum(s2)|的值最大。

思路:

        我们可以看出,要想使两边数组之和最大,直接使负数和非负数分即可,用较大的减去较小的即可。

代码:
 

#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long

using namespace std;

const int N = 100010;

int T;

signed main()
{
	cin >> T;
	
	while(T--)
	{
		int n;
		scanf("%lld", &n);
		int s1 = 0, s2 = 0;
		for (int i = 0; i < n; ++ i) {
			int x;
			scanf("%lld", &x);
			if(x >= 0) s1 += x;
			else s2 -= x;
		}
		if(s1 > s2) printf("%lld\n", s1 - s2);
		else printf("%lld\n", s2 - s1);
	}
	return 0;
 } 

B题

Problem - B - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1747/problem/B题意:

        本题是求n个 "BAN" ,要经过对调转换使得字符串中找不到BAN的字串,要求步数最小。

思路:

        直接将'B' 和'N' 所在位置(B从前开始换, N从后开始换),直到没有"BAN"为止。

代码:
 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

const int N = 110;

int T;

signed main()
{
	scanf("%d", &T);
	int n;
	while(T--)
	{
		scanf("%d", &n);
		int ans = 0;
		ans = n % 2 == 0 ? n / 2 : n / 2 + 1;
		printf("%d\n", ans);
		n *= 3;
		int l = 1, r = n;
        while(ans--)
	    {
		    printf("%d %d\n", l, n);
		    l += 3, n -= 3;
	    }
	}
	return 0;
}

C题

Problem - C - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1747/problem/C题意:
        本题是一个游戏,Alice和Bob轮流,Alice先手,每次可以将a[1] - 1并与其他元素对换,求最终的胜利者。

思路:

        经过分析我们可以看出一个规律,那就是判断初始数组中最小值的位置即可,如果最小值在a[1]处,那么Bob赢,否则Alice赢。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define int long long

using namespace std;

int T;

signed main()
{
	scanf("%d", &T);
	int n;
	while(T--)
	{
		scanf("%lld", &n);
		int x, mi = 0x3f3f3f3f, fis = 0;
		for (int i = 0; i < n; ++ i) {
			scanf("%lld", &x);
			mi = min(mi, x);
			if(fis == 0) fis = x;
		}
		if(mi == fis) puts("Bob");
		else puts("Alice");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Dawpro_加薪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值