CSU 1329: 一行盒子(双向链表)经典 13年省赛题

来源:互联网 发布:淘宝店自动充值软件 编辑:程序博客网 时间:2024/06/05 08:28

1329: 一行盒子

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 872  Solved: 176
[Submit][Status][Web Board]

Description

你有一行盒子,从左到右依次编号为1, 2, 3,…, n。你可以执行四种指令:

1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令)。
2 X Y表示把盒子X移动到盒子Y右边(如果X已经在Y的右边则忽略此指令)。
3 X Y表示交换盒子X和Y的位置。
4 表示反转整条链。

指令保证合法,即X不等于Y。例如,当n=6时在初始状态下执行1 1 4后,盒子序列为2 3 1 4 5 6。接下来执行2 3 5,盒子序列变成2 1 4 5 3 6。再执行3 1 6,得到2 6 4 5 3 1。最终执行4,得到1 3 5 4 6 2。

Input

输入包含不超过10组数据,每组数据第一行为盒子个数n和指令条数m(1<=n,m<=100,000),以下m行每行包含一条指令。

Output

每组数据输出一行,即所有奇数位置的盒子编号之和。位置从左到右编号为1~n。

Sample Input

6 41 1 42 3 53 1 646 31 1 42 3 53 1 6100000 14

Sample Output

Case 1: 12Case 2: 9Case 3: 2500050000

HINT

Source

湖南省第九届大学生计算机程序设计竞赛

解题:数组的下标就是每个所对应的值。

#include<stdio.h>#include<string.h>const int N  = 100005 ;struct NODE{    int l , r ;}node[N];int main(){    int n , m , op , x , y ;    int T = 0;    while(scanf("%d%d",&n,&m)>0)    {        for(int i=0; i<=n; i++)            node[i].l=i-1 , node[i].r = i+1 ;              int flag = 0 , tmp;        while(m--)        {            scanf("%d",&op);            if(op==4)                flag ^= 1 ;            else{                scanf("%d%d",&x,&y);                if(op==3){ //注意3的交换操作                    if(node[x].l==y){                        node[node[x].r].l=y;  node[node[y].l].r=x;                        node[x].l = node[y].l; node[y].l=x;                        node[y].r = node[x].r; node[x].r=y;                    }                    else if(node[y].l==x){                        node[node[y].r].l=x;  node[node[x].l].r=y;                        node[y].l = node[x].l; node[x].l=y;                        node[x].r = node[y].r; node[y].r=x;                    }                    else{                        node[node[x].l].r= y ; node[node[x].r].l = y;                        node[node[y].l].r= x ; node[node[y].r].l = x;                        tmp = node[x].l ; node[x].l=node[y].l; node[y].l=tmp;                        tmp = node[x].r ; node[x].r=node[y].r; node[y].r=tmp;                    }                    continue ;                }                node[node[x].l].r = node[x].r ; //提出x之前 处理x左值的右指向为x右指向的值                node[node[x].r].l = node[x].l ; //同理                if(flag)op = 3 - op ;  //如果4操作次数为奇数次,操作要反向操作                if(op==2){ //x值放在y值的右边                    node[x].l = y ; node[x].r = node[y].r;                    node[node[x].r].l = x ;                    node[y].r = x;                }                else{  //x值放在y值的左边                    node[x].l=node[y].l; node[x].r = y ;                    node[node[x].l].r = x ;                    node[y].l = x ;                }                /*int id = 0 ;                for(int i=1; i<=n; i++)                    printf("%d ",node[id].r) , id = node[id].r;                printf("\n");*/            }        }        if(flag&&(n&1))flag = 0 ;        int k = 1 , id = node[0].r ;        long long ans = 0;        while(k<=n)        {            if((k&1))                ans += id ;            k++ ; id = node[id].r ;        }        if(flag) ans = (long long )n*(n+1)/2 - ans ;        printf("Case %d: %lld\n",++T , ans ) ;    }}


0 0
原创粉丝点击