B - Guess a number!
Posted 霜雪千年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
4
>= 1 Y
< 3 N
<= -3 N
> 55 N
17
2
> 100 Y
< -100 Y
Impossible
解题思路:题意比较简单,就是找出符合要求的区间,然后输出其中任意一个值。注意有可能只有左区间或者只有右区间,若是这样,各做一个标记即可。
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int INF = 2e9; 4 int main(){ 5 int n,x,left=-INF,right=INF;char ch;string eq;bool flag1=false,flag2=false;//[left,right] 6 cin>>n; 7 while(n--){ 8 getchar(); 9 cin>>eq>>x>>ch; 10 if(ch==‘N‘){//修改其值 11 if(eq==">=")eq="<"; 12 else if(eq=="<=")eq=">"; 13 else if(eq==">")eq="<="; 14 else eq=">="; 15 } 16 if(eq=="<"){right=min(right,x-1);flag2=true;}//右区间取最小 17 else if(eq=="<="){right=min(right,x);flag2=true;} 18 else if(eq==">"){left=max(left,x+1);flag1=true;}//左区间取最大 19 else {left=max(left,x);flag1=true;} 20 } 21 if(left>right)cout<<"Impossible"<<endl; 22 else if(flag1)cout<<left<<endl;//可能只有左区间 23 else if(flag2)cout<<right<<endl;//可能只有右区间 24 return 0; 25 }
以上是关于B - Guess a number!的主要内容,如果未能解决你的问题,请参考以下文章
ACM-ICPC 2016 大连赛区现场赛 K. Guess the number && HDU 5981
374. Guess Number Higher or Lower
CodeForces - 416A Guess a number