Submission #834017

#TimeUsernameProblemLanguageResultExecution timeMemory
834017VictorSorting (IOI15_sorting)C++17
36 / 100
28 ms852 KiB
#include "sorting.h" #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define per(i, a, b) for (int i = (b)-1; i >= (a); i--) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) x.size() #define pb push_back typedef vector<int> vi; typedef pair<int,int> ii; typedef vector<ii> vii; typedef long long ll; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef vector<pll> vpll; const ll INF = 1'000'000'007; bool isSorted(int N, int S[]) { rep(i,0,N) if(S[i]!=i) return false; return true; } int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) { P[0] = 0; Q[0] = 0; rep(i,0,M+1) { //cout<<"i = "<<i<<endl; //rep(i,0,N) cout<<S[i]<<' '; //cout<<endl<<endl; if(isSorted(N,S)) return i; swap(S[X[i]],S[Y[i]]); P[i]=Q[i]=0; vi swaps; per(j,0,N) if(S[j]!=j) swaps.pb(j); if(!sz(swaps)) continue; int id=rand()%sz(swaps); int j=swaps[id],pos; rep(k,0,N) if(S[k]==j) { pos=k; break; } P[i]=j; Q[i]=pos; swap(S[j],S[pos]); } return 1; } /* #include <stdlib.h> #include <stdio.h> #include <string.h> #include "sorting.h" static char _buffer[1024]; static int _currentChar = 0; static int _charsNumber = 0; static FILE *_inputFile, *_outputFile; static inline int _read() { if (_charsNumber < 0) { exit(1); } if (!_charsNumber || _currentChar == _charsNumber) { _charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile); _currentChar = 0; } if (_charsNumber <= 0) { return -1; } return _buffer[_currentChar++]; } static inline int _readInt() { int c, x, s; c = _read(); while (c <= 32) c = _read(); x = 0; s = 1; if (c == '-') { s = -1; c = _read(); } while (c > 32) { x *= 10; x += c - '0'; c = _read(); } if (s < 0) x = -x; return x; } int main() { _inputFile = fopen("sorting.in", "rb"); int N, M; N = _readInt(); int *S = (int*)malloc(sizeof(int) * (unsigned int)N); for (int i = 0; i < N; ++i) S[i] = _readInt(); M = _readInt(); int *X = (int*)malloc(sizeof(int) * (unsigned int)M), *Y = (int*)malloc(sizeof(int) * (unsigned int)M); for (int i = 0; i < M; ++i) { X[i] = _readInt(); Y[i] = _readInt(); } int *P = (int*)malloc(sizeof(int) * (unsigned int)M), *Q = (int*)malloc(sizeof(int) * (unsigned int)M); int ans = findSwapPairs(N, S, M, X, Y, P, Q); printf("%d\n", ans); for (int i = 0; i < ans; ++i) printf("%d %d\n", P[i], Q[i]); return 0; } */

Compilation message (stderr)

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:46:22: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   46 |         int id=rand()%sz(swaps);
      |                      ^
sorting.cpp:54:21: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
   54 |         swap(S[j],S[pos]);
      |                     ^~~
#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...