zoukankan      html  css  js  c++  java
  • Codeforces_750_C_(二分查找)

    C. New Year and Rating
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating changes by some value, possibly negative or zero.

    Limak competed in n contests in the year 2016. He remembers that in the i-th contest he competed in the division di(i.e. he belonged to this division just before the start of this contest) and his rating changed by ci just after the contest. Note that negative ci denotes the loss of rating.

    What is the maximum possible rating Limak can have right now, after all n contests? If his rating may be arbitrarily big, print "Infinity". If there is no scenario matching the given information, print "Impossible".

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 200 000).

    The i-th of next n lines contains two integers ci and di ( - 100 ≤ ci ≤ 100, 1 ≤ di ≤ 2), describing Limak's rating change after the i-th contest and his division during the i-th contest contest.

    Output

    If Limak's current rating can be arbitrarily big, print "Infinity" (without quotes). If the situation is impossible, print "Impossible" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the n contests.

    Examples
    input
    3
    -7 1
    5 2
    8 2
    output
    1907
    input
    2
    57 1
    22 2
    output
    Impossible
    input
    1
    -5 1
    output
    Infinity
    input
    4
    27 2
    13 1
    -50 1
    8 2
    output
    1897

    题意:n场比赛,Ci表示第i场比赛后rating的改变量,Di表示第i场比赛开始时选手所属div。问n场比赛结束时选手的最大rating。
    div1:rating>1899;div2:rating<=1899。

    看着是第三道题,以为比较水,掉以轻心,题也没读仔细,思路也没有,只想了模拟。认真审题,认真分析条件啊!!!

    思路:模拟应该是不行的,因为对于第i场比赛开始(或结束)时的rating,不仅由[1,i-1]场比赛决定,而且还与第i+1场比赛开始
    时div有关。 看了CF上别人给这道题贴的标签,恍然大悟,二分查找,(但是由于头天晚上太晚,头脑不清醒还是没有做出来)。
    第二天轻松A掉。
    二分查找:最多200000场比赛,每场rating变化值[-100,100],从[-20000000+1899,20000000+2000]之间二分查找,依次验证
    是否合法,倒序模拟每一场比赛,若能有(-20000000+1899)<=res<=(20000000+1900),那么有解;若res>=20001900,则无限大;
    其他情况则不可能(对于不可能的情况,二分查找时,一定可以在一个区间内结束查找,且无解,此处可以想一想为啥)。

    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    using namespace std;
    #define INF 20000000
    #define N 200005
    
    int change[N],Div[N]; 
    int n;
    
    int relation(int num)
    {
        for(int i=n;i>=1;i--)
        {
            num-=change[i];
            if(Div[i]==1&&num<1900)
                return -1;
            else if(Div[i]==2&&num>1899)
                return 1;
        }
        
        return 0;
    }
    
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;i++)
                scanf("%d%d",&change[i],&Div[i]);
            int l=-20000000,r=20000000+2000,res=-(INF+5),mid;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(relation(mid)==1)
                    r=mid-1;
                else if(relation(mid)==-1)
                    l=mid+1;
                else 
                {
                    res=mid;
                    l=mid+1;    
                }
            }
            //cout<<"mid:"<<mid<<endl;
            //cout<<"res:"<<res<<endl;
                
            if(res>=INF+1900)
                printf("Infinity
    ");
            else if(res>=-20000000+1899)
                printf("%d
    ",res);
            else
                printf("Impossible
    ");
        }
        return 0;
    }
    
    
    

     
  • 相关阅读:
    Java基础学习总结(33)——Java8 十大新特性详解
    Java基础学习总结(33)——Java8 十大新特性详解
    RabbitMQ学习总结(6)——消息的路由分发机制详解
    RabbitMQ学习总结(6)——消息的路由分发机制详解
    Stanford公开课《编译原理》学习笔记(1~4课)
    补习系列(21)-SpringBoot初始化之7招式
    使用modelarts部署bert命名实体识别模型
    全面认识 RUST -- 掌控未来的雷电
    补习系列(20)-大话 WebSocket 与 "尬聊"的实现
    【开发者portal在线开发插件系列二】多条上下行消息(messageId的使用)
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6394313.html
Copyright © 2011-2022 走看看