Codeforces Round #754 (Div. 2)

反思:又是被教训的一场比赛,好久没打了,又菜了很多,三个思维题苦思冥想也想不出来www

A. A.M. Deviation

传送门
题意:三个数a1,a2,a3,求abs(a1 + a3 -2a2)最小值,可以对三个数中的其中两个数加一,另一个减一,该操作次数不限
题解:多次列举会发现结果只会在0和1之间,三个数的和是3的倍数就是0,否则就是1,始终会围绕3展开计算,比如
2 2 6 —> 2 2+1 6-1 -> abs(2 + 6-1 - 2
(2+1)) 也就是2+2+6减3等于7,这样再对3取余就是1,我们总有办法维持和在3的倍数的(-1,1)区间
code:

# include <iostream>
# include <algorithm>
# include <string.h>
# include <cstring>
 
using namespace std;
 
typedef long long ll;
 
const int N = 1e5 + 10;
 
ll a[N];
 
bool cmp(int a, int b){
	return a > b;
}
 
int main(){
	int  n;
	cin >> n;
	while (n --){
		int a , b, c;
		cin >> a >> b >> c;
		int sum = 0;
		sum = (a + b + c)%3;
		if (sum) cout << 1 << endl;
		else cout << 0 << endl;
	}
	return 0;
}

B. Reverse Sort

传送门
题意:给定一个只有0和1的字符串,判断是否是非递减序列。若不是,让需要交换的字符串首尾向里交换位置(如:1~4,1和4,2和3),并输出需要交换的字符下标
题解:用stl的is_sorted函数,判断一下是否是非降序的字符串,若是,输出0,若不是,输出1,建立一个新的字符串ss等于 原来的字符串,并把ss升序排列,for循环一一对应,不相等就存入数组,最后输出数组
code:

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
const int N = 1e5 + 10;
 
int a[N];
int b[N];
 
int main(){
	int n;
	cin >> n;
	while (n --){
		int l;
		cin >> l;
		string s;
		cin >> s;
		if (is_sorted(s.begin(),s.end())){
			cout << 0 << endl;
			continue;
		}
		cout << 1 << endl;
		string ss= s;
		sort (ss.begin(),ss.end());
		vector<int> ans;
		for (int i = 0; i < ss.size(); i ++){
			if (ss[i] != s[i])
				ans.push_back(i + 1);
		}
		cout << ans.size() << " ";
		for (auto i: ans)
			cout << i << " ";
		cout << endl;
	}
	return 0;
}
 

C. Dominant Character

传送门
题意:寻找最短子串并输出其长度,使得a的数量严格大于b和c的数量
题解:直接一一列举,一共七种情况,但当时根本想不出来菜死
code:

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
const int N = 1e5 + 10;
 
int a[N];
int b[N];
 
int main(){
	int n;
	cin >> n;
	while (n --){
		int l;
		cin >> l;
		string s;
		cin >> s;
		int ans = 0;
		string a[7] = {"aa","aba","aca","abca","acba","abbacca","accabba"};
		for (int i = 0; i < 7; i ++){
			if (s.find(a[i]) != -1){
				cout << a[i].size() << endl;
				ans = 1;
				break;
			}
		}
		if (!ans) cout << -1 << endl;
	}
	return 0;
}
posted @ 2021-11-13 17:19  Gsding  阅读(83)  评论(0)    收藏  举报