Submission #1040158

# Submission time Handle Problem Language Result Execution time Memory
1040158 2024-07-31T17:18:07 Z Zicrus Sorting (IOI15_sorting) C++17
20 / 100
2 ms 616 KB
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
 
typedef long long ll;
 
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    vector<ll> h(N);
    for (int i = 0; i < N; i++) h[i] = S[i];
 
    ll ooo = 0;
    for (int i = 0; i < N; i++) {
        if (h[i] != i) ooo++;
    }
    if (ooo == 0) return 0;
    
    vector<ll> revH(N);
    for (int i = 0; i < N; i++) {
        revH[h[i]] = i;
    }
 
    ll res1 =0;
    ll low = 1, high = N;
    while (low < high) {
        ll g = (low + high) / 2;
 
        ll res = 0;
        vector<ll> s = h;
        vector<ll> order = s;
        vector<ll> revOrd = revH, revS = revH;
        for (int j = 0; j < g; j++) {
            swap(order[X[j]], order[Y[j]]);
            swap(revOrd[order[X[j]]], revOrd[order[Y[j]]]);
        }
        int id1 = -1;
        for (int i = 0; i < g; i++) {
            swap(s[X[i]], s[Y[i]]);
            swap(revS[s[X[i]]], revS[s[Y[i]]]);
            int firstOut = -1, id = -1;
            for (int j = id1+1; j < N; j++) {
                if (order[j] != j) {
                    firstOut = order[id = j];
                    break;
                }
            }
            id1 = id;
            if (firstOut == -1) {
                P[res] = Q[res] = 0;
                res++;
            }
            else {
                ll iter = revOrd[id];
                ll v = order[iter];
 
                ll id1 = revS[firstOut];
                ll id2 = revS[v];
                swap(s[id1], s[id2]);
                swap(revS[s[id1]], revS[s[id2]]);
                swap(order[iter], order[id]);
                swap(revOrd[order[iter]], revOrd[order[id]]);
                P[res] = id1;
                Q[res] = id2;
                res++;
            }
        }
        ll ooo = 0;
        for (int i = 0; i < N; i++) {
            if (s[i] != i) ooo++;
        }
        if (ooo == 0) {
            res1 = res;
            high = g;
        }
        else {
            low = g+1;
        }
    }
	return res1;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:42:44: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
   42 |                     firstOut = order[id = j];
      |                                            ^
sorting.cpp:55:20: warning: declaration of 'id1' shadows a previous local [-Wshadow]
   55 |                 ll id1 = revS[firstOut];
      |                    ^~~
sorting.cpp:35:13: note: shadowed declaration is here
   35 |         int id1 = -1;
      |             ^~~
sorting.cpp:61:26: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   61 |                 P[res] = id1;
      |                          ^~~
sorting.cpp:62:26: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   62 |                 Q[res] = id2;
      |                          ^~~
sorting.cpp:66:12: warning: declaration of 'ooo' shadows a previous local [-Wshadow]
   66 |         ll ooo = 0;
      |            ^~~
sorting.cpp:11:8: note: shadowed declaration is here
   11 |     ll ooo = 0;
      |        ^~~
sorting.cpp:78:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   78 |  return res1;
      |         ^~~~
sorting.cpp:7:39: warning: unused parameter 'M' [-Wunused-parameter]
    7 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 424 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 424 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 372 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 424 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 372 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Incorrect 0 ms 348 KB Output isn't correct
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 616 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Incorrect 1 ms 600 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 616 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Incorrect 1 ms 600 KB Output isn't correct
13 Halted 0 ms 0 KB -