以下是针对CCC Junior组一至五题的题解,包括思路讲解和对应的C++代码实现:
题目 J1: Roller Coaster Ride
思路讲解:
本题要求判断给定位置的人是否能在下一班过山车上。我们需要计算火车的总承载人数,即火车的车厢数乘以每节车厢的承载人数。如果当前位置的人数小于或等于总承载人数,则可以上车,输出 "yes";否则输出 "no"。
C++代码:
#include <iostream>
using namespace std;int main() {int N, C, P;cin >> N >> C >> P;if (N <= C * P) {cout << "yes" << endl;} else {cout << "no" << endl;}return 0;
}
题目 J2: Donut Shop
思路讲解:
本题要求计算一天结束时剩余的甜甜圈数量。我们需要初始化一个变量来存储剩余的甜甜圈数量,然后根据每个事件更新这个数量。如果事件是添加('+'),则增加相应的数量;如果事件是出售('-'),则减少相应的数量,但要确保剩余数量不小于0。
C++代码:
#include <iostream>
using namespace std;int main() {int D, E, Q;cin >> D >> E;for (int i = 0; i < E; ++i) {char event;cin >> event >> Q;if (event == '+') {D += Q;} else {D -= Q;if (D < 0) D = 0;}}cout << D << endl;return 0;
}
题目 J3: Product Codes
思路讲解:
本题要求将原始产品代码转换为新格式。新格式要求移除所有小写字母,保留大写字母的顺序,并将所有整数相加。我们可以通过遍历原始代码,识别大写字母和整数,然后按照要求构建新代码。
C++代码:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;int main() {int N;cin >> N;while (N--) {string code;cin >> code;long long sum = 0;string result;for (char c : code) {if (isupper(c)) {result += c;} else if (isdigit(c)) {string num;while (isdigit(c)) {num += c;c = next(code.begin(), distance(code.begin(), &c) + 1)->operator *();}sum += stoll(num);}}result += to_string(sum);cout << result << endl;}return 0;
}
题目 J4: Sunny Days
思路讲解:
本题要求找到最长的连续晴天序列,假设只有一天的数据是错误的。我们可以通过计算每个可能的错误日对最长晴天序列的影响来解决这个问题。首先,找到所有连续的晴天序列;然后,尝试将每个非晴天(P)转换为晴天(S),计算可能的最长晴天序列。
C++代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {int N;cin >> N;vector<char> days(N);for (int i = 0; i < N; ++i) {cin >> days[i];}int maxConsecutive = 0;int current = 0;for (int i = 0; i < N; ++i) {if (days[i] == 'S') {current++;} else {maxConsecutive = max(maxConsecutive, current);current = 0;}}maxConsecutive = max(maxConsecutive, current);int overallMax = 0;for (int i = 0; i <= N; ++i) {int temp = 0, tempMax = 0;for (int j = i; j < N; ++j) {if (days[j] == 'S') {temp++;} else {tempMax = max(tempMax, temp);temp = 0;}}tempMax = max(tempMax, temp);if (i > 0 && days[i - 1] == 'P') tempMax++;overallMax = max(overallMax, tempMax);}cout << overallMax << endl;return 0;
}
题目 J5: Connecting Territories
思路讲解:
本题要求在网格上找到从上到下的最小路径成本。网格上的每个单元格都有一个成本,成本按照一定模式重复。我们可以使用动态规划来解决这个问题,从第一行开始,逐步计算到达每一行每个单元格的最小成本。
C++代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {int R, C, M;cin >> R >> C >> M;vector<vector<int>> cost(R, vector<int>(C));for (int i = 0; i < R; ++i) {for (int j = 0; j < C; ++j) {cost[i][j] = (i * C + j + 1) % M;if (cost[i][j] == 0) cost[i][j] = M;}}vector<vector<int>> dp(R, vector<int>(C, 0));for (int j = 0; j < C; ++j) {dp[0][j] = cost[0][j];}for (int i = 1; i < R; ++i) {for (int j = 0; j < C; ++j) {int minCost = INT_MAX;if (j > 0) minCost = min(minCost, dp[i - 1][j - 1]);if (j < C - 1) minCost = min(minCost, dp[i - 1][j + 1]);minCost = min(minCost, dp[i - 1][j]);dp[i][j] = minCost + cost[i][j];}}int result = INT_MAX;for (int j = 0; j < C; ++j) {result = min(result, dp[R - 1][j]);}cout << result << endl;return 0;
}
这些题解和代码实现了每个题目的基本逻辑,可能需要根据具体的输入输出格式进行调整。