CF EDU 100 C - Busy Robot
C - Busy Robot
模拟
nx : 当前发送当前这次命令时机器人的位置
ed:当前正在执行的操作的结束时间
d:当前的方向
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const ll INF = 1e18;
int n;
ll t[N], x[N];
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> t[i] >> x[i];
t[n+1] = INF;
ll nx = 0, ed = 0, d = 0;
int cnt = 0;
for (int i = 1; i <= n; i++)
{
nx += (min(t[i], ed) - t[i-1]) * d;
if (t[i] >= ed)
{
ed = t[i] + abs(nx - x[i]);
d = (x[i] >= nx ? 1 : -1);
}
int l = nx, r = nx + (min(t[i+1], ed) - t[i]) * d;
if (l > r)
swap(l, r);
if (x[i] >= l && x[i] <= r)
cnt++;
}
cout << cnt << endl;
}
return 0;
}