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




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;  
 }   

阅读更多
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页