Cod sursa(job #3346809)

Utilizator Horia_haivasHaivas Horia Horia_haivas Data 15 martie 2026 12:04:02
Problema Ghiozdan Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
#include<bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")
//#define int long long

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int range_rng(int l, int r)
{
    return uniform_int_distribution<int>(l,r)(rng);
}

short int v[20005];
short int f[205];
short int cate[205][75005];
short int dp[75005];
short int newdp[75005];
deque<int> dq[205];
vector<short int> sol;

signed main()
{
    ///*
    ifstream fin("ghiozdan.in");
    ofstream fout("ghiozdan.out");
    //*/
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,g,i,j,mod,w,bigw;
    fin >> n >> g;
    for (i=1;i<=n;i++)
    {
         fin >> v[i];
         f[v[i]]++;
    }
    dp[0]=0;
    for (w=1;w<=g;w++)
         dp[w]=n+1;
    for (i=1;i<=200;i++)
    {
         for (j=0;j<=i;j++)
              dq[j].clear();
         mod=-1;
         for (w=0;w<=g;w++)
         {
              mod++;
              if(mod==i)
                 mod=0;
              while (!dq[mod].empty() && ((w-dq[mod].front())/i)>f[i])
                     dq[mod].pop_front();
              while (!dq[mod].empty() && ((w-dq[mod].back())/i)+dp[dq[mod].back()]>dp[w])
                     dq[mod].pop_back();
              dq[mod].push_back(w);
              newdp[w]=dp[dq[mod].front()]+((w-dq[mod].front())/i);
              cate[i][w]=((w-dq[mod].front())/i);
         }
         for (w=0;w<=g;w++)
              dp[w]=newdp[w];
    }
    for (w=g;w>=0;w--)
    {
         if (dp[w]!=n+1)
         {
             fout << w << " " << dp[w] << "\n";
             bigw=w;
             break;
         }
    }
    for (i=200;i>=1;i--)
    {
         if (cate[i][bigw]!=0)
         {
             for (j=1;j<=cate[i][bigw];j++)
                  sol.push_back(i);
             bigw-=i*cate[i][bigw];
         }
    }
    for (auto x : sol)
         fout << x << "\n";
    return 0;
}