Submission #1083801

# Submission time Handle Problem Language Result Execution time Memory
1083801 2024-09-04T07:35:52 Z ZeroCool Sorting (IOI15_sorting) C++14
100 / 100
177 ms 32152 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

#define ar array
#define ll long long
#define ld long double

vector<ar<int, 2>> ans, xy, swaps;
bool check(vector<int> p, int m, int n) {
    int s = 0;
    vector<int> e = p;
    vector<int> co(n, 0);
    for (int i = 0; i < n; i++) co[p[i]] = i;
    
    for (int i = 0; i < m; i++) swap(p[xy[i][0]], p[xy[i][1]]);
    
    for (int i = 0; i < n; i++) {
        if (p[i] == i) continue;
        
        while(p[i] != i) {
            int j = p[i];
            swaps.push_back({p[i], p[j]});
            swap(p[i], p[j]);
            s++;
        }
    }
    if (swaps.size() > m)return 0;
    
    while(swaps.size() < m) swaps.push_back({0, 0});
    
    for (int i = 0; i < m; i++) {
        swap(co[e[xy[i][0]]], co[e[xy[i][1]]]);
        swap(e[xy[i][0]], e[xy[i][1]]);
        auto [x, y] = swaps[i];
        swaps[i] = {co[x], co[y]};
        swap(e[co[x]], e[co[y]]);
        swap(co[x], co[y]);
    }
    return s <= m;
}
int findSwapPairs(int n, int A[], int m, int X[], int Y[], int P[], int Q[]) {
    vector<int> p(n);
    for (int i = 0; i < n; i++) p[i] = A[i];
    
    for (int i = 0; i < m; i++) xy.push_back({X[i], Y[i]});
    int l = 0, r = m - 1;
    while(l <= r) {
        int m = (l + r)/2;
        swaps.clear();
        if(check(p, m, n)){
            r = m - 1;
            ans = swaps;
        }else l = m + 1;
        
    }
    for (int i = 0; i < ans.size(); i++) {
        P[i] = ans[i][0];
        Q[i] = ans[i][1];
    }
    return ans.size();
}

Compilation message

sorting.cpp: In function 'bool check(std::vector<int>, int, int)':
sorting.cpp:28:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   28 |     if (swaps.size() > m)return 0;
      |         ~~~~~~~~~~~~~^~~
sorting.cpp:30:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   30 |     while(swaps.size() < m) swaps.push_back({0, 0});
      |           ~~~~~~~~~~~~~^~~
sorting.cpp:35:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   35 |         auto [x, y] = swaps[i];
      |              ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:49:13: warning: declaration of 'int m' shadows a parameter [-Wshadow]
   49 |         int m = (l + r)/2;
      |             ^
sorting.cpp:42:39: note: shadowed declaration is here
   42 | int findSwapPairs(int n, int A[], int m, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
sorting.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int i = 0; i < ans.size(); i++) {
      |                     ~~^~~~~~~~~~~~
sorting.cpp:61:20: warning: conversion from 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   61 |     return ans.size();
      |            ~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 0 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 0 ms 436 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 1 ms 344 KB Output is correct
12 Correct 1 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 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 0 ms 436 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 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 712 KB Output is correct
23 Correct 2 ms 860 KB Output is correct
24 Correct 1 ms 824 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 856 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 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 708 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 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 708 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 144 ms 28252 KB Output is correct
16 Correct 160 ms 28652 KB Output is correct
17 Correct 166 ms 30564 KB Output is correct
18 Correct 69 ms 26840 KB Output is correct
19 Correct 112 ms 29360 KB Output is correct
20 Correct 129 ms 30304 KB Output is correct
21 Correct 124 ms 30368 KB Output is correct
22 Correct 143 ms 25528 KB Output is correct
23 Correct 165 ms 30892 KB Output is correct
24 Correct 176 ms 30856 KB Output is correct
25 Correct 177 ms 30864 KB Output is correct
26 Correct 126 ms 30108 KB Output is correct
27 Correct 122 ms 29356 KB Output is correct
28 Correct 170 ms 30680 KB Output is correct
29 Correct 168 ms 30648 KB Output is correct
30 Correct 90 ms 28952 KB Output is correct
31 Correct 159 ms 31144 KB Output is correct
32 Correct 155 ms 30148 KB Output is correct
33 Correct 174 ms 31156 KB Output is correct
34 Correct 164 ms 30656 KB Output is correct
35 Correct 118 ms 29116 KB Output is correct
36 Correct 62 ms 28524 KB Output is correct
37 Correct 173 ms 32152 KB Output is correct
38 Correct 161 ms 30888 KB Output is correct
39 Correct 171 ms 31420 KB Output is correct