Submission #452147

# Submission time Handle Problem Language Result Execution time Memory
452147 2021-08-03T22:39:44 Z PedroBigMan Sorting (IOI15_sorting) C++14
100 / 100
432 ms 24548 KB
#include "sorting.h"
/*
Author of all code: Pedro BIGMAN Dias
Last edit: 15/02/2021
*/
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 500000000LL
#define EPS 0.00000001
#define pi 3.14159
ll mod=1000000007LL;

template<class A=ll> 
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}

template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} 

vector<vector<ll> > CycleDecomp(vector<ll> p) //cycle decomposition of permutation
{
	ll N = p.size(); vector<vector<ll> > ans; vector<ll> cur;
	vector<bool> visited; REP(i,0,N) {visited.pb(false);} 
	ll node;
	REP(i,0,N)
	{
		if(visited[i]) {continue;}
		node=i; cur.pb(node); node=p[node];
		while(node!=i)
		{
			cur.pb(node); node=p[node];
		}
		REP(i,0,cur.size()) {visited[cur[i]]=true;}
		ans.pb(cur);
		cur.clear();
	}
	return ans;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) 
{
    vector<ll> p; REP(i,0,N) {p.pb(i);}
	ll lo=0; ll hi=M; ll mid; ll R;
	while(lo<hi)
	{
		mid=(hi+lo)/2;
		REP(i,0,N) {p[i]=S[i];}
		REP(i,0,mid) {swap(p[X[i]],p[Y[i]]);}
		ll trans = N-CycleDecomp(p).size();
		if(trans<=mid) {hi=mid;} else {lo=mid+1;}
	}
	R=lo;
	REP(i,0,N) {p[i]=S[i];}
	REP(i,0,R) {swap(p[X[i]],p[Y[i]]);}
	vector<vector<ll> > cycles = CycleDecomp(p);
	vector<pl> swa; 
	REP(i,0,cycles.size()) 
	{
		REP(j,0,cycles[i].size()-1) {swa.pb({cycles[i][j],cycles[i][j+1]});}
	}
	//REP(i,0,swa.size()) {cout<<swa[i].ff<<" "<<swa[i].ss<<endl;}
	REP(i,0,N) {p[i]=S[i];}
	vector<ll> pos; REP(i,0,N) {pos.pb(0LL);}
	REP(i,0,N) {pos[p[i]]=i;}
	REP(i,0,swa.size())
	{
		pos[p[X[i]]]=Y[i]; pos[p[Y[i]]]=X[i];
		swap(p[X[i]],p[Y[i]]);
		P[i]=pos[swa[i].ff]; Q[i]=pos[swa[i].ss];
		swap(pos[swa[i].ff],pos[swa[i].ss]); swap(p[P[i]],p[Q[i]]);
	}
	REP(i,swa.size(),R) {P[i]=0LL; Q[i]=0LL;}
	//cout<<R<<endl; REP(i,0,R) {cout<<P[i]<<" "<<Q[i]<<endl;}
	return R;
}


Compilation message

sorting.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("O3")
      | 
sorting.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    7 | #pragma GCC optimization ("unroll-loops")
      | 
sorting.cpp: In function 'std::vector<std::vector<long long int> > CycleDecomp(std::vector<long long int>)':
sorting.cpp:61:7: warning: declaration of 'i' shadows a previous local [-Wshadow]
   61 |   REP(i,0,cur.size()) {visited[cur[i]]=true;}
      |       ^
sorting.cpp:29:27: note: in definition of macro 'REP'
   29 | #define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
      |                           ^
sorting.cpp:53:6: note: shadowed declaration is here
   53 |  REP(i,0,N)
      |      ^
sorting.cpp:29:27: note: in definition of macro 'REP'
   29 | #define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
      |                           ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:97:21: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
   97 |   P[i]=pos[swa[i].ff]; Q[i]=pos[swa[i].ss];
      |                     ^
sorting.cpp:97:42: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
   97 |   P[i]=pos[swa[i].ff]; Q[i]=pos[swa[i].ss];
      |                                          ^
sorting.cpp:102:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  102 |  return R;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 292 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 292 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 292 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 2 ms 556 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 3 ms 588 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 2 ms 560 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 3 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 3 ms 588 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 2 ms 560 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 3 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 3 ms 460 KB Output is correct
15 Correct 214 ms 19252 KB Output is correct
16 Correct 227 ms 19064 KB Output is correct
17 Correct 264 ms 19972 KB Output is correct
18 Correct 394 ms 23464 KB Output is correct
19 Correct 368 ms 21848 KB Output is correct
20 Correct 361 ms 21600 KB Output is correct
21 Correct 324 ms 21812 KB Output is correct
22 Correct 217 ms 19652 KB Output is correct
23 Correct 265 ms 21128 KB Output is correct
24 Correct 240 ms 20596 KB Output is correct
25 Correct 246 ms 19880 KB Output is correct
26 Correct 363 ms 23052 KB Output is correct
27 Correct 348 ms 22476 KB Output is correct
28 Correct 287 ms 20388 KB Output is correct
29 Correct 298 ms 19852 KB Output is correct
30 Correct 432 ms 23556 KB Output is correct
31 Correct 316 ms 20204 KB Output is correct
32 Correct 229 ms 19748 KB Output is correct
33 Correct 236 ms 20552 KB Output is correct
34 Correct 249 ms 19460 KB Output is correct
35 Correct 378 ms 21532 KB Output is correct
36 Correct 397 ms 24548 KB Output is correct
37 Correct 252 ms 21712 KB Output is correct
38 Correct 243 ms 21160 KB Output is correct
39 Correct 235 ms 21256 KB Output is correct