Submission #679101

# Submission time Handle Problem Language Result Execution time Memory
679101 2023-01-07T13:08:03 Z n0sk1ll Sorting (IOI15_sorting) C++17
0 / 100
3 ms 368 KB
#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)
#define sum_overflow(a,b) __builtin_add_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)
#define mul_overflow(a,b) __builtin_mul_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;







//Note to self: Check for overflow

struct disjoint_set_union
{
    vector<int> up;

    void build(int n)
    {
        up.resize(n,-1);
    }

    int Up(int x)
    {
        if (up[x]<0) return x;
        return up[x]=Up(up[x]);
    }

    bool same(int a, int b)
    {
        return Up(a)==Up(b);
    }

    void dsu(int a, int b)
    {
        a=Up(a),b=Up(b);
        if (a==b) return;
        up[a]=b;
    }
};

bool moze(int n, int* S, int m, int *X, int* Y, int* P, int* Q, int okle)
{
    disjoint_set_union want,cur;
    want.build(n),cur.build(n);
    vector<int> extras;

    ff(i,0,n) want.dsu(i,S[i]);
    bff(i,0,okle)
    {
        if (cur.same(X[i],Y[i]) == want.same(X[i],Y[i])) P[i]=X[i],Q[i]=Y[i],extras.pb(i);
        else P[i]=0,Q[i]=0,want.dsu(X[i],Y[i]);
    }

    ff(i,0,n) if (!cur.same(i,want.Up(i)))
    {
        if (extras.empty()) return false;
        int gde=extras.back(); extras.popb();
        P[gde]=i,Q[gde]=want.Up(i),want.dsu(i,want.Up(i));
    }
    ff(i,0,n) if (!want.same(i,cur.Up(i))) return false;
    return true;
}

int findSwapPairs(int n, int* S, int m, int* X, int* Y, int* P, int* Q)
{
    int l=-1,r=m;
    while (r-l>1)
    {
        int mid=(l+r)/2;
        if (moze(n,S,m,X,Y,P,Q,mid)) r=mid;
        else l=mid;
    }

    moze(n,S,m,X,Y,P,Q,r);
    return r;
}

Compilation message

sorting.cpp: In function 'bool moze(int, int*, int, int*, int*, int*, int*, int)':
sorting.cpp:13:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   13 | #define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
      |                            ^
sorting.cpp:74:5: note: in expansion of macro 'ff'
   74 |     ff(i,0,n) want.dsu(i,S[i]);
      |     ^~
sorting.cpp:15:29: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
      |                             ^
sorting.cpp:75:5: note: in expansion of macro 'bff'
   75 |     bff(i,0,okle)
      |     ^~~
sorting.cpp:13:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   13 | #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) if (!cur.same(i,want.Up(i)))
      |     ^~
sorting.cpp:13:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   13 | #define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
      |                            ^
sorting.cpp:87:5: note: in expansion of macro 'ff'
   87 |     ff(i,0,n) if (!want.same(i,cur.Up(i))) return false;
      |     ^~
sorting.cpp:68:30: warning: unused parameter 'm' [-Wunused-parameter]
   68 | bool moze(int n, int* S, int m, int *X, int* Y, int* P, int* Q, int okle)
      |                          ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 368 KB Output isn't correct
2 Halted 0 ms 0 KB -