Pagini recente » Cod sursa (job #1179735) | Cod sursa (job #1675765) | Cod sursa (job #46852) | Cod sursa (job #304) | Cod sursa (job #1251359)
#include <cstdio>
#include <algorithm>
#include <cstring>
FILE *in,*out;
using namespace std;
int z, rox, teo, sol=2000000000;
int cost, v[610], aux[610], aux2[610];
short int n;
int main()
{
in=fopen("barman.in","rt");
fscanf(in,"%hd", &n);
for(short int i=1; i<=n; i++)
{
fscanf(in,"%d", &v[i]);
aux[i]=v[i];
}
fclose(in);
sort(aux+1, aux+n+1);
for(short int i=1; i<=n; i++)
{
memset(aux2, 0, sizeof(aux2));
cost=0;
for(short int j=1; j<=n; j++)
{
if(aux[j]==v[j]) aux2[j]=2;
else cost+=20;
}
for(short int j=1; j<=n; j++)
if(aux[j]!=v[j])
{
for(z=1; aux[z]!=v[j] || aux2[z]; z++);
aux2[z]=1;
if(z<j){ rox=z; teo=j; }
else { rox=j; teo=z; }
cost+=teo-rox;
}
if(cost<sol)
sol=cost;
for(short int j=0; j<n; j++) aux[j]=aux[j+1];
aux[n]=aux[0];
}
out=fopen("barman.out","wt");
fprintf(out,"%d", sol);
fclose(out);
return 0;
}