B - Guess a number!
Problem description
A TV show called "Guess a number!" is gathering popularity. The whole Berland, the old and the young, are watching the show.
The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions:
- Is it true that y is strictly larger than number x?
- Is it true that y is strictly smaller than number x?
- Is it true that y is larger than or equal to number x?
- Is it true that y is smaller than or equal to number x?
On each question the host answers truthfully, "yes" or "no".
Given the sequence of questions and answers, find any integer value of y that meets the criteria of all answers. If there isn't such value, print "Impossible".
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10000) — the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: "sign x answer", where the signis:
- ">" (for the first type queries),
- "<" (for the second type queries),
- ">=" (for the third type queries),
- "<=" (for the fourth type queries).
All values of x are integer and meet the inequation - 109 ≤ x ≤ 109. The answer is an English letter "Y" (for "yes") or "N" (for "no").
Consequtive elements in lines are separated by a single space.
Output
Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2·109 ≤ y ≤ 2·109. If there are many answers, print any of them. If such value doesn't exist, print word "Impossible" (without the quotes).
Examples
Input
4
>= 1 Y
< 3 N
<= -3 N
> 55 N
Output
17
Input
2
> 100 Y
< -100 Y
Output
Impossible
解题思路:题意比较简单,就是找出符合要求的区间,然后输出其中任意一个值。如果不满足构成区间的条件,则输出"Impossible"。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int INF = 2e9;
int main(){
int n,x,left=-INF,right=INF;char ch;string eq;//初始值区间满足-2*10^9<=[left,right]<=2*10^9
cin>>n;
while(n--){
getchar();
cin>>eq>>x>>ch;
if(ch=='N'){//修改其值
if(eq==">=")eq="<";
else if(eq=="<=")eq=">";
else if(eq==">")eq="<=";
else eq=">=";
}
if(eq=="<")right=min(right,x-);//取右端点的最小值
else if(eq=="<=")right=min(right,x);
else if(eq==">")left=max(left,x+);//取左端点的最大值
else left=max(left,x);
}
if(left>right)cout<<"Impossible"<<endl;//不满足条件
else cout<<left<<endl;//直接输出左端点值(任意值)
return ;
}
B - Guess a number!的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [LeetCode] Number of Segments in a String 字符串中的分段数量
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
随机推荐
- OpenCV:Adaboost训练时数据扩增
更准确的模型需要更多的数据,对于传统非神经网络机器学习方法,不同的特征需要有各自相符合的数据扩增方法. 1. 在使用opencv_traincascade.exe 过程中,图像读取在 classi ...
- EF CodeFirst 基础命令
PM> enable-migrations 已在项目"EasyWeChat.Data"中启用迁移.若要覆盖现有迁移配置,请使用 -Force 参数. PM> add-m ...
- 在滑动3D模型(Cube)时,手指点击进入相应的某一部分
public void OnClickRay() { Ray ray = new Ray(); RaycastHit shootHit; ray.origin = cameras.transform. ...
- 【转载】Jsp页面传Json数据到服务端,转对象或集合进行数据处理
需求:1.将页面数据带到服务端并转成对象,2.将页面的集合数据带到服务端转List实现:用ajax请求传递数据,数据格式为json JS方法: testJsonMethod = function(){ ...
- 记录:Ubuntu下升级Python从2.x到3.x
一.安装Python3 在Ubuntu中的终端输入:sudo apt-get install python3 提示资源被锁住,可能有另外一个程序在占用此资源. 解决方法:输入以下指令解锁资源 sudo ...
- 定位IO瓶颈的方法,iowait低,IO就没有到瓶颈?
通过分析mpstat的iowait和iostat的util%,判断IO瓶颈 IO瓶颈往往是我们可能会忽略的地方(我们常会看top.free.netstat等等,但经常会忽略IO的负载情况),今天给大家 ...
- SDOI2017数字表格
求$\prod_{i=1}^n\prod_{j=1}^n\text{Fib}[\gcd(i,j)]\;\text{mod}\;10^9+7$的值 令$n\leq m$,则有: \begin{align ...
- Python学习笔记之异常处理
1.概念 Python 使用异常对象来表示异常状态,并在遇到错误时引发异常.异常对象未被捕获时,程序将终止并显示一条错误信息 >>> 1/0 # Traceback (most re ...
- docker 容器操作( 以 tomcat 为例 )
一.容器操作 一个镜像可以启动多个容器.比如一个 tomcat 镜像,可以启动多个tomcat 容器,启动后的这些 tomcat 都是各自独立的 二.步骤 1.搜索镜像 [root@localhost ...
- 编写App测试用例的关注点
如何做到测试用例的百分百覆盖一直是测试用例编写过程中的难点,首先在测试时我们经常会遇见一些常见的bug,那么我们可以在编写测试用例时考虑到这些点. 一:关于业务逻辑 ...