Cod sursa(job #1401267)

Utilizator alexandru70Ungurianu Alexandru alexandru70 Data 25 martie 2015 18:59:31
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <utility>
#include <queue>

using namespace std;

ifstream in("kbubblesort.in");
ofstream out("kbubblesort.out");

unsigned v[1000000];

int main() {
	unsigned n,k;
	in >> n >> k;

	for(unsigned i = 0; i < n; ++i) {
		in >> v[i];
	}

	queue<unsigned> to_swap;
	for(unsigned i = 1; i < n; ++i) {
		if(v[i-1] > v[i]) {
			to_swap.push(i);
		}
	}

	unsigned swaps = 0;

	while(swaps < k) {
		unsigned c = to_swap.front();
		to_swap.pop();
		to_swap.push(c);
		while(swaps < k && c < n && v[c-1] > v[c]) {
			swap(v[c-1],v[c]);
			c++;
			swaps++;
		}
	}

	for(unsigned i = 0; i < n; ++i) {
		out << v[i] << ' ';
	}
	out << '\n';
}