集训Day2 T2 新壳栈

Description

小Z设计了一种新的数据结构“新壳栈”。首先,它和传统的栈一样支持压入、弹出操作。此外,其栈顶的前c个元素是它的壳,支持翻转操作。其中,c>2是一个固定的正整数,表示壳的厚度。小Z还希望,每次操作,无论是压入、弹出还是翻转,都仅用与c无关的常数时间完成。聪明的你能帮助她编程实现“新壳栈”吗?

程序期望的实现效果如以下两表所示。其中,输入的第一行是正整数c,之后每行输入都是一条指令。另外,如遇弹出操作时栈为空,或翻转操作时栈中元素不足c个,应当输出相应的错误信息。

指令 涵义

1[空格]e 在栈顶压入元素e

2 弹出(并输出)栈顶元素

3 翻转栈顶的前c个元素

0 退出

Input

第一行输入c,之后每行输入都是一条指令,输入以0结束。

Output

对于每个弹出操作,输出栈顶元素,如果栈为空输出“Error: the stack is empty!”。对于每个翻转操作,如果栈中元素不足c个,输出”Error: less than c elements in the stack!“,注意这里的c在输出时以具体输入的c的值代替。

Sample Input

3

1 1

1 2

1 3

1 4

3

1 5

3

2

2

2

3

2

2

2

0

Sample Output

3

2

5

Error: less than 3 elements in the stack!

4

1

Error: the stack is empty!

Data Constraint
c<=50000

操作数<=1000000

思路:其实就是暴力。。。。正解未知23333
代码如下(暴力):

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,f[1000005],tot;

int read()
{
    int res=0;
    char ch=getchar(); 
    while (ch<'0' || ch>'9')    ch=getchar();
    while (ch>='0' && ch<='9')  res=res*10+ch-'0',ch=getchar();
    return res;
}

void update()
{
    for (int i=1;i<=n/2+1;i++)
    {
        int t=f[tot-n+i];   f[tot-n+i]=f[tot-i+1];  f[tot-i+1]=t;
    }   
}

int main()
{
    //freopen("stk.in","r",stdin);
    //freopen("stk.out","w",stdout);
    scanf("%d",&n);
    int x,y;
    x=read();
    while (!x==0)
    {
        if (x==1)   y=read(),f[++tot]=y;
        if (x==2 && tot==0) printf("Error: the stack is empty!\n");
        if (x==2 && tot>0)  printf("%d\n",f[tot]),tot--;
        if (x==3 && tot<n)
        {
            printf("Error: less than ");
            printf("%d",n);
            printf(" elements in the stack!");
            printf("\n");
        }   
        if (x==3 && tot>=n) update();
        x=read();
    }
    fclose(stdin);
    fclose(stdout);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值