zjyyoj_ based Friends

Based Friends (analog)

Here Insert Picture Description
Here Insert Picture Description
Sample input:

2
0 2 1 3
2
3 2 0 1

Output:

1
0

This question is really to write a big head, long time to find a bug, did not know what was wrong, and eventually found the numbers wrong, that's a plus for a minus, I have been wrong, behind debug seen inside the array the number of exchange not only to find the problem.
In fact, very simple solution, because you can exchange need not be contiguous, that in order to achieve all of the conditions can be exchanged one by one to find, when the input of each digital recording good position, and then, the position of each update to the back of the number of exchange and like values, because the number is no longer in front of the traversed, the final output on the line.

#include<bits/stdc++.h>
using namespace std;
int a[200005],b[200005];
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		int m=2*n;
		for(int i=0;i<m;i++){
			scanf("%d",&b[i]);
			a[b[i]]=i;
		}
		int sum=0;
		for(int i=0;i<m;i+=2){
			if(b[i]&1){
				if(b[i+1]+1==b[i]){//是就不需要处理
					
				}else{//要处理的
					a[b[i+1]]=a[b[i]-1];//把i+1位换到b[i]的朋友的位置
					b[a[b[i]-1]]=b[i+1];//把i+1位的值也赋过去
					sum++;
				}
			}else{//同上
				if(b[i]==b[i+1]-1){
					
				}else{
					a[b[i+1]]=a[b[i]+1];/*当时这里写成了a[b[i+1]]=a[b[i]-1],所以错了*/
					b[a[b[i]+1]]=b[i+1];
					sum++;
				}
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}
Published 35 original articles · won praise 3 · Views 896

Guess you like

Origin blog.csdn.net/weixin_43823753/article/details/104524638