Pagini recente » Cod sursa (job #2867496) | Cod sursa (job #1953666) | Cod sursa (job #2322532) | Cod sursa (job #1320770) | Cod sursa (job #436889)
Cod sursa(job #436889)
#include<stdio.h>
#include<list>
#include<stdlib.h>
using namespace std;
FILE* fin;
FILE* fout;
typedef struct gut{
unsigned int greutate;
unsigned int tmp;
}*gutui;
bool compare_desc(gut first,gut second)
{
if (first.tmp < second.tmp ) return true;
else
if(first.tmp == second.tmp)
{ if(first.greutate<=second.greutate)
return true;
else
return false;
}
return false;
}
int main(){
unsigned int h,u,a,g,maxim;
fin=fopen("gutui.in","r");
fout=fopen("gutui.out","w");
unsigned int n,i,j,max;
int t;
list<gut> cos;
list<gut>::iterator it;
fscanf(fin,"%d %d %d\n",&n,&h,&u);
gut *aux;
if(h%u==0)
{
t=0;
max=h/u;
}else
{max=h/u+1;
t=1;
}
for(i=0;i<n;i++)
{
aux=(gut*)malloc(sizeof(struct gut));
fscanf(fin,"%u %u\n",&a,&g);
if(a+(max-t-(a/u))*u<=h)
aux->tmp=max-t-(a/u-1);
else
aux->tmp=max-t-(a/u);
//aux->inaltime=a;
aux->greutate=g;
cos.push_back(*aux);
free(aux);
}
cos.sort(compare_desc);
for (it=cos.begin(); it!=cos.end(); ++it)
printf("%u %u\n",it->greutate,it->tmp);
unsigned int *matrice;
matrice=(unsigned int*)calloc(max+1,sizeof(unsigned int));
printf("max=%d\n",max);
int suma=0;
int timp=n;
int ind=0;
unsigned int top,poz,full=0;
unsigned int min;
unsigned int pozMin=0;
it=cos.begin();
ind=it->tmp;
poz=it->tmp;
top=it->tmp;
for(i=1;i<=n;i++)
{
if(it->tmp==0)
{matrice[it->tmp]=it->greutate;
suma=suma+it->greutate;
}
else
{pozMin=it->tmp;
for(j=1;j<=it->tmp;j++)
if(matrice[j]<matrice[pozMin])
pozMin=j;
if(pozMin!=it->tmp)
{ suma=suma-matrice[pozMin]+it->greutate;
matrice[pozMin]=it->greutate;
}else
if(matrice[pozMin]<it->greutate)
{ suma=suma-matrice[pozMin]+it->greutate;
matrice[pozMin]=it->greutate;
}
}
it++;
}
/* printf("\n");
for(i=1;i<=top;i++)
printf("%d ",i);
printf("\n");
for(i=1;i<=top;i++)
printf("%d ",matrice[i]);
printf("\n");
*/
printf("%u\n",suma);
fprintf(fout,"%u",suma);
free(matrice);
fclose(fin);
fclose(fout);
return 0;
}