Submission #284486

#TimeUsernameProblemLanguageResultExecution timeMemory
284486SamAndSorting (IOI15_sorting)C++17
20 / 100
557 ms9984 KiB
#include "sorting.h" #include <bits/stdc++.h> using namespace std; #define m_p make_pair #define fi first #define se second #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) const int N = 200005; int n, m; int a[N]; vector<int> g[N]; int c[N]; int k; void dfs(int x) { c[x] = k; for (int i = 0; i < g[x].size(); ++i) { int h = g[x][i]; if (!c[h]) dfs(h); } } vector<int> v[N]; 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) a[i] = S[i]; for (int i = 0; i < m; ++i) P[i] = Q[i] = 0; bool z = true; for (int i = 0; i < n - 1; ++i) { if (a[i] > a[i + 1]) { z = false; break; } } if (z) return 0; for (int i = 0; i < m; ++i) { swap(a[X[i]], a[Y[i]]); bool z = true; for (int i = 0; i < n - 1; ++i) { if (a[i] > a[i + 1]) { z = false; break; } } if (z) return i; for (int x = 0; x < n; ++x) g[x].clear(); for (int j = i + 1; j < m; ++j) { g[X[j]].push_back(Y[j]); g[Y[j]].push_back(X[j]); } for (int x = 0; x < n; ++x) c[x] = 0; k = 0; for (int x = 0; x < n; ++x) { if (!c[x]) { ++k; dfs(x); } } for (int i = 1; i <= k; ++i) v[i].clear(); for (int x = 0; x < n; ++x) v[c[x]].push_back(a[x]); for (int i = 1; i <= k; ++i) sort(all(v[i])); for (int x = 0; x < n; ++x) { if (!binary_search(all(v[c[x]]), x)) { vector<int> vv; for (int y = 0; y < n; ++y) { if (c[y] == c[x]) vv.push_back(y); } int xx = -1; for (int i = 0; i < v[c[x]].size(); ++i) { int u = v[c[x]][i]; if (!binary_search(all(vv), u)) { for (int y = 0; y < n; ++y) { if (a[y] == u) { xx = y; break; } } break; } } assert(xx != -1); for (int y = 0; y < n; ++y) { if (a[y] == x) { swap(a[xx], a[y]); P[i] = xx; Q[i] = y; break; } } break; } } z = true; for (int i = 0; i < n - 1; ++i) { if (a[i] > a[i + 1]) { z = false; break; } } if (z) return (i + 1); } assert(false); return -1; }

Compilation message (stderr)

sorting.cpp: In function 'void dfs(int)':
sorting.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:59:14: warning: declaration of 'z' shadows a previous local [-Wshadow]
   59 |         bool z = true;
      |              ^
sorting.cpp:42:10: note: shadowed declaration is here
   42 |     bool z = true;
      |          ^
sorting.cpp:60:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
   60 |         for (int i = 0; i < n - 1; ++i)
      |                  ^
sorting.cpp:55:14: note: shadowed declaration is here
   55 |     for (int i = 0; i < m; ++i)
      |              ^
sorting.cpp:91:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
   91 |         for (int i = 1; i <= k; ++i)
      |                  ^
sorting.cpp:55:14: note: shadowed declaration is here
   55 |     for (int i = 0; i < m; ++i)
      |              ^
sorting.cpp:95:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
   95 |         for (int i = 1; i <= k; ++i)
      |                  ^
sorting.cpp:55:14: note: shadowed declaration is here
   55 |     for (int i = 0; i < m; ++i)
      |              ^
sorting.cpp:108:26: warning: declaration of 'i' shadows a previous local [-Wshadow]
  108 |                 for (int i = 0; i < v[c[x]].size(); ++i)
      |                          ^
sorting.cpp:55:14: note: shadowed declaration is here
   55 |     for (int i = 0; i < m; ++i)
      |              ^
sorting.cpp:108:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |                 for (int i = 0; i < v[c[x]].size(); ++i)
      |                                 ~~^~~~~~~~~~~~~~~~
sorting.cpp:140:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
  140 |         for (int i = 0; i < n - 1; ++i)
      |                  ^
sorting.cpp:55:14: note: shadowed declaration is here
   55 |     for (int i = 0; i < m; ++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...