답안 #1089731

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089731 2024-09-17T04:37:27 Z vjudge1 Xor Sort (eJOI20_xorsort) C++17
100 / 100
6 ms 1056 KB
/* https://oj.uz/problem/view/eJOI20_xorsort?locale=en: WA */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <limits.h>

unsigned int N, S, A[1000];
std::vector<std::pair<unsigned int, unsigned int>> operations;

void destroysort(void) {
  unsigned int i, j, mask;

  for (j = N; j > 1; --j) {
    if (j == 0) return;
    mask = 0;
    for (i = 0; i < j; ++i)
      mask |= A[i];
    if (mask == 0) return;
    mask = 1 << (sizeof(mask) * CHAR_BIT - __builtin_clz(mask) - 1);
    for (i = 0; i < j - 1; ++i) {
      if (A[i] & mask) {
        if (!(A[i+1] & mask)) {
          A[i+1] ^= A[i];
          operations.push_back({i+1, i});
        }
        A[i] ^= A[i+1];
        operations.push_back({i, i+1});
      }
    }
  }
}

void cursedbubblesort(void) {
  unsigned int i, j, k;

  for (j = N; j > 1; --j) {
    k = 0;
    for (i = 1; i < j; ++i)
      if (A[i] > A[k])
        k = i;
    if (k == j-1) {
      operations.push_back({j-2, j-1});
    } else {
      for (i = k; i < j - 1; ++i)
        operations.push_back({i+1, i});
      for (i = k; i > 1; --i)
        operations.push_back({i-2, i-1});
      for (i = 0; i < j - 1; ++i)
        operations.push_back({i, i+1});
      memmove(&A[k], &A[k+1], sizeof(*A)*(j-k-1));
    }
  }
}

int main(void) {
  (void) scanf("%u %u", &N, &S);
  for (unsigned int i = 0; i < N; ++i)
    (void) scanf(" %u", &A[i]);
  if (S == 1) {
    for (unsigned int i = 0; i < N - 1; ++i)
      operations.push_back({i, i+1});
    cursedbubblesort();
  } else {
    destroysort();
  }
  printf("%u\n", operations.size());
  for (auto operation : operations)
    printf("%u %u\n", operation.first + 1, operation.second + 1);
  return EXIT_SUCCESS;
}

Compilation message

xorsort.cpp: In function 'int main()':
xorsort.cpp:68:12: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'std::vector<std::pair<unsigned int, unsigned int> >::size_type' {aka 'long unsigned int'} [-Wformat=]
   68 |   printf("%u\n", operations.size());
      |           ~^     ~~~~~~~~~~~~~~~~~
      |            |                    |
      |            unsigned int         std::vector<std::pair<unsigned int, unsigned int> >::size_type {aka long unsigned int}
      |           %lu
xorsort.cpp:58:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |   (void) scanf("%u %u", &N, &S);
      |          ~~~~~^~~~~~~~~~~~~~~~~
xorsort.cpp:60:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |     (void) scanf(" %u", &A[i]);
      |            ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 728 KB Output is correct
5 Correct 2 ms 728 KB Output is correct
6 Correct 2 ms 728 KB Output is correct
7 Correct 2 ms 728 KB Output is correct
8 Correct 2 ms 728 KB Output is correct
9 Correct 2 ms 728 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 868 KB Output is correct
13 Correct 4 ms 728 KB Output is correct
14 Correct 2 ms 728 KB Output is correct
15 Correct 2 ms 728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 728 KB Output is correct
5 Correct 2 ms 728 KB Output is correct
6 Correct 2 ms 728 KB Output is correct
7 Correct 2 ms 728 KB Output is correct
8 Correct 2 ms 728 KB Output is correct
9 Correct 2 ms 728 KB Output is correct
10 Correct 5 ms 724 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 868 KB Output is correct
13 Correct 4 ms 728 KB Output is correct
14 Correct 2 ms 728 KB Output is correct
15 Correct 2 ms 728 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 4 ms 980 KB Output is correct
19 Correct 6 ms 984 KB Output is correct
20 Correct 5 ms 1056 KB Output is correct
21 Correct 4 ms 980 KB Output is correct
22 Correct 4 ms 980 KB Output is correct
23 Correct 4 ms 984 KB Output is correct
24 Correct 4 ms 984 KB Output is correct
25 Correct 4 ms 1024 KB Output is correct
26 Correct 4 ms 984 KB Output is correct
27 Correct 4 ms 984 KB Output is correct
28 Correct 4 ms 984 KB Output is correct
29 Correct 6 ms 980 KB Output is correct
30 Correct 4 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 3 ms 984 KB Output is correct
6 Correct 3 ms 1028 KB Output is correct
7 Correct 3 ms 956 KB Output is correct
8 Correct 3 ms 996 KB Output is correct
9 Correct 3 ms 984 KB Output is correct
10 Correct 3 ms 808 KB Output is correct
11 Correct 3 ms 984 KB Output is correct
12 Correct 3 ms 984 KB Output is correct
13 Correct 3 ms 984 KB Output is correct
14 Correct 5 ms 984 KB Output is correct
15 Correct 4 ms 980 KB Output is correct