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 **/
  • 相关阅读:
    数组、List和ArrayList的区别
    Spring 学习笔记(一)——Spring 基本概念和体系概览
    解压大文件提示C盘空间不够的问题
    在Struts2 Action中快速简便的访问Request、Session等变量
    Java 以及JEE环境快速搭建
    JAVA对象布局之对象头(Object Header)
    Docker之两小时入门
    Java创建多线程的几种方式
    JAVA并发之加锁导致的活跃性问题
    JAVA并发之多线程引发的问题剖析以及如何保证线程安全
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11420222.html
Copyright © 2011-2022 走看看