Submission #345681

# Submission time Handle Problem Language Result Execution time Memory
345681 2021-01-07T20:40:29 Z ogibogi2004 Sorting (IOI15_sorting) C++14
0 / 100
2 ms 748 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++)
	{
		if(vis[i]==0)
		{
			int u=i;
			while(vis[u]==0)
			{
				vis[u]=1;
				moves.push_back({seq2[i],seq2[seq2[u]]});
				u=seq2[u];
			}
			cnt++;
			moves.pop_back();
		}
	}
	//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++)
	{
		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]]);
	}
	/*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:46: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]
   46 |  if(moves.size()<=x)
      |     ~~~~~~~~~~~~^~~
sorting.cpp:48: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]
   48 |   while(moves.size()<x)moves.push_back({0,0});
      |         ~~~~~~~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:74: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]
   74 |  for(int i=0;i<moves.size();i++)
      |              ~^~~~~~~~~~~~~
sorting.cpp:88:19: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   88 |  return moves.size();
      |         ~~~~~~~~~~^~
sorting.cpp:69:7: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   69 |  check(ans);
      |  ~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Incorrect 1 ms 492 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Incorrect 1 ms 492 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Incorrect 1 ms 492 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Incorrect 1 ms 492 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 748 KB Output isn't correct
2 Halted 0 ms 0 KB -