Submission #60071

# Submission time Handle Problem Language Result Execution time Memory
60071 2018-07-23T15:33:29 Z Eae02 Sorting (IOI15_sorting) C++14
36 / 100
1000 ms 512 KB
#include "sorting.h"

#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
	std::vector<int> sorted(S, S + N);
	std::sort(all(sorted));
	
	std::vector<int> sInitial(S, S + N);
	
	int min = 0;
	int max = M + 1;
	while (max > min)
	{
		int mid = min + (max - min) / 2;
		
		std::vector<int> finalAccess(N);
		for (int i = 0; i < mid; i++)
		{
			finalAccess[X[i]] = i;
			finalAccess[Y[i]] = i;
		}
		
		std::copy(all(sInitial), S);
		
		int R = 0;
		for (; R < mid; R++)
		{
			if (std::equal(S, S + N, sorted.begin()))
				break;
			
			std::swap(S[X[R]], S[Y[R]]);
			
			int index = -1;
			for (int i = 0; i < N; i++)
			{
				if (R >= finalAccess[i] && S[i] != sorted[i])
				{
					index = i;
					break;
				}
			}
			
			if (index == -1)
			{
				for (int i = 0; i < N; i++)
				{
					if (R + 1 == finalAccess[i] && S[i] != sorted[i])
					{
						index = i;
						break;
					}
				}
				
				if (index == -1 || R + 1 == mid)
				{
					P[R] = Q[R] = 0;
				}
				else
				{
					int val = sorted[index];
					int target = X[R + 1] == index ? Y[R + 1] : X[R + 1];
					
					auto valIt = std::find(S, S + N, val);
					std::swap(S[target], *valIt);
					
					P[R] = target;
					Q[R] = valIt - S;
				}
			}
			else
			{
				int val = sorted[index];
				auto valIt = std::find(S, S + N, val);
				std::swap(S[index], *valIt);
				
				P[R] = index;
				Q[R] = valIt - S;
			}
		}
		
		if (R == mid)
		{
			min = mid;
		}
		else
		{
			return R;
		}
	}
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:70:19: warning: conversion to 'int' from 'long int' may alter its value [-Wconversion]
      Q[R] = valIt - S;
             ~~~~~~^~~
sorting.cpp:80:18: warning: conversion to 'int' from 'long int' may alter its value [-Wconversion]
     Q[R] = valIt - S;
            ~~~~~~^~~
sorting.cpp:93:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 3 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 3 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 416 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 3 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 416 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 256 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 256 KB Output is correct
21 Execution timed out 1056 ms 512 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1038 ms 384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1038 ms 384 KB Time limit exceeded
2 Halted 0 ms 0 KB -