zoukankan      html  css  js  c++  java
  • An Easy Task(简箪题)

    B. An Easy Task

    1000ms
    1000ms
    65536KB
    64-bit integer IO format: %lld      Java class name: Main
    Font Size:  

    You are given an easy task by your supervisor -- to find the best value of X, one of the parameters in an evaluation function, in order to improve the accuracy of the whole program.

    However, after a few days' analysis, you realize that it is far harder than you imagine. There are so many values X can be, and the only way to find the best one among them is to try all these possible values one after another!

    Fortunately, you know that X is an integer and thanks to the previous works by your senior fellow apprentices, you have got n constraints on X. Each constraint must be in one of the following forms:
    1. < k: means that X is less than integer k;
    2. > k: means that X is greater than integer k;
    3. <= k: means that X is less than or equal to integer k;
    4. >= k: means that X is greater than or equal to integer k;
    5. = k: means that X is equal to integer k.

    Now, you are going to figure out how many possible values X can be, so that you can estimate whether it is possible to finish your task before deadline.


    Input

    The first line contains an integer T (1 ≤ T ≤ 10) -- the number of test cases.

    For each test case:
    The first line contains an integer n. 0 ≤ n ≤ 10 000.
    Then follows n lines, each line contains a comparison operator o and an integer k, separated by a single space. o can be one of “>”, “<”, “>=”, “<=”, and “=”. 0 ≤ | k | ≤ 1 000 000 000.
    There is no contradictory between these constraints, in other word, at least one integer value meets all of them.

    Output

    For each test case, output one integer in a single line -- the number of possible values of X, or “-1” if the answer is infinite.

    Sample Input

    1
    2
    > 2
    <= 5

    Sample Output

    3
    #include<stdio.h>
    #define ll long long
    #define inf 9999999999
    int main()
    {
        ll t,n,a,l,r;
        char s[5];
        scanf("%lld",&t);
        while(t--)
        {
            scanf("%lld",&n);
            l=-inf; r=inf;
            int flag=1;
            while(n--)
            {
                scanf("%s%lld",s,&a);
                if(s[1]!=''&&flag)
                {
                    if(s[0]=='>')if(l<a)l=a;
                    if(s[0]=='<'&&r>a)r=a;
                }
                else if(flag)
                {
                    if(s[0]=='>'&&l<a+1)l=a+1;
                    if(s[0]=='<'&&r>a-1)r=a-1;
                    if(s[0]=='=')
                    if(l<=a&&a<=r)l=r=a;else flag=0;
                }
            }
            if(flag==0||l>r)printf("0
    ");
            else if(l==-inf||r==inf)printf("-1
    ");
            else printf("%lld
    ",r-l+1);
    
        }
    }
    


  • 相关阅读:
    Android给ListView设置分割线Divider样式
    Android监听ScrollView滑动到顶端和底部
    .Net——使用.net内置处理程序处理自己定义节点Demo
    Java---25---集合框架共性方法
    网络基础——知识生活化会变得如此简单
    Jquery节点遍历
    Rapha&#235;l 中文帮助文档(API)
    Fitnesse使用系列二
    UVa 10188
    Powershell Mail module, 发送outbox 里的全部邮件(一个.csv文件代表一封邮件)
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6715111.html
Copyright © 2011-2022 走看看