Submission #292045

# Submission time Handle Problem Language Result Execution time Memory
292045 2020-09-06T08:32:22 Z mat_v Sorting (IOI15_sorting) C++14
100 / 100
380 ms 37676 KB
#include "sorting.h"
#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 800005

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], i});
        int fr1 = tmp[i];
        int fr2 = i;
        int t1 = poz[fr1];
        int t2 = poz[fr2];
        poz[fr2] = t1;
        poz[fr1] = t2;
        swap(tmp[t1], tmp[t2]);
    }
    //ff(i,0,n - 1)assert(tmp[i] == i);
    int kol = sols.size();
    //for(auto c:sols)cout << c.xx << " " << c.yy << "\n";
    if(kol > t)return 0;
    ff(i,kol,t)sols.pb({0,0});
    //cout << "ok\n";
    ff(i,0,n - 1)tmp[i] = a[i];
    ff(i,0,n - 1)poz[tmp[i]] = i;
    ff(i,0,t - 1){
        /// prvi
        int fr1 = tmp[x[i]];
        int fr2 = tmp[y[i]];
        int t1 = poz[fr1];
        int t2 = poz[fr2];
        poz[fr2] = t1;
        poz[fr1] = t2;
        swap(tmp[t1], tmp[t2]);

        fr1 = sols[i].xx;
        fr2 = sols[i].yy;
        //cout << i << " " << sols[i].xx << " " << sols[i].yy << "\n";
        t1 = poz[fr1];
        t2 = poz[fr2];
        p[i] = t1;
        q[i] = t2;
        poz[fr2] = t1;
        poz[fr1] = t2;
        swap(tmp[t1], tmp[t2]);
    }
    //ff(i,0,n - 2)assert(tmp[i] < tmp[i + 1]);
    return 1;
}

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(a[i] >= a[i + 1])da = 0;
    }
    if(da)return 0;
    int l = 1;
    int r = m;
   // return check(3);
    //return 1;
    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];
    }
    ff(i,0,l - 1){
        swap(S[X[i]], S[Y[i]]);
        swap(S[P[i]], S[Q[i]]);
    }
    //ff(i,0,n - 2)assert(S[i] < S[i + 1]);
    //assert(l <= m);
    return l;
}

Compilation message

sorting.cpp: In function 'bool check(int)':
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #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,n - 1)tmp[i] = a[i];
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:38:5: note: in expansion of macro 'ff'
   38 |     ff(i,0,t - 1){
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #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)poz[tmp[i]] = i;
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:42:5: note: in expansion of macro 'ff'
   42 |     ff(i,0,n - 1){
      |     ^~
sorting.cpp:54:24: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   54 |     int kol = sols.size();
      |               ~~~~~~~~~^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:57:5: note: in expansion of macro 'ff'
   57 |     ff(i,kol,t)sols.pb({0,0});
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:59:5: note: in expansion of macro 'ff'
   59 |     ff(i,0,n - 1)tmp[i] = a[i];
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:60:5: note: in expansion of macro 'ff'
   60 |     ff(i,0,n - 1)poz[tmp[i]] = i;
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:61:5: note: in expansion of macro 'ff'
   61 |     ff(i,0,t - 1){
      |     ^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #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,m - 1){
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:92:5: note: in expansion of macro 'ff'
   92 |     ff(i,0,n - 1){
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:95:5: note: in expansion of macro 'ff'
   95 |     ff(i,0,m - 1){
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:100:5: note: in expansion of macro 'ff'
  100 |     ff(i,0,n - 2){
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:116:5: note: in expansion of macro 'ff'
  116 |     ff(i,0,m - 1){
      |     ^~
sorting.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
sorting.cpp:120:5: note: in expansion of macro 'ff'
  120 |     ff(i,0,l - 1){
      |     ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 2 ms 896 KB Output is correct
22 Correct 2 ms 896 KB Output is correct
23 Correct 2 ms 1024 KB Output is correct
24 Correct 2 ms 896 KB Output is correct
25 Correct 2 ms 1024 KB Output is correct
26 Correct 2 ms 896 KB Output is correct
27 Correct 2 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 251 ms 25784 KB Output is correct
16 Correct 278 ms 34060 KB Output is correct
17 Correct 347 ms 35748 KB Output is correct
18 Correct 49 ms 22776 KB Output is correct
19 Correct 260 ms 34828 KB Output is correct
20 Correct 263 ms 35604 KB Output is correct
21 Correct 223 ms 35720 KB Output is correct
22 Correct 253 ms 31356 KB Output is correct
23 Correct 259 ms 37044 KB Output is correct
24 Correct 324 ms 36640 KB Output is correct
25 Correct 334 ms 36032 KB Output is correct
26 Correct 249 ms 35700 KB Output is correct
27 Correct 238 ms 34776 KB Output is correct
28 Correct 328 ms 36456 KB Output is correct
29 Correct 347 ms 35968 KB Output is correct
30 Correct 199 ms 34508 KB Output is correct
31 Correct 380 ms 36684 KB Output is correct
32 Correct 271 ms 35864 KB Output is correct
33 Correct 361 ms 36548 KB Output is correct
34 Correct 282 ms 36600 KB Output is correct
35 Correct 203 ms 34320 KB Output is correct
36 Correct 105 ms 33856 KB Output is correct
37 Correct 361 ms 37676 KB Output is correct
38 Correct 346 ms 36036 KB Output is correct
39 Correct 354 ms 36348 KB Output is correct