Submission #793728

# Submission time Handle Problem Language Result Execution time Memory
793728 2023-07-26T06:00:59 Z Jarif_Rahman Sorting (IOI15_sorting) C++17
74 / 100
1000 ms 18504 KB
#include "sorting.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

vector<pair<int, int>> get_swaps(vector<int> S){
    int n = S.size();
    vector<pair<int, int>> swaps;
    vector<int> pos(n);
    for(int i = 0; i < n; i++) pos[S[i]] = i;
    vector<bool> bl(n, 0);

    for(int i = 0; i < n; i++){
        if(bl[S[i]]) continue;
        if(S[i] == i){
            bl[i] = 1;
            continue;
        }
        int j = i;
        while(1){
            if(bl[S[j]]){
                bl[j] = 1;
                break;
            }
            swaps.pb({S[j], j});
            bl[j] = 1;
            j = S[j];
        }

    }

    return swaps;
}

int findSwapPairs(int n, int S[], int m, int X[], int Y[], int P[], int Q[]){
    bool sorted = 1;
    for(int i = 0; i < n; i++) sorted&=(S[i] == i);
    if(sorted) return 0;

    int k;
    int mn = 1e8;

    vector<int> s(n);
    for(int i = 0; i < n; i++) s[i] = S[i];

    for(k = 0; k < m; k++){
        swap(s[X[k]], s[Y[k]]);
        if(get_swaps(s).size() <= k+1) break;
    }

    vector<int> pos(n);
    for(int i = 0; i < n; i++) pos[S[i]] = i;

    auto swaps = get_swaps(s);
    for(int i = 0; i <= k; i++){
        swap(S[X[i]], S[Y[i]]);
        swap(pos[S[X[i]]], pos[S[Y[i]]]);

        if(i >= swaps.size()) P[i] = 0, Q[i] = 0;
        else{
            auto [x, y] = swaps[i];
            swap(pos[x], pos[y]);
            swap(S[pos[x]], S[pos[y]]);
            P[i] = pos[x], Q[i] = pos[y];
        }
    }

    return k+1;
}

Compilation message

sorting.cpp: In function 'std::vector<std::pair<int, int> > get_swaps(std::vector<int>)':
sorting.cpp:11:19: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   11 |     int n = S.size();
      |             ~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:52:32: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |         if(get_swaps(s).size() <= k+1) break;
      |            ~~~~~~~~~~~~~~~~~~~~^~~~~~
sorting.cpp:63:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         if(i >= swaps.size()) P[i] = 0, Q[i] = 0;
      |            ~~^~~~~~~~~~~~~~~
sorting.cpp:45:9: warning: unused variable 'mn' [-Wunused-variable]
   45 |     int mn = 1e8;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 3 ms 420 KB Output is correct
22 Correct 3 ms 468 KB Output is correct
23 Correct 4 ms 440 KB Output is correct
24 Correct 3 ms 416 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 472 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 340 KB Output is correct
2 Correct 37 ms 588 KB Output is correct
3 Correct 35 ms 456 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 4 ms 440 KB Output is correct
7 Correct 11 ms 468 KB Output is correct
8 Correct 35 ms 468 KB Output is correct
9 Correct 36 ms 444 KB Output is correct
10 Correct 37 ms 464 KB Output is correct
11 Correct 29 ms 456 KB Output is correct
12 Correct 11 ms 468 KB Output is correct
13 Correct 25 ms 476 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 340 KB Output is correct
2 Correct 37 ms 588 KB Output is correct
3 Correct 35 ms 456 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 4 ms 440 KB Output is correct
7 Correct 11 ms 468 KB Output is correct
8 Correct 35 ms 468 KB Output is correct
9 Correct 36 ms 444 KB Output is correct
10 Correct 37 ms 464 KB Output is correct
11 Correct 29 ms 456 KB Output is correct
12 Correct 11 ms 468 KB Output is correct
13 Correct 25 ms 476 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Execution timed out 1048 ms 18504 KB Time limit exceeded
16 Halted 0 ms 0 KB -