Cod sursa(job #433719)

Utilizator alexqawsFasui Alexandru alexqaws Data 4 aprilie 2010 02:19:58
Problema Gutui Scor 20
Compilator cpp Status done
Runda teme_upb Marime 3.55 kb
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

typedef struct gs{
    int ng;
    int gg;
    int hg;
} gs;

int h,u;
int n;
gs *gt;
int *luate;
int *nivel;
int nr_luate = 0;

int p;

int gmax = 0;
int nivmax = 0;
int nivmin = UINT_MAX;

int compare (const void * a, const void * b)
{
    int rez;
    rez = ( ((gs *)b)->gg - ((gs *)a)->gg);
    if(rez != 0){
        return rez;
    }
    else{
        return ( ((gs *)b)->hg - ((gs *)a)->hg);
    }
}

int get_gmax(){
    //cout<<"nivmax = "<<nivmax<<", nivmin = "<<nivmin<<", p = "<<p<<", n = "<<n<<endl;
    int i,j;
    int k, barrier;
    
    barrier = nivmax + 1;
    
    for(i=0;i<p;i++){
        if(i==n){
            break;
        }
        if(gt[i].hg>h){
            p++;
            continue;
        }
        k = 0;
        for(j=0;j<nr_luate;j++){ /* cate s-au luat de "mai sus" */
            if(luate[j] >= gt[i].ng){
                k++;
            }
        }
        
        //cout<<"nivmax = "<<nivmax<<", gt.ng = "<<gt[i].ng<<", gt.gg = "<<gt[i].gg<<", k = "<<k<<endl;
        
        if( ( nivmax + 1 - gt[i].ng > k ) && ( gt[i].ng < barrier )){
        //if(nivmax - gt[i].ng - k > 0 ){ //????
            luate[nr_luate++] = gt[i].ng;
            gmax = gmax + gt[i].gg;
            //cout<<"ng = "<<gt[i].ng<<",   g = "<<gt[i].gg<<",   k = "<<k<<",   nivmax + 1= "<<nivmax + 1<<endl;
            
            if( nivmax + 1 - gt[i].ng == k + 1){ //nu mai pot lua nimic de pe acel nivel
                if( gt[i].ng < barrier )
                    barrier = gt[i].ng;
                //cout<<"barrier = "<<barrier<<endl;
            }
            
            //cout <<"            "<< nivmax + 1 - gt[i].ng - k <<endl;
        }
        else{
            p++;
        }
    }
    
    return 0;
}

int read_vec(){
    ifstream fin;
    int i, j;
    //int nvc;
    int ic;
    
    fin.open("gutui.in",ios::in);
    
    fin>>n;
    fin>>h;
    fin>>u;
    
    gt = (gs *)malloc( n * sizeof( gs ));  
    
    //nivmax = h / u + ( ( h % u == 0) ? 0 : 1 ) ;
    //nivmax = h / u + 1; //????
    nivmax = h / u;
    
    nivel = (int *)malloc( (h + 1) * sizeof(int ));
    
    //nvc = nivmax;
    ic = h;
    
    for(i = nivmax; i >=0 ;i--){
        for(j = 0; j<u; j++){
            //cout<<"ic = "<<ic<<", nivcur = "<<i<<endl; 
            if(ic>=0){
                nivel[ic] = i;
                ic--;
            }
        }
    }

    for(i=0;i<n;i++){
        fin>>gt[i].hg;
        fin>>gt[i].gg;
        
        //gt[i].ng = (gt[i].hg / u) + ( (gt[i].hg % u == 0) ? 0 : 1 ) ;
        //gt[i].ng = (gt[i].hg / u) + 1; //????
        //gt[i].ng = (gt[i].hg / u);
        gt[i].ng = nivel[ gt[i].hg ];
        if(gt[i].ng < nivmin){
            nivmin = gt[i].ng;
        }
    }  
    
    p = nivmax - nivmin + 1;
    //p = nivmax - nivmin ; //????
    
    //cout<<nivmax<<" "<<nivmin;
    
    //mai bine pornesc de la h si scad multiplii de u
    
    if(p > n){ //??
        p = n;
    }
    
    luate = (int *)malloc( p * sizeof(int ));
    
    qsort (gt, n, sizeof(gs), compare);
    
    fin.close();
    return 0;
}


int write_sol(){
    ofstream fout;
    fout.open("gutui.out",ios::out);
    
    fout<<gmax;
    
    fout.close();
    return 0;
}

int main(){
     
    read_vec();  
    
    get_gmax();
    
    write_sol();
    
    //cout<<endl<<"gmax = "<< gmax;
    //cin.get();//to_comment
        
    return 0;
}