답안 #172469

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
172469 2020-01-01T15:28:10 Z AlexLuchianov 정렬하기 (IOI15_sorting) C++14
100 / 100
908 ms 29800 KB
#include "sorting.h"
#include <iostream>

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 600000;
int xswap[1 + nmax], yswap[1 + nmax];
int init[1 + nmax];
int pswap[1 + nmax], qswap[1 + nmax];
int n;
int v[1 + nmax], f[1 + nmax], invf[1 + nmax], freq[1 + nmax];

int test(int moves){
  for(int i = 0; i < moves; i++)
    pswap[i] = qswap[i] = 0;
  for(int i = 0; i < n; i++)
    v[i] = init[i];
  for(int i = 0; i < n; i++) {
    f[i] = i;
    invf[i] = i;
    freq[v[i]] = i;
  }
  for(int i = moves - 1; 0 <= i; i--) {
    std::swap(f[xswap[i]], f[yswap[i]]);
    invf[f[xswap[i]]] = xswap[i];
    invf[f[yswap[i]]] = yswap[i];
  }
  int ptr = 0;

  for(int i = 0; i < moves; i++){
    std::swap(f[xswap[i]], f[yswap[i]]);

    invf[f[xswap[i]]] = xswap[i];
    invf[f[yswap[i]]] = yswap[i];

    std::swap(v[xswap[i]], v[yswap[i]] );
    freq[v[xswap[i]]] = xswap[i];
    freq[v[yswap[i]]] = yswap[i];

    if(ptr < n){
      while(ptr < n && v[invf[ptr]] == ptr)
        ptr++;
      if(ptr < n){
        pswap[i] = invf[ptr];
        qswap[i] = freq[ptr];
        std::swap(v[pswap[i]], v[qswap[i]] );
        freq[v[pswap[i]]] = pswap[i];
        freq[v[qswap[i]]] = qswap[i];
        ptr++;
      }
    }
  }
  while(ptr < n && v[invf[ptr]] == ptr)
    ptr++;
  return ptr == n;
}

int binarysearch(int from, int to){
  if(from < to){
    int mid = (from + to) / 2;
    if(test(mid))
      return binarysearch(from, mid);
    else
      return binarysearch(mid + 1, to);
  } else
    return from;
}

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++)
    init[i] = S[i];
  for(int i = 0; i < M; i++) {
    xswap[i] = X[i];
    yswap[i] = Y[i];
  }
  int pos = binarysearch(0, M);
  test(pos);
  for(int i = 0; i < pos; i++) {
    P[i] = pswap[i];
    Q[i] = qswap[i];
	}
	return pos;
}
/*
5
4 3 2 1 0
6
0 1
1 2
2 3
3 4
0 1
1 2
*/

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 760 KB Output is correct
22 Correct 3 ms 636 KB Output is correct
23 Correct 3 ms 760 KB Output is correct
24 Correct 3 ms 636 KB Output is correct
25 Correct 3 ms 760 KB Output is correct
26 Correct 3 ms 632 KB Output is correct
27 Correct 3 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 4 ms 636 KB Output is correct
3 Correct 4 ms 508 KB Output is correct
4 Correct 3 ms 760 KB Output is correct
5 Correct 3 ms 636 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 4 ms 636 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 4 ms 632 KB Output is correct
11 Correct 4 ms 632 KB Output is correct
12 Correct 3 ms 632 KB Output is correct
13 Correct 4 ms 632 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 4 ms 636 KB Output is correct
3 Correct 4 ms 508 KB Output is correct
4 Correct 3 ms 760 KB Output is correct
5 Correct 3 ms 636 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 4 ms 636 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 4 ms 632 KB Output is correct
11 Correct 4 ms 632 KB Output is correct
12 Correct 3 ms 632 KB Output is correct
13 Correct 4 ms 632 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 401 ms 18708 KB Output is correct
16 Correct 561 ms 27128 KB Output is correct
17 Correct 744 ms 28396 KB Output is correct
18 Correct 131 ms 23544 KB Output is correct
19 Correct 349 ms 26916 KB Output is correct
20 Correct 357 ms 27768 KB Output is correct
21 Correct 358 ms 27768 KB Output is correct
22 Correct 393 ms 23932 KB Output is correct
23 Correct 445 ms 29556 KB Output is correct
24 Correct 712 ms 29120 KB Output is correct
25 Correct 693 ms 28664 KB Output is correct
26 Correct 455 ms 27868 KB Output is correct
27 Correct 393 ms 27000 KB Output is correct
28 Correct 680 ms 28920 KB Output is correct
29 Correct 604 ms 28280 KB Output is correct
30 Correct 312 ms 26360 KB Output is correct
31 Correct 621 ms 28892 KB Output is correct
32 Correct 416 ms 28536 KB Output is correct
33 Correct 723 ms 28920 KB Output is correct
34 Correct 612 ms 28920 KB Output is correct
35 Correct 351 ms 26616 KB Output is correct
36 Correct 84 ms 25340 KB Output is correct
37 Correct 637 ms 29800 KB Output is correct
38 Correct 908 ms 28792 KB Output is correct
39 Correct 630 ms 28920 KB Output is correct