zoukankan      html  css  js  c++  java
  • [模拟] Codefroces 1175B Catch Overflow!

    题目:http://codeforces.com/contest/1175/problem/B

    B. Catch Overflow!
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a function ff written in some basic language. The function accepts an integer value, which is immediately written into some variable xx. xx is an integer variable and can be assigned values from 00 to 2321232−1. The function contains three types of commands:

    • for nn — for loop;
    • end — every command between "for nn" and corresponding "end" is executed nn times;
    • add — adds 1 to xx.

    After the execution of these commands, value of xx is returned.

    Every "for nn" is matched with "end", thus the function is guaranteed to be valid. "for nn" can be immediately followed by "end"."add" command can be outside of any for loops.

    Notice that "add" commands might overflow the value of xx! It means that the value of xxbecomes greater than 2321232−1 after some "add" command.

    Now you run f(0)f(0) and wonder if the resulting value of xx is correct or some overflow made it incorrect.

    If overflow happened then output "OVERFLOW!!!", otherwise print the resulting value of xx.

    Input

    The first line contains a single integer ll (1l1051≤l≤105) — the number of lines in the function.

    Each of the next ll lines contains a single command of one of three types:

    • for nn (1n1001≤n≤100) — for loop;
    • end — every command between "for nn" and corresponding "end" is executed nn times;
    • add — adds 1 to xx.
    Output

    If overflow happened during execution of f(0)f(0), then output "OVERFLOW!!!", otherwise print the resulting value of xx.

    Examples
    input
    Copy
    9
    add
    for 43
    end
    for 10
    for 15
    add
    end
    add
    end
    
    output
    Copy
    161
    
    input
    Copy
    2
    for 62
    end
    
    output
    Copy
    0
    
    input
    Copy
    11
    for 100
    for 100
    for 100
    for 100
    for 100
    add
    end
    end
    end
    end
    end
    
    output
    Copy
    OVERFLOW!!!
    
    Note

    In the first example the first "add" is executed 1 time, the second "add" is executed 150 times and the last "add" is executed 10 times. Note that "for nn" can be immediately followed by "end" and that "add" can be outside of any for loops.

    In the second example there are no commands "add", thus the returning value is 0.

    In the third example "add" command is executed too many times, which causes xx to go over 2321232−1.

    题意:

    有l个询问,3种命令,add是给x加1,for n是循环n次,end是对应for n的结束,初始x为0,问l次操作后x是否会溢出int(2的32次方-1),如果不会则输出操作过后的x,否则输出OVERFLOW!!!

    思路:

    最后进的for和最先进的end匹配,所以想到用栈来保存每个for的影响(当前for的影响是当前的n*(以前for的累乘),在有add时把这个for的影响加入答案,在有end时把这个for的影响从栈中pop掉

    注意:1.如果大于等于inf就要标记溢出

       2.模拟栈如果访问上一个元素则先单独在外++tp,不然本地都是对的但OJ的输出会迷之出错(不知道为什么)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int amn=1e5+5;
     5 int main(){
     6     ll inf=1,ans=0,tmp=0;
     7     for(int i=1;i<=32;i++)inf*=2;
     8     ll l,tp=0,n,st[amn],have=0;
     9     bool overflow=0;
    10     string order;
    11     ios::sync_with_stdio(0);
    12     cin>>l;
    13     st[++tp]=1;
    14     while(l--){
    15         cin>>order;
    16         if(order=="add"){
    17             if(!overflow){
    18                 ans+=st[tp];    ///加上当前for造成的影响
    19                 if(ans>=inf)overflow=1;  ///溢出标记,是否大于等于inf
    20             }
    21         }
    22         else if(order=="for"){
    23             cin>>n;
    24             tp++;
    25             st[tp]=(min(inf,n*st[tp-1]));  ///当前这个for造成的影响是当前的n和前面的for的影响,相当于前缀积
    26         }
    27         else
    28             tp--;       ///碰到end后消去当前for的影响
    29     }
    30     if(overflow)printf("OVERFLOW!!!
    ");
    31     else printf("%lld
    ",ans);
    32 }
    33 /**
    34 有l个询问,3种命令,add是给x加1,for n是循环n次,end是对应for n的结束,初始x为0,问l次操作后x是否会溢出int(2的32次方-1),如果不会则输出操作过后的x,否则输出OVERFLOW!!!
    35 最后进的for和最先进的end匹配,所以想到用栈来保存每个for的影响(当前for的影响是当前的n*(以前for的累乘),在有add时把这个for的影响加入答案,在有end时把这个for的影响从栈中pop掉
    36 **/
  • 相关阅读:
    server version for the right syntax to use near 'USING BTREE 数据库文件版本不合导致的错误
    百度网盘,FTP上传异常、上传失败的解决办法
    zencart产品属性dropmenu select只有一个选择项时自动变成radio单选的解决办法
    火车采集小结
    dedecms织梦移站后替换数据库中文件路径命令
    dedecms织梦网站本地迁移到服务器后,后台更新栏目文档提示模板文件不存在,无法解析文档!的解决办法
    Addthis分享插件后url乱码的解决办法
    dedecms织梦做中英文(多语言)网站步骤详解
    递归的参数和返回值
    【图论算法】Dijkstra&BFS
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11420222.html
Copyright © 2011-2022 走看看