Cod sursa(job #955467)

Utilizator DaNutZ2UuUUBB Bora Dan DaNutZ2UuU Data 31 mai 2013 20:17:28
Problema Barman Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <algorithm>
#include <map>
#include <list>
using namespace std;
 
ifstream f("barman.in");
ofstream g("barman.out");
 
int n,i,v[601],vp[601],p[601],ni,ANS=1<<30,sol;
 
map<int, list <int> > D;
void permuta (int j) {
    for (int i=1;i<=n;i++,j++) {
        if (j>n) j=1;
        p[i]=vp[j];
    }
    sol=0;
}
int poz(int x) {
    if (x<0) x*=-1;
    return x;
}
int main () {
    f >> n;
    for (i=1;i<=n;i++) {
        f >> v[i];
        vp[i]=vp[n+i]=v[i];
    }
    sort(vp+1,vp+n+1);
    for (ni=1;ni<=n;ni++) {
        permuta(ni);
        D.erase(D.begin(),D.end());
        for (i=1;i<=n;i++)
            if (v[i]!=p[i])
                sol+=20,D[v[i]].push_back(i);
        for (i=1;i<=n;i++)
            if (v[i]!=p[i]) {
                sol+=poz(i-D[p[i]].front());
                D[p[i]].pop_front();
            }
        ANS=min(ANS,sol);
    }
    g << ANS << '\n';
    f.close();g.close();
    return 0;
}