CF EDU 100 C - Busy Robot
阿新 • • 發佈:2022-05-29
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; }