题目思路
遍历每一个指令且计算每个指令机器人需要的移动时间,累加答案并更新这个机器人的位置
注意
当 机器人走的时候, 机器人走不了 但是当 机器人走的时候, 机器人能走
按下按钮还要 秒时间
需要与 比较 确保时间不为负数
代码
#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;
}