Submission #560361

# Submission time Handle Problem Language Result Execution time Memory
560361 2022-05-11T10:21:30 Z Koosha_mv Sorting (IOI15_sorting) C++14
100 / 100
187 ms 27832 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#include "sorting.h"
 
const int N=2e5+99;
 
int n,m,a[N],b[N],s[N],t[N],pos[N],mark[N];
vector<pair<int,int>> ans;
 
int Cnt(){
	fill(mark,mark+N,0);
	int cnt=0;
	f(i,0,n){
		if(mark[i]) continue ;
		cnt++;
		for(int u=i;mark[u]==0;u=b[u]){
			mark[u]=1;
		}
	}
	return cnt;
}
bool check(int x){
	f(i,0,n) b[i]=a[i];
	f(i,0,x+1) swap(b[s[i]],b[t[i]]);
	return n-Cnt()<=x+1;
}
void do_it(){
	fill(mark,mark+N,0);
	ans.clear();
	f(i,0,n){
		if(mark[i]) continue ;
		vector<int> vec;
		for(int u=i;mark[u]==0;u=b[u]){
			mark[u]=1;
			vec.pb(u);
		}
		f(i,1,vec.size()) ans.pb({vec[i-1],vec[i]});
	}
}
int findSwapPairs(int _n, int A[], int _m, int S[], int T[], int P[], int Q[]) {
  n=_n,m=_m;
  f(i,0,n) a[i]=A[i];
  f(i,0,m) P[i]=Q[i]=0;
	int c=1;
	f(i,0,n){
		a[i]=A[i];
		s[i]=S[i];
		t[i]=T[i];
		if(a[i]!=i) c=0;
	}
	if(c) return 0;
	int l=-1,r=m;
	while(l+1<r){
		int mid=(l+r)>>1;
		if(check(mid)) r=mid;
		else l=mid;
	}
	c=check(r);
	do_it();
	while(ans.size()<r+1) ans.pb({0,0});
	f(i,0,n) pos[a[i]]=i;
	f(i,0,r+1){
		swap(pos[a[s[i]]],pos[a[t[i]]]);
		swap(a[s[i]],a[t[i]]);
		P[i]=pos[ans[i].F];
		Q[i]=pos[ans[i].S];
		swap(pos[a[P[i]]],pos[a[Q[i]]]);
		swap(a[P[i]],a[Q[i]]);
	}
	f(i,0,n) if(a[i]!=i){
		assert(0);
	}
	return r+1;
}

Compilation message

sorting.cpp: In function 'void do_it()':
sorting.cpp:56:5: warning: declaration of 'i' shadows a previous local [-Wshadow]
   56 |   f(i,1,vec.size()) ans.pb({vec[i-1],vec[i]});
      |     ^
sorting.cpp:8:26: note: in definition of macro 'f'
    8 | #define f(i,a,b) for(int i=a;i<b;i++)
      |                          ^
sorting.cpp:49:4: note: shadowed declaration is here
   49 |  f(i,0,n){
      |    ^
sorting.cpp:8:26: note: in definition of macro 'f'
    8 | #define f(i,a,b) for(int i=a;i<b;i++)
      |                          ^
sorting.cpp:8:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   56 |   f(i,1,vec.size()) ans.pb({vec[i-1],vec[i]});
      |     ~~~~~~~~~~~~~~             
sorting.cpp:56:3: note: in expansion of macro 'f'
   56 |   f(i,1,vec.size()) ans.pb({vec[i-1],vec[i]});
      |   ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:79:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   79 |  while(ans.size()<r+1) ans.pb({0,0});
      |        ~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1096 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1096 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 2 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 1364 KB Output is correct
22 Correct 2 ms 1236 KB Output is correct
23 Correct 2 ms 1364 KB Output is correct
24 Correct 2 ms 1236 KB Output is correct
25 Correct 2 ms 1236 KB Output is correct
26 Correct 2 ms 1364 KB Output is correct
27 Correct 2 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1236 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 2 ms 1236 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 1236 KB Output is correct
6 Correct 2 ms 1236 KB Output is correct
7 Correct 2 ms 1236 KB Output is correct
8 Correct 2 ms 1236 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 3 ms 1264 KB Output is correct
11 Correct 2 ms 1236 KB Output is correct
12 Correct 3 ms 1236 KB Output is correct
13 Correct 2 ms 1236 KB Output is correct
14 Correct 2 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1236 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 2 ms 1236 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 1236 KB Output is correct
6 Correct 2 ms 1236 KB Output is correct
7 Correct 2 ms 1236 KB Output is correct
8 Correct 2 ms 1236 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 3 ms 1264 KB Output is correct
11 Correct 2 ms 1236 KB Output is correct
12 Correct 3 ms 1236 KB Output is correct
13 Correct 2 ms 1236 KB Output is correct
14 Correct 2 ms 1236 KB Output is correct
15 Correct 144 ms 17576 KB Output is correct
16 Correct 170 ms 25720 KB Output is correct
17 Correct 166 ms 27084 KB Output is correct
18 Correct 51 ms 19908 KB Output is correct
19 Correct 153 ms 25344 KB Output is correct
20 Correct 150 ms 26132 KB Output is correct
21 Correct 151 ms 26116 KB Output is correct
22 Correct 145 ms 22580 KB Output is correct
23 Correct 163 ms 27832 KB Output is correct
24 Correct 185 ms 27740 KB Output is correct
25 Correct 186 ms 27556 KB Output is correct
26 Correct 164 ms 26088 KB Output is correct
27 Correct 139 ms 25436 KB Output is correct
28 Correct 181 ms 27328 KB Output is correct
29 Correct 187 ms 26656 KB Output is correct
30 Correct 110 ms 24836 KB Output is correct
31 Correct 173 ms 27244 KB Output is correct
32 Correct 170 ms 27072 KB Output is correct
33 Correct 184 ms 27760 KB Output is correct
34 Correct 163 ms 27440 KB Output is correct
35 Correct 143 ms 25008 KB Output is correct
36 Correct 67 ms 23596 KB Output is correct
37 Correct 173 ms 27724 KB Output is correct
38 Correct 178 ms 27004 KB Output is correct
39 Correct 175 ms 26536 KB Output is correct