G - Boxes in a Line (数组模拟双向链表)

转载 2018年04月16日 13:35:46




G - Boxes in a Line 

You have n boxes in a line on the table numbered 1...n from left to right. Your task is to simulate 4 kinds of commands:
• 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y )
• 2 X Y : move box X to the right to Y (ignore this if X is already the right of Y )
• 3 X Y : swap box X and Y
• 4: reverse the whole line.
Commands are guaranteed to be valid, i.e. X will be not equal to Y .
For example, if n = 6, after executing 1 1 4, the line becomes 2 3 1 4 5 6. Then after executing 2 3 5, the line becomes 2 1 4 5 3 6. Then after executing 3 1 6, the line becomes 2 6 4 5 3 1. Then after executing 4, then line becomes 1 3 5 4 6 2


Input

There will be at most 10 test cases. Each test case begins with a line containing 2 integers n, m (1 ≤ n,m ≤ 100,000). Each of the following m lines contain a command.


Output

For each test case, print the sum of numbers at odd-indexed positions. Positions are numbered 1 to n from left to right.

Sample Input

6 4
1 1 4
2 3 5
3 1 6
4

6 3
1 1 4
2 3 5
3 1 6

100000 1
4
Sample Output
Case 1: 12
Case 2: 9
Case 3: 2500050000  

我用list写的超时了。。。
代码:                 

#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
using namespace std;

int main(void)
{
	list<int> lis;
	int n,m,j=1;
	while(~scanf("%d %d",&n,&m))
	{
		int i;
		for(i=1;i<=n;i++)
		{
			lis.push_back(i);
		}
		while(m--)
		{
			int run;
			scanf("%d",&run);
			if(run==1)
			{
				int x,y;
				scanf("%d %d",&x,&y);
			    list<int>::iterator it;
				it=find(lis.begin(),lis.end(),y);
				--it;
				if(*it!=x)
				{
					lis.remove(x);
					lis.insert(it,x);
				}
			}
			else if(run==2)
			{
				int x,y;
				scanf("%d %d",&x,&y);
				list<int>::iterator it;
				it=find(lis.begin(),lis.end(),y);
				++it;
				if(it==lis.end())
				{
					lis.remove(x);
					lis.push_back(x);
				}
				else if(*it!=x)
				{
					lis.remove(x);
					it=lis.insert(it,x);
				}
			}
			else if(run==3)
			{
				int x,y,temp;
				scanf("%d %d",&x,&y);
				list<int>::iterator it1=find(lis.begin(),lis.end(),x);
				list<int>::iterator it2=find(lis.begin(),lis.end(),y);
				temp=*it1;
				*it1=*it2;
				*it2=temp;
			}
			else
			{
				lis.reverse();
			}
		}
		long long sum=0;
		list<int>::iterator it;
		for(it=lis.begin();it!=lis.end();it++)
		{
			sum+=*it;
			it++;
		}
		printf("case %d: %lld\n",j++,sum);
		lis.clear();
	}
	return 0;
} 
最后网上查了下,学会了用数组模拟链表的方式。(第一次学)

#include<iostream> 
#include<cstdio>
#include<cstring>
using namespace std;  
int L[100010];//   
int R[100010];  
void Link(int &x,int &y) //左右链接,注意顺序 
{  
    R[x]=y;  
    L[y]=x;  
}  
  
int main(void)  
{  
    int i,j=0;  
    int n,m;  
    while(~scanf("%d %d",&n,&m))  
    {  
        R[0]=1;  
        L[0]=n;  
        for(i=1;i<=n;i++)  
        {  
            L[i]=i-1;  
            R[i]=i+1;  
        }  
		R[n]=0; 
        int op,x,y,dir=1,Ry,Rx,Lx,Ly;  
        while(m--)  
        {  
            scanf("%d",&op);  
            if(op!=4)  
            scanf("%d %d",&x,&y);  
            else  
            {  
                dir=!dir; 
                continue;  
            }  
            if(!dir&&op!=3)  
            {  
                op=3-op;//  如果链表逆转了,则左右相反 
            }  
            if(op==1&&R[x]==y) continue; //考虑已经满足条件的情况 
            if(op==2&&L[x]==y) continue;  
            Ry=R[y],Rx=R[x],Lx=L[x],Ly=L[y];  
            if(op==1)  
            {  
                Link(Lx,Rx);  
                Link(Ly,x);  
                Link(x,y); //自己想象 ... Lx x Rx....Ly y Ry ..... 
            }  
            else if(op==2)  
            {  
                Link(Lx,Rx);  
                Link(y,x);  
                Link(x,Ry);  
            }  
            else if(op==3)  
            {  
                if(R[x]==y)  //..Lx x (Rx Ly) y Ry ...
                {  
                    Link(Lx,y);  
                    Link(y,x);  
                    Link(x,Ry);  
                }  
                else if(L[x]==y)  //..Ly y (Ry Lx) x Rx ...
                {  
                    Link(Ly,x);  
                    Link(x,y);  
                    Link(y,Rx);  
                }  
                else  
                {  
                    Link(Lx,y);  
                    Link(y,Rx);  
                    Link(Ly,x);
                    Link(x,Ry);
                }  
            }  
        }  
        int b=0;
		long long ans=0; 
        for(i=1;i<=n;i++)  
        {  
            b=R[b];//R[0]指向第一个元素从R[0]开始 
            if(i&1)//奇数 
			{
				ans+=b;
            }
        } 
        if(!(dir)&&!(n%1))//考虑链表逆转的情况//n是奇数时不用考虑 
        {
        	ans=(long long)n*n/2+n/2-ans;
        }
        printf("Case %d: %lld\n",++j,ans);
    }  
    return 0;  
 }   

UVA12657 Boxes in a Line【双向链表】【数组模拟】

题目大意:你有一行盒子,从左到右编号为1~n,现在有4种操作。 1 X Y 表示把X盒子移到Y盒子的左边 2 X Y 表示把X盒子移到Y盒子的右边 3 X Y 表示交换X盒子和Y盒子的位置 4 将盒子...
  • u011676797
  • u011676797
  • 2014-12-11 09:12:58
  • 1052

UVA 12657 Boxes in a Line(双向链表)

You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simul...
  • acm_fighting
  • acm_fighting
  • 2016-03-25 20:30:57
  • 631

UVA 12657 Boxes in a Line

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem...
  • zhang_xueping
  • zhang_xueping
  • 2015-08-17 21:50:40
  • 604

链表-双向链表&&UVa12657 Boxes in a Line(移动盒子)的理解与解析

You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simul...
  • ProLightsfxjh
  • ProLightsfxjh
  • 2015-11-02 07:10:43
  • 1251

UVA 12657 Boxes in a Line 模拟 双向链表

想都不用想,用list直接模拟肯定超时,只能自己手写         用两个数组le和ri,写一个双向链表,le[x]表示盒子x左边的盒子编号,ri[x]表示盒子x右边的盒子编号,实际上就是一个双向...
  • Good_night_Sion_
  • Good_night_Sion_
  • 2017-03-02 19:46:20
  • 267

UVa--12657 Boxes in a line(双向链表)

UVA--12657 题解: lrj大法。 采用双向链表,编号为 i 的的盒子左右两边的盒子编号分别为 left[i] 和 right[i] 。 技巧:操作4反转需要修改所有元素的指针,为避免时间消...
  • xushao_Movens
  • xushao_Movens
  • 2016-02-13 22:31:28
  • 281

Uva-12657 Boxes in a Line(双向链表)

注意操作3是通过数字寻找位置的,不会受是否翻转的影响 #include #include using namespace std; #define BG 100005 int be[BG],...
  • Russian_Blue_Cat
  • Russian_Blue_Cat
  • 2015-08-24 20:59:36
  • 196

uva12657 Boxes in a Line 双向链表

题意:给你一行盒子,从左 到右依次编号为1,2,3....n,有四种输入指令 1 x y 把盒子x移动到盒子y左边 (如果x已经在y左边则忽略这条指令) 2 x y 把盒子x移动到盒子y右边(如果...
  • dlutjwh
  • dlutjwh
  • 2017-06-06 08:51:21
  • 121

uva 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include #include #include #include #include using namespace std; list l; list::it...
  • u013382399
  • u013382399
  • 2014-07-26 15:11:49
  • 1037

湖南省赛 B - Boxes in a Line 模拟循环链表

• 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) • 2 X Y : move bo...
  • Summer__show_
  • Summer__show_
  • 2016-08-26 16:50:03
  • 364
收藏助手
不良信息举报
您举报文章:G - Boxes in a Line (数组模拟双向链表)
举报原因:
原因补充:

(最多只允许输入30个字)