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

Compiler report

input/code.cpp: In function 'void swap(int*, int)':
input/code.cpp:9:36: error: 'memcpy' was not declared in this scope
  memcpy(buf, swapped, swapcount * 4);
                                    ^

Code

#include <iostream>
#include <string>

void swap(int * buf, int swapcount) {
	int * swapped = new int[swapcount];
	for (int i = 0; i < swapcount; i++) {
		swapped[i] = buf[swapcount - 1 - i];
	}
	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;
}