Submission #552027

# Submission time Handle Problem Language Result Execution time Memory
552027 2022-04-22T08:29:55 Z Theo830 Sorting (IOI15_sorting) C++17
100 / 100
333 ms 21828 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;
    int m = M;
    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(po == 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:40:25: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   40 |             pos[S[i]] = i;
      |                         ^
sorting.cpp:50:28: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   50 |             post[fin[i]] = i;
      |                            ^
sorting.cpp:83:21: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   83 |         pos[S[i]] = i;
      |                     ^
sorting.cpp:93:24: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   93 |         post[fin[i]] = i;
      |                        ^
sorting.cpp:104:20: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  104 |             P[j] = xx,Q[j] = yy;
      |                    ^~
sorting.cpp:104:30: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  104 |             P[j] = xx,Q[j] = yy;
      |                              ^~
sorting.cpp:115:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  115 |  return ans;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 312 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 304 KB Output is correct
20 Correct 0 ms 304 KB Output is correct
21 Correct 2 ms 444 KB Output is correct
22 Correct 1 ms 432 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 448 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 448 KB Output is correct
9 Correct 2 ms 444 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 448 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 440 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 448 KB Output is correct
9 Correct 2 ms 444 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 206 ms 19284 KB Output is correct
16 Correct 227 ms 19656 KB Output is correct
17 Correct 308 ms 20816 KB Output is correct
18 Correct 44 ms 13388 KB Output is correct
19 Correct 225 ms 19136 KB Output is correct
20 Correct 229 ms 19800 KB Output is correct
21 Correct 231 ms 19824 KB Output is correct
22 Correct 233 ms 16068 KB Output is correct
23 Correct 256 ms 21588 KB Output is correct
24 Correct 333 ms 21276 KB Output is correct
25 Correct 314 ms 20960 KB Output is correct
26 Correct 215 ms 19796 KB Output is correct
27 Correct 206 ms 19120 KB Output is correct
28 Correct 309 ms 20992 KB Output is correct
29 Correct 314 ms 20528 KB Output is correct
30 Correct 154 ms 18516 KB Output is correct
31 Correct 289 ms 20956 KB Output is correct
32 Correct 225 ms 20812 KB Output is correct
33 Correct 297 ms 21172 KB Output is correct
34 Correct 258 ms 21148 KB Output is correct
35 Correct 207 ms 18872 KB Output is correct
36 Correct 76 ms 17588 KB Output is correct
37 Correct 312 ms 21828 KB Output is correct
38 Correct 305 ms 21016 KB Output is correct
39 Correct 313 ms 21084 KB Output is correct