Submission #257871

#TimeUsernameProblemLanguageResultExecution timeMemory
257871shrek12357정렬하기 (IOI15_sorting)C++14
0 / 100
1 ms640 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include "sorting.h"
using namespace std;
 
vector<pair<int, int>> toChange;
vector<int> xAns, yAns;
 
bool check(int t, int nums[], int n, int x[], int y[]) {
	int in[n + 11], out[n + 11];
 
    for(int i = 0; i < n; i++){
        in[i] = out[i] = nums[i];
    }
 
    for(int i = 0; i < t; i++){
        swap(out[x[i]], out[y[i]]);
    }
 
    bool viz[n + 11];
   for(int i =0 ; i < n + 5; i++){
     viz[i] = false;
   }
 
    int k = 0;
    for(int i = 0; i < n; i++){
 
        if(viz[i])continue;
 
        vector<int>cur;
 
        int v = out[i];
        while(v != i){
            cur.push_back(v);
            v = out[v];
        }
        cur.push_back(v);
 
        for(int j = cur.size() - 1; j > 0; j--){
            toChange[k++] = {cur[0], cur[j]};
        }
 
        for(auto it : cur)viz[it] = 1;
    }
 
    if(k > t)return 0;
}
 
 
 
void getAns(int mid, int nums[], int n, int x[], int y[]) {
	map<int, int> pos;
	for (int i = 0; i < n; i++) {
		pos[nums[i]] = i;
	}
	for (int i = 0; i < toChange.size(); i++) {
		int a = x[i], b = y[i];
		swap(nums[a], nums[b]);
		pos[nums[a]] = a;
		pos[nums[b]] = b;
 
		int c = toChange[i].first, d = toChange[i].second;
		nums[pos[c]] = d;
		nums[pos[d]] = c;
		xAns.push_back(pos[c]);
		yAns.push_back(pos[d]);
		int tmp = pos[d];
		pos[d] = pos[c];
		pos[c] = tmp;
	}
	for (int i = 0; i < mid - toChange.size(); i++) {
		xAns.push_back(0);
		yAns.push_back(0);
	}
}
 
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	int lo = -1;
	int hi = M;
	while (lo + 1 < hi) {
		int mid = (lo + hi) / 2;
		if (check(mid,S,N,X,Y)) {
			hi = mid;
		}
		else {
			lo = mid;
		}
		toChange.clear();
	}
	check(hi, S, N, X, Y);
	getAns(hi, S, N, X, Y);
	for (int i = 0; i < xAns.size(); i++) {
		P[i] = xAns[i];
		Q[i] = yAns[i];
	}
	return hi;
}

Compilation message (stderr)

sorting.cpp: In function 'bool check(int, int*, int, int*, int*)':
sorting.cpp:47:32: warning: conversion to 'int' from 'std::vector<int>::size_type {aka long unsigned int}' may alter its value [-Wconversion]
         for(int j = cur.size() - 1; j > 0; j--){
                     ~~~~~~~~~~~^~~
sorting.cpp:18:6: warning: variable 'in' set but not used [-Wunused-but-set-variable]
  int in[n + 11], out[n + 11];
      ^~
sorting.cpp: In function 'void getAns(int, int*, int, int*, int*)':
sorting.cpp:64:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < toChange.size(); i++) {
                  ~~^~~~~~~~~~~~~~~~~
sorting.cpp:79:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < mid - toChange.size(); i++) {
                  ~~^~~~~~~~~~~~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:100:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < xAns.size(); i++) {
                  ~~^~~~~~~~~~~~~
sorting.cpp: In function 'bool check(int, int*, int, int*, int*)':
sorting.cpp:55:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
#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...