제출 #365472

#제출 시각아이디문제언어결과실행 시간메모리
365472Sparky_09정렬하기 (IOI15_sorting)C++17
100 / 100
178 ms21356 KiB
#include "sorting.h"
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define trav(a, x) for(auto& a : x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;

void usaco(string s){
  freopen((s+".in").c_str(), "r", stdin);
  freopen((s+".out").c_str(), "w", stdout);
}

const int N = 6e5 + 9;
int C[N], n, *A, *B, pt[N], dy[N];
bool vis[N];

bool Check(int x){
	int cnt = 0, cb = 0;
	for (int i = 0; i < n; ++i) if (!vis[i]) {
		++cnt;
		for (int j = i; ; j = C[j]){
			vis[j] = 1;
			if (!vis[C[j]]) A[cb] = i, B[cb] = C[j], ++cb;
			else break;
		}
	}
	if(cb > x) return 0;
	for(int i = cb; i < x; ++i) A[i] = B[i] = 0;
	return 1;
}

int findSwapPairs(int NN, int S[], int M, int X[], int Y[], int P[], int Q[]){
  int l = 0, r = NN, mid; A = P; B = Q; n = NN;
	while (l <= r){
		mid = (l + r) >> 1;
		for(int i = 0; i < n; ++i) C[i] = S[i], vis[i] = 0;
		for(int i = 0; i < mid; ++i) swap(C[X[i]], C[Y[i]]);
		if(Check(mid)) r = mid - 1;
		else l = mid + 1;
	}
	for(int i = 0; i < n; ++i) C[i] = S[i], vis[i] = 0;
	for(int i = 0; i < l; ++i) swap(C[X[i]], C[Y[i]]);
	Check(l);
	for(int i = 0; i < n; ++i) pt[i] = dy[i] = i;
	for(int i = l - 1; ~i; --i){
		P[i] = dy[P[i]]; Q[i] = dy[Q[i]];
		swap(pt[X[i]], pt[Y[i]]);
		dy[pt[X[i]]] = X[i];
		dy[pt[Y[i]]] = Y[i];
	}
	return l;
}
/*
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
#ifdef LOCAL_DEFINE
	freopen("input.txt", "r", stdin);
#endif

}
*/

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

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:37:40: warning: unused parameter 'M' [-Wunused-parameter]
   37 | int findSwapPairs(int NN, int S[], int M, int X[], int Y[], int P[], int Q[]){
      |                                    ~~~~^
sorting.cpp: In function 'void usaco(std::string)':
sorting.cpp:14:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   14 |   freopen((s+".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sorting.cpp:15:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   15 |   freopen((s+".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...