码迷,mamicode.com
首页 > 其他好文 > 详细

双向链表 UVa12657(未完成)TLE&&WA

时间:2015-08-07 21:40:18      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <cstdio>
  5 
  6 using namespace std;
  7 
  8 struct Node
  9 {
 10     int date;
 11     Node* next;
 12     Node* pre;
 13     Node(){;pre=NULL;next=NULL;}
 14 };
 15 
 16 inline int Scan()
 17 {
 18     char ch;
 19     int a = 0;
 20     while((ch = getchar())>=0 && ch<=9)
 21         a = a*10+ch-0;
 22     return a;
 23 }
 24 
 25 int main()
 26 {
 27     Node* arr[200000];
 28     int num=1;
 29     int n,m;
 30     Node* tmp=new Node();
 31     Node* head=new Node();
 32     Node* last=new Node();
 33     while(scanf("%d%d",&n,&m)!=EOF)
 34     {
 35         getchar();
 36         int num4=0;
 37         head->next=last;
 38         last->pre=head;
 39         for(int i=1;i<=n;i++)
 40         {
 41             arr[i]=last;
 42             last->date=i;
 43             last->next=new Node();
 44             last->next->pre=last;
 45             last=last->next;
 46             last->next=NULL;
 47         }
 48         int order;
 49         int a,b;
 50         for(int i=0;i<m;i++)
 51         {
 52             order=Scan();
 53             if(order==3)
 54             {
 55                 a=Scan();
 56                 b=Scan();
 57                 Node* ta=arr[a];
 58                 Node* tb=arr[b];
 59                 ta->pre->next=tb;
 60                 tb->pre->next=ta;
 61                 ta->next->pre=tb;
 62                 tb->next->pre=ta;
 63                 tmp=ta->pre;
 64                 ta->pre=tb->pre;
 65                 tb->pre=tmp;
 66                 tmp=ta->next;
 67                 ta->next=tb->next;
 68                 tb->next=tmp;
 69             }
 70             else if(order==2)
 71             {
 72                 a=Scan();
 73                 b=Scan();
 74                 Node* ta=arr[a];
 75                 Node* tb=arr[b];
 76                 ta->pre->next=ta->next;
 77                 ta->next->pre=ta->pre;
 78                 ta->next=tb->next;
 79                 tb->next->pre=ta;
 80                 tb->next=ta;
 81                 ta->pre=tb;
 82             }
 83             else if(order==1)
 84             {
 85                 a=Scan();
 86                 b=Scan();
 87                 Node* ta=arr[a];
 88                 Node* tb=arr[b];
 89                 ta->pre->next=ta->next;
 90                 ta->next->pre=ta->pre;
 91                 ta->next=tb;
 92                 ta->pre=tb->pre;
 93                 tb->pre->next=ta;
 94                 tb->pre=ta;
 95             }
 96             else if(order==4)
 97             {
 98                 num4++;
 99             }
100         }
101         long long reans=0;
102         if(num4%2==0)
103         {
104             Node* ans =head->next;
105             int flag=1;
106             while(ans->next!=NULL)
107             {
108                 if(flag)
109                 {
110                     reans+=ans->date;
111                 }
112                 flag=1-flag;
113                 ans=ans->next;
114             }
115         }
116         else
117         {
118             Node* ans =last->pre;
119             int flag=1;
120             while(ans->pre!=NULL)
121             {
122                 if(flag)
123                 {
124                     reans+=ans->date;
125                 }
126                 flag=1-flag;
127                 ans=ans->pre;
128             }
129         }
130         printf("Case %d: %I64d\n",num,reans);
131        // cout<<"Case "<<num<<": "<<reans<<endl;
132         num++;
133     }
134     return 0;
135 }
View Code

 

双向链表 UVa12657(未完成)TLE&&WA

标签:

原文地址:http://www.cnblogs.com/wsruning/p/4711740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!