UVA12657 moving Boxes in a Line

Luogu problem surface

I started to hand in a \ (\ RM STL list \) and tried to lose water. As a result, \ (\ verb!RE! \) flew.

So honestly hand hit the structure linked list.

General idea of the topic

You have \ (n \) boxes numbered \ (1\cdots n \) from left to right on a line on the table. Your task is to simulate four operations:

  • 1 X Y: move the box number \ (X \) to the left of the box number \ (Y \) (ignore if \ (X \) is already on the left of \ (Y \).

  • 2 X Y: move the box number \ (X \) to the right of the box number \ (Y \) (ignore if \ (X \) is already on the right of \ (Y \).

  • 3 X Y: exchange the position of box number \ (X \) and box number \ (Y \).

  • 4: Inverts the entire line.

For each group of data, the sum of the numbers of their odd positions is output.

Topic analysis

\(1,2,3 \) operations can obviously be handled with the linked list \ (\ operatorname{O}(1) \), and we focus on the operation \ (4 \).

If we manually turn around, \ (\ rm STL \) can come, but the linked list will obviously \ (\ verb!T! \) fly.

Because we only want the final answer, we don't need to care about the form in the middle. We want to mark it.

\(rev \) indicates that the linked list is in a U-turn state if and only if \ (rev=1 \).

Notice an interesting property of the \ (\ operatorname{xor} \) operation:

\(1\operatorname{xor}1=0,0\operatorname{xor}1=1\).

Therefore, if the current operation is \ (4 \), make \ (rev\gets rev~\operatorname{xor}1 \).

If the current linked list is inverted and operated as \ (1 \) or \ (2 \), then————

Let's look at an interesting property:

\(2\operatorname{xor}3=1,1\operatorname{xor}3=2\).

(XOR madman belongs to yes)

So let \ (opt\gets opt~\operatorname{xor}3 \), \ (opt \) represent the instruction sequence number.

It's good to operate. It's easy to understand. After all, the sequence is turned over.

Finally, the answer is \ (ans=List[i].val\{i\in 2\times k+1,k\in \mathcal{N} \} \).

Special judgment:

\(ans\gets \dfrac{n\times(n+1)}{2}-ans \), if and only if \ (n=2\times k(k\in \mathcal{N}) \) and \ (rev=1 \).

Indicates that the current sequence has an even length and is flipped over.

for instance:

1 -> 2 -> 3 -> 4 -> 5 -> 6
=>
6 -> 5 -> 4 -> 3 -> 2 -> 1

At this time \ (ANS = \ sum \ limits {I = 1} ^ {\ tfrac {n}{2}-1} (2 \ times I + 1) - ans \) is the above formula.

code

The code given here does not consider many situations of this problem. Please pay attention to initialization when reading \ (\ rm Coding \).

______ See ancestors

const int MA=100005;

struct List
{
	int val;
	
	int pre;
	
	int nxt;
};

List lst[MA];

int n,m;

inline void add(int u,int v)//u -> v
{
	lst[u].nxt=v;
	
	lst[v].pre=u;
}

#undef int

int main(void)
{
	#define int long long
	
	n=read(),m=read();
	
	lst[0].nxt=1;
	
	for(register int i=1;i<=n;i++)
	{
		lst[i].val=i;
		
		lst[i].pre=i-1,lst[i].nxt=i+1;
	}
	
	lst[n].nxt=0;
	
	int rev=0;
	
	for(register int i=1;i<=m;i++)
	{
		int opt=read();
		
		if(rev==1 && opt<=2)
		{
			opt^=3;
		}
		
		if(opt==1)//Move x to the left of y  
		{
			int x=read(),y=read();
			
			if(lst[y].pre!=x)
			{
				add(lst[x].pre,lst[x].nxt);
				
				int t=lst[y].pre;
				
				add(x,y);
				
				add(t,x);
			}
		}
		
		else if(opt==2)//Move x to the right of y 
		{
			int x=read(),y=read();
			
			if(lst[y].nxt!=x)
			{
				add(lst[x].pre,lst[x].nxt);
				
				int t=lst[y].nxt;
				
				add(y,x);
				
				add(x,t);
			}
		} 
		
		else if(opt==3)
		{
			int x=read(),y=read();
			
			if(lst[y].nxt==x)
			{
				swap(x,y);
			}
			
			if(lst[x].nxt==y)
			{
				int t=lst[x].pre;
				
				add(x,lst[y].nxt);
				
				add(y,x);
				
				add(t,y);
			}
			
			else
			{
				int ta=lst[x].pre,tb=lst[x].nxt;
				
				add(lst[y].pre,x);
				
				add(x,lst[y].nxt);
				
				add(ta,y);
				
				add(y,tb);
			}
		}
		
		else
		{
			rev^=1;
		}
	}
	
	int ans(0),idx(0);
	
	for(register int i=lst[0].nxt;i;i=lst[i].nxt)
	{
		idx++;
		
		if(idx%2==1)
		{
			ans+=lst[i].val;
		}
	}
	
	if(rev==1 && n%2==0)
	{
		printf("%lld\n",(n*(n+1)>>1ll)-ans);
		
		return 0;
	}
	
	printf("%lld\n",ans);
	
	return 0; 
}

Tags: data structure linked list Simulation

Posted on Sun, 28 Nov 2021 15:37:16 -0500 by Rushyo