Cod sursa(job #2380479)

Utilizator Andrei1998Constantinescu Andrei-Costin Andrei1998 Data 15 martie 2019 01:17:59
Problema Barman Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <bits/stdc++.h>

using namespace std;

typedef long long int lint;

lint solve(const vector <pair <int, int> > &v) {
    const int N = v.size();
    vector <pair <int, int> > v2;
    for (int i = 0; i < N; ++i) {
        v2.emplace_back(v[i].first, v[i].second);
    }
    sort(v2.begin(), v2.end());
    lint sum = 0;
    for (int j = 0; j < N; ++j) {
        if (v2[j].second != j) {
            sum += 20;
        }
        sum += abs(v[v2[j].second].second - v[j].second);
    }
    return sum;
}

int main() {
    ifstream cin("barman.in");
    ofstream cout("barman.out");
    int N;
    cin >> N;
    vector <pair <int, int> > v(N);
    for (int i = 0; i < N; ++i) {
        cin >> v[i].first;
        v[i].second = i;
    }
    lint ans = numeric_limits <lint> :: max();
    for (int i = 0; i < N; ++i) {
        ans = min(ans, solve(v));
        rotate(v.begin(), v.begin() + 1, v.end());
    }
    cout << ans << endl;
    return 0;
}