Cod sursa(job #1511347)

Utilizator EpictetStamatin Cristian Epictet Data 26 octombrie 2015 17:00:19
Problema Barman Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int n, sol = 2000000000, V[610], P[610], F[610];

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
    {
        fin >> V[i];
        P[i] = V[i];
    }

    sort (P + 1, P + 1 + n);
    for (int k = 1; k <= n; k++)
    {
        P[0] = P[1];
        for (int i = 1; i < n; i++) P[i] = P[i + 1];
        P[n] = P[0];

        int cost = 0;
        for (int i = 1; i <= n; i++)
        {
            if (P[i] == V[i] || F[i] == k) continue;
            for (int j = i + 1; j <= n; j++)
            {
                if (V[j] == P[j]) continue;
                if (V[i] == P[j])
                {
                    cost += (40 + 2 * (j - i));
                    F[i] = F[j] = k;
                    break;
                }
            }
        }

        sol = min (sol, cost);
    }

    fout << sol << '\n';
    return 0;
}