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";