洛谷 P1165 日志分析

题目描述

M 海运公司最近要对旗下仓库的货物进出情况进行统计。目前他们所拥有的唯一记录就是一个记录集装箱进出情况的日志。该日志记录了两类操作:第一类操作为集装箱入库操作,以及该次入库的集装箱重量;第二类操作为集装箱的出库操作。这些记录都严格按时间顺序排列。集装箱入库和出库的规则为先进后出,即每次出库操作出库的集装箱为当前在仓库里所有集装箱中最晚入库的集装箱。

出于分析目的,分析人员在日志中随机插入了若干第三类操作――查询操作。分析日志时,每遇到一次查询操作,都要报告出当前仓库中最大集装箱的重量。

输入输出格式

输入格式:

 

包含N+1 行:

第一行为1 个正整数N,对应于日志内所含操作的总数。

接下来的N 行,分别属于以下三种格式之一:

格式1: 0 X //一次集装箱入库操作,正整数X表示该次入库的集装箱的重量

格式2: 1 //一次集装箱出库操作,(就当时而言)最后入库的集装箱出库

格式3: 2 //一次查询操作,要求分析程序输出当前仓库内最大集装箱的重量

当仓库为空时你应该忽略出库操作,当仓库为空查询时你应该输出0。

 

输出格式:

 

输出行数等于日志中查询操作的次数。每行为一个正整数,表示查询结果。

 

输入输出样例

输入样例#1:
13
0 1
0 2
2
0 4
0 2
2
1
2
1
1
2
1
2
输出样例#1:
2
4
4
1
0

说明

对于20%的数据,有N≤10;

对于40%的数据,有N≤1000;

对于100%的数据,有N≤200000,X≤10^8。

 

模拟栈

操作1 :插入一个数,因为栈是先进后出,所以栈顶只需存储最大值即可,因为2操作询问的只是最大值,小于栈顶的是不会被访问的,所以插入也没用。

操作2:直接--top。

操作3:因为栈顶就是最大值,直接输出。

屠龙宝刀点击就送

#include <algorithm>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <queue>
#define N 400500

inline void read(int &x)
{
    register char ch=getchar();
    for(x=0;!isdigit(ch);ch=getchar());
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
}
int maxn,a[N],n,stack[N],top;
int max(int a,int b) {return a>b?a:b;} 
int main()
{
    read(n);
    for(int opt,x;n--;)
    {
        read(opt);
        if(!opt)
        {
            read(x);
            stack[++top]=x;
            stack[top]=max(stack[top],stack[top-1]);
        }
        else if(opt==1)
        {
            if(!top) continue;
            top--;
        }
        else
        {
            if(!top) {printf("0\n");continue;}
            printf("%d\n",stack[top]);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/ruojisun/p/7387567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取USB摄像头的1080p的JPEG格式的图片20180608_1806.7z 电脑上的系统:ubuntu14.04 // http://www.linuxidc.com/Linux/2011-03/33020.htm // V4L2摄像头获取单幅图片测试程序(MMAP模式) // [日期:2011-03-06] 来源:Linux社区 作者:aokikyon [字体:大 中 小] // // #加了点注释 // // #Rockie Cheng // printf #include #include // memset #include #include #include #include // close write usleep read #include #include #include #include #include #include // mmap #include #include #include #include // pthread_create pthread_join #include #define CLEAR(x) memset (&(x), 0, sizeof (x)) #define REQ_COUNT 6 #define uchar unsigned char struct buffer { void * start; size_t length; }; static char * dev_name = "/dev/video0";//摄像头设备名 static int fd = -1; struct buffer * buffers = NULL; // static unsigned int n_buffers = 0; // 2012-7-13 11:33 camera flag // static int iFlagCamera = 0; volatile int iFlagCamera = 0; // 函数名称:thread1 // 函数功能:用于接受键盘的输入,之后通知thread2抓图 // 参数列表: // 返回值 : void thread1(void) { char ch; printf("\n !!!!Warning!!!!\n Max 1000 color bmp\n Input the char: \n"); printf("\n !!!!press o an capture 1 frame picture! \n"); printf("\n !!!!press t an capture 10 frame picture! \n"); printf("\n !!!!press h an capture 100 frame picture! \n"); while(1) { while((ch=getchar()) != '\n') { printf("%c\n", ch); if('o'==ch) { iFlagCamera = 1; printf("thread1=%d\n", iFlagCamera); } else if('t'==ch) { iFlagCamera = 10; printf("thread1=%d\n", iFlagCamera); } else if('h'==ch) { iFlagCamera = 101; printf("thread1=%d\n", iFlagCamera); } else if('q'==ch) { iFlagCamera = 10001; printf("thread1=%d\n", iFlagCamera

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值