Submission #233228

# Submission time Handle Problem Language Result Execution time Memory
233228 2020-05-20T02:48:54 Z FieryPhoenix Sorting (IOI15_sorting) C++11
100 / 100
242 ms 34048 KB
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
#define INF 2001001001
#define MOD 1000000007

int N,S[200001];
int M,X[600001],Y[600001];
int arr[200001];
bool vis[200001];
int to[200001];
deque<pair<int,int>>q;
int pos[200001];

void dfs(int node){
  vis[node]=true;
  if (!vis[arr[node]]){
    q.push_back({node,arr[node]});
    dfs(arr[node]);
    //cout<<"Q: "<<arr[node]<<' '<<arr[to[node]]<<endl;
  }
}

bool check(int mid){
  q.clear();
  for (int i=0;i<N;i++){
    arr[i]=S[i];
    vis[i]=false;
  }
  for (int i=1;i<=mid;i++)
    swap(arr[X[i]],arr[Y[i]]);
 int comps=0;
  for (int i=0;i<N;i++){
    pos[arr[i]]=i;
    if (!vis[arr[i]]){
      comps++;
      q.push_back({i,arr[i]});
      dfs(arr[i]);
      q.pop_back();
    }
  }
  return N-comps<=mid;
}
int findSwapPairs(int n,int s[],int m, int x[], int y[], int P[], int Q[]){
  N=n;
  for (int i=0;i<N;i++)
    S[i]=s[i];
  M=m;
  for (int i=1;i<=M;i++){
    X[i]=x[i-1];
    Y[i]=y[i-1];
  }
  int low=-1,high=M+1;
  while (low+1<high){
    int mid=(low+high)/2;
    if (check(mid))
      high=mid;
    else
      low=mid;
  }
  check(high);
  for (int i=high;i>=1;i--){
    if (!q.empty()){
      P[i-1]=pos[q[0].first];
      Q[i-1]=pos[q[0].second];
      swap(pos[arr[X[i]]],pos[arr[Y[i]]]);
      swap(arr[X[i]],arr[Y[i]]);
      q.pop_front();
    }
  }
  for (int i=1;i<=high;i++){
    swap(S[X[i]],S[Y[i]]);
    swap(S[P[i-1]],S[Q[i-1]]);
  }
  for (int i=0;i<N;i++)
    if (S[i]!=i)
      assert(0);
  return high;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 6 ms 616 KB Output is correct
22 Correct 6 ms 768 KB Output is correct
23 Correct 6 ms 768 KB Output is correct
24 Correct 6 ms 640 KB Output is correct
25 Correct 5 ms 768 KB Output is correct
26 Correct 5 ms 768 KB Output is correct
27 Correct 5 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 6 ms 768 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 6 ms 640 KB Output is correct
6 Correct 6 ms 640 KB Output is correct
7 Correct 6 ms 660 KB Output is correct
8 Correct 7 ms 640 KB Output is correct
9 Correct 7 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 768 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 6 ms 640 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 6 ms 768 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 6 ms 640 KB Output is correct
6 Correct 6 ms 640 KB Output is correct
7 Correct 6 ms 660 KB Output is correct
8 Correct 7 ms 640 KB Output is correct
9 Correct 7 ms 640 KB Output is correct
10 Correct 6 ms 640 KB Output is correct
11 Correct 6 ms 768 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
13 Correct 6 ms 640 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 192 ms 29200 KB Output is correct
16 Correct 206 ms 30056 KB Output is correct
17 Correct 238 ms 32072 KB Output is correct
18 Correct 123 ms 24824 KB Output is correct
19 Correct 189 ms 28104 KB Output is correct
20 Correct 207 ms 27528 KB Output is correct
21 Correct 201 ms 29196 KB Output is correct
22 Correct 190 ms 24032 KB Output is correct
23 Correct 222 ms 33432 KB Output is correct
24 Correct 228 ms 32816 KB Output is correct
25 Correct 229 ms 32172 KB Output is correct
26 Correct 200 ms 25208 KB Output is correct
27 Correct 182 ms 23992 KB Output is correct
28 Correct 238 ms 31832 KB Output is correct
29 Correct 214 ms 25976 KB Output is correct
30 Correct 158 ms 23124 KB Output is correct
31 Correct 216 ms 26536 KB Output is correct
32 Correct 218 ms 32192 KB Output is correct
33 Correct 224 ms 32612 KB Output is correct
34 Correct 209 ms 27384 KB Output is correct
35 Correct 187 ms 27128 KB Output is correct
36 Correct 108 ms 21624 KB Output is correct
37 Correct 242 ms 34048 KB Output is correct
38 Correct 229 ms 32660 KB Output is correct
39 Correct 234 ms 32724 KB Output is correct