Mais um lab de CES-11! espero que aproveitem!
Código:
#include<stdio.h>
#include<stdlib.h>
typedef struct node node;
typedef struct lista lista;
typedef struct arvore arvore;
/*************************************
struct dos nos pra criar as listas ligadas
*************************************/
struct node{
int valor;
node *prox;
};
/*************************************
struct das listas maximais crescente
*************************************/
struct lista{
int tamanho;
node *first;
};
/*************************************
Funcao sift
*************************************/
void sift(lista heap[], int n, int i){
int esq = 2*i;
int dir = 2*i + 1;
int menor = i;
if(esq <= n && heap[esq].tamanho < heap[i].tamanho)
menor = esq;
if(dir <= n && heap[dir].tamanho < heap[menor].tamanho)
menor = dir;
if(menor != i){
lista aux = heap[i];
heap[i] = heap[menor];
heap[menor] = aux;
sift(heap, n, menor);
}
}
/*************************************
funcao build para transformar um vetor em heap
*************************************/
void build(lista heap[], int n){
for(int i = n/2; i > 0; i--)
sift(heap, n, i);
}
int main(){
lista heap[400];
FILE *entrada, *saida;
node *aux;
lista maximal;
int atual, anterior, n; ///n eh o tamanho do heap
int tempo = 0;
//Inicia-se a leitura do documento (primeiro elemento)
maximal.tamanho = 1;
n = 0;
entrada = fopen("entrada.txt", "r");
saida = fopen("saida", "w");
maximal.first = (node*)malloc(sizeof(node));
aux = maximal.first;
aux->prox = NULL;
fscanf(entrada, "%d", &anterior);
aux->valor = anterior;
//Continua a leitura ate o final
while(fscanf(entrada, "%d", &atual) > 0){
if(atual >= anterior){
aux->prox = (node*)malloc(sizeof(node));
aux = aux->prox;
aux->prox = NULL;
maximal.tamanho ++;
aux->valor = atual;
}
else{
n ++;
heap[n] = maximal;
maximal.first = (node*)malloc(sizeof(node));
maximal.tamanho = 1;
aux = maximal.first;
aux->valor = atual;
aux->prox = NULL;
}
anterior = atual;
}
n ++;
heap[n] = maximal;
///transforma o vetor heap[] em um heap de minimo
build(heap, n);
while(n > 1){
///junçao de listas
node *v1, *v2;
v1 = heap[1].first;
if(n < 3 || heap[2].tamanho <= heap[3].tamanho)
v2 = heap[2].first;
else
v2 = heap[3].first;
if(v2->valor < v1->valor){
heap[1].first = v2;
v2 = v2->prox;
heap[1].first->prox = v1;
v1 = heap[1].first;
}
while(v1->prox != NULL && v2 != NULL){
if(v1->prox->valor <= v2->valor)
v1 = v1->prox;
else{
aux = v1->prox;
v1->prox = v2;
v2 = v2->prox;
v1 = v1->prox;
v1->prox = aux;
}
}
if(v2 != NULL)
v1->prox = v2;
if(n < 3 || heap[2].tamanho <= heap[3].tamanho)
heap[1].tamanho += heap[2].tamanho;
else
heap[1].tamanho += heap[3].tamanho;
//fim da juncao de duas listas
tempo += heap[1].tamanho;
if(n < 3 || heap[2].tamanho <= heap[3].tamanho)
heap[2] = heap[3];
n--;//diminui o tamanho do heap[]
for(int i = 4; i <= n; i++)
heap[i-1] = heap[i];
sift(heap, n, 1);//reaordenar o array
}
aux = heap[1].first;
while(aux != NULL){
fprintf(saida, "%d\n", aux->valor);
aux = aux->prox;
}
fprintf(saida, "%d\n", tempo);
fclose(entrada);
fclose(saida);
return 0;
}
Nenhum comentário:
Postar um comentário