Problem:
Bozosort
Bozosort, as well as Bogosort, is a very inefficient sort algorithm. It is a random number-based algorithm that sorts sequence elements following the steps as below:
Randomly select two elements and swap them.
Verify if all the elements are sorted in increasing order.
Finish if sorted, else return to 1.
To analyze Bozosort, you have decided to simulate the process using several predetermined pairs of elements.
You are given several commands to swap two elements. Make a program to evaluate how many times you have to run the command before the sequence is aligned in increasing order.
Input
The input is given in the following format.
$N$
$a_1$ $a_2$ ... $a_N$
$Q$
$x_1$ $y_1$
$x_2$ $y_2$
$...$
$x_Q$ $y_Q$
The first line provides the number of sequence elements $N$ ($2 \leq N \leq 300,000$). The second line provides an array of integers $a_i$ ($1 \leq a_i \leq 10^9$) that constitutes the sequence. Each of the subsequent $Q$ lines provides a pair of integers $x_i,y_i$ ($1 \leq x_i,y_i \leq N$) that represent the $i$-th command, which swaps the two elements indicated by $x_i$ and $y_i$ ($x_i \ne y_i$).
Output
If neat alignment in increasing order is realized the first time after execution of multiple commands, output at what time it was. Output
0
if the initial sequence is aligned in increasing order, and
-1
if exhaustive execution still failed to attain the goal.
Sample Input 1
6
9 7 5 6 3 1
3
1 6
2 5
3 4
Sample Output 1
2
Sample Input 2
4
4 3 2 1
2
1 2
3 4
Sample Output 2
-1
Sample Input 3
5
1 1 1 2 2
1
1 2
Sample Input 3
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)

#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)

using namespace std;

using ll = long long;
using PLL = pair<ll, ll>;

template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }

int main() {
    int N; scanf("%d", &N);
    vector<int> A(N);
    rep (i, N) scanf("%d", &A[i]);
    int Q; scanf("%d", &Q);
    int cnt = 0;
    rep (i, N - 1) cnt += A[i] > A[i + 1];
    auto calc = [&](int i) {
        ll res = 0;
        if (i != 0 && A[i - 1] > A[i]) ++res;
        if (i != N - 1 && A[i] > A[i + 1]) ++res;
        return res;
    };
    rep (i, Q) {
        if (cnt == 0) {
            printf("%d\n", i);
            return 0;
        }
        int x, y; scanf("%d%d", &x, &y);
        --x; --y;
        if (x - 1 == y) swap(x, y);
        if (x + 1 == y) {
            if (x != 0 && A[x - 1] > A[x]) --cnt;
            if (y != N - 1 && A[y] > A[y + 1]) --cnt;
            if (A[x] > A[y]) --cnt;
            swap(A[x], A[y]);
            if (x != 0 && A[x - 1] > A[x]) ++cnt;
            if (y != N - 1 && A[y] > A[y + 1]) ++cnt;
            if (A[x] > A[y]) ++cnt;
        }
        else {
            cnt -= calc(x) + calc(y);
            swap(A[x], A[y]);
            cnt += calc(x) + calc(y);
        }
    }
    if (cnt == 0) printf("%d\n", Q);
    else puts("-1");
}
accuracy: 1
time_ms: 50
memory_kb: 4492
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int N;
    cin>>N;
    set<int> s;
    vector<int> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
        s.insert(A[i]);
    }
    vector<int> B = A;
    sort(B.begin(),B.end());
    int c = 0;
    for(int i=0;i<N;i++) c += (A[i] == B[i]);
    if(c == N){
        cout<<0<<endl;
        return 0;
    }
    int Q;
    cin>>Q;
    for(int z=1;z<=Q;z++){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        c -= (A[a] == B[a]);
        c -= (A[b] == B[b]);
        swap(A[a],A[b]);
        c += (A[a] == B[a]);
        c += (A[b] == B[b]);
        if(c == N){
            cout<<z<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
}
accuracy: 1
time_ms: 210
memory_kb: 21844
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    auto sorta = a;
    sort(sorta.begin(),sorta.end());
    int cnt = 0;
    for(int i=0;i<n;i++) if(a[i] == sorta[i]) cnt++;
    if(cnt == n)
    {
        cout<<0<<endl;
        return 0;
    }
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
    {
        int x,y;
        cin>>x>>y;
        x--,y--;
        if(a[x] == sorta[x]) cnt--;
        if(a[y] == sorta[y]) cnt--;
        swap(a[x],a[y]);
        //for(auto ai : a) cout<<ai<<" ";
        //cout<<endl;
        if(a[x] == sorta[x]) cnt++;
        if(a[y] == sorta[y]) cnt++;
        if(cnt == n)
        {
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 5476
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main() {
	int N;cin >> N;
	vector<int> A(N);
	map<int, int> mns, mxs;
	for (int i = 0;i < N;i++) {
		cin >> A[i];
		mns[A[i]] = N + 100;
		mxs[A[i]] = -1;
	}
	vector<int> B = A;
	sort(B.begin(), B.end());
	for (int i = 0;i < N;i++) {
		mns[B[i]] = min(mns[B[i]], i);
		mxs[B[i]] = max(mxs[B[i]], i);
	}
	int cr = 0;
	for (int i = 0;i < N;i++) if (mns[A[i]] <= i and i <= mxs[A[i]]) cr++;
	int Q;cin >> Q;
	int ret = -1;
	if (cr == N) ret = 0;
	for (int i = 0;i < Q;i++)  {
		int x, y;cin >> x >> y;
		x--;y--;
		if (mns[A[x]] <= x and x <= mxs[A[x]]) cr--;
		if (mns[A[y]] <= y and y <= mxs[A[y]]) cr--;
		swap(A[x], A[y]);
		if (mns[A[x]] <= x and x <= mxs[A[x]]) cr++;
		if (mns[A[y]] <= y and y <= mxs[A[y]]) cr++;
		if (ret == -1 and cr == N) ret = i + 1;
	}
	cout << ret << endl;
}
accuracy: 1
time_ms: 750
memory_kb: 33792
final_rank: 18

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int main(){
  int n; cin >> n;
  vector<int> A(n);
  for(int i=0;i<n;++i) cin >> A[i];
  vector<int> Ans(n);
  for(int i=0;i<n;++i) Ans[i]=A[i];
  sort(Ans.begin(),Ans.end());
  int now=0;
  for(int i=0;i<n;++i) if(A[i]==Ans[i]) now++;
  if(now==n){
      cout << 0 << endl;
      return 0;
    }
  int q; cin >> q;
  for(int i=0;i<q;++i){
    int a,b; cin >> a >> b;
    a--;b--;
    if(A[a]==Ans[a])now--;
    if(A[b]==Ans[b])now--;
    if(A[a]==Ans[b])now++;
    if(A[b]==Ans[a])now++;
    int u=A[a];
    A[a]=A[b];
    A[b]=u;
    if(now==n){
      cout << i+1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 5472
final_rank: 8

code_snippet: #include<iostream>
#include<string>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include <unordered_map>
#include<iomanip>
#include<numeric>
#include<stack>
#include<tuple>
#include<unordered_set>
#define all(a) (a).begin(), (a).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, x) for(long long i = 0; i < (x); i++)
#define REP(i, x, limit) for (ll i = (ll)x; i <= (ll)limit; i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;
int X[4] = { 1, 0, -1, 0 };//右、上、左、下
int Y[4] = { 0, 1, 0, -1 };

int main() {
	ll N,Q;
	cin >> N;
	vector<ll>ans(N), nomal(N);
	rep(i, N) {
		cin >> nomal[i];
	}
	ans = nomal;
	sort(all(ans));
	ll ok = 0;
	rep(i, N) {
		if (nomal[i] == ans[i]) {
			ok++;
		}
	}
	if (ok == N) {
		cout << '0' << endl;
		return 0;
	}
	cin >> Q;
	rep(i, Q) {
		ll a, b;
		cin >> a >> b;
		if (nomal[a - 1] == ans[a - 1])ok--;
		if (nomal[b - 1] == ans[b - 1])ok--;
		swap(nomal[a - 1], nomal[b - 1]);
		if (nomal[a - 1] == ans[a - 1])ok++;
		if (nomal[b - 1] == ans[b - 1])ok++;
		if (ok == N) {
			cout << i + 1 << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 7780
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return true;} return false;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return true;} return false;}
constexpr int INF = 1e9;

int main() {
    int N, Q; cin >> N;
    vi a(N);
    for (int i = 0; i < N; i++) cin >> a[i];
    cin >> Q;
    vi x(Q), y(Q);
    for (int i = 0; i < Q; i++) {
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
    }
    vi b = a;
    sort(b.begin(), b.end());
    int cnt = 0;
    for (int i = 0; i < N; i++) if (a[i]==b[i]) cnt++;
    if (cnt == N) {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 0; i < Q; i++) {
        int k = 0;
        if (a[x[i]]==b[x[i]]) k--;
        if (a[y[i]]==b[y[i]]) k--;
        if (a[x[i]]==b[y[i]]) k++;
        if (a[y[i]]==b[x[i]]) k++;
        swap(a[x[i]], a[y[i]]);
        cnt += k;
        if (cnt == N) {
            cout << i+1 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
accuracy: 1
time_ms: 170
memory_kb: 7840
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define all(a) (a).begin(),(a).end()

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    
    int N;
    cin >> N;
    vector<int> A(N), sorted(N);
    rep(i, 0, N) {
        cin >> A[i];
        sorted[i] = A[i];
    }
    
    sort(all(sorted));
    
    int ok = 0;
    rep(i, 0, N) if (sorted[i] == A[i]) ++ok;
    
    if (ok == N) {
        cout << 0 << '\n';
        return 0;
    }
    
    int Q;
    cin >> Q;
    rep(query, 0, Q) {
        int x, y;
        cin >> x >> y;
        --x, --y;
        
        if (sorted[x] == A[x]) --ok;
        if (sorted[y] == A[y]) --ok;
        
        swap(A[x], A[y]);
        
        if (sorted[x] == A[x]) ++ok;
        if (sorted[y] == A[y]) ++ok;
        
        if (ok == N) {
            cout << query + 1 << '\n';
            return 0;
        }
    }
    cout << -1 << '\n';
}
accuracy: 1
time_ms: 50
memory_kb: 5400
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,N) for(ll i=0;i<(ll)N;i++)
#define all(X) X.begin(),X.end()

int main(){
    ll N;cin>>N;
    vector<ll>A(N);
    rep(i,N)cin>>A[i];
    int Q;cin>>Q;
    vector<ll>B=A;
    sort(all(B));
    ll dif = 0;
    rep(i,N)if(A[i]!=B[i])dif++;
    rep(q,Q){
        if(dif==0){
            cout<<q<<endl;
            return 0;
        }
        int x,y;cin>>x>>y;x--;y--;
        if(A[x]==B[x])dif++;
        if(A[y]==B[y])dif++;
        if(A[x]==B[y])dif--;
        if(A[y]==B[x])dif--;
        swap(A[x],A[y]);
    }
    if(dif==0)cout<<Q<<endl;
    else cout<<-1<<endl;
}
accuracy: 1
time_ms: 170
memory_kb: 7816
final_rank: 12

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
#define rep(i,n) for(ll i=0; i<n; i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define print(a) cout<<a<<endl
typedef long long ll;
#define yn(flg) if(flg){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YN(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define so(a) sort(a.begin(),a.end())
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define pb push_back
#define a2i(a,s) (ll)(a-s)
#define i2a(s,a) (char)(s+a)
#define ssize(a) a.size()
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<pair<ll,ll>,ll> P3;
typedef pair<pair<ll,ll>,pair<ll,ll>> P4;

typedef pair<ll, ll> Pll;
typedef pair<ll,Pll> Plll;
typedef pair<Pii, int> Piii;
const ll INF = 1000000000000000000;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//vector<ll> saiki(ll bit,ll n){
//	vector<ll> s(n);
//	for(ll i=0;i<n;i++){
//		if(bit&(1<<i)){
//			s[i]=1;
//		}else s[i]=0;
//	}
//	return s;
//}
int main(){
//入力

	cin.tie(0);
	ios::sync_with_stdio(0);

	ll n;
	cin>>n;
	vector<ll> a(n);
	vector<ll> b(n);

	rep(i,n){
		cin>>a[i];
		b[i]=a[i];
	}
	ll q;
	cin>>q;
	vector<Pll> V(q);
	rep(i,q){
		ll x,y;
		cin>>x>>y;
		V[i]={x,y};
	}
//ソート
	sort(b.begin(),b.end());
	vector<ll> ans(n,0);
	ll v=n;
	rep(i,n){
		if(a[i]==b[i]){
			ans[i]=1;
			v--;
		}
	}
	if(v==0){
		cout<<0<<endl;
		return 0;
	}
//判定
	rep(i,q){
		ll x=V[i].first,y=V[i].second;
		swap(a[x-1],a[y-1]);
		if(a[x-1]==b[x-1]){
			v-=1-ans[x-1];
			ans[x-1]=1;
		}else{
			v+=ans[x-1];
			ans[x-1]=0;
		}
		if(a[y-1]==b[y-1]){
			v-=1-ans[y-1];
			ans[y-1]=1;
		}else{
			v+=ans[y-1];
			ans[y-1]=0;
		}
		if(v==0){
			cout<<i+1<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;

	return 0;

}
accuracy: 1
time_ms: 50
memory_kb: 14840
final_rank: 14

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
#define rep(i,n) for(ll i=0; i<n; i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define print(a) cout<<a<<endl
typedef long long ll;
#define yn(flg) if(flg){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YN(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define so(a) sort(a.begin(),a.end())
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define pb push_back
#define a2i(a,s) (ll)(a-s)
#define i2a(s,a) (char)(s+a)
#define ssize(a) a.size()
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<pair<ll,ll>,ll> P3;
typedef pair<pair<ll,ll>,pair<ll,ll>> P4;

typedef pair<ll, ll> Pll;
typedef pair<ll,Pll> Plll;
typedef pair<Pii, int> Piii;
const ll INF = 1000000000000000000;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//vector<ll> saiki(ll bit,ll n){
//	vector<ll> s(n);
//	for(ll i=0;i<n;i++){
//		if(bit&(1<<i)){
//			s[i]=1;
//		}else s[i]=0;
//	}
//	return s;
//}
int main(){
//入力

	cin.tie(0);
	ios::sync_with_stdio(0);

	ll n;
	cin>>n;
	vector<ll> a(n);
	vector<ll> b(n);

	rep(i,n){
		cin>>a[i];
		b[i]=a[i];
	}
	ll q;
	cin>>q;
	vector<Pll> V(q);
	rep(i,q){
		ll x,y;
		cin>>x>>y;
		V[i]={x,y};
	}
//ソート
	sort(b.begin(),b.end());
	vector<ll> ans(n,0);
	ll v=n;
	rep(i,n){
		if(a[i]==b[i]){
			ans[i]=1;
			v--;
		}
	}
	if(v==0){
		cout<<0<<endl;
		return 0;
	}
//判定
	rep(i,q){
		ll x=V[i].first,y=V[i].second;
		ll B=max(a[x-1],a[y-1]),S=min(a[x-1],a[y-1]);
		a[min(x-1,y-1)]=S;
		a[max(x-1,y-1)]=B;
		if(a[x-1]==b[x-1]){
			v-=1-ans[x-1];
			ans[x-1]=1;
		}else{
			v+=ans[x-1];
			ans[x-1]=0;
		}
		if(a[y-1]==b[y-1]){
			v-=1-ans[y-1];
			ans[y-1]=1;
		}else{
			v+=ans[y-1];
			ans[y-1]=0;
		}
		if(v==0){
			cout<<i+1<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;

	return 0;

}
accuracy: 0.34615384615384615
time_ms: 40
memory_kb: 14612
final_rank: 20

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
#define rep(i,n) for(ll i=0; i<n; i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define print(a) cout<<a<<endl
typedef long long ll;
#define yn(flg) if(flg){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YN(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define so(a) sort(a.begin(),a.end())
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define pb push_back
#define a2i(a,s) (ll)(a-s)
#define i2a(s,a) (char)(s+a)
#define ssize(a) a.size()
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<pair<ll,ll>,ll> P3;
typedef pair<pair<ll,ll>,pair<ll,ll>> P4;

typedef pair<ll, ll> Pll;
typedef pair<ll,Pll> Plll;
typedef pair<Pii, int> Piii;
const ll INF = 1000000000000000000;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//vector<ll> saiki(ll bit,ll n){
//	vector<ll> s(n);
//	for(ll i=0;i<n;i++){
//		if(bit&(1<<i)){
//			s[i]=1;
//		}else s[i]=0;
//	}
//	return s;
//}
int main(){
//入力

	cin.tie(0);
	ios::sync_with_stdio(0);

	ll n;
	cin>>n;
	vector<ll> a(n);
	vector<ll> b(n);

	rep(i,n){
		cin>>a[i];
		b[i]=a[i];
	}
	ll q;
	cin>>q;

//ソート
	sort(b.begin(),b.end());
	vector<ll> ans(n);
	ll v=n;
	rep(i,n){
		if(a[i]==b[i]){
			ans[i]=1;
			v--;
		}
	}
	if(v==0){
		cout<<0<<endl;
		return 0;
	}
//判定
	rep(i,q){
		ll x,y;
		cin>>x>>y;
		ll B=max(a[x-1],a[y-1]),S=min(a[x-1],a[y-1]);
		a[min(x-1,y-1)]=S;
		a[max(x-1,y-1)]=B;
		if(a[x-1]==b[x-1]){
			v-=1-ans[x-1];
			ans[x-1]=1;
		}else{
			v+=ans[x-1];
			ans[x-1]=0;
		}
		if(a[y-1]==b[y-1]){
			v-=1-ans[y-1];
			ans[y-1]=1;
		}else{
			v+=ans[y-1];
			ans[y-1]=0;
		}
		if(v==0){
			cout<<i+1<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;

	return 0;

}
accuracy: 0.34615384615384615
time_ms: 20
memory_kb: 9992
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(T) T.begin(), T.end()
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
    // bozo sort
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    ll q;
    cin >> q;
    vector<ll> b(n);
    rep(i, n) b[i] = a[i];
    sort(all(b));

    ll now = 0;
    rep(i, n) {
        if (b[i] != a[i]) now++;
    }
    if (now == 0) {
        cout << 0 << endl;
        return 0;
    }
    rep(qi, q) {
        ll x, y;
        cin >> x >> y;
        x--;
        y--;

        ll old = 0, next = 0;
        if (b[x] == a[x]) old++;
        if (b[y] == a[y]) old++;
        swap(a[x], a[y]);
        if (b[x] == a[x]) next++;
        if (b[y] == a[y]) next++;
        ll diff = next - old;  // あたらしくそろった数
        now -= diff;
        if (now == 0) {
            cout << qi + 1 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
accuracy: 1
time_ms: 130
memory_kb: 8052
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3") 
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x) 
#endif
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  __MAKE_TIME__  Problem: __PROBLEM__ / Link: __CONTEST_URL__  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */



int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	LL N; cin >> N;
	VL a(N);
	FOR(i, 0, N) {
		cin >> a[i];
	}
	VL b(a);
	SORT(b);
	int Q; cin >> Q;
	VL x(Q), y(Q);
	FOR(i, 0, Q) {
		cin >> x[i] >> y[i];
		x[i]--, y[i]--;
	}
	// 完成形は定まる
	VL oks(N, 0);
	FOR(i, 0, N) {
		oks[i] = (a[i] == b[i]);
	}
	LL oksum = accumulate(ALL(oks), 0LL);
	if (oksum == N) {
		cout << 0 << endl; return 0;
	}

	FOR(i, 0, Q) {
		oksum -= oks[x[i]];
		oksum -= oks[y[i]];

		swap(a[x[i]], a[y[i]]);
		oks[x[i]] = (a[x[i]] == b[x[i]]);
		oks[y[i]] = (a[y[i]] == b[y[i]]);

		oksum += oks[x[i]];
		oksum += oks[y[i]];

		if (oksum == N) {
			cout << i + 1 << endl; return 0;
		}
	}

	//
	cout << -1 << endl;

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 14896
final_rank: 15

code_snippet: // 基本
#include <bits/stdc++.h>
using namespace std;
// 型名の省略
using ll = long long;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
// 定数
constexpr long long MOD = 1000000007LL;
template <class T>
constexpr T INF = 0;
template <>
constexpr int INF<int> = 1001001001;
template <>
constexpr long long INF<long long> = 1LL << 61;
template <>
constexpr double INF<double> = INF<long long>;
template <>
constexpr long double INF<long double> = INF<long long>;
// 便利関数
// 入出力処理
template <class... T>
void input(T &...a)
{
    (cin >> ... >> a);
}
void print()
{
    cout << '\n';
}
template <class T, class... Ts>
void print(const T &a, const Ts &...b)
{
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';
}
// 繰り返し処理
#define overload4(a, b, c, d, e, ...) e
#define rep1(a) for (int i = 0; i < a; i++)
#define rep2(i, a) for (int i = 0; i < a; i++)
#define rep3(i, a, b) for (int i = a; i < b; i++)
#define rep4(i, a, b, c) for (int i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
// 配列処理
template <class T>
bool operator==(const vector<T> &a, const vector<T> &b)
{
    return (a.size() == b.size()) && equal(a.cbegin(), a.cend(), b.cbegin());
}
template <class T>
bool operator!=(const vector<T> &a, const vector<T> &b)
{
    return !(a == b);
}
// 最小・最大処理
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class... Args>
inline auto all_max(Args... args)
{
    return max(initializer_list<common_type_t<Args...>>{args...});
}
template <class... Args>
inline auto all_min(Args... args)
{
    return min(initializer_list<common_type_t<Args...>>{args...});
}
// 追加ライブラリ

/// ここからコードを書く

int main()
{
    cin.tie(0);
    int n;
    input(n);
    vc<int> a(n), a_sorted(n);
    rep(i, n)
    {
        input(a[i]);
    }
    a_sorted = a;
    sort(a_sorted.begin(), a_sorted.end());
    int cur = 0;
    rep(i, n)
    {
        if (a[i] == a_sorted[i])
        {
            cur++;
        }
    }
    if (cur == n)
    {
        print(0);
        return 0;
    }
    int q;
    input(q);
    rep(iq, q)
    {
        int x, y;
        input(x, y);
        x--;
        y--;
        int xi = (a[x] == a_sorted[x] ? 1 : 0);
        int yi = (a[y] == a_sorted[y] ? 1 : 0);
        int temp = a[x];
        a[x] = a[y];
        a[y] = temp;
        int xj = (a[x] == a_sorted[x] ? 1 : 0);
        int yj = (a[y] == a_sorted[y] ? 1 : 0);
        if (xi == 1 && xj == 0)
        {
            cur--;
        }
        else if (xi == 0 && xj == 1)
        {
            cur++;
        }
        if (yi == 1 && yj == 0)
        {
            cur--;
        }
        else if (yi == 0 && yj == 1)
        {
            cur++;
        }
        if (cur == n)
        {
            print(iq + 1);
            return 0;
        }
    }
    print(-1);
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 5632
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int INF = 1 << 30;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    b = a;
    sort(b.begin(), b.end());
    int q;
    cin >> q;

    int ans = 0, ok = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] == b[i]) ok++;
    }
    if (ok == n) {
        cout << ans << endl;
        return 0;
    }
    // ok = 2
    while (q--) {
        ans++;
        int x, y;
        cin >> x >> y;
        x--, y--;

        if (a[x] == b[x]) ok--;
        if (a[y] == b[y]) ok--;
        swap(a[x], a[y]);
        if (a[x] == b[x]) ok++;
        if (a[y] == b[y]) ok++;
        if (ok == n) {
            cout << ans << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5760
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INF = 1e9 + 1;
const ll INFL = 1e18 + 1;
const double PI = 3.141592653589793238;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";

const vector<int> dx = {-1, 0, 1, 0};
const vector<int> dy = {0, 1, 0, -1};
const vector<int> dx8 = {-1, 0, 1, 0, -1, -1, 1, 1};
const vector<int> dy8 = {0, 1, 0, -1, -1, 1, 1, -1};
bool inRange(int i, int N) { return (0 <= i && i < N); }
bool inField(int i, int j, int H, int W) { return (0 <= i && i < H && 0 <= j && j < W); }
int encode(int i, int j, int W) { return (i * W + j); }

#define euclid(x1, y1, x2, y2) (((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
#define manhattan(x1, y1, x2, y2) (abs((x1) - (x2)) + abs((y1) - (y2)))

int randInt(int a, int b) { return a + rand() % (b - a + 1); }
double randDouble() { return 1.0 * rand() / RAND_MAX; }

int op(int a, int b) { return max(a, b); }
int e() { return -INF; }

#define rep(i, n) for(int i = 0;i < (int)(n); i++)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define chmin(a, b) a = min(a,b)
#define chmax(a, b) a = max(a,b)
#define sz(a) ((int) a.size())
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YN(a) cout << ((a) ? "Yes" : "No") << endl

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int N;
    cin >> N;

    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    int Q;
    cin >> Q;

    vector<int> X(Q), Y(Q);
    for (int i = 0; i < Q; i++) {
        cin >> X[i] >> Y[i];
        X[i]--; Y[i]--;
    }

    // 前処理
    vector<int> B = A;
    sort(B.begin(), B.end());

    // same := 「A[i] = B[i]」となるiの個数
    int same = 0;
    for (int i = 0; i < N; i++) {
        if (A[i] == B[i]) same++;
    }

    // クエリ処理
    if (same == N) {
        cout << 0 << endl;
        return 0;
    }

    for (int i = 0; i < Q; i++) {
        if (A[X[i]] == B[X[i]]) same--;
        if (A[Y[i]] == B[Y[i]]) same--;
        if (A[Y[i]] == B[X[i]]) same++;
        if (A[X[i]] == B[Y[i]]) same++;
        swap(A[X[i]], A[Y[i]]);

        if (same == N) {
            cout << i + 1 << endl;
            return 0;
        }
    }

    cout << -1 << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 7804
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;

template <typename T>
using vc = vector<T>;
template <typename T>
using vv = vc<vc<T>>;

//-------------1.型系---------------
using ll = long long;
ll INF = 2e18;

using ld = long double;
using bl = bool;
using mint = modint998244353;
// using mint = modint1000000007;
//   using mint = modint;
//   mint::set_mod(m);

template <class T>
using pq = priority_queue<T, vc<T>>;
template <class T>
using pq_g = priority_queue<T, vc<T>, greater<T>>;
//-----------------------------------

//-------------2.配列系--------------

using pii = pair<int, int>;
using pll = pair<long long, long long>;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
template <class T>
istream &operator>>(istream &i, vc<T> &v)
{
    rep(j, size(v)) i >> v[j];
    return i;
}

using ar2 = array<ll, 2>;

//----------------------------------

//--------3.コード短縮化とか---------
const double PI = 3.141592653589793238;
const int inf = 1073741823;
const ll infl = 1LL << 60;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define nfor(i, s, n) for (ll i = s; i < n; i++)
#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--)

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YN                     \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    } // if(a==b)YN;

#define vc_unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define vc_rotate(v) rotate(v.begin(), v.begin() + 1, v.end());

#define pop_cnt(s) ll(popcount(uint64_t(s)))

#define next_p(v) next_permutation(v.begin(), v.end())

//-------------------------------

//---------4.グリッド系----------
vector<int> dx = {1, 0, -1, 0}; //  dx={1,1,0,-1,-1,-1,0,1};
vector<int> dy = {0, 1, 0, -1}; //  dy={0,1,1,1,0,-1,-1,-1};

bool out_grid(ll i, ll j, ll h, ll w = -1)
{
    if (w == -1)
    {
        w = h;
    }
    return (!(0 <= i && i < h && 0 <= j && j < w));
}

#define vvl_rotate(v)                                   \
    {                                                   \
        ll n = size(v);                                 \
        vvl nx(n, vl(n));                               \
        rep(i, n) rep(j, n) nx[j][n - i - 1] = v[i][j]; \
        swap(nx, v);                                    \
    } // 時計回りに90°回転
// #define vvl_rotate(v) {ll n = size(v);vvl
// nx(n,vl(n));rep(i,n)rep(j,n)nx[n-j-1][i]=v[i][j];swap(nx,v);}//反時計周りに90°回転

#define vs_rotate(v)                                    \
    {                                                   \
        ll n = size(v);                                 \
        vs nx(n, string(n, '.'));                       \
        rep(i, n) rep(j, n) nx[j][n - i - 1] = v[i][j]; \
        swap(nx, v);                                    \
    } // 文字列版 時計回りに90°回転
// #define vs_rotate(v) {ll n = size(v);vs
// nx(n,string(n,'.'));rep(i,n)rep(j,n)nx[n-j-1][i]=v[i][j];swap(nx,v);}//文字列版　反時計周りに90°回転

#define vvl_transpos(v)                         \
    {                                           \
        ll n = size(v);                         \
        vvl nx(n, vl(n));                       \
        rep(i, n) rep(j, n) nx[j][i] = v[i][j]; \
        swap(nx, v);                            \
    }
#define vs_transpos(v)                          \
    {                                           \
        ll n = size(v);                         \
        vs nx(n, string(n, '.'));               \
        rep(i, n) rep(j, n) nx[j][i] = v[i][j]; \
        swap(nx, v);                            \
    }

//--------------------------------

//-----------5.数学系--------------
#define euclid(x, y) ((x) * (x) + (y) * (y)) // ユークリッド距離 2乗のまま
#define manhattan(x1, x2, y1, y2) \
    (abs(x1 - x2) + abs(y1 - y2)) // マンハッタン距離 = |x1-x2|+|y1-y2|

template <class T>
T tousa_sum1(T l, T d, T r)
{ // 初項,公差,末項 で総和を求める
    T wide = (r - l) / d + 1;
    return (l + r) * wide / 2;
}
template <class T>
T tousa_sum2(T a, T d, T n)
{ // 初項,交差,項数 で総和を求める
    return (a * 2 + d * (n - 1)) * n / 2;
}
ll kousa_kousuu(ll l, ll r, ll d)
{ // 初項,末項,交差 で等差数列の項数を求める
    return (r - l) / d + 1;
}
mint touhi_sum(mint a, mint r,
               ll n)
{ // 初項,公比,項数で等比数列の総和を求める
    if (r == 1)
    {
        return a * n;
    }
    mint bunsi = a * (r.pow(n) - mint(1));
    mint bunbo = r - 1;
    return bunsi / bunbo;
}

ll nc2(ll x) { return x * (x - 1) / 2; }
ll nc3(ll x) { return x * (x - 1) * (x - 2) / 6; }

//----------------------------------------------

//-----------6.デバックや出力系------------------
void print(ld x) { printf("%.20Lf\n", x); }

#define print_vec(v)                   \
    {                                  \
        ll n = size(v);                \
        rep(i, n) cout << v[i] << " "; \
        cout << endl;                  \
    } // 一次元配列を出力する(改行なし)

#define vc_cout(v)                      \
    {                                   \
        ll n = size(v);                 \
        rep(i, n) cout << v[i] << endl; \
    } // 一次元配列を出力する(改行あり)
#define vv_cout(v)                                         \
    {                                                      \
        ll n = size(v);                                    \
        rep(i, n)                                          \
        {                                                  \
            rep(j, size(v[i])) { cout << v[i][j] << ' '; } \
            cout << endl;                                  \
        }                                                  \
    } // 二次元配列を出力する
//----------------------------------------------

long long modpow(long long a, long long n, long long mod)
{
    long long res = 1;
    while (n > 0)
    {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(10);

    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    rep(i, n) cin >> a[i].first, a[i].second = i;

    sort(all(a));
    vector<int> p(n);
    rep(i, n) p[a[i].second] = a[i].first;

    map<int, pair<int, int>> mp;
    rep(i, n)
    {
        int l = lower_bound(all(a), make_pair(a[i].first, -1)) - a.begin();
        int r = lower_bound(all(a), make_pair(a[i].first, n)) - a.begin();
        mp[a[i].first].first = l, mp[a[i].first].second = r;
    }

    int ans = 0;
    rep(i, n) if (mp[p[i]].first <= i && i < mp[p[i]].second) ans++;

    bool f = false;
    if (ans == n)
    {
        cout << 0 << endl;
        f = true;
    }
    int q;
    cin >> q;
    rep(i, q)
    {
        int x, y;
        cin >> x >> y, x--, y--;
        if (f)
            continue;
        if (mp[p[x]].first <= x && x < mp[p[x]].second)
            ans--;
        if (mp[p[y]].first <= y && y < mp[p[y]].second)
            ans--;
        swap(p[x], p[y]);
        if (mp[p[x]].first <= x && x < mp[p[x]].second)
            ans++;
        if (mp[p[y]].first <= y && y < mp[p[y]].second)
            ans++;
        if (ans == n)
        {
            cout << i + 1 << endl;
            f = true;
        }
    }
    if (!f)
        cout << -1 << endl;
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 25740
final_rank: 17

code_snippet: #ifdef GOHACHI
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
template<class T> using VC = vector<T>;
template<class T,class TT> using P = pair<T,TT>;
template<class T,class TT,class TTT> using TP = tuple<T,TT,TTT>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> using pqR = priority_queue<T>;
#define F first
#define S second
#define INF LLONG_MAX
#define MOD 998244353
ll DX[4] = { 0 , 1 , 0 , -1 };
ll DY[4] = { -1 , 0 , 1 , 0 };
#define st(x,y) sort(x,x+y)
#define stR(x,y) sort(x,x+y,greater<ll>())
#define ST(x) sort(x.begin(),x.end())
#define STR(x) sort(x.rbegin(),x.rend())
#define rep(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define repR(i,a,b) for(ll i=(ll)(a);i>=(ll)(b);i--)
#define rev(x,y) reverse(x,x+y)
#define REV(x) reverse(x.begin(),x.end())
#define dec(x) fixed << setprecision(x)
#define POW(x,y) (ll)pow(x,y)
ll c,cc=-1;
void solve() {
	ll n; cin>>n;
	ll a[n],p[n];
	rep(i,0,n) { cin>>a[i]; p[i]=a[i]; }
	st(p,n);
	rep(i,0,n) if (a[i]!=p[i]) c++;
	ll q; cin>>q;
	ll x,y;
	if (c==0) cc=0;
	else {
		rep(i,0,q) { cin>>x>>y;
			x--; y--;
			ll xa=a[x];
			ll yb=a[y];
			swap(a[x],a[y]);
			if (xa!=p[x] && a[x]==p[x]) c--;
			else if (xa==p[x] && a[x]!=p[x]) c++;
			if (yb!=p[y] && a[y]==p[y]) c--;
			else if (yb==p[y] && a[y]!=p[y]) c++;
			if (c==0) { cc=i+1; break; }
		}
	}
	cout<<cc<<'\n';
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T=1;
    while(T--) solve();
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 8080
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n,a[300001],q,x,y,i,b[300001],flg,cnt,lg;
int main(void){
	cnt=0;
	flg=1;
	lg=0;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b,b+n);
	for(i=0;i<n;i++){
		if(a[i]!=b[i]){
			flg=0;
			cnt++;
		}
	}
		cin>>q;
		for(i=0;i<q;i++){
			cin>>x>>y;
			x--;
			y--;
			if(a[x]==b[x]) cnt++;
			if(a[x]==b[y]) cnt--;
			if(a[y]==b[y]) cnt++;
			if(a[y]==b[x]) cnt--;
			if(cnt==0 && flg==0){
				flg=1;
				lg=i+1;
			}
			swap(a[x],a[y]);
		}
	if(flg==0) cout<<-1<<endl;
	else cout<<lg<<endl;
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 5800
final_rank: 7