답안 #1089354

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089354 2024-09-16T10:41:58 Z ZyadH1 Xor Sort (eJOI20_xorsort) C++14
0 / 100
558 ms 524288 KB
#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[200];
vector<array<int , 2>>ans;
void swp (int a , int b){//from a to b
    if(a > b){
        for(int i = a ; i > b ; i--){
            ans.push_back({i , i - 1});
            ans.push_back({i - 1 , i});
            ans.push_back({i , i - 1});
            swap(A[i] , A[i - 1]);
        }
    }
    else {
        for(int i = a ; i < b ; i++){
            ans.push_back({i , i + 1});
            ans.push_back({i + 1 , i});
            ans.push_back({i , i + 1});
            swap(A[i] , A[i + 1]);
        }
    }
    return;
}
signed 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);
  map<int , int>mp;
  for(int i = 0 ; i < n; i++){
      mp[b[i]] = i;
  }
  for(int i = n - 1; i >= 0 ; i--){
      if(i != mp[A[i]]){
          swp(i , mp[A[i]]);
          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

xorsort.cpp: In function 'int main()':
xorsort.cpp:104: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]
  104 |   for(int i = 0 ; i < ans.size() ; i++){
      |                   ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 558 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 558 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Runtime error 544 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -