Submission #778393

# Submission time Handle Problem Language Result Execution time Memory
778393 2023-07-10T09:07:23 Z danikoynov Sorting (IOI15_sorting) C++14
100 / 100
153 ms 18340 KB
#include "sorting.h"
#include<bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 10;
int n, arr[maxn], used[maxn];
vector < pair < int, int > > edges;
int count_steps()
{
    for (int i = 0; i < n; i ++)
        used[i] = 0;
    int ans = 0;
    for (int i = 0; i < n; i ++)
    {
        if (used[i])
            continue;
        int v = arr[i], cnt = 0;
        while(v != i)
        {
            used[v] = 1;
            cnt ++;
            v = arr[v];
        }
        used[v] = 1;
        cnt ++;
        ans = ans + (cnt - 1);
    }
    return ans;
}

void build_edges()
{
    for (int i = 0; i < n; i ++)
        used[i] = 0;
    for (int i = 0; i < n; i ++)
    {
        if (used[i])
            continue;
        vector < int > ord;
        int v = arr[i];
        while(v != i)
        {
            ord.push_back(v);
            used[v] = 1;
            v = arr[v];
        }
        ord.push_back(i);
        used[i] = 1;

        for (int i = 0; i < ord.size() - 1; i ++)
            edges.push_back({arr[ord[i]], arr[ord[i + 1]]});
    }
}

int pos[maxn];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
    n = N;
    bool sorted = true;
    for (int i = 0; i < N; i ++)
        if (S[i] != i)
            sorted = false;

    if (sorted)
        return 0;
    for (int i = 0; i < N; i ++)
        arr[i] = S[i];

    int l = 0, r = M - 1;
    while(l <= r)
    {
        int m = (l + r) / 2;
        for (int i = 0; i < N; i ++)
            arr[i] = S[i];
        for (int i = 0; i <= m; i ++)
            swap(arr[X[i]], arr[Y[i]]);
            //cout << "check " << m << " " << count_steps() << endl;
        if (count_steps() <= m + 1)
            r = m - 1;
        else
            l = m + 1;
    }
    int pivot = l;
        for (int i = 0; i < N; i ++)
            arr[i] = S[i];
        for (int i = 0; i <= pivot; i ++)
            swap(arr[X[i]], arr[Y[i]]);
    /**for (int i = 0; i < M; i ++)
    {
        swap(arr[X[i]], arr[Y[i]]);
        int steps = count_steps();

        if (steps <= i + 1)
        {
            pivot = i;
            break;
        }
    }
    cout << pivot << endl;*/
    ///assert(pivot != -1);
    ///cout << pivot << endl;
    ///exit(0);
    build_edges();
    for (int i = 0; i < n; i ++)
        pos[S[i]] = i, arr[i] = S[i];
    for (int i = 0; i < edges.size(); i ++)
    {
        swap(arr[X[i]], arr[Y[i]]);
    swap(pos[arr[X[i]]], pos[arr[Y[i]]]);

        ///cout << edges[i].first << " : " << edges[i].second << endl;
        P[i] = pos[edges[i].first];
        Q[i] = pos[edges[i].second];
        swap(arr[Q[i]], arr[P[i]]);
        swap(pos[edges[i].first], pos[edges[i].second]);

            ///cout << id1 << " : " << id2 << endl;

        ///swap(pos[X[i]], pos[Y[i]]);
    }
    int left = pivot - edges.size() + 1;
    if (left % 2 == 0)
    {
        for (int i = edges.size(); i <= pivot; i ++)
        {
            P[i] = 0;
            Q[i] = 1;
        }
    }
    else
    {
        for (int i = edges.size(); i < pivot; i ++)
        {
            P[i] = 0;
            Q[i] = 1;
        }
        P[pivot] = Q[pivot] = 0;
    }

    /**for (int i = 0; i <= pivot; i ++)
    { swap(S[X[i]], S[Y[i]]);
        swap(S[P[i]], S[Q[i]]);

    }*/
    /**for (int i = 0; i < N; i ++)
        cout << S[i] << " ";
    cout << endl;*/
    ///cout << "stop " << edges.size() << " " << pivot << endl;


    return pivot + 1;
}


Compilation message

sorting.cpp: In function 'void build_edges()':
sorting.cpp:51:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
   51 |         for (int i = 0; i < ord.size() - 1; i ++)
      |                  ^
sorting.cpp:36:14: note: shadowed declaration is here
   36 |     for (int i = 0; i < n; i ++)
      |              ^
sorting.cpp:51:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for (int i = 0; i < ord.size() - 1; i ++)
      |                         ~~^~~~~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:107:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for (int i = 0; i < edges.size(); i ++)
      |                     ~~^~~~~~~~~~~~~~
sorting.cpp:122:37: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  122 |     int left = pivot - edges.size() + 1;
      |                ~~~~~~~~~~~~~~~~~~~~~^~~
sorting.cpp:125:32: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  125 |         for (int i = edges.size(); i <= pivot; i ++)
      |                      ~~~~~~~~~~^~
sorting.cpp:133:32: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  133 |         for (int i = edges.size(); i < pivot; i ++)
      |                      ~~~~~~~~~~^~
# 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 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 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 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 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 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 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 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 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 436 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 111 ms 12664 KB Output is correct
16 Correct 125 ms 17068 KB Output is correct
17 Correct 138 ms 17796 KB Output is correct
18 Correct 40 ms 9620 KB Output is correct
19 Correct 112 ms 14936 KB Output is correct
20 Correct 114 ms 15420 KB Output is correct
21 Correct 122 ms 15572 KB Output is correct
22 Correct 111 ms 17204 KB Output is correct
23 Correct 131 ms 18172 KB Output is correct
24 Correct 132 ms 18260 KB Output is correct
25 Correct 137 ms 17992 KB Output is correct
26 Correct 113 ms 15420 KB Output is correct
27 Correct 104 ms 14808 KB Output is correct
28 Correct 132 ms 17716 KB Output is correct
29 Correct 131 ms 17084 KB Output is correct
30 Correct 86 ms 13848 KB Output is correct
31 Correct 153 ms 17292 KB Output is correct
32 Correct 136 ms 17920 KB Output is correct
33 Correct 137 ms 18340 KB Output is correct
34 Correct 134 ms 17788 KB Output is correct
35 Correct 127 ms 14984 KB Output is correct
36 Correct 61 ms 12420 KB Output is correct
37 Correct 136 ms 18108 KB Output is correct
38 Correct 139 ms 17644 KB Output is correct
39 Correct 147 ms 17900 KB Output is correct