跳到正文

题解

P13132 Saving The Universe Again

SunShine2012约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟
获赞
1
题目
P13132

版本

  1. v1当前

无脑打框架

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

#define int long long
#define io ios::sync_with_stdio(0), cin.tie(), cout.tie()
//
int T, D, c, s, y; //
string P;
void out(int x){
	//
	cout << "Case #" << x << ": " << y << '\n';
}
signed main(){
	io;
	cin >> T;
	for (int x = 1; x <= T; x++){
		//
		c = s = y = 0;
		cin >> D >> P;
		//
		out(x);
	}return 0;
}

分析

二进制。每个 S 前面有多少 C,damagedamage(损害)就增加 22 的多少次方。

发现每次都将最后一个 CS 交换,这是因为贪心,想要最大程度地减少 damagedamage,这时 damagedamage 减少这个 S 对应的 damagedamage 的一半。

于是开一个 pp 数组存每个 S 对应的 damagedamage。

C++
const int _P_l = 30;
int p[_P_l];

想让 damage≤Ddamage\le D,循环:

C++
while (s > D){
	//
}if (s > D){cout << "Case #" << x << ": IMPOSSIBLE\n"; return;}

穿插介绍一个函数 rfind,用于找最后一个……的首字符位置。

如 P.rfind("CS") 返回最后一个 CS\texttt{CS} 的 C\texttt{C} 的位置。

于是,out 函数就写好了:

C++
void out(int x){
	while (s > D){
		pos = P.rfind("CS");
		if (pos == string::npos){cout << "Case #" << x << ": IMPOSSIBLE\n"; return;}
		P[pos] = 'S', P[pos + 1] = 'C', s -= 1 << (p[pos] = p[pos + 1] - 1), p[pos + 1] = 0, y++;
	}if (s > D){cout << "Case #" << x << ": IMPOSSIBLE\n"; return;}
	cout << "Case #" << x << ": " << y << '\n';
}

别忘记清空 pp:

C++
memset(p, 0, sizeof p);

完整代码

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

#define int long long
#define io ios::sync_with_stdio(0), cin.tie(), cout.tie()
const int _P_l = 30;
int T, D, c, s, y, pos, p[_P_l];
string P;
void out(int x){
	while (s > D){
		pos = P.rfind("CS");
		if (pos == string::npos){cout << "Case #" << x << ": IMPOSSIBLE\n"; return;}
		P[pos] = 'S', P[pos + 1] = 'C', s -= 1 << (p[pos] = p[pos + 1] - 1), p[pos + 1] = 0, y++;
	}if (s > D){cout << "Case #" << x << ": IMPOSSIBLE\n"; return;}
	cout << "Case #" << x << ": " << y << '\n';
}
signed main(){
	io;
	cin >> T;
	for (int x = 1; x <= T; x++){
		memset(p, 0, sizeof p);
		c = s = y = 0;
		cin >> D >> P;
		for (int i = 0; i < P.size(); i++) if (P[i] == 'C') c++; else s += 1 << c, p[i] = c;
		out(x);
	}return 0;
}
原文