#include #include #include struct stick_struct { int index; int height; int penalty; }; struct hole_struct { int depth; bool is_filled; int num_sticks; int stick_indexes[100]; }; int compare_sticks(const void *a, const void *b); int sort_sticks(struct stick_struct sticks[], int n); int calculate_score(struct hole_struct holes[], int num_holes, int penalty_sum); void place_stick(struct hole_struct holes[], int *num_holes, struct stick_struct stick, struct stick_struct sticks[]); // Amount of Sticks int n; // Depth of the Holes int b; // Temporary value int temp; int main() { // ########################################## // // ########### INPUT STARTS HERE ############ // // ########################################## // // Open the input file FILE *input_file = fopen("sticks.in", "r"); // Check if the file is opened successfully if (input_file == NULL) { fprintf(stderr, "Error opening input.in\n"); return 1; // Return an error code } // Read the amount of sticks from the file fscanf(input_file, "%d", &n); // Read the depth of the holes from the file fscanf(input_file, "%d", &b); // Create an array of sticks struct stick_struct sticks[n]; // Set all the indexes for the sticks for (int i = 0; i < n; i++) { sticks[i].index = i; } // Get the heights for all the sticks for (int i = 0; i < n; i++) { fscanf(input_file, "%d", &temp); sticks[i].height = temp; } // Get the penalties for all the sticks for (int i = 0; i < n; i++) { fscanf(input_file, "%d", &temp); sticks[i].penalty = temp; } // Close the input file fclose(input_file); // ########################################## // // ############ INPUT ENDS HERE ############# // // ########################################## // // Sort the sticks from the highest penalty to the lowest sort_sticks(sticks, n); // ########################################## // // ######## HOLE LOGIC STARTS HERE ########## // // ########################################## // // Set the initial depth of the holes int initial_depth = b; // Start with a hole int num_holes = 1; // Allocate memory for the first hole and set its information struct hole_struct *holes = (struct hole_struct *)malloc(sizeof(struct hole_struct)); holes[0].depth = initial_depth; holes[0].is_filled = false; holes[0].num_sticks = 0; // Initialize stick_indexes for the first hole for (int j = 0; j < 100; j++) { holes[0].stick_indexes[j] = 101; } // ########################################## // // ######### HOLE LOGIC ENDS HERE ########### // // ########################################## // // Iterate through all the sticks and try to place them into holes int penalty_sum = 0; for (int i = 0; i < n; i++) { place_stick(holes, &num_holes, sticks[i], sticks); penalty_sum += sticks[i].penalty; } // ########################################## // // ####### OUTPUT LOGIC STARTS HERE ######### // // ########################################## // // Open the output file FILE *output_file = fopen("sticks.out", "w"); // Check if the file is opened successfully if (output_file == NULL) { fprintf(stderr, "Error opening sticks.out\n"); return 1; // Return an error code } fprintf(output_file, "%d\n", num_holes); for (int i = 0; i < num_holes; i++) { fprintf(output_file, "%d ", holes[i].num_sticks); for (int j = 0; j < 100; j++) { if (holes[i].stick_indexes[j] >= 0 && holes[i].stick_indexes[j] < 101) fprintf(output_file, "%d ", holes[i].stick_indexes[j]); } if (i != num_holes - 1) fprintf(output_file, "\n"); } // Close the output file fclose(output_file); // Free dynamically allocated memory when done free(holes); return 0; // Return 0 to indicate successful execution } // Comparison function for qsort int compare_sticks(const void *a, const void *b) { // Compare sticks based on penalties in descending order return ((struct stick_struct *)b)->penalty - ((struct stick_struct *)a)->penalty; } int sort_sticks(struct stick_struct sticks[], int n) { // Use qsort to sort the sticks array based on penalties qsort(sticks, n, sizeof(struct stick_struct), compare_sticks); return 0; // Return 0 for successful sorting } void place_stick(struct hole_struct holes[], int *num_holes, struct stick_struct stick, struct stick_struct sticks[]) { int penalty_sum = stick.penalty; // Initialize with the penalty of the current stick // Iterate through existing holes to find a suitable one for (int i = 0; i < *num_holes; i++) { if (!holes[i].is_filled && holes[i].depth > stick.height) { // Stick fits into the hole holes[i].depth -= stick.height; holes[i].num_sticks += 1; holes[i].stick_indexes[holes[i].num_sticks - 1] = stick.index + 1; if (holes[i].depth == 0) holes[i].is_filled = true; return; // Exit the function after placing the stick } } // If no existing hole can accommodate the stick, calculate the score for creating a new hole int score_with_new_hole = calculate_score(holes, *num_holes, penalty_sum); // Calculate the score for using an existing hole with penalty ONLY IF THE DEPTH IS BIGGER THAN 0 int score_with_existing_hole; int last_hole_index = *num_holes - 1; if (last_hole_index >= 0 && holes[last_hole_index].depth > 0) { // Correct the parameters for the calculate_score function penalty_sum = 0; // Reset penalty_sum for local usage for (int j = 0; j < holes[last_hole_index].num_sticks; j++) { int index = holes[last_hole_index].stick_indexes[j] - 1; penalty_sum += sticks[index].penalty; } score_with_existing_hole = calculate_score(holes, *num_holes - 1, penalty_sum); } else { // Set a high score to ensure a new hole is created if depth is 0 or no holes exist score_with_existing_hole = score_with_new_hole + 1; } // Decide whether to create a new hole or use an existing one based on the score if (score_with_new_hole < score_with_existing_hole) { // Create a new hole *num_holes += 1; // Increment num_holes before realloc holes = (struct hole_struct *)realloc(holes, (*num_holes) * sizeof(struct hole_struct)); holes[*num_holes - 1].depth = b; holes[*num_holes - 1].is_filled = false; holes[*num_holes - 1].num_sticks = 1; holes[*num_holes - 1].stick_indexes[0] = stick.index + 1; } else { // Use an existing hole with penalty last_hole_index = *num_holes - 1; if (last_hole_index >= 0 && holes[last_hole_index].depth > 0) { holes[last_hole_index].depth -= stick.height; holes[last_hole_index].num_sticks += 1; if (holes[last_hole_index].depth == 0) holes[last_hole_index].is_filled = true; holes[last_hole_index].stick_indexes[holes[last_hole_index].num_sticks - 1] = stick.index + 1; } } } int calculate_score(struct hole_struct holes[], int num_holes, int penalty_sum) { return num_holes * num_holes * num_holes + penalty_sum; }