Pagini recente » Cod sursa (job #2363109) | Cod sursa (job #1469410) | Cod sursa (job #2938978) | Cod sursa (job #2965307) | Cod sursa (job #1327497)
#include <algorithm>
#include <fstream>
using namespace std;
const int MAX_N = 666;
int n, a[MAX_N];
int sorted[MAX_N];
bool ok[MAX_N];
inline void rotate_left() {
int aux = sorted[1];
for (int i = 1; i < n; ++ i) {
sorted[i] = sorted[i + 1];
}
sorted[n] = aux;
}
int main() {
ifstream fin("barman.in");
ofstream fout("barman.out");
fin >> n;
for (int i = 1; i <= n; ++i) {
fin >> a[i];
sorted[i] = a[i];
}
sort(sorted + 1, sorted + n + 1);
int solution = 1.e9;
for (int rotation = 1; rotation <= n; ++ rotation) {
int answer = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] == sorted[i]) {
ok[i] = true;
} else {
ok[i] = false;
}
}
for (int i = 1; i <= n; ++i) {
if (a[i] == sorted[i]) {
continue;
}
for (int j = 1; j <= n; ++j) {
if (!ok[j] && a[i] == sorted[j]) {
ok[j] = true;
answer += 20 + abs(i - j);
break;
}
}
}
solution = min(solution, answer);
rotate_left();
}
fout << solution << "\n";
return 0;
}