Educational Codeforces Round 100-C. Busy Robot

如夢如花發表於2020-12-19

Busy Robot

題目:

You have a robot that can move along a number line. At time moment 0 it stands at point 0.

You give n commands to the robot: at time ti seconds you command the robot to go to point xi. Whenever the robot receives a command, it starts moving towards the point xi with the speed of 1 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 1 move to point 5, at time 3 move to point 0 and at time 6 move to point 4. Then the robot stands at 0 until time 1, then starts moving towards 5, ignores the second command, reaches 5 at time 6 and immediately starts moving to 4 to execute the third command. At time 7 it reaches 4 and stops there.

You call the command i successful, if there is a time moment in the range [ti,ti+1] (i. e. after you give this command and before you give another one, both bounds inclusive; we consider tn+1=+∞) when the robot is at point xi. 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 t (1≤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 n (1≤n≤105) — the number of commands.

The next n lines describe the commands. The i-th of these lines contains two integers ti and xi (1≤ti≤109, −109≤xi≤109) — the time and the point of the i-th command.

The commands are ordered by time, that is, ti<ti+1 for all possible i.

The sum of n over test cases does not exceed 105.

output

For each testcase output a single integer — the number of successful commands.

Example

input
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 -7ti+
19 -5

output
1
2
0
2
1
1
0
2

思路:

模擬robot運動的路線,如果robot在ti-ti+1時間內到達ai,則有效加1

#include<bits/stdc++.h>
using namespace std;
long long int x[100005];
long long int a[100005];
long long int c[10005];
int main(){
int t;
cin>>t;
while(t--){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x[i]>>a[i];
	}
	long long int t=0,s=0,ss=0,tt=0;
	int ans=0;
	for(int j=1;j<=n;j++){
		
		if(j==1){
			t=x[j]+fabs(a[j]);
			ss=s;
			s=a[j];
			tt=x[j];
		}
		else if(x[j]>=t){
			t=x[j]+fabs(a[j]-s);
			ss=s;
			tt=x[j];
			s=a[j];
		}
 
		if(j!=n){	
			if(ss>=s){
			if(a[j]<=ss&&a[j]>=s)
			{	
				int s1=ss-(x[j]-tt);
				int s2=ss-(x[j+1]-tt);
				
				if(s1>=a[j]&&s2<=a[j])
				ans++;
		}
	}
	else{
		if(a[j]>ss&&a[j]<=s){
		int s1=ss+(x[j]-tt);
		int s2=ss+(x[j+1]-tt);
				if(s1<=a[j]&&s2>=a[j])
				ans++;
		}
	}
}
 
else{
	if(ss>=s){
			if(a[j]<=ss&&a[j]>=s)
			{
				int s1=ss-(x[j]-tt);
				
				if(a[j]<=s1&&a[j]>=s)
				ans++;
		}
	}
	else{
		if(a[j]>ss&&a[j]<=s){
			int s1=ss+(x[j]-tt);
			
				if(a[j]>=s1&&a[j]<=s)
				ans++;
		}
	}
}
} 
cout<<ans<<endl;
}
}

相關文章