Submission #1326092

#TimeUsernameProblemLanguageResultExecution timeMemory
1326092SmuggingSpunSorting (IOI15_sorting)C++20
54 / 100
89 ms668 KiB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
const int lim = 2e5 + 5;
int n, m, s[lim], x[lim * 3], y[lim * 3];
bool check_sorted(){
  for(int i = 0; i < n; i++){
    if(s[i] != i){
      return false;
    }
  }
  return true;
}
namespace sub12{
  int solve(int p[], int q[]){
    int ans = 0;
    for(int i = 0; i < n; i++){
      if(s[i] != i){
        swap(s[p[ans] = i], s[q[ans] = s[i]]);
        ans++;
        i--;
      }
    }
    return ans;
  }
}
namespace sub3{
  int solve(int p[], int q[]){
    int ans = 0;
    for(int x = 2; x < n; x++){
      int i = find(s, s + n, x) - s;
      swap(s[0], s[1]);
      if(i == 0){
        i = 1;
      }
      else if(i == 1){
        i = 0;
      }
      swap(s[p[ans] = i], s[q[ans] = x]);
      ans++;
    }
    if(s[0] > s[1]){
      p[ans] = q[ans] = 0;
      ans++;
    }
    return ans;
  }
}
namespace sub4{
  int solve(int p[], int q[]){
    memset(p, 0, sizeof(p));
    memset(q, 0, sizeof(q));
    vector<int>pos(n);
    for(int i = 0; i < m; i++){
      iota(pos.begin(), pos.end(), 0);
      for(int j = i + 1; j < m; j++){
        swap(pos[x[j]], pos[y[j]]);
      }
      swap(s[x[i]], s[y[i]]);
      for(int j = 0; j < n; j++){
        if(s[pos[j]] != j){
          swap(s[p[i] = pos[j]], s[q[i] = find(s, s + n, j) - s]);
          break;
        }
      }
    }
    return m;
  }
}
namespace sub5{
  int solve(int p[], int q[]){
    vector<int>pos(n);
    int low = 0, high = m - 1, ans;
    while(low <= high){
      int mid = (low + high) >> 1, cnt = 0;
      iota(pos.begin(), pos.end(), 0);
      for(int j = 0; j < m; j++){
        swap(pos[x[j]], pos[y[j]]);
      }
      vector<int>S(s, s + n);
      for(int j = 0; j < n; j++){
        if(S[pos[j]] != j){
          swap(S[pos[j]], S[find(S.begin(), S.end(), j) - S.begin()]);
          cnt++;
        }
      }
      if(cnt <= mid){
        high = (ans = mid) - 1;
      }
      else{
        low = mid + 1;
      }
    }
    memset(p, 0, sizeof(p));
    memset(q, 0, sizeof(q));
    for(int i = 0; i < ans; i++){
      iota(pos.begin(), pos.end(), 0);
      for(int j = i + 1; j < m; j++){
        swap(pos[x[j]], pos[y[j]]);
      }
      swap(s[x[i]], s[y[i]]);
      for(int j = 0; j < n; j++){
        if(s[pos[j]] != j){
          swap(s[p[i] = pos[j]], s[q[i] = find(s, s + n, j) - s]);
          break;
        }
      }
    }
    return ans;
  }
}
namespace sub6{
  int solve(int p[], int q[]){

  }
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]){
  memcpy(s, S, sizeof(int) * (n = N));
  memcpy(x, X, sizeof(int) * (m = M));
  memcpy(y, Y, sizeof(int) * m);
  if(n == 1){
    return 0;
  }
  if(m == 30 * n){
    int mx = *max_element(x, x + m), my = *max_element(y, y + m);
    if(mx == 0){
      if(my == 0){
        return sub12::solve(P, Q);
      }
      if(my == 1){
        return sub3::solve(P, Q);
      }
    }
    return sub4::solve(P, Q);
  }
  if(n <= 2000){
    return sub5::solve(P, Q);
  }
  return sub6::solve(P, Q);
}

Compilation message (stderr)

sorting.cpp: In function 'int sub4::solve(int*, int*)':
sorting.cpp:51:25: warning: 'sizeof' on array function parameter 'p' will return size of 'int*' [-Wsizeof-array-argument]
   51 |     memset(p, 0, sizeof(p));
      |                        ~^~
sorting.cpp:50:17: note: declared here
   50 |   int solve(int p[], int q[]){
      |             ~~~~^~~
sorting.cpp:52:25: warning: 'sizeof' on array function parameter 'q' will return size of 'int*' [-Wsizeof-array-argument]
   52 |     memset(q, 0, sizeof(q));
      |                        ~^~
sorting.cpp:50:26: note: declared here
   50 |   int solve(int p[], int q[]){
      |                      ~~~~^~~
sorting.cpp: In function 'int sub5::solve(int*, int*)':
sorting.cpp:94:25: warning: 'sizeof' on array function parameter 'p' will return size of 'int*' [-Wsizeof-array-argument]
   94 |     memset(p, 0, sizeof(p));
      |                        ~^~
sorting.cpp:71:17: note: declared here
   71 |   int solve(int p[], int q[]){
      |             ~~~~^~~
sorting.cpp:95:25: warning: 'sizeof' on array function parameter 'q' will return size of 'int*' [-Wsizeof-array-argument]
   95 |     memset(q, 0, sizeof(q));
      |                        ~^~
sorting.cpp:71:26: note: declared here
   71 |   int solve(int p[], int q[]){
      |                      ~~~~^~~
sorting.cpp: In function 'int sub6::solve(int*, int*)':
sorting.cpp:115:3: warning: no return statement in function returning non-void [-Wreturn-type]
  115 |   }
      |   ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...