跳到正文

题解

题解:P13361 [GCJ 2011 Qualification] Bot Trust

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

文章信息

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

版本

  1. v1当前

题目思路

遍历每一个指令且计算每个指令机器人需要的移动时间,累加答案并更新这个机器人的位置

注意

1.1. 当 OO 机器人走的时候, BB 机器人走不了 但是当 BB 机器人走的时候,OO 机器人能走

2.2. 按下按钮还要 11 秒时间

3.3. 需要与 00 比较 确保时间不为负数

代码

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+3;
struct node {
	char ch;
	int index;
};
signed main() {
	int T;
	cin >> T;
	for(int x = 1; x <= T; x ++) {
		int n;
		cin >> n;
		node a[N];
		int ans = 0;
		int index_O = 1, index_B = 1, ans_O = 0, ans_B = 0;
		for(int i = 0; i < n; i ++) {
			cin >> a[i].ch >> a[i].index;
			if(a[i].ch == 'O') {
				int to = max(0ll, abs(a[i].index-index_O)-ans_O)+1; // 计算位置
				ans += to;
				ans_O = 0;
				ans_B += to;
				index_O = a[i].index; // 更新位置
			} else {
				int to = max(0ll, abs(a[i].index-index_B)-ans_B) + 1; // 与0比确保答案最后不为负数
				ans += to;
				ans_B = 0;
				ans_O += to;
				index_B = a[i].index; // 与O机器人同理
			}
		}
		cout << "Case #" << x << ": " << ans << '\n';
	}
	return 0;
}

AC记录

原文