1463C - Busy Robot

1463C - Busy Robot

Question: There is a robot moving on a one-dimensional number axis. When its speed is 1, 0s, he is at the origin and gives n instructions, pi, ti p_i, t_ipi,ti, Said in pi p_ipiLet the robot start to ti t_iti, If the robot gives an instruction while on the move, it will ignore. Definition: The successful instruction is at ti, ti + 1 t_i, t_{i + 1}ti,ti+1Reached pi p_ipi, The ignore command may also be successful (

Just hard simulation (... a little detail so mark

  1. longlong
  2. I didn’t read the question clearly at the beginning*

code

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e5 + 10;
struct node {
    
    
	ll t, p;	
} mp[N]; 

int main() {
    
    
//	freopen("in.txt", "r", stdin);
	ll T;
	cin >> T;
	while(T--) {
    
    
		ll n;
		cin >> n;
		ll ans = 0;
		for(ll i = 1; i <= n; ++ i) {
    
    
			cin >> mp[i].t >> mp[i].p;
		}
		mp[n + 1].t = 2e18;
		ll tim = 1, lastt = 1, pos = 0, lastp = 0;
		for(ll i = 1; i <= n; ++ i) {
    
    
			
			if(mp[i].t < tim) {
    
    
				ll dir;
				if(pos < lastp) dir = -1;
				else if(pos == lastp) dir = 0;
				else dir = 1;
				
				ll nowp = lastp + dir * (mp[i].t - lastt);
				ll nowp2 = lastp + dir * ((min(tim, mp[i + 1].t)) - lastt);
				if((nowp <= mp[i].p && mp[i].p <= nowp2) ||
					(nowp2 <= mp[i].p && mp[i].p <= nowp)) ++ ans;
			}
			else {
    
    
				lastt = max(tim, mp[i].t);
				tim = mp[i].t + abs(mp[i].p - pos) ; 
				lastp = pos;
				pos = mp[i].p;
				if(tim <= mp[i + 1].t) ++ ans;	
			}
		}
		cout << ans << endl;
	}
	
} 

Guess you like

Origin blog.csdn.net/qq_39602052/article/details/113914629