Codeforces Round #241 (Div. 2), problem: (A) Guess a number! solution

WARNING: Below post is intended only for gray coders of CF! Dont waste your time if you are on higher levels.

Codeforces Round #241 (Div. 2), problem: (A) Guess a number! : Problem - A - Codeforces

Codeforces Round #241 (Div. 2), problem: (A) Guess a number! solution: S4pcpK - Online C++ Compiler & Debugging Tool - Ideone.com

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int n, x, l=-2*1e9, r=2*1e9;
    char s1[5], s2[5];
    scanf("%d", &n);
    while(n--) {
        scanf("%s%d%s", s1, &x, s2);
        if(s1[0]=='>' && s1[1]=='=' && s2[0]=='Y' && x>l) l=x;
        else if(s1[0]=='>' && s1[1]!='=' && s2[0]=='Y' && x+1>l) l=x+1;
        else if(s1[0]=='>' && s1[1]=='=' && s2[0]=='N' && x-1<r) r=x-1;
        else if(s1[0]=='>' && s1[1]!='=' && s2[0]=='N' && x<r) r=x;
        else if(s1[0]=='<' && s1[1]=='=' && s2[0]=='Y' && x<r) r=x;
        else if(s1[0]=='<' && s1[1]!='=' && s2[0]=='Y' && x-1<r) r=x-1;
        else if(s1[0]=='<' && s1[1]=='=' && s2[0]=='N' && x+1>l) l=x+1;
        else if(s1[0]=='<' && s1[1]!='=' && s2[0]=='N' && x>l) l=x;
    }
    if(l<=r) printf("%d", l);
    else printf("Impossible");
    return 0;
}

note: first of all, think that whole numbers between -2000000000 and 2000000000 are acceptable, because there is no restraint. assign l=-2 * 1e9 (the most left hand side) and r=2 * 1e9 (the rightmost hand side). and then focus on constraints, based on them try to minimize your left or right hand side. to get the idea look pictures below:

alt text

that is before any contsraints. and now we apply constraints:

alt text

and after constraints the left handside and right handside set to new points. after applying all the constraints we check whether if left handside is still less than or equal to right hand side, if(l<=r), if yes we print whatever number between that range. if left hand side already passed right handside, if(l>r), then print Impossible.

got the idea? and now looking for more challenging one? then peek at it: Codechef Guessing Game - A3 Problem - CodeChef

4 Likes