제출 #299347

#제출 시각아이디문제언어결과실행 시간메모리
299347Hideo정렬하기 (IOI15_sorting)C++17
74 / 100
260 ms20192 KiB
#include "sorting.h" //#include "grader.cpp" #include <bits/stdc++.h> using namespace std; #define ll long long #define all(s) s.begin(), s.end() #define pb push_back #define fr first #define sc second #define vi vector < int > #define pi pair < int, int > const int MN = 3e5 + 7; vector < pi > sw, out; int a[MN], b[MN], r[MN]; int x[MN], y[MN]; int n, m; bool check (int k){ for (int i = 0; i < n; i++) a[i] = b[i]; for (int i = 0; i < k; i++) swap(a[x[i]], a[y[i]]); queue < pi > q; for (int i = 0; i < n; i++) r[a[i]] = i; for (int i = 0; i < n; i++) if (a[a[i]] == i) q.push({i, a[i]}); int l = 0, cnt = 0; vector < pi > kek; while (l < n){ while (!q.empty()){ int v = q.front().fr, u = q.front().sc; q.pop(); if (r[v] == v || r[u] == u || a[v] != u || a[a[v]] != v) continue; kek.pb({v, u}); swap(a[v], a[u]); swap(r[a[v]], r[a[u]]); cnt++; if (cnt > k) return false; } if (r[l] != l){ int v = l, u = r[l]; kek.pb({v, a[v]}); swap(a[v], a[u]); swap(r[a[v]], r[a[u]]); if (a[a[u]] == u) q.push({u, a[u]}); cnt++; if (cnt > k) return false; } l++; } for (int i = 0; i < n; i++) if (a[i] != i) return false; sw = kek; return true; } void simulate (int k){ while (sw.size() != k) sw.pb({0, 0}); for (int i = 0; i < n; i++){ a[i] = b[i]; r[a[i]] = i; } for (int i = 0; i < k; i++){ swap(a[x[i]], a[y[i]]); swap(r[a[x[i]]], r[a[y[i]]]); int v = r[sw[i].fr], u = r[sw[i].sc]; out.pb({v, u}); swap(a[v], a[u]); swap(r[a[v]], r[a[u]]); } } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { n = N; m = M; bool sorted = true; for (int i = 0; i < n; i++){ b[i] = S[i]; if (b[i] != i) sorted = false; } if (sorted) return 0; for (int i = 0; i < m; i++){ x[i] = X[i]; y[i] = Y[i]; } int lf = 0, rg = M; while (lf + 1 < rg){ int mid = (lf + rg) >> 1; if (check(mid)) rg = mid; else lf = mid; } for (int i = max(1, rg - 10); i < rg; i++){ if (check(i)){ assert(false); } } simulate(rg); for (int i = 0; i < out.size(); i++){ P[i] = out[i].fr; Q[i] = out[i].sc; } return rg; }

컴파일 시 표준 에러 (stderr) 메시지

sorting.cpp: In function 'void simulate(int)':
sorting.cpp:69:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   69 |     while (sw.size() != k)
      |            ~~~~~~~~~~^~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:113: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]
  113 |     for (int i = 0; i < out.size(); i++){
      |                     ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...