csu 1365 双向链表模拟超时
1365: Play with Chain
Time Limit: 5 Sec Memory Limit: 128 MB
Submit:
21 Solved: 5
[Submit][Status][Web
Board]
Description
YaoYao is fond of playing his chains. He has
a chain containing n diamonds on it. Diamonds are numbered from 1 to n.
At
first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.
He will
perform two types of operations:
CUT a b c: He
will first cut down the chain from the ath diamond to the
bth diamond. And then insert it after the cth diamond on
the remaining chain.
For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We
perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would
be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th
diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.
FLIP a
b: We first cut down the chain from the ath diamond to
the bth diamond. Then reverse the chain and put them back to the
original position.
For example, if we perform “FLIP 2 6” on the chain: 1 2 6
7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8
He wants to
know what the chain looks like after perform m operations. Could you help him?
Input
There will be multiple test cases in a test
data.
For each test case, the first line contains two numbers: n and m (1≤n,
m≤3*100000), indicating the total number of diamonds on the chain and the number
of operations respectively.
Then m lines follow, each line contains one
operation. The command is like this:
CUT a b c // Means a CUT operation, 1 ≤
a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1).
FLIP a b // Means a FLIP operation, 1 ≤ a
< b ≤ n.
The input ends up with two negative
numbers, which should not be processed as a case.
Output
For each test case, you should print a line
with n numbers. The ith number is the number of the ith
diamond on the chain.
Sample Input
8 2
CUT 3 5 4
FLIP 2 6
-1 -1
Sample Output
1 4 3 7 6 2 5 8
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; struct node
{
int num;
struct node *next;
struct node *father;
};
struct node *head;
void mem(struct node *p)
{
p->num=;
p->next=NULL;
p->father=NULL;
}
void CUT(int l,int r,int k,int n)
{
if(k==l-)return;
if(l==&&r==n)return ;
struct node *p,*q,*st,*ed,*hxl;
int i,cur;
if(k<l) cur=r;
else
{
cur=r-l++k;
k=cur;
}
p=head;
for(i=;i<=cur&&p!=NULL;i++)
{
p=p->next;
if(i==l) st=p;
if(i==r) ed=p;
if(i==k) hxl=p;
}
if(k==) hxl=head;
p=st->father;
q=ed->next;
p->next=q;
if(q!=NULL) q->father=p; p=hxl->next;
ed->next=hxl->next;
if(p!=NULL) p->father=ed; hxl->next=st;
st->father=hxl;
}
void FLIP(int l,int r)
{
int i,tmp,tom;
struct node *st,*ed,*q;
q=head;
for(i=;i<=r;i++)
{
q=q->next;
if(i==l) st=q;
if(i==r) ed=q;
}
tom=(r-l+)/;
while(tom--)
{
tmp=st->num;
st->num=ed->num;
ed->num=tmp; st=st->next;
ed=ed->father;
}
}
int main()
{
int n,m,i;
int l,r,k;
char cur[];
struct node *p,*q;
while(scanf("%d%d",&n,&m)>)
{
if(n==-&&m==-)break;
head=(struct node*)malloc(sizeof(struct node));
mem(head);
p=head;
for(i=;i<=n;i++)
{
q=(struct node*)malloc(sizeof(struct node));
q->num=i;
q->next=p->next;
p->next=q;
q->father=p;
p=q;
}
getchar();
while(m--)
{
scanf("%s",cur);
if(cur[]=='C')
{
scanf("%d%d%d",&l,&r,&k);
CUT(l,r,k,n);
}
else if(cur[]=='F')
{
scanf("%d%d",&l,&r);
FLIP(l,r);
}
}
p=head;
for(i=;i<=n;i++)
{
q=p;
p=p->next;
free(q);
printf("%d",p->num);
if(i!=n)printf(" ");
else printf("\n");
}
free(p);
}
return ;
}
csu 1365 双向链表模拟超时的更多相关文章
- csu - 1537: Miscalculation (模拟题)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1537 因为给出的式子是必定合法的,只要用两个栈分别保存符号和数字.算出答案后和从左至右算的答案比对 ...
- ACM/ICPC 之 用双向链表 or 模拟栈 解“栈混洗”问题-火车调度(TSH OJ - Train)
本篇用双向链表和模拟栈混洗过程两种解答方式具体解答“栈混洗”的应用问题 有关栈混洗的定义和解释在此篇:手记-栈与队列相关 列车调度(Train) 描述 某列车调度站的铁道联接结构如Figure 1所示 ...
- fiddler模拟timeout超时场景
fiddler模拟网络超时: 用fiddler模拟网络请求超时 最近要测试程序对cgi 请求超时的兼容,所以就需要模拟超时,第一个想到的就是fiddler工具,说一下具体的做法: Rules -> ...
- 2014-11-3 NOIP模拟赛3
字符串 string.pas/c/cpp 1S/256MB [题目描述] 现在给一个字符串,你要做的就是当这个字符串中存在两个挨着的字符是相同的时就将这两个字符消除.需要注意的是,当把这两个字符消除后 ...
- zuul超时问题
转载:http://blog.csdn.net/tianyaleixiaowu/article/details/78772269 是这样的,今天碰到了微服务响应超时问题,而且超时时间特别短,2秒就超时 ...
- Zuul超时问题,微服务响应超时,zuul进行熔断
天碰到了微服务响应超时问题,而且超时时间特别短,2秒就超时,zuul就走熔断了. 我采用zuul作为网关,根据不同的访问路径进行微服务的路由,譬如有个服务是user,我访问user服务的某个接口时,该 ...
- HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]&l ...
- 怎样用Fiddler模拟网络超时
转自:http://materliu.github.io/all/web/2014/04/28/fiddler-timeout.html 用fiddler模拟网络请求超时 用fiddler模拟网络 ...
- SpringCloud之Feign 负载均衡请求超时时间
版本声明: SpringCloud:Greenwich.SR4 SpringBoot:2.1.9.RELEASE Feign调用服务的默认时长是1秒钟,也就是如果超过1秒没连接上或者超过1秒没响应,那 ...
随机推荐
- spring包下载方法
http://blog.csdn.net/liangtiaoxian/article/details/52780747 https://jingyan.baidu.com/article/2fb0ba ...
- Q4m使用手册
q4m是基于mysql存储引擎的轻量级消息队列,通过扩展SQL语法来操作消息队列,使用简单,容易上手,开发人员基本不用再进行学习和熟悉.Q4M支持多发送方,多接收方,接收方相互不影响,php项目中异步 ...
- Utils工具方法集插件详解
var Utils = function(){}; Utils.text = { stripTags: function (val) { return val.replace(/<\/?[^&g ...
- 实例的初始化由JVM装载类的时候进行,保证了线程的安全性
在23种设计模式中,单例是最简单的设计模式,但是也是很常用的设计模式.从单例的五种实现方式中我们可以看到程序员对性能的不懈追求.下面我将分析单例的五种实现方式的优缺点,并对其在多线程环境下的性能进行测 ...
- 解决self.encoding = charset_by_name(self.charset).encoding
解决self.encoding = charset_by_name(self.charset).encoding def createMysqlTable(tablename): # config = ...
- ASP.NET:Application,Session,Cookie,ViewState和Cache之间的区别(转)
在ASP.NET中,有很多种保存信息的对象.例如:Application,Session,Cookie,ViewState和Cache等,那么它们有什么区别呢?每一种对象应用的环境是什么? 为了更清楚 ...
- shell ssh远程执行命令
[root@backup shell]# vi backup.sh #!/bin/sh ipAddress=172.17.167.38 ssh -tt root@$ipAddress -p 22 &l ...
- FPGA实战操作(2) -- PCIe总线(协议简述)
目录 1. PCIe基础知识 2. 事务层协议 2.1 数据包结构 2.2 帧头含义详述 3. 报文举例 3.1 寄存器读报文 3.2 完成报文 4. 机制简述 4.1 Non-Posted和Post ...
- Android屏幕尺寸单位转换
最近在看Android群英传这本书,书中有一节涉及到了,屏幕尺寸与单位.觉得以后可能会用到,做个笔记. PPI(pixels per inch) ,又称为DPI,它是由对角线的像素点数除以屏幕的大小得 ...
- Mac 10.12安装Git管理工具SourceTree
说明:Git的GUI工具应该是这款最好用. 下载: (链接: https://pan.baidu.com/s/1mhRr35Y 密码: vv67)