Cod sursa(job #1770318)

Utilizator ducu34Albastroiu Radu Gabriel ducu34 Data 4 octombrie 2016 01:27:44
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.02 kb
//
//  main.cpp
//  CF 375
//
//  Created by Albastroiu Radu on 10/3/16.
//  Copyright © 2016 Albastroiu Radu. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <string>
#include <deque>
#include <unordered_map>
#include <cstdint>

using namespace std;

long long n,m,i,j,x,y,lakes,v[5001],deseuri, MEDIA, Nr_Changes;

unordered_map<long long, long long> songs;

int main()
{
    
#ifndef ONLINE_JUDGE
    freopen("date.in","r",stdin);
    freopen("date.out","w",stdout);
#endif
    
    cin >> n >> m;
    
    for(i=1;i<=m;i++)
    {
        songs[i]++;
        songs[i]--;
    }
    
    for(i=1;i<=n;i++)
    {
        cin>>v[i];
        
        songs[v[i]]++;
        
        if(v[i]>m)
            deseuri++;
    }
    
    MEDIA = n / m;
    
    Nr_Changes = 0;
    
    for( auto &element : songs)
    {
        if( element.second < MEDIA && element.first <= m )
        {
            if(deseuri > 0)
            {
                for(i=1;i<=n;i++)
                {
                    if(v[i] > m)
                    {
                        Nr_Changes++;
                        v[i] = element.first;
                        element.second++;
                        deseuri--;
                        if(element.second >= MEDIA)
                            break;
                    }
                }
            }
            if(element.second < MEDIA)
            {
                for(i=1;i<=n;i++)
                {
                    if(songs[v[i]] > MEDIA)
                    {
                        Nr_Changes++;
                        songs[v[i]]--;
                        element.second++;
                        v[i] = element.first;
                        if(element.second >= MEDIA)
                            break;
                    }
                }
            }
        }
    }
    
    cout<<MEDIA<<" "<<Nr_Changes<<"\n";
    for(i=1;i<=n;i++)
    {
        cout<<v[i]<<" ";
    }
    
    return 0;
}