Submission #778168

# Submission time Handle Problem Language Result Execution time Memory
778168 2023-07-10T06:57:27 Z boris_mihov Sorting (IOI15_sorting) C++17
74 / 100
1000 ms 16776 KB
#include "sorting.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>

typedef long long llong;
const int MAXN = 200000 + 10;
const int INF  = 1e9;

int n;
int p[MAXN];
int pos[MAXN];

bool vis[MAXN];
std::vector <std::pair <int,int>> ops;
std::stack <std::pair <int,int>> st;

void getOps()
{
    ops.clear();
    std::fill(vis + 1, vis + 1 + n, false);
    for (int i = 1 ; i <= n ; ++i)
    {
        if (vis[i])
        {
            continue;
        }

        while (!st.empty())
        {
            st.pop();
        }

        int curr = i;
        while (!vis[curr])
        {
            vis[curr] = true;
            curr = p[curr];
        
            if (!vis[curr])
            {
                st.push({p[curr], p[i]});
            }
        }

        while (!st.empty())
        {
            ops.push_back(st.top());
            st.pop();
        }
    }
}

void makeSwap(int x, int y)
{
    std::swap(p[x], p[y]);
    pos[p[x]] = x;
    pos[p[y]] = y;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) 
{
    n = N;
    for (int i = 1 ; i <= n ; ++i)
    {
        p[i] = S[i - 1] + 1;
    }

    getOps();
    if (ops.empty())
    {
        return 0;
    }

    for (int i = 1 ; i <= M ; ++i)
    {
        std::swap(p[X[i - 1] + 1], p[Y[i - 1] + 1]);
        getOps();

        if (ops.size() <= i)
        {
            while (ops.size() < i)
            {
                ops.push_back({1, 1});
            }

            break;
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        p[i] = S[i - 1] + 1;
        pos[p[i]] = i;
    }

    for (int i = 0 ; i < ops.size() ; ++i)
    {
        makeSwap(X[i] + 1, Y[i] + 1);
        P[i] = pos[ops[i].first] - 1;
        Q[i] = pos[ops[i].second] - 1;
        makeSwap(P[i] + 1, Q[i] + 1);
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        assert(p[i] == i);
    }

    return ops.size();
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:83:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   83 |         if (ops.size() <= i)
      |             ~~~~~~~~~~~^~~~
sorting.cpp:85:31: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   85 |             while (ops.size() < i)
      |                    ~~~~~~~~~~~^~~
sorting.cpp:100:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for (int i = 0 ; i < ops.size() ; ++i)
      |                      ~~^~~~~~~~~~~~
sorting.cpp:113:20: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  113 |     return ops.size();
      |            ~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 264 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 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 264 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 0 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 0 ms 340 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 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 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 264 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 0 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 0 ms 340 KB Output is correct
13 Correct 0 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 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 3 ms 464 KB Output is correct
22 Correct 3 ms 456 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 420 KB Output is correct
2 Correct 27 ms 444 KB Output is correct
3 Correct 24 ms 424 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 8 ms 404 KB Output is correct
8 Correct 26 ms 472 KB Output is correct
9 Correct 29 ms 472 KB Output is correct
10 Correct 27 ms 468 KB Output is correct
11 Correct 19 ms 480 KB Output is correct
12 Correct 7 ms 468 KB Output is correct
13 Correct 20 ms 456 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 420 KB Output is correct
2 Correct 27 ms 444 KB Output is correct
3 Correct 24 ms 424 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 8 ms 404 KB Output is correct
8 Correct 26 ms 472 KB Output is correct
9 Correct 29 ms 472 KB Output is correct
10 Correct 27 ms 468 KB Output is correct
11 Correct 19 ms 480 KB Output is correct
12 Correct 7 ms 468 KB Output is correct
13 Correct 20 ms 456 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Execution timed out 1056 ms 16776 KB Time limit exceeded
16 Halted 0 ms 0 KB -