CSES - Datatähti 2025 alku - Results
Submission details
Task:Tikut
Sender:zfoil
Submission time:2024-11-10 16:43:39 +0200
Language:C
Status:COMPILE ERROR

Compiler report

input/code.c: In function 'insort':
input/code.c:29:98: error: 'Ll_node' has no member named 'longest'
   29 |         if (ll_node->longest_piece > (*head)->longest_piece || (ll_node->longest_piece == (*head)->longest piece && ll_node->original_height > (*head)->original_height)) {
      |                                                                                                  ^~
input/code.c:29:107: error: expected ')' before 'piece'
   29 |         if (ll_node->longest_piece > (*head)->longest_piece || (ll_node->longest_piece == (*head)->longest piece && ll_node->original_height > (*head)->original_height)) {
      |                                                                ~                                          ^~~~~~
      |                                                                                                           )
input/code.c: In function 'main':
input/code.c:104:18: warning: unused variable 'tmp_stick' [-Wunused-variable]
  104 |         Ll_node...

Code

#include <stdio.h>
#include <stdlib.h>

typedef struct Ll_node Ll_node;

struct Ll_node {
	int original_height;
	int longest_piece;
	int shortest_piece;
	int number_of_cuts;
	Ll_node *next;
};

void update_longest_piece(Ll_node *ll_node)
{
	int preliminary_value = ll_node->original_height / (ll_node->number_of_cuts + 1);
	ll_node->shortest_piece = preliminary_value;
	if (preliminary_value * (ll_node->number_of_cuts + 1) == ll_node->original_height) {
		ll_node->longest_piece = preliminary_value;
	} else {
		ll_node->longest_piece = preliminary_value + 1;
	}
}

void insort(Ll_node *ll_node, Ll_node **head)
{
	Ll_node *current_ll_node = *head;
	if (ll_node->longest_piece > (*head)->longest_piece || (ll_node->longest_piece == (*head)->longest piece && ll_node->original_height > (*head)->original_height)) {
		ll_node->next = *head;
		*head = ll_node;
		return;
	}
	while (current_ll_node->next != NULL && current_ll_node->next->longest_piece > ll_node->longest_piece) {
		current_ll_node = current_ll_node->next;
	}
	ll_node->next = current_ll_node->next;
	current_ll_node->next = ll_node;
}

void print_ll(Ll_node *head)
{
	Ll_node *ll_node = head;
	while (ll_node != NULL) {
		printf("Original length: %d, longest piece: %d, shortest_piece: %d\n", ll_node->original_height, ll_node->longest_piece, ll_node->shortest_piece);
		ll_node = ll_node->next;
	}
	printf("\n");
}

void distribute_cut(Ll_node **head)
{
	Ll_node *loose_head;
	(*head)->number_of_cuts++;
	update_longest_piece(*head);
	if ((*head)->next == NULL) {
		return;
	}
	loose_head = *head;
	*head = (*head)->next;
	insort(loose_head, head);
}

Ll_node *parse_ll(int number_of_sticks)
{
	int stick_height, i;
	Ll_node *head, *tmp_stick;
	for (i = 0; i < number_of_sticks; i++) {
		tmp_stick = malloc(sizeof(*tmp_stick));
		scanf("%d", &stick_height);
		tmp_stick->original_height = stick_height;
		tmp_stick->longest_piece = stick_height;
		tmp_stick->shortest_piece = stick_height;
		tmp_stick->number_of_cuts = 0;
		tmp_stick->next = NULL;
		if (i == 0) {
			head = tmp_stick;
			continue;
		}
		insort(tmp_stick, &head);
	}
	return head;
}

int find_maximum_difference(Ll_node *head)
{
	int max = -1, min = 1000000001;
	Ll_node *ll_node = head;
	while (ll_node != NULL) {
		if (ll_node->longest_piece > max) {
			max = ll_node->longest_piece;
		}
		if (ll_node->shortest_piece < min) {
			min = ll_node->shortest_piece;
		}
		ll_node = ll_node->next;
	}
	return max - min;
}

int main(void)
{
	int number_of_sticks, max_number_of_cuts, i;
	Ll_node *tmp_stick, *head;
	scanf("%d%d", &number_of_sticks, &max_number_of_cuts);
	head = parse_ll(number_of_sticks);
	for (i = 0; i < max_number_of_cuts; i++) {
		distribute_cut(&head);
		printf("%d ", find_maximum_difference(head));
	}
	return 0;
}