跳到正文

题解

题解:P13202 [GCJ 2016 #3] Teaching Assistant

#SUSPENDED#约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月9日
篇幅
约 2 分钟
题目
P13202

版本

  1. v1当前

洛谷 P13202 [GCJ 2016 #3] Teaching Assistant 题解

解题思路

我们令你申请题集的类型为 t1t_1,此时助教的心情为 x1x_1,你提交题集的类型为 t2t_2,此时助教的心情为 x2x_2,助教的打分为 anan。

那么有如下情况:

an={10t1=x1 and t2=x25{t1=x1 and t2≠x2t1≠x1 and t2=x20t1≠x1 and t2≠x2an = \begin{cases} 10 & t_1 = x_1\ \text{and}\ t_2 = x_2 \\ 5 & \begin{cases} t_1 = x_1\ \text{and}\ t_2 \ne x_2 \\ t_1 \ne x_1\ \text{and}\ t_2 = x_2 \\ \end{cases} \\ 0 & t_1 \ne x_1\ \text{and}\ t_2 \ne x_2 \\ \end{cases}

很容易发现这是贪心算法,那么我们可以这样:

  1. 当遇到与栈顶字符相同的字符时,弹出栈顶并计数一次相同配对。
  2. 当遇到与栈顶字符不同的字符时,将当前字符压入栈中。

答案

令课程持续 2n2n 天,相同配对有 cntcnt 个,则有如下方程。

ans=10×n2+5×cntans = 10\times\frac{n}{2} + 5\times cnt
ans=5×(n+cnt)ans = 5\times(n + cnt)

时间复杂度

O(∣S∣)O(\lvert S\rvert),其中 ∣S∣\lvert S\rvert 指字符串 SS 的长度(过不了我道理蜡西)。

AC Code

C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int T;
	cin >> T;
	while (T--) {
		string s;
		cin >> s;
		int n = s.size() >> 1;
		vector<char> st;
		int cnt = 0;
		for (char c : s) {
			if (!st.empty() && st.back() == c) {
				st.pop_back();
				cnt++;
			} else st.push_back(c);
		}
		int ans = 5 * (n + cnt);
		cout << "Case #" << t << ": " << ans << '\n';
	}
	return 0;
}

完结撒花!

原文