Pagini recente » Cod sursa (job #1119999) | Cod sursa (job #3142980) | Cod sursa (job #1140616) | Cod sursa (job #3178579) | Cod sursa (job #441952)
Cod sursa(job #441952)
#include <stdio.h>
#include <stdlib.h>
typedef struct nod {
long int value;
struct nod *next;
}Nod;
typedef Nod* List;
long int n,hmax,u,rangs;
//calculez suma elementelor din lista
long int sum(List l)
{
Nod *first;
long int s=0;
first = l;
while (l) {
s += (l) -> value;
(l) = (l) -> next;
}
// printf("suma merge, s= %ld\n",s);
l = first;
return s;
}
//elimin primul element din lista
long int delfirst(List *l)
{
long int val;
Nod *aux;
val = (*l) -> value;
aux = *l;
// printf("Am sters un element\n");
(*l) = (*l) -> next;
free(aux);
return val;
}
//calculez lungimea listei
long int lung(List l)
{
long int lng = 0;
Nod *first;
first = l;
while (l)
{
lng++;
l = l ->next;
}
l = first;
return lng;
}
//sterg ultimul element din lista (cu cea mai mica greutate)
long int dellast(List *l)
{
long int val;
Nod *aux, *first;
first = *l;
if ((*l) -> next == NULL)
{
val = (*l) -> value;
aux = *l;
*l = NULL;
free(aux);
return val;
}
else if ((*l) == NULL)
return 0;
//verific daca urmatorul element nu este ultimul
while ((*l) -> next -> next !=NULL)
(*l) = (*l) -> next;
val = (*l) -> next -> value;
aux = (*l) -> next;
(*l) -> next = NULL;
// printf("Am sters un element\n");
free(aux);
(*l) = first;
return val;
}
//returnez valoarea primului element
long int top(List l)
{
long int val;
val = (l) -> value;
// printf("Top list afisare \n");
return val;
}
//ultimul element din lista
long int last(List l)
{
long int val;
Nod *first;
first = l;
while (l -> next != NULL)
l = l -> next;
val = (l) -> value;
// printf("Top list afisare \n");
return val;
}
//adaog elementul in lista ordonind elementele in ordine descrescatoare incepind de la virf.
void add(List *l,long int val)
{
Nod *first,*aux;
aux = (Nod *) malloc (sizeof (struct nod) );
aux -> value = val;
first = *l;
//e primul element daca lista e vida
if ((*l) == NULL)
{
(*l) = aux;
(*l) -> next = NULL;
return;
}
else
//daca e mai mare ca primu element
if (((*l) -> value < val) && ((*l) -> next != NULL))
{
aux -> next = (*l);
(*l) = aux;
return;
}
//daca lista are doar un element se compara cu el si se insereaza
if ((*l) -> next == NULL)
{
if ((*l) -> value < val)
{
aux -> next = (*l);
(*l) = aux;
return;
}
else
{
aux -> next = NULL;
(*l) -> next = aux;
return;
}
}
//parcurg lista pina cind elementul meu este mai mare decit urmatorul din lista sau nu mai exista urmatorul element
while (( (*l) -> next) && ((*l) -> next -> value > val) )
(*l) = (*l) -> next;
//daca exista urm element salvez adresa lui
if ((*l) -> next)
aux -> next = (*l) -> next;
else
aux -> next = NULL; //daca nu e final
(*l) -> next = aux; //adaog elentul la lista
(*l) = first; //ma reintorc la inceputul listei
return;
}
int main()
{
long int i,t= 0;
List *lists;
List gutui;
long int high,weight,j;
FILE *f = fopen("gutui.in","r");
FILE *f1 = fopen("gutui.out","w");
fscanf(f,"%ld %ld %ld",&n,&hmax,&u);
lists = (List *) malloc (n * sizeof(struct nod) );
gutui = (Nod *) malloc (sizeof(struct nod));
gutui = NULL;
rangs = hmax/u +1;
//initializari
for (i = 0; i < rangs; i++)
{
lists[i] = (Nod *) malloc (sizeof(struct nod) ) ;
lists[i] = NULL;
}
//citirea datelor
for (i = 0; i < n; i++)
{
fscanf(f,"%ld %ld",&high,&weight);
if (high <= hmax)
{
j =(high / u) + ((high % u + u - 1) / u);
//printf("%ld %ld \n",j,i);
add(&lists[j],weight);
}
}
printf("%ld \n",lung(lists[0]));
//formarea listei cu gutui cu greutatea totala maxima
for (i=rangs -1; i >= 0; i--)
{
t++;
printf("Ordinul: %ld \n",i);
if (lists[i] !=NULL)
{
printf("am inserat %ld \n",top(lists[i]));
add(&gutui,delfirst(&lists[i]));
}
while (lists[i] !=NULL)
{
if (lung(gutui) < (t))
{
printf("am inserat simplu %ld \n",top(lists[i]));
add(&gutui,delfirst(&lists[i]));
}
else
if (top(lists[i]) > last(gutui))
{
printf("am inserat %ld \n",top(lists[i]));
printf("am sters %ld \n",last(gutui));
dellast(&gutui);
add(&gutui,delfirst(&lists[i]));
}
else lists[i] = NULL;
}
}
fprintf(f1,"%ld\n",sum(gutui));
fclose(f);
fclose(f1);
return 0;
}