【水读入】#56 A. Where Are My Flakes?

A. Where Are My Flakes?
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One morning the Cereal Guy found out that all his cereal flakes were gone. He found a note instead of them. It turned out that his smart roommate hid the flakes in one of n boxes. The boxes stand in one row, they are numbered from 1 to n from the left to the right. The roommate left hints like "Hidden to the left of the i-th box" ("To the left of i"), "Hidden to the right of the i-th box" ("To the right of i"). Such hints mean that there are no flakes in the i-th box as well. The Cereal Guy wants to know the minimal number of boxes he necessarily needs to check to find the flakes considering all the hints. Or he wants to find out that the hints are contradictory and the roommate lied to him, that is, no box has the flakes.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000) which represent the number of boxes and the number of hints correspondingly. Next m lines contain hints like "To the left of i" and "To the right of i", where i is integer (1 ≤ i ≤ n). The hints may coincide.

Output

The answer should contain exactly one integer — the number of boxes that should necessarily be checked or "-1" if the hints are contradictory.

Sample test(s)
input
2 1
To the left of 2
output
1
input
3 2
To the right of 1
To the right of 2
output
1
input
3 1
To the left of 3
output
2
input
3 2
To the left of 2
To the right of 1
output
-1


这道题的意思是说给n个箱子(按照1-n的编号),有m句话,告诉你对的箱子在X号箱子的左边或者右边,问你最终符合条件的箱子有多少个,如果一个都没有输出-1

字符串读入咯~ 记住用getline哟,cin或者%s都是空格截断的~

#include <cstdio>
#include <memory>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	string s;
	int n,m,l,r,ans;	cin>>n>>m;
	l=0,r=n+1,ans=0;
	char huiche,dir;	scanf("%c",&huiche);
	for(int i=0;i<m;i++)
	{
		getline(cin,s);
		int len=s.length()-1,num=0,tlen;
		dir=s[7],tlen=len;
		//cout<<dir<<endl;
		while(s[tlen]!=' ')tlen--;
		for(int j=1;j+tlen<=s.length()-1;tlen++)
			num= num*10 + s[tlen+j]-'0';
		if(dir=='l')
		{
			r=min(r,num);
			if(num<l || num==1) ans=-1;
		}
		else if(dir=='r')
		{
			l=max(l,num);
			if(num>r || num==n)	ans=-1;
		}
		if(r<l || ans==-1)break;
	}
	
	if(ans==-1 || r-l==1)	cout<<"-1";
	else
	{
		ans=r-l-1;
		cout<<ans;
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值