C/C++教程

Codeforces Round #744 (Div. 3) 题解

本文主要是介绍Codeforces Round #744 (Div. 3) 题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

A. Casimir's String Solitaire

思路

因为只能同时擦去 A B,或 B C,所以只有在 A 的数量加 C 的数量等于 B 的数量时,才能完全擦除。

代码

#include <bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define I inline
#define ll long long
#define Con const
#define CI Con int
#define RI register int
#define W while
#define gc getchar
#define D isdigit(c=gc())
#define pc(c) putchar((c))
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define Gmax(x,y) (x<(y)&&(x=(y)))
#define Gmin(x,y) (x>(y)&&(x=(y)))
#define ms(a,x) memset((a),(x),sizeof(a))
using namespace std;
namespace FastIO
{
	CI FS = 1e5; int Top = 0; char S[FS];
	I void readc (char& c) {W (isspace (c = gc ()));}
	Tp I void read (Ty& x) {char c; int f = 1; x = 0; W (! D) f = c ^ '-' ? 1 : -1; W (x = (x * 10) + (c ^ 48), D); x *= f;}
	Ts I void read (Ty& x, Ar&... y) {read (x); read (y...);}
	Tp I void write (Ty x) {x < 0 && (pc ('-'), x = -x, 0); W (S[++ Top] = x % 10 + '0', x /= 10); W (Top) pc (S[Top --]);}
	Tp I void writeln (Ty x) {write (x); pc ('\n');}
} using namespace FastIO;
int T; int n; 
int main () {
	RI i, j; read (T); W (T --) {
		int sum1 = 0, sum2 = 0;
		string s;
		getline (cin, s);
		for (RI i = 1; i <= s.length (); ++ i) {
			if (s[i - 1] == 'A' || s[i - 1] == 'C') ++ sum1;
			else ++ sum2;
		}
		if (sum1 == sum2) printf ("YES\n");
		else printf ("NO\n");
	}
	return 0;
}

B. Shifting Sort

思路

我们可以使用选择排序的思想。依次从小到大找数,然后进行一次偏移操作,将数放在对应的地方。

124357,其中前两个已经排好序。那么我们先查找第三大的数,此例为 3,发现 3 在第 4 个,而它应该在第 3 个。所以我们需要对 \([3,4]\) 执行一次 \(d = 1\) 的操作。

代码

#include <bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define I inline
#define ll long long
#define Con const
#define CI Con int
#define RI register int
#define W while
#define gc getchar
#define D isdigit(c=gc())
#define pc(c) putchar((c))
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define Gmax(x,y) (x<(y)&&(x=(y)))
#define Gmin(x,y) (x>(y)&&(x=(y)))
#define ms(a,x) memset((a),(x),sizeof(a))
using namespace std;
namespace FastIO
{
	CI FS = 1e5; int Top = 0; char S[FS];
	I void readc (char& c) {W (isspace (c = gc ()));}
	Tp I void read (Ty& x) {char c; int f = 1; x = 0; W (! D) f = c ^ '-' ? 1 : -1; W (x = (x * 10) + (c ^ 48), D); x *= f;}
	Ts I void read (Ty& x, Ar&... y) {read (x); read (y...);}
	Tp I void write (Ty x) {x < 0 && (pc ('-'), x = -x, 0); W (S[++ Top] = x % 10 + '0', x /= 10); W (Top) pc (S[Top --]);}
	Tp I void writeln (Ty x) {write (x); pc ('\n');}
} using namespace FastIO;
CI N = 55; int a[N + 5], n, T, tot, ans[N][3]; 
void change (int l, int r, int d) {
	RI i, j; ++ tot; ans[tot][0] = l; ans[tot][1] = r; ans[tot][2] = d;
	int num[55]; for (i = l; i <= r; ++ i) num[i] = a[i];
	for (i = l; i <= r - d; ++ i) a[i] = num[i + d];
	for (i = r - d + 1, j = l; i <= r; ++ i, ++ j) a[i] = num[j];
}
int main () {
	RI i, j; read (T); W (T --) {
		read (n); for (i = 1; i <= n; ++ i) read (a[i]);
		tot = 0;
//		for (i = 1; i <= n; ++ i) sum[i] = a[i]; sort (sum + 1, sum + n + 1); int cnt = unique (sum + 1, sum + n + 1) - sum - 1;
//		for (i = 1; i <= n; ++ i) a[i] = lower_bound (sum + 1, sum + cnt + 1, a[i]) - sum;
		for (i = 1; i <= n; ++ i) {
			int id, minn = 0x7fffffff; for (j = i; j <= n; ++ j) if (a[j] < minn) minn = a[j], id = j;
			if (id - i != 0) change (i, id, id - i);
		}
		printf ("%d\n", tot); for (i = 1; i <= tot; ++ i) printf ("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]);
//		for (i = 1; i <= n; ++ i) printf ("%d ", a[i]); printf ("\n");
	}
	return 0;
}
这篇关于Codeforces Round #744 (Div. 3) 题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!