zoukankan      html  css  js  c++  java
  • Gym

    http://codeforces.com/gym/101190/attachments

    Input file:

    hard.in

    Output file:

    hard.out

     Helen had come upon a piece of code that uses a lot of “magical constants”. She found a logical expression that checks if an integer x belongs to a certain set of ranges, like the one shown below:

     x >= 5 && x <= 10 ||

     x >= 7 && x <= 20 ||

     x <= 2 ||

     x >= 21 && x <= 25 ||

     x >= 8 && x <= 10 ||

     x >= 100

     Helen does not like “magical constants”, so she decided to refactor this expression and all similar ones in such a way, that the refactored expression still has the same Boolean result for all integers x, but it uses as few integer constants in its text as possible.

     Integers in this problem, including integer x, come from the range of all signed 16 bit integers starting from −2 15 (−32 768) to 215 − 1 (32 767) inclusive.

    Input

     The input file contains at most 1000 lines. Each line consists of either one comparison or two comparisons separated by logical and operator “&&”. Each comparison starts with “x”, followed by greater-or-equals operator “>=” or less-or-equals operator “<=”, followed by an integer constant. When two comparisons are in the same line, the first one is always greater-or-equals, followed by less-or-equals.

     All lines, but the last one, are terminated by logical or operator “||”. All tokens in a line are separated by a single space and there are no trailing or leading spaces.

    Output

     Write the refactored expression to the output file in the same format as in the input. You can arrange lines in any order, as long as the resulting expression has the right format, produces the same Boolean result on all integers x, and contains the minimal possible number of integer constants in its text. Numbers must be formatted without leading zeros and there must be precisely one space between tokens on a line.

     Write a single line with the word “true” if the expression is true on all integers. Write a single line with the word “false” if the expression is false on all integers.

    Examples

    hard.in

    x >= 5 && x <= 10 ||

    x >= 7 && x <= 20 ||

    x <= 2 ||

    x >= 21 && x <= 25 ||

    x >= 8 && x <= 10 ||

    x >= 100

    hard.out

    x <= 2 ||

    x >= 5 && x <= 25 ||

    x >= 100

    hard.in

    x >= 10 && x <= 0

    hard.out

    false

    hard.in

    x <= 10 ||

    x >= 0

    hard.out

    true

    hard.in

    x >= -32768

    hard.out

    true

    题意:

    [−32768,32767]这是总区间,

    求的是所给区间在[−32768,32767]上的区间,

    没有输出false,总区间包含于所给区间输出true,否则输出所求区间

    思路:

    排下序,能并起来的并起来,并不起来的就当是新区间,

    坑不少,比赛的时候WA了好久也没写对,

    这里我加几个样例填下坑:

    input:  

    x >= 0 && x <= 10 ||

    x >= 90

    output:

    x >= 90

    input:

    x <= -32768

    output:

    x <= -32768

    input:

    x <= -32767

    output:

    false

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<cmath>
      4 #include<algorithm>
      5 #include<iostream>
      6 
      7 const int init_left = -32768;
      8 const int init_right = 32767;
      9 
     10 using namespace std;
     11 
     12 struct node
     13 {
     14     int left, right;
     15 } str[50000], init;
     16 
     17 int judge(char c)
     18 {
     19     if(c>='0'&&c<='9') return 1;
     20     else return 0;
     21 }
     22 
     23 int cmp(struct node a, struct node b)
     24 {
     25     if(a.left != b.left) return a.left < b.left;
     26     else return a.right > b.right;
     27 }
     28 
     29 
     30 int main()
     31 {
     32     freopen("hard.in","r",stdin);
     33     freopen("hard.out","w",stdout);
     34     int len, i, num, j, x;
     35     char a[10005];
     36     num = 0;
     37     init.left = init_left;
     38     init.right = init_right;
     39     while(1)
     40     {
     41         str[num] = init;
     42         gets(a);
     43         len = strlen(a);
     44         for(i=0; i<len; i++)
     45         {
     46             if(a[i]=='=')
     47             {
     48                 for(j=i; j<len; j++)
     49                 {
     50                     if(judge(a[j]))
     51                     {
     52                         x = 0;
     53                         while(judge(a[j]))
     54                         {
     55                             x = x * 10 + a[j]-'0';
     56                             j++;
     57                         }
     58                         if(a[i+1]=='-'||a[i+2]=='-'||a[i+3]=='-') x = -x;
     59                         if(a[i-1]=='<') str[num].right = x;
     60                         else str[num].left = x;
     61                         break;
     62                     }
     63                 }
     64             }
     65         }
     66         num++;
     67         if(a[len-1]!='|') break;
     68     }
     69     sort(str, str+num, cmp);
     70     int cur = 0, sum = 0;
     71     for(i=0; i<num; i++)
     72     {
     73         if(str[i].left>str[i].right)
     74         {
     75             sum++;
     76             continue;
     77         }
     78         if(str[i].left<=str[cur].right+1) str[cur].right = max(str[cur].right, str[i].right);
     79         else
     80         {
     81             cur++;
     82             str[cur] = str[i];
     83         }
     84     }
     85 
     86     if(cur==0&&str[0].left==init_left&&str[0].right==init_right) printf("true
    ");
     87     else if(sum==num) printf("false
    ");
     88     else
     89     {
     90         for(i=0; i<=cur; i++)
     91         {
     92             if(str[i].left<=init_left) printf("x <= %d", str[i].right);
     93             else if(str[i].right>=init_right) printf("x >= %d", str[i].left);
     94             else printf("x >= %d && x <= %d", str[i].left, str[i].right);
     95             if(i==cur) printf("
    ");
     96             else printf(" ||
    ");
     97         }
     98     }
     99     return 0;
    100 }
  • 相关阅读:
    js 各种常用js验证
    js url校验
    最近遇到的技术问题
    a标签的target的四个值
    新系统用到的新知识
    7 天打造前端性能监控系统
    前端必读:浏览器内部工作原理
    怎么判断ThreadPool线程池里的任务都执行完毕
    docker 在window 10 专业版的安装 && .net core 在docker的部署
    .net core 中后台获取前台 数据(post)的方法
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11398351.html
Copyright © 2011-2022 走看看