답안 #316484

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
316484 2020-10-26T12:49:49 Z mohamedsobhi777 정렬하기 (IOI15_sorting) C++14
74 / 100
1000 ms 8056 KB
#include <bits/stdc++.h>
#include "sorting.h"

using namespace std;
const int _N = 2e5;

int n;
int s[_N], _s[_N];
int x[_N], y[_N];
int nxt[_N];
int t;
int viz[_N];

int check()
{
        ++t;
        int ret = 0;
        for (int i = 0; i < n; ++i)
                nxt[i] = s[i];
        for (int i = 0; i < n; ++i)
        {
                int cnt = 1;
                int cur = nxt[i];
                if (viz[i] == t)
                        continue;
                while (cur != i)
                {
                        viz[cur] = t;
                        cur = nxt[cur];
                        ++cnt;
                }
                viz[cur] = t;
                ret += --cnt;
        }
        return ret;
}
vector<pair<int, int>> ans, ans2;

void solve(int X)
{

        memset(nxt, -1, sizeof nxt);
        for (int i = 0; i < n; ++i)
        {
                nxt[i] = s[i];
        }

        map<int, int> vis;
        for (int i = 0; i < n; ++i)
        {
                if (vis[i])
                        continue;
                int cur = nxt[i];
                while (cur != i)
                {
                        ++vis[cur];
                        ans.push_back({i, cur});
                        cur = nxt[cur];
                }
                ++vis[cur];
        }
        while (ans.size() < X)
        {
                ans.push_back({0, 0});
        }
        ans2.resize(X);

        for (int i = 0; i < X; ++i)
        {
                int rep[n + 1];
                for (int j = 0; j < n; ++j)
                        rep[j] = j;
                for (int j = i + 1; j < X; ++j)
                {
                        swap(rep[x[j]], rep[y[j]]);
                }
                ans2[i] = {rep[ans[i].first], rep[ans[i].second]};
                swap(rep[x[i]], rep[y[i]]);
        }
        for (int i = 0; i < X; ++i)
        {
                swap(_s[x[i]], _s[y[i]]);
                swap(_s[ans2[i].first], _s[ans2[i].second]);
        }
}

void print()
{
        for (int i = 0; i < n; ++i)
        {
                cout << s[i] << " ";
        }
        cout << "-----------\n";
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
        n = N;
        for (int i = 0; i < N; ++i)
        {
                s[i] = S[i];
                _s[i] = S[i];
        }
        if (check() == 0)
        {
                return 0;
        }
        for (int i = 0; i < M; ++i)
        {
                x[i] = X[i];
                y[i] = Y[i];
                swap(s[X[i]], s[Y[i]]);
                if (check() <= i + 1)
                {
                        solve(i + 1);
                        for (int j = 0; j <= i; ++j)
                        {
                                P[j] = ans2[j].first;
                                Q[j] = ans2[j].second;
                        }
                        return ++i;
                }
        }
        return 0;
}

Compilation message

sorting.cpp: In function 'void solve(int)':
sorting.cpp:62:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   62 |         while (ans.size() < X)
      |                ~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1152 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 1 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1152 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 1 ms 1152 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 1152 KB Output is correct
15 Correct 1 ms 1152 KB Output is correct
16 Correct 1 ms 1152 KB Output is correct
17 Correct 1 ms 1152 KB Output is correct
18 Correct 1 ms 1152 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 3 ms 1280 KB Output is correct
22 Correct 3 ms 1280 KB Output is correct
23 Correct 3 ms 1280 KB Output is correct
24 Correct 3 ms 1280 KB Output is correct
25 Correct 2 ms 1280 KB Output is correct
26 Correct 2 ms 1280 KB Output is correct
27 Correct 3 ms 1280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 1408 KB Output is correct
2 Correct 26 ms 1408 KB Output is correct
3 Correct 25 ms 1408 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 3 ms 1280 KB Output is correct
6 Correct 5 ms 1280 KB Output is correct
7 Correct 15 ms 1408 KB Output is correct
8 Correct 25 ms 1408 KB Output is correct
9 Correct 26 ms 1516 KB Output is correct
10 Correct 26 ms 1400 KB Output is correct
11 Correct 25 ms 1408 KB Output is correct
12 Correct 16 ms 1408 KB Output is correct
13 Correct 29 ms 1400 KB Output is correct
14 Correct 2 ms 1280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 1408 KB Output is correct
2 Correct 26 ms 1408 KB Output is correct
3 Correct 25 ms 1408 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 3 ms 1280 KB Output is correct
6 Correct 5 ms 1280 KB Output is correct
7 Correct 15 ms 1408 KB Output is correct
8 Correct 25 ms 1408 KB Output is correct
9 Correct 26 ms 1516 KB Output is correct
10 Correct 26 ms 1400 KB Output is correct
11 Correct 25 ms 1408 KB Output is correct
12 Correct 16 ms 1408 KB Output is correct
13 Correct 29 ms 1400 KB Output is correct
14 Correct 2 ms 1280 KB Output is correct
15 Execution timed out 1095 ms 8056 KB Time limit exceeded
16 Halted 0 ms 0 KB -