Submission #291430

#TimeUsernameProblemLanguageResultExecution timeMemory
291430mat_vSorting (IOI15_sorting)C++14
0 / 100
4 ms1152 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 1000000007
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define maxn 200005

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());



int n;
int m;
int a[maxn];
int x[maxn];
int y[maxn];
int p[maxn];
int q[maxn];
int tmp[maxn];
int poz[maxn];

bool check(int t){
    vector<pii> sols;
    ff(i,0,n - 1)tmp[i] = a[i];
    ff(i,0,t - 1){
        swap(tmp[x[i]], tmp[y[i]]);
    }
    ff(i,0,n - 1)poz[tmp[i]] = i;
    ff(i,0,n - 1){
        if(poz[i] == i)continue;
        sols.pb({tmp[i], tmp[poz[i]]});
        int pom = tmp[i];
        poz[pom] = poz[tmp[poz[i]]];
        swap(tmp[i], tmp[poz[i]]);
    }
    int kol = sols.size();
    if(kol > t)return 0;
    ff(i,0,n - 1)tmp[i] = a[i];
    ff(i,0,n - 1)poz[tmp[i]] = i;
    ff(i,0,kol - 1){
        /// prvi
        int fr1 = x[i];
        int fr2 = y[i];
        int t1 = poz[fr1];
        int t2 = poz[fr2];
        poz[fr2] = t1;
        poz[fr1] = t2;
        swap(tmp[x[i]], tmp[y[i]]);

        fr1 = poz[sols[i].xx];
        fr2 = poz[sols[i].yy];
        t1 = poz[fr1];
        t2 = poz[fr2];
        p[i] = fr1;
        q[i] = fr2;
        poz[fr2] = t1;
        poz[fr1] = t2;
        swap(tmp[fr1], tmp[fr2]);

    }
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n = N;
    m = M;
    ff(i,0,m - 1){
        p[i] = q[i] = 0;
    }
    ff(i,0,n - 1){
        a[i] = S[i];
    }
    ff(i,0,m - 1){
        x[i] = X[i];
        y[i] = Y[i];
    }
    bool da = 1;
    ff(i,0,n - 2){
        if(x[i] >= x[i + 1])da = 0;
    }
    if(da)return 0;
    int l = 1;
    int r = m;
    while(l < r){
        int mid = (l + r) / 2;
        if(check(mid)){
            r = mid;
        }
        else l = mid + 1;
    }
    check(l);
    ff(i,0,m - 1){
        P[i] = p[i];
        Q[i] = q[i];
    }
    return l;
}

Compilation message (stderr)

sorting.cpp: In function 'bool check(int)':
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:36:5: note: in expansion of macro 'ff'
   36 |     ff(i,0,n - 1)tmp[i] = a[i];
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:37:5: note: in expansion of macro 'ff'
   37 |     ff(i,0,t - 1){
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:40:5: note: in expansion of macro 'ff'
   40 |     ff(i,0,n - 1)poz[tmp[i]] = i;
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:41:5: note: in expansion of macro 'ff'
   41 |     ff(i,0,n - 1){
      |     ^~
sorting.cpp:48:24: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   48 |     int kol = sols.size();
      |               ~~~~~~~~~^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:50:5: note: in expansion of macro 'ff'
   50 |     ff(i,0,n - 1)tmp[i] = a[i];
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:51:5: note: in expansion of macro 'ff'
   51 |     ff(i,0,n - 1)poz[tmp[i]] = i;
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:52:5: note: in expansion of macro 'ff'
   52 |     ff(i,0,kol - 1){
      |     ^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:78:5: note: in expansion of macro 'ff'
   78 |     ff(i,0,m - 1){
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:81:5: note: in expansion of macro 'ff'
   81 |     ff(i,0,n - 1){
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:84:5: note: in expansion of macro 'ff'
   84 |     ff(i,0,m - 1){
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:89:5: note: in expansion of macro 'ff'
   89 |     ff(i,0,n - 2){
      |     ^~
sorting.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:103:5: note: in expansion of macro 'ff'
  103 |     ff(i,0,m - 1){
      |     ^~
sorting.cpp: In function 'bool check(int)':
sorting.cpp:35:17: warning: control reaches end of non-void function [-Wreturn-type]
   35 |     vector<pii> sols;
      |                 ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...