Cod sursa(job #2179535)

Utilizator NeredesinI am not real Neredesin Data 20 martie 2018 11:58:28
Problema Barman Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <climits>

using namespace std;

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

const int NMAX = 600;

int n, c;
int res = INT_MAX;
int v[1 + NMAX];
int a[1 + NMAX];
bool ok[1 + NMAX];

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

  for(int k = 1; k <= n; k++) {
    int aux = v[1];
    for(int i = 1; i < n; i++)
      v[i] = v[i + 1];
    v[n] = aux;

    for(int i = 1; i <= n; i++)
      if(a[i] == v[i])
        ok[i] = 1;
      else
        ok[i] = 0;
    c = 0;
    for(int i = 1; i <= n; i++) {
        if(a[i] != v[i]) {
          int j;
          for(j = 1; ok[j] || a[i] != v[j]; j++);
          ok[j] = 1;
          c += max(i, j) - min(i, j) + 20;
        }
    }
    res = min(res, c);
  }

  out << res << '\n';

  in.close();
  out.close();
  return 0;
}