CodeForces - 416A Guess a number

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 416A Guess a number相关的知识,希望对你有一定的参考价值。

A. Guess a number!
time limit per test1 second
memory limit per test256 megabytes

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 sign is:

“>” (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  - 10^9 ^≤ 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

问题链接CodeForces - 416A Guess a number!
问题简述:(略)
问题分析:(略)

AC的C语言程序如下:

/* CodeForces - 416A Guess a number! */

#include <stdio.h>

#define Y 2000000000

int main()

    int n, x, miny = -Y, maxy = Y;
    char sign[3], ans[2];
    scanf("%d", &n);
    while (n--) 
        scanf("%s%d%s", sign, &x, ans);
        if (ans[0] == 'Y') 
            if (sign[0] =='>' && sign[1] == '=') 
                if (miny < x) miny = x;
             else if (sign[0] == '>') 
                if (miny <= x) miny = x + 1;
             else if (sign[0] == '<' && sign[1] == '=') 
                if (maxy > x) maxy = x;
             else if (sign[0] == '<') 
                if (maxy >= x) maxy = x - 1;
            
         else if (ans[0] == 'N') 
            if (sign[0] =='<' && sign[1] == '=') 
                if (miny <= x) miny = x + 1;
             else if (sign[0] == '<') 
                if (miny < x) miny = x;
             else if (sign[0] == '>' && sign[1] == '=') 
                if (maxy >= x) maxy = x - 1;
             else if (sign[0] == '>') 
                if (maxy > x) maxy = x;
            
        
    

    if (miny <= maxy) printf("%d\\n", miny);
    else printf("Impossible\\n");

    return 0;

AC的C++语言程序如下:

/* CodeForces - 416A Guess a number! */

#include <bits/stdc++.h>

using namespace std;

const int Y = 2000000000;
int main()

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n, x, miny = -Y, maxy = Y;
    string sign;
    char ans;

    cin >> n;
    while (n--) 
        cin >> sign >> x >> ans;
        if (ans == 'N') 
            if (sign == ">") sign = "<=";
            else if (sign == "<") sign = ">=";
            else if (sign == ">=") sign ="<";
            else if (sign == "<=") sign = ">";
        
        if (sign == ">" && miny <= x) miny = x + 1;
        else if (sign == "<" && maxy >= x) maxy = x - 1;
        else if (sign == ">=" && miny < x) miny = x;
        else if (sign == "<=" && maxy > x) maxy = x;
    

    if (miny <= maxy)
        cout << miny << endl;
    else
        cout << "Impossible" << endl;

    return 0;

以上是关于CodeForces - 416A Guess a number的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 618B Guess the Permutation

codeforces Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) B Guess the Permutation

Codeforces1355E Guess Divisors Count

CodeForces 1363D. Guess The Maximums

CodeForces 1363D. Guess The Maximums

Codeforces Round #245 (Div. 1)——Guess the Tree