Cod sursa(job #3332275)

Utilizator AlexandruTigauTigau Alexandru AlexandruTigau Data 5 ianuarie 2026 20:24:25
Problema Barman Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <vector>
using namespace std;
ifstream f("barman.in");
ofstream g("barman.out");
int a[605], s[605];
unordered_map<int, vector<int>> mp;
int main()
{
    int n, ans = 2e9;
    f >> n;
    for(int i = 0; i < n; i++)
    {
        f >> a[i];
        s[i] = a[i];
        if(!mp[a[i]].size())
            mp[a[i]].push_back(1);
        mp[a[i]].push_back(i);
    }
    sort(s, s + n);
    for(int i = 0; i < n; i++)
    {
        int partans = 0;
        for(int j = 0; j < n; j++)
        {
            int x = s[(i + j) % n];
            if(x == a[j]) continue;
            while(a[mp[x][mp[x][0]]] == s[(mp[x][mp[x][0]] + j) % n])
                mp[x][0]++;
            partans += 20 + abs(mp[x][mp[x][0]] - j);
            mp[x][0]++;
        }
        ans = min(ans, partans);
        for(auto& j : mp)
            j.second[0] = 1;
    }
    g << ans;
    return 0;
}