Submission #552026

# Submission time Handle Problem Language Result Execution time Memory
552026 2022-04-22T08:29:20 Z Theo830 Sorting (IOI15_sorting) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "sorting.h"
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int n = N;
    if(is_sorted(S,S+N)){
        return 0;
    }
    ll l = 1,r = m;
    ll ans = m;
    while(l <= r){
        ll mid = (l+r)/2;
        int cur[n];
        int fin[n];
        int pos[n],post[n];
        f(i,0,n){
            pos[S[i]] = i;
        }
        f(i,0,n){
            cur[i] = S[i];
            fin[i] = S[i];
        }
        f(j,0,mid){
            swap(fin[X[j]],fin[Y[j]]);
        }
        f(i,0,n){
            post[fin[i]] = i;
        }
        ll po = 0;
        f(j,0,mid){
            swap(pos[cur[X[j]]],pos[cur[Y[j]]]);
            swap(cur[X[j]],cur[Y[j]]);
            while(po < n && fin[po] == po){
                po++;
            }
            if(po < n){
                ll xx = pos[po],yy = pos[fin[po]];
                swap(pos[cur[xx]],pos[cur[yy]]);
                swap(cur[xx],cur[yy]);
                xx = post[po],yy = post[fin[po]];
                swap(post[fin[xx]],post[fin[yy]]);
                swap(fin[xx],fin[yy]);
            }
        }
        while(po < n && fin[po] == po){
            po++;
        }
        if(pos == n){
            r = mid - 1;
            ans = min(ans,mid);
        }
        else{
            l = mid + 1;
        }
    }
    int cur[n];
    int fin[n];
    int pos[n],post[n];
    f(i,0,n){
        pos[S[i]] = i;
    }
    f(i,0,n){
        cur[i] = S[i];
        fin[i] = S[i];
    }
    f(j,0,ans){
        swap(fin[X[j]],fin[Y[j]]);
    }
    f(i,0,n){
        post[fin[i]] = i;
    }
    ll po = 0;
    f(j,0,ans){
        swap(pos[cur[X[j]]],pos[cur[Y[j]]]);
        swap(cur[X[j]],cur[Y[j]]);
        while(po < n && fin[po] == po){
            po++;
        }
        if(po < n){
            ll xx = pos[po],yy = pos[fin[po]];
            P[j] = xx,Q[j] = yy;
            swap(pos[cur[xx]],pos[cur[yy]]);
            swap(cur[xx],cur[yy]);
            xx = post[po],yy = post[fin[po]];
            swap(post[fin[xx]],post[fin[yy]]);
            swap(fin[xx],fin[yy]);
        }
        else{
            P[j] = Q[j] = 0;
        }
    }
	return ans;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:31:18: error: 'm' was not declared in this scope
   31 |     ll l = 1,r = m;
      |                  ^
sorting.cpp:39:25: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   39 |             pos[S[i]] = i;
      |                         ^
sorting.cpp:49:28: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   49 |             post[fin[i]] = i;
      |                            ^
sorting.cpp:70:16: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   70 |         if(pos == n){
      |            ~~~~^~~~
sorting.cpp:82:21: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   82 |         pos[S[i]] = i;
      |                     ^
sorting.cpp:92:24: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   92 |         post[fin[i]] = i;
      |                        ^
sorting.cpp:103:20: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  103 |             P[j] = xx,Q[j] = yy;
      |                    ^~
sorting.cpp:103:30: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  103 |             P[j] = xx,Q[j] = yy;
      |                              ^~
sorting.cpp:114:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  114 |  return ans;
      |         ^~~
sorting.cpp:26:39: warning: unused parameter 'M' [-Wunused-parameter]
   26 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^