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 ...
随机推荐
- Caffe: Vs13添加CUDA支持
1. 右键工程 点击:Building Dependency 右击:Build Customizations 点击选项:CUDA 7.5 2.添加C++依赖: cudart.lib kernel32 ...
- SLAM: Inverse Depth Parametrization for Monocular SALM
首语: 此文实现客观的评测了使线性化的反转深度的效果.整篇只在表明反转可以线性化,解决距离增加带来的增长问题,有多少优势--%! 我的天呢!我竟然完整得翻译了一遍. 使用标记点地图构建SLAM的方法, ...
- C# 检测字符串是否为数字
long n; 1. ], ].All(char.IsDigit); //识别空字符时候 会认为是数字 string str0 = ""; string str1 = " ...
- react工具库
采用了react框架后,需要找到一些常用的库,常见的需求比如: 1)react生成二维码 2)react的轮播banner图 随着react的社区的壮大,以上的需求都有专门的库帮我们做这个: 1)re ...
- Visual Studio 2015 开发 Linux 和树莓派 程序的 C++环境
可以创建 树莓派 和 linux控制台应用. 创建后的 readme , 有各个设置的说明 你需要输入你虚拟主机, 编译环境linux虚拟机 的简单配置,另外, 4月5日的版本 如果 你的linux ...
- js与Jquery的对比
// document.getElementById("divCommit").style.display="none";// document.g ...
- tomcat 热加载设置
找到tomcat项目的apache-tomcat-8.0.30\conf\context.xml,打开进行编辑,把Context项中加上 reloadable="true" < ...
- PAT_A1155#Heap Paths
Source: PAT A1155 Heap Paths (30 分) Description: In computer science, a heap is a specialized tree-b ...
- RBM(受限玻尔兹曼机)和深层信念网络(Deep Brief Network)
目录: 一.RBM 二.Deep Brief Network 三.Deep Autoencoder 一.RBM 1.定义[无监督学习] RBM记住三个要诀:1)两层结构图,可视层和隐藏层:[没输出层] ...
- 【剑指Offer】8、跳台阶
题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 解题思路: 首先考虑最简单的情况,如果只有1级台阶, ...