Cod sursa(job #439638)

Utilizator daniel.pleteaPletea Daniel daniel.pletea Data 11 aprilie 2010 17:48:53
Problema Gutui Scor 100
Compilator cpp Status done
Runda teme_upb Marime 2.21 kb
#include <stdio.h>
//#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>

using namespace std;

FILE* fs;
FILE* fd;
int N, max_height, height_inc,i,j,x,increase=0,rest, cate_gutui, max, k, maxh;//,min,k, minmin,//kk,kkk,kkkk,maxh,r; 
int aux;
long long int max_weight;



typedef struct {
   int height;
   int weight;
} gutuie;

gutuie tree[100001];

int compare (const void * a, const void * b)
{
  if(  ( (*(gutuie*)a).height - (*(gutuie*)b).height )  == 0 )
    return ( (*(gutuie*)b).weight - (*(gutuie*)a).weight );
  else return ( (*(gutuie*)a).height - (*(gutuie*)b).height ); 
}


main() {
    int i, j;
    vector<int> coll;
    make_heap (coll.begin(), coll.end());
    
    fs = fopen("gutui.in","r");
    fd = fopen("gutui.out","w");
    
    if (fs==NULL) {
       printf("Eroare");
       exit (1);
    }
    
    if (fd==NULL) {
       printf("Eroare");
       exit (1);
    }
    
    maxh=0;
    fscanf(fs,"%i",&N);   
    fscanf(fs,"%i",&max_height);
    fscanf(fs,"%i",&height_inc);
    rest = max_height%height_inc;
    max_height=max_height/height_inc;
    //printf("salut");
    for(i=0;i<N;i++){
       fscanf(fs,"%i",&x);
       if(x%height_inc>rest)
          tree[i].height = x / height_inc + 1;
       else
          tree[i].height = x / height_inc;
       fscanf(fs,"%i",&x);
       tree[i].weight = x;
       //if(maxh<tree[i].height &&tree[i].height<=max_height) maxh=tree[i].height;
    }
    
    j=0;
    max_weight=0;
    qsort(tree,N,sizeof(gutuie),compare);
    for(i=0;i<=max_height;i++)
    {
       while(i == tree[j].height){
         coll.push_back (tree[j].weight);
         push_heap (coll.begin(), coll.end());
         j++;        
       }
       if(coll.size()>0){
          max_weight = max_weight + coll.front();
          pop_heap(coll.begin(), coll.end());
          coll.pop_back();
       }
    }  
    
    fprintf(fd,"%lli",max_weight);
    
    fclose(fs);
    fclose(fd);
    return 0;
    
}