Submission #345682

# Submission time Handle Problem Language Result Execution time Memory
345682 2021-01-07T20:58:30 Z ogibogi2004 Sorting (IOI15_sorting) C++14
100 / 100
158 ms 32420 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN=2e5+6;
vector<pair<int,int> >swaps;
int seq[MAXN],seq1[MAXN],seq2[MAXN];
bool vis[MAXN];
int where[MAXN];
int n;
vector<pair<int,int> >moves;
bool check(int x)
{
	for(int i=0;i<n;i++)
	{
		seq2[i]=seq[i];
	}
	for(int j=0;j<x&&j<swaps.size();j++)
	{
		swap(seq2[swaps[j].first],seq2[swaps[j].second]);
	}
	int cnt=0;
	memset(vis,0,sizeof(vis));
	moves.clear();
	/*cout<<x<<":";
	for(int i=0;i<n;i++)
	{
		cout<<seq1[i]<<" ";
	}
	cout<<endl;*/
	/*for(int i=0;i<n;i++)cout<<seq2[i]<<" ";
	cout<<endl;*/
	for(int i=0;i<n;i++)
	{
		if(seq2[i]!=i)
		{
			int u=i;
			for(;;)
			{
				int j=seq2[i];
				moves.push_back({seq2[i],seq2[j]});
				//cout<<i<<" "<<j<<endl;
				//cout<<seq2
				swap(seq2[i],seq2[j]);
				if(seq2[i]==i)break;
			}
			//swap(seq2[i],seq2[u]);
			cnt++;
			//moves.pop_back();
		}
	}
	//cout<<endl;
	//cout<<x<<" "<<cnt<<endl;
	if(moves.size()<=x)
	{
		while(moves.size()<x)moves.push_back({0,0});
		return 1;
	}
	else return 0;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n=N;
    for(int i=0;i<n;i++)seq[i]=S[i];
    for(int i=0;i<M;i++)swaps.push_back({X[i],Y[i]});
    int low=0,high=M,mid,ans;
    while(low<=high)
    {
		mid=(low+high)/2;
		if(check(mid))
		{
			ans=mid;
			high=mid-1;
		}
		else low=mid+1;
	}
	//cout<<ans<<endl;
	check(ans);
	for(int i=0;i<N;i++)
	{
		where[S[i]]=i;
	}
	/*for(int i=0;i<moves.size();i++)
	{
		cout<<moves[i].first<<" "<<moves[i].second<<endl;
	}*/
	for(int i=0;i<moves.size();i++)
	{
		int w1=S[X[i]],w2=S[Y[i]];
		swap(S[X[i]],S[Y[i]]);
		swap(where[w1],where[w2]);
		P[i]=where[moves[i].first];
		Q[i]=where[moves[i].second];
		swap(where[moves[i].first],where[moves[i].second]);
		swap(S[P[i]],S[Q[i]]);
		/*cout<<i<<":\n";
		for(int j=0;j<n;j++)
		{
			cout<<S[j]<<" ";
		}
		cout<<endl;
		for(int j=0;j<n;j++)
		{
			cout<<where[j]<<" ";
		}
		cout<<endl;*/
	}
	/*for(int i=0;i<moves.size();i++)
	{
		P[i]=moves[i].first;Q[i]=moves[i].second;
	}*/
	return moves.size();
}

Compilation message

sorting.cpp: In function 'bool check(int)':
sorting.cpp:17:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |  for(int j=0;j<x&&j<swaps.size();j++)
      |                   ~^~~~~~~~~~~~~
sorting.cpp:36:8: warning: unused variable 'u' [-Wunused-variable]
   36 |    int u=i;
      |        ^
sorting.cpp:53:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   53 |  if(moves.size()<=x)
      |     ~~~~~~~~~~~~^~~
sorting.cpp:55:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   55 |   while(moves.size()<x)moves.push_back({0,0});
      |         ~~~~~~~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:85:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |  for(int i=0;i<moves.size();i++)
      |              ~^~~~~~~~~~~~~
sorting.cpp:110:19: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  110 |  return moves.size();
      |         ~~~~~~~~~~^~
sorting.cpp:76:7: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |  check(ans);
      |  ~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 760 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 760 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 452 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 592 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 2 ms 712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 760 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 452 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 1 ms 592 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 2 ms 712 KB Output is correct
13 Correct 1 ms 592 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 592 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 2 ms 1104 KB Output is correct
22 Correct 2 ms 2896 KB Output is correct
23 Correct 2 ms 1104 KB Output is correct
24 Correct 2 ms 1104 KB Output is correct
25 Correct 2 ms 1104 KB Output is correct
26 Correct 2 ms 1104 KB Output is correct
27 Correct 2 ms 2896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2804 KB Output is correct
2 Correct 2 ms 1100 KB Output is correct
3 Correct 2 ms 848 KB Output is correct
4 Correct 2 ms 848 KB Output is correct
5 Correct 2 ms 848 KB Output is correct
6 Correct 2 ms 848 KB Output is correct
7 Correct 2 ms 864 KB Output is correct
8 Correct 2 ms 848 KB Output is correct
9 Correct 2 ms 848 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 848 KB Output is correct
12 Correct 2 ms 848 KB Output is correct
13 Correct 3 ms 848 KB Output is correct
14 Correct 1 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2804 KB Output is correct
2 Correct 2 ms 1100 KB Output is correct
3 Correct 2 ms 848 KB Output is correct
4 Correct 2 ms 848 KB Output is correct
5 Correct 2 ms 848 KB Output is correct
6 Correct 2 ms 848 KB Output is correct
7 Correct 2 ms 864 KB Output is correct
8 Correct 2 ms 848 KB Output is correct
9 Correct 2 ms 848 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 848 KB Output is correct
12 Correct 2 ms 848 KB Output is correct
13 Correct 3 ms 848 KB Output is correct
14 Correct 1 ms 720 KB Output is correct
15 Correct 128 ms 28288 KB Output is correct
16 Correct 140 ms 28832 KB Output is correct
17 Correct 149 ms 30620 KB Output is correct
18 Correct 67 ms 26524 KB Output is correct
19 Correct 114 ms 29564 KB Output is correct
20 Correct 113 ms 30364 KB Output is correct
21 Correct 114 ms 30576 KB Output is correct
22 Correct 119 ms 25572 KB Output is correct
23 Correct 138 ms 31104 KB Output is correct
24 Correct 146 ms 31060 KB Output is correct
25 Correct 158 ms 31132 KB Output is correct
26 Correct 117 ms 30364 KB Output is correct
27 Correct 110 ms 29480 KB Output is correct
28 Correct 152 ms 30780 KB Output is correct
29 Correct 146 ms 30876 KB Output is correct
30 Correct 96 ms 29000 KB Output is correct
31 Correct 140 ms 31388 KB Output is correct
32 Correct 140 ms 30360 KB Output is correct
33 Correct 140 ms 31840 KB Output is correct
34 Correct 137 ms 31020 KB Output is correct
35 Correct 109 ms 29308 KB Output is correct
36 Correct 64 ms 28080 KB Output is correct
37 Correct 157 ms 32420 KB Output is correct
38 Correct 135 ms 31088 KB Output is correct
39 Correct 134 ms 31644 KB Output is correct