Cod sursa(job #436059)

Utilizator daniel.pleteaPletea Daniel daniel.pletea Data 8 aprilie 2010 01:20:44
Problema Gutui Scor 70
Compilator c Status done
Runda teme_upb Marime 3.47 kb
#include <stdio.h>
//#include <conio.h>
#include <string.h>
#include <stdlib.h>

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


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

gutuie tree[100001];
gutuie newtree[100001];

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

int main(){
    
    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;
       tree[i].getIt=0;
       if(maxh<tree[i].height &&tree[i].height<=max_height) maxh=tree[i].height;
    }
    
    //if(maxh>max_height){
    //   kk=0;
    //}
    kk=max_height - maxh;
    
    qsort(tree,N,sizeof(gutuie),compare);
    
    
    kkk=0; kkkk=0;
    for(i=N-1;i>=0;i--){
            if(max_height - tree[i].height == kk){
              
               //printf("salut %i %i %i\n", tree[i].height, tree[i].weight,kk);
               newtree[kkkk]=tree[i];
               kkk++; kkkk++;
               if(kkk>kk) { kk++; kkk=0; }
               }
            else
               if(max_height - tree[i].height > kk){
                 //printf("salut2 %i %i\n", tree[i].height, tree[i].weight);
                 newtree[kkkk]=tree[i];
                 kk=max_height- tree[i].height;
                 kkk=1;
                 kkkk++;
            } 
    }
    
    //for(i=0;i<N;i++)
       //printf("%i %i \n", tree[i].height, tree[i].weight);
    qsort(newtree,kkkk,sizeof(gutuie),compare);
    
    //for(i=0;i<kkkk;i++)
         //printf("%i %i %i\n", newtree[i].height, newtree[i].weight, newtree[i].getIt);
    
    //minmin = tree[0].weight;
    
    for(i=kkkk-1;i>=0;i--)
       if((newtree[i].height+increase)<=max_height){
          increase++;
          newtree[i].getIt=1;
       }
       else{
            min=newtree[i].weight;
            k=-1;
            for(j=kkkk-1;j>i;j--)
               if(newtree[i].height!=newtree[j].height)
                  if(newtree[i].weight>newtree[j].weight && newtree[j].weight<min && newtree[j].getIt==1){
                     min=newtree[j].weight;
                     k=j;
                  }
            if(k!=-1){
               //printf("boule %i \n",k);
               newtree[k].getIt=0;
               newtree[i].getIt=1;
            }
            
       }
       
       //for(i=0;i<N;i++)
         //printf("%i %i %i\n", tree[i].height, tree[i].weight, tree[i].getIt);
       
       for(i=0;i<kkkk;i++) if(newtree[i].getIt==1) max_weight+=newtree[i].weight;
       
    
       
   // printf("%i",max_weight);
    fprintf(fd,"%i",max_weight);
    
    fclose(fs);
    fclose(fd);
    
    //getch();
    
}