답안 #679167

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679167 2023-01-07T15:40:55 Z n0sk1ll 정렬하기 (IOI15_sorting) C++14
100 / 100
210 ms 23096 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)
{
    vector<int> p(n);
    ff(i,0,n) p[i]=S[i];
    ff(i,0,okle) swap(p[X[i]],p[Y[i]]);

    disjoint_set_union nesto;
    nesto.build(n);

    int komp=0;
    ff(i,0,n) nesto.dsu(i,p[i]);
    ff(i,0,n) if (nesto.up[i]<0) komp++;
    return komp+okle>=n; //znaci mogu da unitsim svaku (veliku) komponentu >:)
}

bool vi[600005];
vector<int> z[2];

void konstruktuj(int n, int *S, int m, int *X, int *Y, int *P, int *Q, int okle)
{
    vector<int> p(n),gdep(n);
    ff(i,0,n) p[i]=S[i];

    fill(vi,vi+n,false);
	ff(i,0,okle) swap(p[X[i]],p[Y[i]]);
	ff(i,0,n) if(!vi[i]) while(!vi[i]) {vi[i]=true; if(!vi[p[i]]) {z[0].push_back(i); z[1].push_back(p[i]);} i=p[i];}
	ff(i,0,n) gdep[S[i]]=i;
	for(int i=0;i<okle;i++)
	{
		swap(S[X[i]],S[Y[i]]);
		gdep[S[X[i]]]=X[i];
		gdep[S[Y[i]]]=Y[i];
		if(i>=z[0].size()) P[i]=Q[i]=0;
		else
		{
			if(z[0][i]<z[1][i]) swap(z[0][i],z[1][i]);
			P[i]=gdep[z[0][i]];
			Q[i]=gdep[z[1][i]];
			swap(S[P[i]],S[Q[i]]);
			gdep[S[P[i]]]=P[i];
			gdep[S[Q[i]]]=Q[i];

		}
	}
}

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;
    }

    konstruktuj(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:71:5: note: in expansion of macro 'ff'
   71 |     ff(i,0,n) p[i]=S[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:72:5: note: in expansion of macro 'ff'
   72 |     ff(i,0,okle) swap(p[X[i]],p[Y[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:78:5: note: in expansion of macro 'ff'
   78 |     ff(i,0,n) nesto.dsu(i,p[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:79:5: note: in expansion of macro 'ff'
   79 |     ff(i,0,n) if (nesto.up[i]<0) komp++;
      |     ^~
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)
      |                          ~~~~^
sorting.cpp:68:54: warning: unused parameter 'P' [-Wunused-parameter]
   68 | bool moze(int n, int* S, int m, int *X, int* Y, int* P, int* Q, int okle)
      |                                                 ~~~~~^
sorting.cpp:68:62: warning: unused parameter 'Q' [-Wunused-parameter]
   68 | bool moze(int n, int* S, int m, int *X, int* Y, int* P, int* Q, int okle)
      |                                                         ~~~~~^
sorting.cpp: In function 'void konstruktuj(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:89:5: note: in expansion of macro 'ff'
   89 |     ff(i,0,n) p[i]=S[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:92:2: note: in expansion of macro 'ff'
   92 |  ff(i,0,okle) swap(p[X[i]],p[Y[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:93:2: note: in expansion of macro 'ff'
   93 |  ff(i,0,n) if(!vi[i]) while(!vi[i]) {vi[i]=true; if(!vi[p[i]]) {z[0].push_back(i); z[1].push_back(p[i]);} i=p[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:94:2: note: in expansion of macro 'ff'
   94 |  ff(i,0,n) gdep[S[i]]=i;
      |  ^~
sorting.cpp:100:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |   if(i>=z[0].size()) P[i]=Q[i]=0;
      |      ~^~~~~~~~~~~~~
sorting.cpp:86:37: warning: unused parameter 'm' [-Wunused-parameter]
   86 | void konstruktuj(int n, int *S, int m, int *X, int *Y, int *P, int *Q, int okle)
      |                                 ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 484 KB Output is correct
25 Correct 1 ms 512 KB Output is correct
26 Correct 1 ms 436 KB Output is correct
27 Correct 1 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 392 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 392 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 162 ms 19452 KB Output is correct
16 Correct 159 ms 20128 KB Output is correct
17 Correct 177 ms 20988 KB Output is correct
18 Correct 73 ms 15036 KB Output is correct
19 Correct 137 ms 19004 KB Output is correct
20 Correct 151 ms 19648 KB Output is correct
21 Correct 145 ms 19680 KB Output is correct
22 Correct 146 ms 17704 KB Output is correct
23 Correct 173 ms 23096 KB Output is correct
24 Correct 181 ms 22724 KB Output is correct
25 Correct 171 ms 22452 KB Output is correct
26 Correct 147 ms 19612 KB Output is correct
27 Correct 137 ms 18988 KB Output is correct
28 Correct 175 ms 22532 KB Output is correct
29 Correct 179 ms 21780 KB Output is correct
30 Correct 121 ms 17976 KB Output is correct
31 Correct 167 ms 22316 KB Output is correct
32 Correct 166 ms 22168 KB Output is correct
33 Correct 210 ms 22716 KB Output is correct
34 Correct 174 ms 22636 KB Output is correct
35 Correct 147 ms 18804 KB Output is correct
36 Correct 62 ms 16080 KB Output is correct
37 Correct 188 ms 22768 KB Output is correct
38 Correct 183 ms 22348 KB Output is correct
39 Correct 192 ms 21996 KB Output is correct