답안 #778271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
778271 2023-07-10T08:04:05 Z boris_mihov 정렬하기 (IOI15_sorting) C++17
100 / 100
158 ms 23628 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;
    }
    
    int l = 0, r = M + 1, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        for (int i = 1 ; i <= n ; ++i)
        {
            p[i] = S[i - 1] + 1;
        }

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

        getOps();
        if (ops.size() <= mid) r = mid;
        else l = mid;
    }

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

    for (int i = 1 ; i <= r ; ++i)
    {
        std::swap(p[X[i - 1] + 1], p[Y[i - 1] + 1]);
    }
    
    getOps();
    while (ops.size() < r)
    {
        ops.push_back({1, 1});
    }

    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:92: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]
   92 |         if (ops.size() <= mid) r = mid;
      |             ~~~~~~~~~~~^~~~~~
sorting.cpp:107: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]
  107 |     while (ops.size() < r)
      |            ~~~~~~~~~~~^~~
sorting.cpp:118: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]
  118 |     for (int i = 0 ; i < ops.size() ; ++i)
      |                      ~~^~~~~~~~~~~~
sorting.cpp:131:20: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  131 |     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 1 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 1 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 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 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
# 결과 실행 시간 메모리 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 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 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 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 1 ms 340 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 472 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 388 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 388 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 128 ms 18008 KB Output is correct
16 Correct 134 ms 21252 KB Output is correct
17 Correct 146 ms 22588 KB Output is correct
18 Correct 36 ms 14260 KB Output is correct
19 Correct 110 ms 20416 KB Output is correct
20 Correct 117 ms 20616 KB Output is correct
21 Correct 122 ms 21276 KB Output is correct
22 Correct 123 ms 16908 KB Output is correct
23 Correct 140 ms 23308 KB Output is correct
24 Correct 139 ms 23016 KB Output is correct
25 Correct 150 ms 22844 KB Output is correct
26 Correct 121 ms 21704 KB Output is correct
27 Correct 106 ms 20228 KB Output is correct
28 Correct 140 ms 22412 KB Output is correct
29 Correct 135 ms 20668 KB Output is correct
30 Correct 81 ms 18056 KB Output is correct
31 Correct 143 ms 21052 KB Output is correct
32 Correct 131 ms 22508 KB Output is correct
33 Correct 142 ms 22844 KB Output is correct
34 Correct 158 ms 21480 KB Output is correct
35 Correct 118 ms 20144 KB Output is correct
36 Correct 44 ms 16040 KB Output is correct
37 Correct 141 ms 23628 KB Output is correct
38 Correct 136 ms 22652 KB Output is correct
39 Correct 138 ms 22816 KB Output is correct