Problem - C - CodeforcesC. Busy Robot
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You have a robot that can move along a number line. At time moment 00 it stands at point 00.
You give nn commands to the robot: at time titi seconds you command the robot to go to point xixi. Whenever the robot receives a command, it starts moving towards the point xixi with the speed of 11 unit per second, and he stops when he reaches that point. However, while the robot is moving, it ignores all the other commands that you give him.
For example, suppose you give three commands to the robot: at time 11 move to point 55, at time 33 move to point 00 and at time 66 move to point 44. Then the robot stands at 00 until time 11, then starts moving towards 55, ignores the second command, reaches 55 at time 66 and immediately starts moving to 44 to execute the third command. At time 77 it reaches 44 and stops there.
You call the command ii successful, if there is a time moment in the range [ti,ti+1][ti,ti+1] (i. e. after you give this command and before you give another one, both bounds inclusive; we consider tn+1=+∞tn+1=+∞) when the robot is at point xixi. Count the number of successful commands. Note that it is possible that an ignored command is successful.
Input
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The next lines describe the test cases.
The first line of a test case contains a single integer nn (1≤n≤1051≤n≤105) — the number of commands.
The next nn lines describe the commands. The ii-th of these lines contains two integers titi and xixi (1≤ti≤1091≤ti≤109, −109≤xi≤109−109≤xi≤109) — the time and the point of the ii-th command.
The commands are ordered by time, that is, ti<ti+1ti<ti+1 for all possible ii.
The sum of nn over test cases does not exceed 105105.
Output
For each testcase output a single integer — the number of successful commands.
Example
input
Copy
8 3 1 5 3 0 6 4 3 1 5 2 4 10 -5 5 2 -5 3 1 4 1 5 1 6 1 4 3 3 5 -3 9 2 12 0 8 1 1 2 -6 7 2 8 3 12 -9 14 2 18 -1 23 9 5 1 -4 4 -7 6 -1 7 -3 8 -7 2 1 2 2 -2 6 3 10 5 5 8 0 12 -4 14 -7 19 -5
output
Copy
1 2 0 2 1 1 0 2
Note
The movements of the robot in the first test case are described in the problem statement. Only the last command is successful.
In the second test case the second command is successful: the robot passes through target point 44 at time 55. Also, the last command is eventually successful.
In the third test case no command is successful, and the robot stops at −5−5 at time moment 77.
Here are the 00-indexed sequences of the positions of the robot in each second for each testcase of the example. After the cut all the positions are equal to the last one:
- [0,0,1,2,3,4,5,4,4,…][0,0,1,2,3,4,5,4,4,…]
- [0,0,1,2,3,4,5,5,5,5,5,4,3,2,1,0,−1,−2,−3,−4,−5,−5,…][0,0,1,2,3,4,5,5,5,5,5,4,3,2,1,0,−1,−2,−3,−4,−5,−5,…]
- [0,0,0,−1,−2,−3,−4,−5,−5,…][0,0,0,−1,−2,−3,−4,−5,−5,…]
- [0,0,0,0,1,2,3,3,3,3,2,2,2,1,0,0,…][0,0,0,0,1,2,3,3,3,3,2,2,2,1,0,0,…]
- [0,0,1,0,−1,−2,−3,−4,−5,−6,−6,−6,−6,−7,−8,−9,−9,−9,−9,−8,−7,−6,−5,−4,−3,−2,−1,−1,…][0,0,1,0,−1,−2,−3,−4,−5,−6,−6,−6,−6,−7,−8,−9,−9,−9,−9,−8,−7,−6,−5,−4,−3,−2,−1,−1,…]
- [0,0,−1,−2,−3,−4,−4,−3,−2,−1,−1,…][0,0,−1,−2,−3,−4,−4,−3,−2,−1,−1,…]
- [0,0,1,2,2,…][0,0,1,2,2,…]
- [0,0,0,0,1,2,3,4,5,6,7,8,9,10,10,9,8,7,6,5,4,3,2,1,0,−1,−2,−3,−4,−5,−6,−7,−7,…]
- =====================================================================
模拟题,只需要记录当前出发点,目标点,出发时间,到达时间即可,细节较多,不过题目很人性,基本上样例过了就能过掉全部情况,不要忘了龙龙
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll t[100000+10],a[100000+10];
int main()
{/*
//记录一个当前目标点的位置,以及起始点的位置,这样就获得了方向
顺便记录一个出发点的时间,与到达目的地的时间
如果遍历的点,在这一时间段内,那么就被忽略
如果恰好在这一区间内,就计算其经过时间,否则设置为无效
如果遍历到的点,在到达目标点之后,那么就以这一个点为出发点
更新接下来的答案*/
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>t[i]>>a[i];
}
ll now=0,goal=a[1],l=t[1],r=t[1]+abs(a[1]);
t[n+1]=1e18;
int ans=0;
if(t[1]<=r&&r<=t[2])
ans++;
for(int i=2;i<=n;i++)
{
if(r<=t[i])
{
//要改变
now=goal;
goal=a[i];
l=t[i];
r=t[i]+abs(a[i]-now);
// cout<<now<<" "<<goal<<" "<<t[i]<<" "<<r<<endl;
if(t[i]<=r&&r<=t[i+1])
ans++;
}
else
{
if(now<=a[i]&&a[i]<=goal)
{
ll temptime=l+abs(a[i]-now);
if(temptime>=t[i]&&temptime<=t[i+1])
ans++;
}
else if(goal<=a[i]&&a[i]<=now)
{
ll temptime=l+abs(a[i]-now);
if(temptime>=t[i]&&temptime<=t[i+1])
ans++;
}
}
}
cout<<ans<<endl;
}
return 0;
}