Submission #885851

# Submission time Handle Problem Language Result Execution time Memory
885851 2023-12-10T21:30:04 Z JakobZorz Sorting (IOI15_sorting) C++17
74 / 100
1000 ms 15956 KB
#include"sorting.h"
#include<iostream>
#include<vector>
using namespace std;

int n;
int*x,*y,*s;

vector<pair<int,int>>construct(int m){
    vector<pair<int,int>>ans(m);
    vector<int>perm(n),curr(n),inv_perm(n);
    for(int i=0;i<n;i++){
        perm[i]=i;
        inv_perm[i]=i;
        curr[i]=s[i];
    }
    
    for(int i=0;i<m;i++)
        swap(curr[x[i]],curr[y[i]]);
    
    for(int i=m-1;i>=0;i--){
        swap(perm[x[i]],perm[y[i]]);
        swap(inv_perm[perm[x[i]]],inv_perm[perm[y[i]]]);
    }
    
    for(int i=0;i<m;i++){
        swap(perm[x[i]],perm[y[i]]);
        swap(inv_perm[perm[x[i]]],inv_perm[perm[y[i]]]);
        
        for(int j=0;j<n;j++){
            if(curr[j]!=j){
                ans[i].first=inv_perm[j];
                for(int k=0;k<n;k++){
                    if(curr[k]==j){
                        swap(curr[j],curr[k]);
                        ans[i].second=inv_perm[k];
                        break;
                    }
                }
                break;
            }
        }
    }
    
    for(int i=0;i<n;i++)
        if(curr[i]!=i)
            return {};
    
    return ans;
}

int findSwapPairs(int N,int S[],int _,int X[],int Y[],int P[],int Q[]){
    n=N;
    x=X;
    y=Y;
    s=S;
    bool already_done=true;
    for(int i=0;i<n;i++)
        if(s[i]!=i)
            already_done=false;
    if(already_done)
        return 0;
    
    int l=0,r=n;
    while(l<r-1){
        int mid=(l+r)/2;
        if(construct(mid).empty())
            l=mid;
        else
            r=mid;
    }
    int opt=r;
    
    vector<pair<int,int>>res=construct(opt);
    
    for(int i=0;i<opt;i++){
        P[i]=res[i].first;
        Q[i]=res[i].second;
    }
    
    /*vector<int>test(n);
    for(int i=0;i<n;i++)
        test[i]=s[i];
    
    for(int i=0;i<opt;i++){
        swap(test[x[i]],test[y[i]]);
        swap(test[P[i]],test[Q[i]]);
        bool sorted=true;
        for(int j=0;j<n;j++)
            if(test[j]!=j)
                sorted=false;
        if(sorted)
            return i+1;
    }
    
#ifdef JAKOB
    for(int i:test)
        cout<<i<<" ";
    cout<<"\n";
#endif*/
	return opt;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:52:37: warning: unused parameter '_' [-Wunused-parameter]
   52 | int findSwapPairs(int N,int S[],int _,int X[],int Y[],int P[],int Q[]){
      |                                 ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 360 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 344 KB Output is correct
24 Correct 2 ms 600 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 512 KB Output is correct
2 Correct 30 ms 588 KB Output is correct
3 Correct 30 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 7 ms 348 KB Output is correct
7 Correct 20 ms 596 KB Output is correct
8 Correct 29 ms 580 KB Output is correct
9 Correct 30 ms 596 KB Output is correct
10 Correct 30 ms 592 KB Output is correct
11 Correct 27 ms 596 KB Output is correct
12 Correct 20 ms 588 KB Output is correct
13 Correct 31 ms 576 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 512 KB Output is correct
2 Correct 30 ms 588 KB Output is correct
3 Correct 30 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 7 ms 348 KB Output is correct
7 Correct 20 ms 596 KB Output is correct
8 Correct 29 ms 580 KB Output is correct
9 Correct 30 ms 596 KB Output is correct
10 Correct 30 ms 592 KB Output is correct
11 Correct 27 ms 596 KB Output is correct
12 Correct 20 ms 588 KB Output is correct
13 Correct 31 ms 576 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Execution timed out 1025 ms 15956 KB Time limit exceeded
16 Halted 0 ms 0 KB -