답안 #278250

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
278250 2020-08-21T11:27:21 Z davitmarg 정렬하기 (IOI15_sorting) C++17
100 / 100
455 ms 29920 KB
/*
DavitMarg
In a honky-tonk,
Down in Mexico
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
#define fastIO ios::sync_with_stdio(false); cin.tie(0)
using namespace std;
const int N = 3 * 200005;

#ifndef death
#include "sorting.h"
#endif

int x[N], y[N], s[N], a[N], pos[N], f[N], g[N];
int n, m;
vector<pair<int, int>> ans;

void add(int x, int y, bool sw = 1)
{
    if(sw)
        ans.push_back(MP(x, y));
    else
    {
        swap(g[x], g[y]);
        f[g[x]] = x;
        f[g[y]] = y;
    }
    swap(a[x], a[y]);
    pos[a[x]] = x;
    pos[a[y]] = y;
}

bool check(int k)
{
    ans.clear();
    for (int i = 0; i < n; i++)
    {
        a[i] = s[i];
        f[i] = i;
        pos[s[i]] = i;
    }
    for (int i = 0; i < k; i++)
        swap(f[x[i]], f[y[i]]);
    for (int i = 0; i < n; i++)
        g[f[i]] = i;
    int last = 0;
    for (int i = 0; i < k; i++)
    {
        add(x[i], y[i],0);
        while (last < n && pos[last] == f[last])
            last++;
        if (last >= n)
        {
            ans.push_back(MP(0, 0));
            continue;
        }
        add(pos[last], f[last]);
        last++;
    }
    for (int i = 0; i < n; i++)
        if (a[i] != i)
            return 0;
    return 1;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
    n = N;
    m = M;

    for (int i = 0; i < n; i++)
        s[i] = S[i];
    for (int i = 0; i < m; i++)
    {
        x[i] = X[i];
        y[i] = Y[i];
    }

    int l=0, r=m, mid, res=m;
    while (l <= r)
    {
        mid = (l + r) / 2;
        if (check(mid))
        {
            r = mid - 1;
            res = mid;
        }
        else
            l = mid + 1;
    }  

    check(res);

    for (int i = 0; i < ans.size(); i++)
    {
        P[i] = ans[i].first;
        Q[i] = ans[i].second;
    }
    return ans.size();
}


#ifdef death
int nn, mm,ss[N], xx[N], yy[N], pp[N], qq[N];
int main()
{
    cin >> nn >> mm;
    for (int i = 0; i < nn; i++)
        cin >> ss[i];
    for (int i = 0; i < mm; i++)
        cin >> xx[i] >> yy[i];
    mm=findSwapPairs(nn, ss, mm, xx, yy, pp, qq);
    for (int i = 0; i < mm; i++)
    {
        cout << pp[i] << " : " << qq[i] << endl;
        swap(ss[xx[i]], ss[yy[i]]);
        swap(ss[pp[i]], ss[qq[i]]);
    }
    
    cout << endl;
    for (int i = 0; i < nn; i++)
        cout << ss[i] << " ";
    cout << endl;
        
    return 0;
}
#endif

/*

5 5 
4 3 2 1 0
1 1
4 0
2 3
1 4
0 4


10 7
0 1 2 3 4 5 6 7 9 8
0 0
0 0
0 0
0 0
0 0
0 0
0 0


*/

Compilation message

sorting.cpp: In function 'void add(int, int, bool)':
sorting.cpp:41:35: warning: declaration of 'y' shadows a global declaration [-Wshadow]
   41 | void add(int x, int y, bool sw = 1)
      |                                   ^
sorting.cpp:37:11: note: shadowed declaration is here
   37 | int x[N], y[N], s[N], a[N], pos[N], f[N], g[N];
      |           ^
sorting.cpp:41:35: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   41 | void add(int x, int y, bool sw = 1)
      |                                   ^
sorting.cpp:37:5: note: shadowed declaration is here
   37 | int x[N], y[N], s[N], a[N], pos[N], f[N], g[N];
      |     ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:89:76: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   89 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
      |                                                                            ^
sorting.cpp:31:11: note: shadowed declaration is here
   31 | const int N = 3 * 200005;
      |           ^
sorting.cpp:117: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]
  117 |     for (int i = 0; i < ans.size(); i++)
      |                     ~~^~~~~~~~~~~~
sorting.cpp:122:20: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  122 |     return ans.size();
      |            ~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 472 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 472 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 2 ms 884 KB Output is correct
22 Correct 2 ms 896 KB Output is correct
23 Correct 2 ms 896 KB Output is correct
24 Correct 2 ms 896 KB Output is correct
25 Correct 2 ms 896 KB Output is correct
26 Correct 2 ms 896 KB Output is correct
27 Correct 2 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 268 ms 26464 KB Output is correct
16 Correct 308 ms 27104 KB Output is correct
17 Correct 431 ms 28512 KB Output is correct
18 Correct 118 ms 25704 KB Output is correct
19 Correct 279 ms 27104 KB Output is correct
20 Correct 300 ms 28000 KB Output is correct
21 Correct 316 ms 28008 KB Output is correct
22 Correct 294 ms 24160 KB Output is correct
23 Correct 286 ms 29664 KB Output is correct
24 Correct 423 ms 29152 KB Output is correct
25 Correct 399 ms 28768 KB Output is correct
26 Correct 296 ms 27872 KB Output is correct
27 Correct 274 ms 27112 KB Output is correct
28 Correct 393 ms 28872 KB Output is correct
29 Correct 344 ms 28388 KB Output is correct
30 Correct 201 ms 27240 KB Output is correct
31 Correct 395 ms 29024 KB Output is correct
32 Correct 343 ms 28540 KB Output is correct
33 Correct 455 ms 29024 KB Output is correct
34 Correct 331 ms 29024 KB Output is correct
35 Correct 263 ms 26684 KB Output is correct
36 Correct 97 ms 27240 KB Output is correct
37 Correct 407 ms 29920 KB Output is correct
38 Correct 388 ms 28768 KB Output is correct
39 Correct 381 ms 29024 KB Output is correct