Codeforces Round #650 (Div. 3)题解

本文详细解析了Codeforces Round #650 (Div. 3)的六道题目,包括A Short Substrings、B Even Array、C Social Distance、D Task On The Board、E Necklace Assembly以及F Flying Sort (Easy Version)。每道题目分别阐述了题意、解题思路,并给出了AC代码。
摘要由CSDN通过智能技术生成

Codeforces Round #650 (Div. 3)

A.Short Substrings

题目大意

设原有的字符串为s,现在给你由s转化而来的t,让你输出原有的字符串s
其中t = s[0]s[1]s[1]s[2]s[2]s[3]…s[n - 1]s[n - 1]s[n]

解题思路

容易得到s = t[i % 2 == 0] + t[t.length() - 1]

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int main() {
   
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
   
		string s;
		cin >> s;
		int l = s.length();
		for (int i = 0; i < l; ++i) {
   
			if (i % 2 == 0) cout << s[i];
		}
		cout << s[l - 1] << '\n';
	}
	return 0;
}

B.Even Array

题目大意

给你一个长度为n的数组a,你可以将数组中的任意两个数进行交换,问你最少交换多少次可以使得任意的i有a[i] % 2 == i % 2,如果不能满足那么输出-1

解题思路

显然需要交换的是a[i] % 2 != i % 2的数,统计其中奇数和偶数的个数,如果相等则最小次数为奇数个数,否则输出-1

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int a[maxn];
int main() {
   
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
   
		int n;
		cin >> n;
		for (int i = 0; i < n; ++i) cin >> a[i];
		int x = 0, y = 0;
		for (int i = 0; i < n; ++i) {
   
			if (a[i] % 2 == i % 2) continue;
			else {
   
				if (a[i] % 2 == 0) ++x;
				else ++y;
			}
		}
		if (x == y) cout << x << '\n';
		else cout << "-1\n";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值