当前位置: 首页 > 工具软件 > Forcefield > 使用案例 >

Forcefield

柯清野
2023-12-01

 Forcefield


While Luke Skywalker was trying to acquire confindental data from the imperial flagship, he got into a trap. Now he is standing on the very edge of a long pipe inside the spaceship's reactor. To deal with the situation, he can move only forward. There is a stormtrooper standing at some point in front of Luke, and he have just made a shot from a blaster towards Luke. At the same moment, R2-D2 has accidentally activated the spaceship's forcefield. The forcefield generators are located on the pipe. The generators are powerful enough to change the bolt movement direction.

Each generator reflects a bolt of plasma when it comes from the front side and lets it fly through when it comes from the back. However, if the bolt comes from the front, it destroys the generator. Therefore, there are two types of generators: generators of one type face towards Luke, and generators of the other type face in the opposite direction.

After a generator is destroyed, it no longer affects the bolt. The stormtrooper's batteries are low on charge, so he cannot shoot anymore and now is lying on the bottom of the pipe so that the bolt will not hit him.

When the bolt reaches Luke, he must reflect it with the use of his lightsaber. But because of the forcefield, after being reflected, the bolt just changes direction to the opposite and continues to fly along the pipe. Luke cannot move until all the generators are destroyed. Additionally, if after destroying all the generators, the bolt flies towards Luke again, he must reflect it one more time. So, now he wonders if it's possible to destroy all the generators, and if so, how many times he will have to reflect the plasma bolt.

Input

The first line of the input contains two integers N and X, the number of generators and the distance between Luke and the stormtrooper (1 ≤ N ≤ 100 0001 ≤ X ≤ 109).

The next N lines contain two integers each. The i-th line contains xi, the distance between Luke and the i-th generator, and the type of the generator (1 ≤ xi ≤ 109; the type is 1 if the generator is faced towards Luke, or 0 otherwise).

The generators are given in the order of increasing distance from Luke (that is, xi < xj if i < j). The position of the stormtrooper doesn't coincide with any of the generators.

Output

Print  - 1 if it's not possible to destroy all the generators.

Otherwise, print a single integer: the number of times Luke will have to reflect the bolt of plasma.

Example
Input
2 3
1 1
2 1
Output
3
#include<stdio.h>
/*
一个人向卢克开一枪(只开一枪),当子弹到达卢克时卢克必须反弹子弹。
当子弹从右向左经过状态为1的发射器时直接通过,经过状态为0的发射器时,发射器炸并且子弹反弹 
当从左往右经过状态为0的发射器时直接通过,经过状态为1的发射器时,发射器炸并且子弹反弹 
开枪的人的右边也可能会有发射器
*/
int main(){
	long long a,x,temp,sum;
	int t,i,n,flag1,flag2,flag3;
	while(scanf("%d%lld",&n,&x)!=EOF){
		temp=0;
		flag1=0;
		flag2=0;
		flag3=0;
		sum=0;
		for(i=0;i<n;i++){
			scanf("%lld%d",&a,&t);
			if(a<x&&t==0){//当开枪得人左边存在为0的发电机 
				flag1=1;
			}
			/*
				sum计算状态为1的个数 
				从左往右输入 当发电机的状态是0时temp--;sum不加; 
				当temp<0 && 状态为1时temp++;
				当temp==0并且 状态为1 sum++; 
				0可以和右边的 1抵消 
			*/
			if(t!=1)
				temp--;
			else if(t==1 && temp<0){
				if(temp==-1)//当出现1时temp为-1 
					flag2=1;
				temp++;
			}
			else if(t==1 && temp==0)
				sum++;
		}
		if(flag1==1&&flag2==0&&temp==-1){
			//起点 0 发射点(输出0) 
			//还有就是当发射点左边有至少一个0并且每次出现1时temp都小于等于-2 (输出0) 
			printf("0\n");
		}
		else{//普通情况:
			if(temp!=0)
			printf("-1\n");
		else
			printf("%lld\n",sum+1);
		}
		
	}
	return 0;
}



 类似资料:

相关阅读

相关文章

相关问答