Cod sursa(job #2764771)

Utilizator DragosC1Dragos DragosC1 Data 22 iulie 2021 15:12:12
Problema Barman Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#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;
}