제출 #345666

#제출 시각아이디문제언어결과실행 시간메모리
345666ogibogi2004정렬하기 (IOI15_sorting)C++14
20 / 100
2 ms748 KiB
#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];
bool vis[MAXN];
int n;
vector<pair<int,int> >moves;
bool check(int x)
{
	for(int i=0;i<n;i++)
	{
		seq1[i]=seq[i];
	}
	for(int j=0;j<=x&&j<swaps.size();j++)
	{
		swap(seq1[swaps[j].first],seq1[swaps[j].second]);
	}
	int cnt=0;
	memset(vis,0,sizeof(vis));
	moves.clear();
	for(int i=0;i<n;i++)
	{
		if(vis[i]==0)
		{
			int u=i;
			while(vis[u]==0)
			{
				vis[u]=1;
				moves.push_back({i,seq1[u]});
				u=seq1[u];
			}
			cnt++;
			moves.pop_back();
		}
	}
	//cout<<x<<" "<<cnt<<endl;
	if(n-cnt<=x+1)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=n,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<moves.size();i++)
	{
		P[i]=moves[i].first;Q[i]=moves[i].second;
	}
	return moves.size();
}

컴파일 시 표준 에러 (stderr) 메시지

sorting.cpp: In function 'bool check(int)':
sorting.cpp:16:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int j=0;j<=x&&j<swaps.size();j++)
      |                    ~^~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:59: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]
   59 |  for(int i=0;i<moves.size();i++)
      |              ~^~~~~~~~~~~~~
sorting.cpp:63:19: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   63 |  return moves.size();
      |         ~~~~~~~~~~^~
sorting.cpp:58:7: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   58 |  check(ans);
      |  ~~~~~^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...