CSES - Datatähti 2017 alku - Results
Submission details
Task:Järjestys
Sender:cattes
Submission time:2016-10-08 23:19:11 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void swap(int*, int)':
input/code.cpp:9:2: error: 'memcpy' is not a member of 'std'
  std::memcpy(buf, swapped, swapcount * 4);
  ^
input/code.cpp:9:2: note: suggested alternative:
In file included from /usr/include/features.h:374:0,
                 from /usr/include/x86_64-linux-gnu/c++/4.8/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h:426,
                 from /usr/include/c++/4.8/iostream:38,
                 from input/code.cpp:1:
/usr/include/x86_64-linux-gnu/bits/string3.h:48:1: note:   'memcpy'
 __NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
 ^

Code

#include <iostream>
#include <string.h>

void swap(int * buf, int swapcount) {
	int * swapped = new int[swapcount];
	for (int i = 0; i < swapcount; i++) {
		swapped[i] = buf[swapcount - 1 - i];
	}
	std::memcpy(buf, swapped, swapcount * 4);
}

int search(int * buf, int buflen, int searchfor) {
	for (int i = 0; i < buflen; i++) {
		if (buf[i] == searchfor) {
			return i;
		}
	}
	return -1;
}

int main() {
	
	int tableSize;

	std::cin >> tableSize;
	std::cin.get();

	int * table = new int[tableSize];

	for (int i = 0; i < tableSize; i++) {
		std::cin >> table[i];
	}
	std::cin.get();

	//do the thing
	int * swaps = new int[500000];
	int swapcount = 0;

	for (int i = 0; i < tableSize; i++) {
		int found = search(table, tableSize, tableSize-i);
		if (found != tableSize - 1 - i) {
			swap(table, found + 1);
			swap(table, tableSize - i);
			//record the swaps
			swaps[swapcount] = found + 1;
			swaps[swapcount + 1] = tableSize - i;
			swapcount = swapcount + 2;
		}
	}

	std::cout << swapcount << "\n";
	for (int i = 0; i < swapcount; i++) {
		std::cout << swaps[i] << " ";
	}
	std::cout << "\n";



	std::cin.get();
	return 0;
}