Pagini recente » Cod sursa (job #192314) | Cod sursa (job #841366) | Cod sursa (job #18554) | Cod sursa (job #2068558) | Cod sursa (job #2764771)
#include <fstream>
#include <algorithm>
#include <iostream>
using namespace std;
int n;
int a[602];
bool ok[602];
int sortat[602];
int Min = 2e9;
void read() {
int i;
ifstream f("barman.in");
f >> n;
for (i = 1; i <= n; i++)
f >> a[i];
f.close();
}
void solve() {
int i, l, j, k, ans, aux;
for (i = 1; i <= n; i++)
sortat[i] = a[i];
sort(sortat + 1, sortat + n + 1);
for (i = 1; i <= n; i++) {
ans = 0;
for (j = 1; j <= n; j++)
ok[j] = (sortat[j] == a[j]);
for (j = 1; j <= n; j++)
if (!(sortat[j] == a[j]))
for (k = 1; k <= n; k++)
if (!ok[k] && a[j] == sortat[k]) {
ok[k] = 1;
ans += 20 + abs(j - k);
break;
}
Min = min(ans, Min);
aux = sortat[n];
for (j = n; j >= 1; j--)
sortat[j + 1] = sortat[j];
sortat[1] = aux;
}
}
void output() {
ofstream g("barman.out");
g << Min;
g.close();
}
int main() {
read();
solve();
output();
return 0;
}