Cod sursa(job #2292931)

Utilizator PredescuSebastianIonPredescu Sebastian Ion PredescuSebastianIon Data 30 noiembrie 2018 11:54:36
Problema Barman Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("barman.in");
ofstream g("barman.out");
int a[602],b[602],n,sol=1<<30,s;
bool ok[602],c[602];
int main()
{

    f>>n;
    for(int i=1;i<=n;i++)
    {
        f>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++)
    {
        s=0;
        for(int j=1;j<=n;j++)
        {
            if(a[j]==b[j])
            {
                ok[j]=1;
                c[j]=1;
            }
            else
            {
                ok[j]=0;
                c[j]=0;
            }
        }
        for(int j=1;j<=n;j++)
        {
            if(!ok[j])
            {
                for(int k=1;k<=n;k++)
                {
                    if(a[j]==b[k] && !c[k])
                    {
                        c[k]=1;
                        s+=abs(j-k)+20;
                        break;
                    }
                }
            }
        }
        sol=min(s,sol);
        int aux=b[1];
        for(int i=1;i<n;i++)
        {
            b[i]=b[i+1];
        }
        b[n]=aux;
    }
    g<<sol<<'\n';
    return 0;
}