鬼吹灯之怒睛湘西
Time Limit: 2000 ms
Memory Limit: 65536 KiB
Submit
Statistic
Problem Description
给你n个小球,从左到右编号依次为1,2,3,4,5,6.........n排成一行。现在有以下2种操作:A x y表示把编号为x小球移动到编号为y的小球的左边(和y相邻)。Q x为询问编号为x的小球左边的球号,如果x左边没有小球的话输出"cyk666"。
Input
第一行输入一个T,表示有T组测试数据。(1<=T<=100)
随后每一组测试数据第一行是两个整数N,M,其中N表示球的个数(1 随后有M行询问,第一个字符是操作类型s。
当s为’A’时,输入x,y表示把编号为x小球移动到编号为y的小球的左边。
当s为’Q’时,输入x表示询问小球x左边的球号。保证(1<=x<=N,1<=y<=N,1<=N<=100000)
Output
输出每次询问的球号,如果这样的小球不存在,输出"cyk666"(不包括引号)。
Sample Input
1
6 5
A 1 2
A 1 4
A 3 5
Q 5
Q 2
Sample Output
3
cyk666
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *h,*t,*p;
int main()
{
int T,n,m,a,b,i;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
h=(struct node *)malloc(sizeof(struct node));
t=h;
for(i=1; i<=n; i++)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=i;
p->next=NULL;
t->next=p;
t=p;
}
char s;
while(m--)
{
getchar();
scanf("%c",&s);
if(s=='A')
{
scanf("%d%d",&a,&b);
t=h;
p=h->next;
while(p->data!=a)//查找a的位置
{
p=p->next;
t=t->next;
}
t->next=p->next;//删除原来的a元素
p=t->next;
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
t=h;
while(t->next->data!=b)//查找b的位置
{
t=t->next;
}
p->next=t->next;
t->next=p;//将a插入到b前面去
}
if(s=='Q')//询问操作也是个查找操作
{
int x;
scanf("%d",&x);
p=h;
while(p->next->data!=x&&p)
{
p=p->next;
}
if(p==h||p==NULL)//若查找到的小球为首尾均不符合题意应舍去。
{
printf("cyk666\n");
}
else
{
printf("%d\n",p->data);
}
}
}
}
return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m;
struct aa
{
aa *nxt=0;
aa *pre=0;
int date;
} a[200000];
int main()
{
int T;
cin>>T;
while (T--)
{
cin>>n>>m;
for (int i=1; i<=n; i++)
{
a[i].date=i;
a[i].nxt=&a[i+1];
a[i].pre=&a[i-1];
}
while (m--)
{
char s;
cin>>s;
int aa,b;
if (s=='A')
{
cin>>aa>>b;
(*(a[aa].nxt)).pre = a[aa].pre;
(*(a[aa].pre)).nxt = a[aa].nxt;
((a[aa].pre)) = a[b].pre;
(*(a[b].pre)).nxt=&a[aa];
a[aa].nxt=&a[b];
a[b].pre=&a[aa];
}
else
{
cin>>b;
if ((*(a[b].pre)).date==0)
{
cout<<"cyk666"<<endl;
}
else
{
cout<< (*(a[b].pre)).date<<endl;
}
}
}
}
return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m;
int zuo[200000];
int you[200000];
int main()
{
int T;
cin>>T;
while (T--)
{
cin>>n>>m;
for (int i=1; i<=n; i++)
{
zuo[i]=i-1;
you[i]=i+1;
}
you[n]=0;
while (m--)
{
char s;
cin>>s;
int aa,b;
if (s=='A')
{
cin>>aa>>b;
if (!zuo[aa])
{
zuo[you[aa]]=0;
}
else if (!you[aa])
{
you[zuo[aa]]=0;
}
else
{
you[zuo[aa]]=you[aa];
zuo[you[aa]] = zuo[aa];
}
if (!zuo[b])
{
zuo[aa]=0;
zuo[b]=aa;
you[aa]=b;
}
else
{
you[zuo[b]]=aa;
zuo[aa]=zuo[b];
zuo[b]=aa;
you[aa]=b;
}
}
else
{
cin>>b;
if (!zuo[b])
{
cout<<"cyk666"<<endl;
}
else
{
cout<< zuo[b]<<endl;
}
}
}
}
return 0;
}