Cod sursa(job #3172918)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 21 noiembrie 2023 16:35:43
Problema Barman Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include<bits/stdc++.h>

using namespace std;

ifstream f("barman.in");
ofstream g("barman.out");

int n, i, j;
int a[605];
int v[605];
int w[605];

int main()
{
    f >> n;
    for(int i = 1; i <= n; ++i) {
        f >> a[i];
        v[i] = a[i];
    }

    sort(v + 1, v + n + 1);
    int k = n;
    int sol = 1 << 30;
    while(k--) {
        int aux = v[1];
        for(i = 1; i < n; ++i)
            v[i] = v[i + 1];
       
        v[n] = aux;
        for(i = 1; i <= n; ++i)
            w[i] = (a[i] == v[i]);
    
        int cost = 0;
        for(i = 1; i <= n; ++i)
            if(a[i] != v[i] ) {
                for(j = 1; w[j] || a[i] != v[j]; ++j);
                    w[j] = 1;
                    cost += 20 + abs(j - i);
            }

            sol = min(sol, cost);
    }
    g << sol << '\n';
    return 0;
}