Submission #314590

# Submission time Handle Problem Language Result Execution time Memory
314590 2020-10-20T11:56:14 Z talant117408 Sorting (IOI15_sorting) C++17
0 / 100
97 ms 632 KB
#include "sorting.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <ll, ll> pii;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 
inline bool isvowel(char ch){
	ch = tolower(ch);
	return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
 
inline bool isprime(int n){
	if(n < 2 || (n%2 == 0 && n != 2)) return false;
	for(int i = 3; i*i <= n; i++)
		if(n%i == 0) return false;
	return true;
}
 
int findSwapPairs(int N, int s[], int M, int X[], int Y[], int P[], int Q[]) {
    
    int i, j, p[N], pos[N], S[N];
    for(i = 0; i < N; i++){
        S[i] = s[i];
        p[S[i]] = pos[S[i]] = i;
    }
    int tmpS[N], tmpos[N];
    for(j = 0; j < N; j++){
        tmpS[j] = S[j];
        tmpos[j] = pos[j];
    }
    
    for(i = 0; i < M; i++){
        swap(pos[S[X[i]]], pos[S[Y[i]]]);
        swap(S[X[i]], S[Y[i]]);
        
        vector <pii> res;
        vector <int> vis(N), cyc;
        
        for(j = 0; j < N; j++){
            tmpS[j] = S[j];
            tmpos[j] = pos[j];
        }
        for(j = 0; j < N; j++){
            if(vis[j]) continue;
            
            int k = j;
            while(!vis[k]){
                vis[k] = 1;
                cyc.pb(tmpS[k]);
                k = tmpS[k];
            }
            
            for(k = 0; k < sz(cyc)-1; k++){
                res.pb(mp(cyc[k], cyc[k+1]));
                swap(tmpos[cyc[k]], tmpos[cyc[k+1]]);
                swap(tmpS[tmpos[cyc[k]]], tmpS[tmpos[cyc[k+1]]]);
                swap(cyc[k], cyc[k+1]);
            }
            cyc.clear();
        }
        
        if(sz(res) <= i+1){
            vector <pii> res1;
            
            for(j = 0; j < N; j ++){
                tmpS[j] = s[j];
                tmpos[j] = p[j];
            }
            for(j = 0; j < sz(res); j++){
                swap(tmpS[X[j]], tmpS[Y[j]]);
                swap(tmpos[tmpS[X[j]]], tmpos[tmpS[Y[j]]]);
                res1.pb(mp(tmpos[res[j].first], tmpos[res[j].second]));
                swap(tmpS[tmpos[res[j].first]], tmpS[tmpos[res[j].second]]);
                swap(tmpos[tmpS[tmpos[res[j].first]]], tmpos[tmpS[tmpos[res[j].second]]]);
            }
            
            while(sz(res1) <= i){
                res1.pb(mp(0, 0));
            }
            for(j = 0; j < sz(res1); j++){
                P[j] = res1[j].first;
                Q[j] = res1[j].second;
            }
            return sz(res1);
        }
    }
    
    return 0;
}


Compilation message

sorting.cpp: In function 'bool isvowel(char)':
sorting.cpp:23:14: warning: conversion from 'int' to 'char' may change value [-Wconversion]
   23 |  ch = tolower(ch);
      |       ~~~~~~~^~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:96:32: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   96 |                 P[j] = res1[j].first;
sorting.cpp:97:32: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   97 |                 Q[j] = res1[j].second;
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 256 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 97 ms 632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 97 ms 632 KB Output isn't correct
2 Halted 0 ms 0 KB -