This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
// #define int long long
#define endl '\n'
const int mxn = 501;
const int mod = 1e8;
// int dx[] = {0 , 0 , 1 , -1};
// int dy[] = {1 , -1 , 0 , 0};
//DFS
//----------------------------------
// vector<int> adj[mxn];
// int n , m;
// bool vis[mxn];
// int P[mxn];
// int rem[mxn];
// void dfs(int i){
// vis[i] = 1;
// for(auto j : adj[i]){
// if(!vis[j]){
// P[j] = i;
// dfs(j);
// }
// }
// return;
// }
//----------------------------------
//Seg-Tree
//----------------------------------
// int Tree[1 << (int)(ceil(log2(mxn))) + 1];
// int N = 1 << (int)(ceil(log2(mxn)));
// int l ,r;
// int Search(int i , int lr , int rr){
// if(lr >= l and rr <= r){
// return Tree[i];
// }
// if(lr > r or rr < l) return -1;
// return max(Search(i * 2 , lr , (lr + rr) / 2) , Search(i * 2 + 1 , (lr + rr) / 2 + 1 , rr));
// }
// void update(int i){
// while(i /= 2){
// Tree[i] = max(Tree[i * 2] , Tree[i * 2 + 1]);
// }
// return;
// }
//----------------------------------
//comp
//----------------------------------
// map<int , int>mp;
// map<int , int>pm;
// void comp(set<int>s)
// {
// int idx = 0;
// for(auto i : s){
// mp[i] = idx;
// pm[idx] = i;
// idx ++;
// }
// }
//----------------------------------
int n , s;
int A[300];
vector<array<int , 2>>ans;
map<int , int>mp;
int main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> s;
int b[n];
for(int i = 0 ; i < n; i++){
cin >> A[i];
b[i] = A[i];
}
sort(b , b + n);
for(int i = 0 ; i < n; i++){
mp[b[i]] = i;
}
for(int i = n - 1; i >= 0 ; i--){
if(i != mp[A[i]]){
if(i < mp[A[i]]){
for(int j = i ; j < mp[A[i]] ; j++){
swap(A[j] , A[j + 1]);
ans.push_back({j , j + 1});
ans.push_back({j + 1 , j});
ans.push_back({j , j + 1});
}
}
else {
for(int j = i ; j > mp[A[i]] ; j--){
swap(A[j] , A[j - 1]);
ans.push_back({j , j - 1});
ans.push_back({j - 1 , j});
ans.push_back({j , j - 1});
}
}
i++;
}
}
cout << ans.size() << endl;
for(int i = 0 ; i < ans.size() ; i++){
cout << ans[i][0] + 1 << " " << ans[i][1] + 1 << endl;
}
}
Compilation message (stderr)
xorsort.cpp: In function 'int main()':
xorsort.cpp:100:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int i = 0 ; i < ans.size() ; i++){
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |