답안 #59176

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
59176 2018-07-20T22:10:24 Z TadijaSebez 정렬하기 (IOI15_sorting) C++11
100 / 100
286 ms 18116 KB
#include <stdio.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
const int N=200050;
const int M=3*N;
int a[N],b[N],c[N],id[N],qx[M],qy[M];
bool was[N];
int Calc(int n)
{
	int i,j,ret=0;
	for(i=1;i<=n;i++) was[i]=0;
	for(i=1;i<=n;i++)
	{
		if(was[i]) continue;
		ret++;
		for(j=i;!was[j];j=b[j]) was[j]=1;
	}
	return n-ret;
}
void Swap(int &a, int &b){ a^=b;b^=a;a^=b;}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
	int n,m,i,j,x,y;
	//scanf("%i",&n);
	n=N;
	for(i=1;i<=n;i++) a[i]=S[i-1]+1;//scanf("%i",&a[i]),a[i]++;
	//scanf("%i",&m);
	m=M;
	for(i=1;i<=m;i++) qx[i]=X[i-1]+1,qy[i]=Y[i-1]+1;//scanf("%i %i",&qx[i],&qy[i]),qx[i]++,qy[i]++;
	int top=m,bot=0,mid,ans=m;
	while(top>=bot)
	{
		mid=top+bot>>1;
		for(i=1;i<=n;i++) b[i]=a[i];
		for(i=1;i<=mid;i++)
		{
			x=qx[i];y=qy[i];
			if(x!=y) Swap(b[x],b[y]);
		}
		//for(i=1;i<=n;i++) printf("%i ",b[i]);printf("\n");
		//printf("%i %i\n",mid,Calc(n));
		if(Calc(n)<=mid) ans=mid,top=mid-1;
		else bot=mid+1;
	}
	//printf("%i\n",ans);
	for(i=1;i<=n;i++) b[i]=c[i]=a[i],id[a[i]]=i,was[i]=0;
	for(i=1;i<=ans;i++)
	{
		x=qx[i];y=qy[i];
		if(x!=y) Swap(b[x],b[y]);
	}
	int k=0;
	vector<pair<int,int> > sol;
	for(i=1;i<=n;i++)
	{
		//for(j=1;j<=n;j++) printf("%i ",b[j]);printf("\n");
		if(b[i]!=i)
		{
			k++;
			x=qx[k];y=qy[k];
			if(x!=y) Swap(id[c[x]],id[c[y]]),Swap(c[x],c[y]);
			//printf("<- %i %i\n",b[i]-1,b[b[i]]-1);
			//printf("%i %i\n",id[b[i]]-1,id[b[b[i]]]-1);
			sol.pb(mp(id[b[i]]-1,id[b[b[i]]]-1));
			Swap(c[id[b[i]]],c[id[b[b[i]]]]);
			Swap(id[b[i]],id[b[b[i]]]);
			int p=b[i];
			Swap(b[i],b[p]);
			i--;
		}
	}
	while(k<ans) sol.pb(mp(0,0)),k++;//printf("0 0\n"),k++;
	for(i=0;i<ans;i++) P[i]=sol[i].first,Q[i]=sol[i].second;
	return ans;
}

Compilation message

sorting.cpp: In function 'void Swap(int&, int&)':
sorting.cpp:22:25: warning: declaration of 'b' shadows a global declaration [-Wshadow]
 void Swap(int &a, int &b){ a^=b;b^=a;a^=b;}
                         ^
sorting.cpp:8:10: note: shadowed declaration is here
 int a[N],b[N],c[N],id[N],qx[M],qy[M];
          ^
sorting.cpp:22:25: warning: declaration of 'a' shadows a global declaration [-Wshadow]
 void Swap(int &a, int &b){ a^=b;b^=a;a^=b;}
                         ^
sorting.cpp:8:5: note: shadowed declaration is here
 int a[N],b[N],c[N],id[N],qx[M],qy[M];
     ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:23:76: warning: declaration of 'M' shadows a global declaration [-Wshadow]
 int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
                                                                            ^
sorting.cpp:7:11: note: shadowed declaration is here
 const int M=3*N;
           ^
sorting.cpp:23:76: warning: declaration of 'N' shadows a global declaration [-Wshadow]
 int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
                                                                            ^
sorting.cpp:6:11: note: shadowed declaration is here
 const int N=200050;
           ^
sorting.cpp:35:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   mid=top+bot>>1;
       ~~~^~~~
sorting.cpp:25:12: warning: unused variable 'j' [-Wunused-variable]
  int n,m,i,j,x,y;
            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 4 ms 640 KB Output is correct
23 Correct 4 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 193 ms 15324 KB Output is correct
16 Correct 207 ms 15708 KB Output is correct
17 Correct 200 ms 16912 KB Output is correct
18 Correct 68 ms 12792 KB Output is correct
19 Correct 144 ms 15532 KB Output is correct
20 Correct 197 ms 16052 KB Output is correct
21 Correct 169 ms 16068 KB Output is correct
22 Correct 211 ms 16632 KB Output is correct
23 Correct 201 ms 17072 KB Output is correct
24 Correct 198 ms 17008 KB Output is correct
25 Correct 241 ms 17156 KB Output is correct
26 Correct 194 ms 15964 KB Output is correct
27 Correct 170 ms 15320 KB Output is correct
28 Correct 201 ms 16720 KB Output is correct
29 Correct 286 ms 16852 KB Output is correct
30 Correct 116 ms 14956 KB Output is correct
31 Correct 227 ms 17208 KB Output is correct
32 Correct 212 ms 16532 KB Output is correct
33 Correct 204 ms 17272 KB Output is correct
34 Correct 212 ms 16960 KB Output is correct
35 Correct 167 ms 15360 KB Output is correct
36 Correct 62 ms 13816 KB Output is correct
37 Correct 237 ms 18116 KB Output is correct
38 Correct 226 ms 17156 KB Output is correct
39 Correct 232 ms 17528 KB Output is correct