Collecting Numbers II

题目描述

You are given an array that contains each number between 1 ... n exactly once. Your task is to collect the numbers from 1 to n in increasing order.
On each round, you go through the array from left to right and collect as many numbers as possible.
Given m operations that swap two numbers in the array, your task is to report the number of rounds after each operation.

输入

The first line has two integers n and m(1 ≤ n, m ≤ 2*105): the array size and the number of operations.
The next line has n integers x1,x2,...,xn: the numbers in the array.
Finally, there are m lines that describe the operations. Each line has two integers a and b(1 ≤ a,b ≤ n): the numbers at positions a and b are swapped.

输出

Print m integers: the number of rounds after each swap.

样例输入 Copy
5 3
4 2 1 5 3
2 3
1 5
2 3
样例输出 Copy
2
3
4

 上一题的加强版,加入了数字交换

计算出初始轮次后考虑x[a],x[a]+1,x[b],x[b]+1

如果里面有数据重复需要去掉(用set,手写一直错不知道哪里错了反正一直错,卡了一晚上我服了哈哈最后一个set就过了,受不了,大概是已经四了吧)

代码

#include
using namespace std;
typedef long long ll;
int n,m,x[200009],pos[200009];
int check(int a){
	if(a<2||a>n)return 0;
	if(pos[a]&a){
	int b=0;
	for(auto i:a)b+=check(i);
	return b;
}
int jab(int a,int b){
	int k=0,t=0;
	k=sump({x[a],x[a]+1,x[b],x[b]+1});
	swap(pos[x[a]],pos[x[b]]);swap(x[a],x[b]);
	t=sump({x[a],x[a]+1,x[b],x[b]+1});
	return -k+t;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i){
		scanf("%d",&x[i]);
		pos[x[i]]=i;
	}
	int ans=1,a,b;
	for(int i=2;i<=n;++i){
		if(pos[i]

你可能感兴趣的:(数学,排序,算法)