跳到正文

题解

题解:AT_arc177_b [ARC177B] Puzzle of Lamps

Ambrosia约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年7月3日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 3 分钟

版本

  1. v1当前

题目大意

有一个一开始全为 00 的序列,有如下两种操作:

  • 操作 AA:将最左边的 00 变成 11。
  • 操作 BB:将最左边的 11 变成 00。

给你一个序列,构造任意一种方案进行若干次操作后得到这个序列。

题目思路

通过瞪眼大法,我们可以发现,这个家伙与前缀和有那么一丝丝的相似。

从后往前扫,每一次找到第一个不连续的 11 和 00,按下标分别输出 AA 和 BB 即可。

AC Code

C++
#include <cstring>
#include <iostream>
#include <algorithm>

#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f
#define lnf 0x3f3f3f3f3f3f3f3f

//#prgama GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Os")
//#pragma GCC optimize("Ofast")

using namespace std;

inline bool input (int x) {
	x = 0;
	int f = 1;
	bool flag = 0;
	char c = getchar ();
	
	while (!isdigit(c)) {
		if (c == '-') {
			c = getchar ();
			
			if (isdigit (c)) {
				f = -1;
				break;
			}
		} else {
			c = getchar ();
		}
	}
	
	while (isdigit (c)) {
		flag = 1;
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar ();
	}
	
	x *= f;
	return flag;
}

inline bool output (int x) {
	if (x < 0) {
		putchar ('-');
		x = -x;
	}
	
	char write[25], cnt = 0;
	
	do {
		write[++ cnt] = x % 10;
		x /= 10;
	} while (x);
	
	do {
		putchar (write[cnt --] | 48);
	} while (cnt);
	
	return x;
}

const int N = 30;

void init ();

int n;
char a[N + 5];
bool flag;
string ans;

signed main () {
	init ();
	input (n);
	cin >> a + 1;
	
	for (int i = n; i; -- i) {
		if (a[i] == '1' && !flag) {
			flag = 1;
			
			for (int j = 1; j <= i; ++ j) {
				ans += "A";
			}
		}
		
		if (a[i] == '0' &&  flag) {
			flag = 0;
			
			for (int j = 1; j <= i; ++ j) {
				ans += "B";
			}
		}
	}
	
	output (ans.size ());
	puts ("");
	cout << ans;
	return 0;
}

void init () {
//	freopen (".in", "r", stdin);
//	freopen (".out", "w", stdout);
//	ios::sync_with_stdio (false);
//	cin.tie (0);
//	cout.tie (0);
}
原文