专栏文章

题解:P12834 [蓝桥杯 2025 国 B] 项链排列

P12834题解参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@mioet2t6
此快照首次捕获于
2025/12/02 18:02
3 个月前
此快照最后确认于
2025/12/02 18:02
3 个月前
查看原文

一道赤裸裸的贪心+构造题

1. 题意:

题意很清楚了eee……我太懒了,没写

2. 解法:

考虑一个答案串中贡献“变化次数”的子串,要么是 {L开头的,LQLQLQ Q开头的,QLQLQL \begin{cases} 以 L 开头的,LQLQLQ \dots\ \\ 以 Q 开头的,QLQLQL \dots\ \end{cases}
我们定义两类贡献子段贡献的变化次数为 CC 时,需要 LLQQ 的数量为: LneediQneedi(i=1,2表示第几类贡献子段)Lneed_i,Qneed_i(i=1,2表示第几类贡献子段)
容易得容易得 Lneed1=C/2+1Qneed1=(C+1)/2Lneed_1=C/2+1,Qneed_1=(C+1)/2 Lneed2=(C+1)/2Qneed2=C/2+1Lneed_2=(C+1)/2,Qneed_2=C/2+1
显然如果能用第一种就用,不够再用第二种。 接下来根据题意构造即可。

3. 代码:

CPP
#include <bits/stdc++.h>
using namespace std;

int a, b, c;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> a >> b >> c;
	if (c == 0) { // 判断是否无解
		if (a > 0 && b > 0) {
			cout << -1 << endl;
			return 0;
		} else { // 否则有解
			for (int i = 1; i <= a; i ++) cout << "L";
			for (int i = 1; i <= b; i ++) cout << "Q";
			cout << endl;
			return 0;
		}
	}
	int Lneed = c / 2 + 1, Rneed = (c + 1) / 2;
	if (a >= Lneed && b >= Rneed) {
		for (int i = 1; i <= a - Lneed; i ++) cout << "L";
		for (int i = 1; i <= Lneed + Rneed - 1; i ++) {
			if (i % 2) cout << "L";
			else cout << "Q";
		}
		if ((Lneed + Rneed) % 2) {
			for (int i = 1; i <= b - Rneed; i ++) cout << "Q";
			cout << "L" << endl;
		} else {
			cout << "Q";
			for (int i = 1; i <= b - Rneed; i ++) cout << "Q";
		}
	} else {
		swap(Lneed, Rneed);
		if (a < Lneed || b < Rneed) {
			cout << -1 << endl;
			return 0;
		}
		for (int i = 1; i <= Lneed + Rneed; i ++) {
			if (i % 2) cout << "Q";
			else cout << "L";
		}
		for (int i = 1; i <= b - Rneed; i ++) cout << "Q";
	}
	return 0;
}

评论

0 条评论,欢迎与作者交流。

正在加载评论...