Submission #311806

# Submission time Handle Problem Language Result Execution time Memory
311806 2020-10-11T19:29:39 Z hhh07 Sorting (IOI15_sorting) C++14
0 / 100
4 ms 512 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include "sorting.h"

using namespace std;


int moze(int n, int s[], int m, int x[], int y[], int p[], int q[]){
    int pos[n], s_copy[n], s_copy2[n];
    
    for (int i = 0; i < n; i++)
        pos[s[i]] = i, s_copy[i] = s[i], s_copy2[i] = s[i];
    
    for (int i = 0; i < m; i++)
        swap(s[x[i]], s[y[i]]);
    
    int kraj_pos[n];
    for (int i = 0; i < n; i++)
        kraj_pos[s[i]] = i;
    
    int f[n], g[n];
    for (int i = 0; i < n; i++){
        f[i] = kraj_pos[s_copy[i]];
        g[kraj_pos[s_copy[i]]] = i;
    }
    
    int cnt = 0;
    for (int i = 0; i < m; i++){
        swap(f[x[i]], f[y[i]]);
        g[f[x[i]]] = x[i]; g[f[y[i]]] = y[i];
        
        swap(s_copy2[x[i]], s_copy2[y[i]]);
        pos[s_copy2[x[i]]] = x[i]; pos[s_copy2[y[i]]] = y[i];
        
        bool t = false;
        while(cnt < n){
            int idx = g[cnt];
            
            if (s_copy2[idx] != cnt){
                int a = idx, b = pos[cnt];
                swap(s_copy2[a], s_copy2[b]);
                p[i] = a; q[i] = b;
                pos[s_copy[a]] = a; pos[s_copy2[b]] = b;
                t = true;
                break;
            }
            cnt++;
        }
        
        if (!t)
            p[i] = q[i] = 0;
        else
            cnt++;
    }
    
    for (int i = 0; i < n; i++){
        s[i] = s_copy[i];
        if (s_copy2[i] != i)
            return false;
    }
    
    return true;
}

int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
    int beg = 0, end = m;
    
    while(beg < end){
        int mid = (beg + end)/2;
        for (int i = 0; i < n; i++)
            p[i] = 0, q[i] = 0;
        if (moze(n, s, mid, x, y, p, q))
            end = mid;
        else
            beg = mid + 1;
    }
    
    return beg;
}




# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Incorrect 0 ms 256 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Incorrect 0 ms 256 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Incorrect 0 ms 256 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Incorrect 0 ms 256 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 512 KB Output isn't correct
2 Halted 0 ms 0 KB -