Submission #1025361

# Submission time Handle Problem Language Result Execution time Memory
1025361 2024-07-17T00:16:55 Z vjudge1 Sorting (IOI15_sorting) C++17
100 / 100
129 ms 22936 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
bitset<200100>vis;
int howmanydoineed(vector<int>v){
    int n=v.size(),ans=n;
	for(int i=0;i<n;i++)
		if(!vis[i]){
			ans--;
			int k=i;
			do {
				vis[k]=1;
				k=v[k];
			}while(k-i);
		}
	vis.reset();
	return ans;
}
vector<pair<int,int>>SWP;
void getswaps(vector<int>v){
	int n=v.size();
	for(int i=0;i<n;i++)
		if(!vis[i]){
			int k=i,VL=v[i];
			do {
				SWP.push_back({VL,v[v[k]]});
				VL=v[v[k]];
				vis[k]=1;
				k=v[k];
			}while(k-i);
			SWP.pop_back();
		}
	vis.reset();
}
bool docheck(int N,int k,int S[],int X[],int Y[]){
	vector<int>v(N);
	for(int i=0;i<N;i++)
		v[i]=S[i];
	for(int i=0;i<k;i++)
		swap(v[X[i]],v[Y[i]]);
	return howmanydoineed(v)<=k;
}
void docheck2(int N,int k,int S[],int X[],int Y[]){
	vector<int>v(N);
	for(int i=0;i<N;i++)
		v[i]=S[i];
	for(int i=0;i<k;i++)
		swap(v[X[i]],v[Y[i]]);
	getswaps(v);
	if(SWP.size()<k)
		SWP.push_back({0,0});
}
vector<int>vals;
vector<int>pos;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	int l=0,r=M,res=1e9;
	while(l<=r){
		int mid=l+r>>1;
		if(docheck(N,mid,S,X,Y))
			r=mid-1,res=mid;
		else l=mid+1;
	}
	docheck2(N,res,S,X,Y);
	pos.resize(N);
	vals.resize(N);
	for(int i=0;i<N;i++)
		vals[i]=S[i],pos[S[i]]=i;
	int k=0;
	for(auto[x,y]:SWP){
		int ermx=X[k],ermy=Y[k];
		int K=vals[ermx],M=vals[ermy];
		swap(vals[ermx],vals[ermy]);
		swap(pos[K],pos[M]);
		int l=pos[x],r=pos[y];
		P[k]=l,Q[k++]=r;
		swap(vals[l],vals[r]);
		swap(pos[x],pos[y]);
	}
	return res;
}

Compilation message

sorting.cpp: In function 'int howmanydoineed(std::vector<int>)':
sorting.cpp:6:17: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
    6 |     int n=v.size(),ans=n;
      |           ~~~~~~^~
sorting.cpp: In function 'void getswaps(std::vector<int>)':
sorting.cpp:21:14: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   21 |  int n=v.size();
      |        ~~~~~~^~
sorting.cpp: In function 'void docheck2(int, int, int*, int*, int*)':
sorting.cpp:50:15: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |  if(SWP.size()<k)
      |     ~~~~~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:58:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   58 |   int mid=l+r>>1;
      |           ~^~
sorting.cpp:71:20: warning: declaration of 'int M' shadows a parameter [-Wshadow]
   71 |   int K=vals[ermx],M=vals[ermy];
      |                    ^
sorting.cpp:55:39: note: shadowed declaration is here
   55 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
sorting.cpp:74:7: warning: declaration of 'l' shadows a previous local [-Wshadow]
   74 |   int l=pos[x],r=pos[y];
      |       ^
sorting.cpp:56:6: note: shadowed declaration is here
   56 |  int l=0,r=M,res=1e9;
      |      ^
sorting.cpp:74:16: warning: declaration of 'r' shadows a previous local [-Wshadow]
   74 |   int l=pos[x],r=pos[y];
      |                ^
sorting.cpp:56:10: note: shadowed declaration is here
   56 |  int l=0,r=M,res=1e9;
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 392 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 392 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 436 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 392 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 436 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 452 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 352 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 628 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 572 KB Output is correct
25 Correct 1 ms 856 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 105 ms 19604 KB Output is correct
16 Correct 106 ms 20088 KB Output is correct
17 Correct 117 ms 21460 KB Output is correct
18 Correct 61 ms 15052 KB Output is correct
19 Correct 106 ms 19436 KB Output is correct
20 Correct 111 ms 20056 KB Output is correct
21 Correct 110 ms 20148 KB Output is correct
22 Correct 102 ms 16412 KB Output is correct
23 Correct 105 ms 21756 KB Output is correct
24 Correct 125 ms 21636 KB Output is correct
25 Correct 127 ms 21828 KB Output is correct
26 Correct 106 ms 20000 KB Output is correct
27 Correct 100 ms 19300 KB Output is correct
28 Correct 124 ms 21192 KB Output is correct
29 Correct 116 ms 21340 KB Output is correct
30 Correct 81 ms 17968 KB Output is correct
31 Correct 124 ms 21708 KB Output is correct
32 Correct 112 ms 20948 KB Output is correct
33 Correct 119 ms 21888 KB Output is correct
34 Correct 117 ms 21712 KB Output is correct
35 Correct 101 ms 19272 KB Output is correct
36 Correct 53 ms 16076 KB Output is correct
37 Correct 121 ms 22936 KB Output is correct
38 Correct 129 ms 21772 KB Output is correct
39 Correct 117 ms 22288 KB Output is correct