UVa-12657 - Boxes in a Line

用数组写感觉萌萌哒啊,本题可以写个link函数,更加方便

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxx=100010;
 6 int r[maxx],l[maxx];
 7 int n,m;
 8 long long ans;
 9 int main()
10 {
11     int cnt=0;
12     while(~scanf("%d%d",&n,&m))
13     {
14         ans=0;
15         for(int i=1;i<=n;i++)
16         {
17             r[i]=i+1;
18             l[i]=i-1;
19         }
20         r[0]=1;
21         r[n]=0;
22         l[1]=0;
23         l[0]=n;
24         bool f=0;
25         while(m--)
26         {
27             int op,x,y;
28             scanf("%d",&op);
29             if(op==4) f=!f;
30             else
31             {
32                 scanf("%d%d",&x,&y);
33                 if((f==0&&op==1)||(f==1&&op==2))
34                 {
35                     r[l[x]]=r[x];
36                     l[r[x]]=l[x];
37                     l[x]=l[y];
38                     r[l[y]]=x;
39                     r[x]=y;
40                     l[y]=x;
41                 }
42                 else if((f==0&&op==2)||(f==1&&op==1))
43                 {
44                     r[l[x]]=r[x];
45                     l[r[x]]=l[x];
46                     r[x]=r[y];
47                     l[r[y]]=x;
48                     l[x]=y;
49                     r[y]=x;
50                 }
51                 else if(op==3)
52                 {
53                     if(r[x]==y)
54                     {
55                         r[x]=r[y];
56                         l[r[y]]=x;
57                         r[l[x]]=y;
58                         l[y]=l[x];
59                         l[x]=y;
60                         r[y]=x;
61                     }
62                     else if(l[x]==y)
63                     {
64                         r[l[y]]=x;
65                         l[x]=l[y];
66                         l[r[x]]=y;
67                         r[y]=r[x];
68                         r[x]=y;
69                         l[y]=x;
70                     }
71                     else
72                     {
73                         r[l[x]]=y;
74                         l[r[x]]=y;
75                         r[l[y]]=x;
76                         l[r[y]]=x;
77                         swap(r[x],r[y]);
78                         swap(l[x],l[y]);
79                     }
80                 }
81             }
82         }
83         if(f==0)
84         {
85             for(int p=r[0],i=1;p!=0&&i<=n;p=r[p],i++)
86                 if(i%2==1) ans+=p;
87         }
88         else
89         {
90             for(int p=l[0],i=1;p!=0&&i<=n;p=l[p],i++)
91                 if(i%2==1) ans+=p;
92         }
93         printf("Case %d: %lld\n",++cnt,ans);
94     }
95 }

 

你可能感兴趣的:(uva)