답안 #778165

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
778165 2023-07-10T06:56:32 Z boris_mihov 정렬하기 (IOI15_sorting) C++17
54 / 100
25 ms 468 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);
    int cntCycles = 0;

    for (int i = 1 ; i <= n ; ++i)
    {
        if (vis[i])
        {
            continue;
        }

        cntCycles++;
        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();
        }
    }

    assert(ops.size() == n - cntCycles);
}

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;
    }

    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

In file included from /usr/include/c++/10/cassert:44,
                 from sorting.cpp:5:
sorting.cpp: In function 'void getOps()':
sorting.cpp:59:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   59 |     assert(ops.size() == n - cntCycles);
      |            ~~~~~~~~~~~^~~~~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:82: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]
   82 |         if (ops.size() <= i)
      |             ~~~~~~~~~~~^~~~
sorting.cpp:84: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]
   84 |             while (ops.size() < i)
      |                    ~~~~~~~~~~~^~~
sorting.cpp:99: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]
   99 |     for (int i = 0 ; i < ops.size() ; ++i)
      |                      ~~^~~~~~~~~~~~
sorting.cpp:112:20: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  112 |     return ops.size();
      |            ~~~~~~~~^~
# 결과 실행 시간 메모리 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 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
# 결과 실행 시간 메모리 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 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
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 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 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 0 ms 340 KB Output is correct
16 Correct 0 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 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 1 ms 356 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 420 KB Output is correct
2 Correct 25 ms 408 KB Output is correct
3 Correct 22 ms 412 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 420 KB Output is correct
2 Correct 25 ms 408 KB Output is correct
3 Correct 22 ms 412 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -