Cod sursa(job #2592674)

Utilizator anamariatoaderAna Toader anamariatoader Data 2 aprilie 2020 01:27:26
Problema Zebughil Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include <cstring>
#include <algorithm>

using namespace std;
ifstream fin("zebughil.in");
ofstream fout("zebughil.out");

int t,i,j,n,g,x,mx,a[20],h[20],k;
int cmp(int a, int b){
    return a>b;
}

int caut(int d){
    for(int e=k;e>0;e--)
        if(h[e]>=d)
            return e;
    return 1;
}

int main()
{
    for(t=1;t<4;t++){
       fin>>n>>g;
       for(i=1;i<=n;i++)
           fin>>a[i];
        sort(a+1,a+n+1,cmp);
        for(i=1;i<=n;i++){
           x=a[i];
           if(mx >= x){
              int p = caut(x);
              int v = h[p]-x;
              for(j=p;j<=k;j++)
                h[j]=h[j+1];
              k--;
              p = caut(v);
              for(j=k;j>=p+1;j--)
                h[j+1]=h[j];
              h[p+1]=v;
              k++;
           }
           else{
                for(j=k;j>=1;j--)
                   h[j+1]=h[j];
                h[1]=g-x;
                k++;
           }
           mx=h[1];
       }
       fout<<k<<'\n';
       memset(h,0,sizeof(h));
       k=0, mx=0;
    }
    return 0;
}