ZOJ 4016 Mergeable Stack(from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)
模拟题,用链表来进行模拟
# include <stdio.h>
# include <stdlib.h>
typedef struct node
{
int num;
struct node *q;
struct node *h;
}node;
struct Node
{
node *start;
node *end;
int sum;
}m[];
int main()
{
int k;
scanf("%d", &k);
while (k--)
{
int n, p;
scanf("%d%d", &n, &p);
for (int i = ; i <= n; i++)
{
m[i].end = NULL;
m[i].start = NULL;
m[i].sum = ;
}
for (int i = ; i<p; i++)
{
int op, s, v, t;
scanf("%d", &op);
if (op == )
{
scanf("%d%d", &s, &v);
if (m[s].sum == )
{
m[s].start = (node *)malloc(sizeof(node));
m[s].start->num = v;
m[s].start->q = NULL;
m[s].start->h = NULL;
m[s].end = NULL;
m[s].sum = ;
}
else
if (m[s].sum == )
{
m[s].end = (node *)malloc(sizeof(node));
m[s].end->num = v;
m[s].end->q = m[s].start;
m[s].end->h = NULL;
m[s].start->h = m[s].end;
m[s].sum = ;
}
else
{
node * z = (node *)malloc(sizeof(node));
z->num = v;
z->q = m[s].end;
z->h = NULL;
m[s].end->h = z;
m[s].end = z;
m[s].sum++;
}
}
else
if (op == )
{
scanf("%d", &s);
if (m[s].sum == )
printf("EMPTY\n");
else
{
m[s].sum--;
if (m[s].sum != )
printf("%d\n", m[s].end->num);
else
printf("%d\n", m[s].start->num);
if (m[s].sum != )
{
node *z = m[s].end->q;
z->h = NULL;
m[s].end = z;
}
else
{
m[s].end = NULL;
m[s].start = NULL;
}
}
}
else
{
scanf("%d%d", &s, &t);
if (m[s].sum == )
{
m[s].end = m[t].end;
m[s].start = m[t].start;
m[s].sum = m[t].sum;
m[t].sum = ;
m[t].end = NULL;
m[t].start = NULL;
}
else
if (m[s].sum == )
{
m[s].sum += m[t].sum;
m[t].sum = ;
if (m[t].start != NULL)
{
m[s].end = m[t].start;
m[s].start->h = m[t].start;
m[s].end->q = m[s].start;
if (m[t].end != NULL)
m[s].end = m[t].end;
else
m[s].end = m[t].start;
}
m[t].start = NULL;
m[t].end = NULL;
}
else
{
m[s].sum += m[t].sum;
m[t].sum = ;
if (m[t].start != NULL)
{
m[s].end->h = m[t].start;
m[t].start->q = m[s].end;
if (m[t].end != NULL)
m[s].end = m[t].end;
else
m[s].end = m[t].start;
}
m[t].start = NULL;
m[t].end = NULL;
}
}
}
}
//system("pause");
return ;
}
ZOJ 4016 Mergeable Stack(from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)的更多相关文章
- ZOJ 4019 Schrödinger's Knapsack (from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)
题意: 第一类物品的价值为k1,第二类物品价值为k2,背包的体积是 c ,第一类物品有n 个,每个体积为S11,S12,S13,S14.....S1n ; 第二类物品有 m 个,每个体积为 S21,S ...
- 152 - - G Traffic Light 搜索(The 18th Zhejiang University Programming Contest Sponsored by TuSimple )
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738 题意 给你一个map 每个格子里有一个红绿灯,用0,1表示 ...
- zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)
题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple
Pretty Matrix Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid's birthday is coming. As hi ...
- Mergeable Stack 直接list内置函数。(152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple)
题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上. 题解:直接用list 的pop_back,push_back,splice 模拟, 坑:用splice ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack
题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...
- The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror) B"Even Number Theory"(找规律???)
传送门 题意: 给出了三个新定义: E-prime : ∀ num ∈ E,不存在两个偶数a,b,使得 num=a*b;(简言之,num的一对因子不能全为偶数) E-prime factorizati ...
- ZOJ 4016 Mergeable Stack(利用list模拟多个栈的合并,STL的应用,splice函数!!!)
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, ther ...
- ZOJ - 4016 Mergeable Stack (STL 双向链表)
[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) ...
随机推荐
- win10安装VMware
首先下载VMware安装包: 双击安装: 点击:“uninstall”安装,这个过程需要一些时间 点击“Next” 选择典型或者自定义安装 这里我选择的是典型安装,点击“Next”: 选择是否检查更新 ...
- ABAP调用新任务代码
*&调用函数‘ZMLTOTAL_CHECK’启用新任务'jx'执行'ZMLSCP1_FR0003'; IF zmlcbwlcgdd_ok[] is not INITIAL. CALL FUNC ...
- chatty: uid=10549(u0_a549) com.exampleidentical 40 lines
主要是因为你的app在短时间内输出太多次的日志,导致日志丢失 As soon as app considered 'chatty' by logcat (more than 5 lines per s ...
- ArcGIS服务器的feature图层限制
今天遇到了esri.layers.FeatureLayer发布一个宗地图层,里面有些数据未显示,导致数据显示不全,原来是服务中数据返回参数限制. ArcGIS的feature图层(在JavaScrip ...
- 测试,测试开发,QA,QM,QC--------- 测试之路勿跑偏
测试,测试开发,QA,QM,QC可能是测试行业里的细分角色了,加了不少群学习,看到不同人有对自己不同的角色定位.我也做了这挺长时间的测试,也和大部分测试同胞一样,为了一份好的工作学习各种各样的技术,但 ...
- hdu-5734 Acperience(数学)
题目链接: Acperience Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Eclipse 插件使用
1. AmaterasUML:UML 类图(class diagram) 注意这里是先编写好代码,通过插件根据代码逻辑关系生成类图: 安装AmaterasUML前,需要先安装 GEF,采用 eclip ...
- bootstrap 学习笔记(4)---- 按钮
平常我们自己写按钮,这次不用我们自己写 了,直接应用bootstrap中的按钮样式,就能设计出很漂亮的按钮样式.接下来就让我们一起学习吧. 1.可以作为按钮使用的标签或元素:<a>< ...
- Linux下监控网卡流量的软件iftop
官网上说使用iftop需要libpcap和libcurses这两个包. 用命令查找了一下 # rpm -qa | grep libpcap libpcap-0.9.4-15.el5 只找到了这个,缺 ...
- (测试)LaTeX公式
\[a^2+b^2=c^2\] \[F(\omega)=\mathcal{F}[f(t)]=\int_{-\infty}^\infty f(t)e^{-iwt}\,dt\] \[\sum_{i=0}^ ...