Cod sursa(job #2725059)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 18 martie 2021 12:59:40
Problema Barman Scor 100
Compilator cpp-64 Status done
Runda simularesimulare Marime 0.84 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define MIN(a, b) (((a)<(b))?(a):(b))
#define MAX(a, b) (((a)>(b))?(a):(b))

std::ifstream fin("barman.in");
std::ofstream fout("barman.out");

const int N = 605;

std::vector<int>match;
int ok[N], v[N], mn = 1000000000;

void shift() {
	int f = match[0];
	for(int i=1;i<match.size();i++) match[i-1] = match[i];
	match[match.size()-1] = f;
}

int main() {
	int n;
	fin>>n;
	for(int i=0;i<n;i++) fin>>v[i], match.push_back(v[i]);
	std::sort(match.begin(), match.end());
	for(int rot = 1; rot<=n; rot++) {
		int ans = 0;
		for(int i=0;i<n;i++) ok[i] = (v[i]==match[i]);
		for(int i=0;i<n;i++) if(!(v[i]==match[i]))
			for(int j=0;j<n;j++) if(!ok[j] and v[i]==match[j]) {
				ok[j] = 1, ans+=20+MAX(i-j, j-i);
				break;
			}
		mn = MIN(ans, mn);
		shift();
	}
	fout<<mn;
}