集训Day2 T2 新壳栈
2017-10-19 19:53
239 查看
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
代码如下(暴力):
小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); }
相关文章推荐
- [2017雅礼集训]day2 T2 二分图关键点&&博弈
- FJ省队集训DAY2 T2
- shoi2012 day2(想看T2的算了吧我也不会)
- [2017雅礼集训]day10 T2 数列 最长上升子序列
- ZJOI2017 day2 T2 线段树 想法题
- [霍尔定理]「2017 山东一轮集训 Day2」LOJ 6062——PAIR
- 省队集训Round3 DAY2
- Noip 提高组 2011 Day2 T2 聪明的质检员 二分法
- 【2018寒假集训 Day2】【动态规划】回文字
- FJ省队集训DAY3 T2
- 多校round2-day2-t2 净化
- 5月集训Day2考试
- Noip2013 Day2 T2 花匠(贪心)
- 集训题解-Day2
- UOJ 264 NOIP2016 DAY2 T2 浅谈队列单调性及辅助队列时间戳
- [DP] 计蒜客 2017 NOIP模拟赛(二)Day2 T2.紫色百合
- NOIP2011 DAY2 T2 浅谈二分答案
- 集训Day2 T1 收银员
- 【2018寒假集训 Day2】【动态规划】回文字
- NOIP2014 day2 T2 洛谷P2296 寻找道路