有一些小球,从左至右编号为1 2 3 ...... n
执行A 1 4后,小球1被移到4的左边
执行B 3 5后,小球3被移到5的右边
样例输入:
6 2
A 1 4
B 3 5
样例输出:
214536
1 #include<stdio.h> 2 const int N=50000; 3 struct Node 4 { 5 int order; 6 Node *left,*right; 7 }node[N]; 8 9 void CreateNode(int n) 10 { 11 int i; 12 for(i = 1;i <= n;i++) 13 { 14 node[i].order = i; 15 node[i].right = &node[i+1]; 16 node[i+1].left= &node[i]; 17 } 18 node[0].left = NULL; 19 node[0].right = &node[1]; 20 node[1].left = &node[0]; 21 node[i].right = NULL; 22 } 23 void A(int x,int y) 24 { 25 Node *p = &node[x],*q = &node[y]; 26 p->right->left=p->left; 27 p->left->right=p->right; 28 p->right=q; 29 p->left=q->left; 30 q->left->right=p; 31 q->left=p; 32 } 33 34 void B(int x,int y) 35 { 36 Node *p = &node[x],*q = &node[y]; 37 p->right->left = p->left; 38 p->left->right = p->right; 39 p->left = q; 40 p->right = q->right; 41 q->right->left = p; 42 q->right = p; 43 } 44 int main() 45 { 46 int n,m; 47 char cmd; 48 int x,y; 49 scanf("%d%d",&n,&m); 50 CreateNode(n); 51 while(m--) 52 { 53 scanf("%*c%c%d%d",&cmd,&x,&y); 54 if(cmd == 'A') 55 A(x,y); 56 if(cmd == 'B') 57 B(x,y); 58 } 59 Node *l = &node[0]; 60 l = l->right; 61 while(l->right) 62 { 63 printf("%d",l->order); 64 l = l->right; 65 } 66 return 0; 67 }
在此感谢这篇博客给我的帮助:http://blog.csdn.net/taotaotaotao910429/article/details/7831479