Cod sursa(job #437726)

Utilizator VladTVlad Tudose VladT Data 10 aprilie 2010 00:36:28
Problema Gutui Scor 70
Compilator cpp Status done
Runda teme_upb Marime 1.52 kb
#include <iostream>
#include <list>
using namespace std;


typedef struct
{
    long height, weight;
    long level;        
} Gutuie;

bool compareH(Gutuie a, Gutuie b)
{
     if( (a.height - b.height) < 0 )
         return true;
     return false;
 
 
}

int main()
{
    long N, H, U, sum=0;
    int i, curent;
    Gutuie elem;
    list<Gutuie> gutui;
    list<Gutuie>::iterator it,aux, it2;
    
    FILE * in = fopen("gutui.in", "r");
    if(in==NULL)
		return -1;	

	fscanf(in, "%d", &N);
	fscanf(in, "%d", &H);
	fscanf(in, "%d", &U);
	cout<<N<<" "<<H<<" "<<U<<endl; 
	
	for(i=0; i<N; i++)
	{
		fscanf(in, "%ld", &elem.height);
        fscanf(in, "%ld", &elem.weight);
        gutui.push_front(elem);
        cout << "h:" << elem.height << " w:" << elem.weight;
        cout << endl;
	}
	cout << endl;
	gutui.sort(compareH);
	for (it=gutui.begin(); it!=gutui.end(); ++it)
	{        
        
		it->level=(H-it->height)/U + 1;
	}
	fclose(in);
	while(!gutui.empty())
	{
         it=gutui.begin();
         curent=it->level;
         if(curent==0)
             break;
         aux=it;
         while(curent==it->level)
         {
             it->level--;
             if(aux->weight < it->weight)
             {
                  aux=it;                     
             }
             ++it;
         }
         sum+=aux->weight;
         aux=gutui.erase(aux);
    }
    FILE * out = fopen("gutui.out", "w");
    fprintf(out,"%d", sum);
    fclose(out);	
    return 0;    
}