#include <stdio.h> #include <stdlib.h> typedef struct { int depth; int time; } Station;…
- User
#include <stdio.h> #include <stdlib.h>
typedef struct { int depth; int time; } Station;
void heapify(int *heap, int size, int i) { int minimum = i; int l = 2 * i + 1; int r = 2 * i + 2;
if (l < size && heap[l] < heap[minimum]) minimum = l; if (r < size && heap[r] < heap[minimum]) minimum = r; if (minimum != i) { int temp = heap[minimum]; heap[minimum] = heap[i]; heap[i] = temp; heapify(heap, size, minimum); }}
int compare(const void *a, const void *b) { Station *stationA = (Station *)a; Station *stationB = (Station *)b; return stationA->depth - stationB->depth; }
int deleteMin(int *heap, int *size) { if (*size == 0) return -1; int min = heap[0]; heap[0] = heap[--(*size)]; heapify(heap, *size, 0); return min; }
void insert(int heap[], int *size, int value) { heap[(*size)++] = value; for (int i = *size / 2 - 1; i >= 0; i--) { heapify(heap, *size, i); } }
void func(Station stations[], int num_stations, int ini_oxygen, int final_depth, int additional_oxygen) { qsort(stations, num_stations, sizeof(Station), compare); int heap[num_stations]; int heap_size = 0; int current_time = 0, max_depth = ini_oxygen;
for (int i = 0; i < num_stations; ++i) { while (max_depth < stations[i].depth && heap_size > 0) { int refill_time = deleteMin(heap, &heap_size); current_time += refill_time; max_depth += additional_oxygen; } if (max_depth < stations[i].depth) break; insert(heap, &heap_size, stations[i].time); if (max_depth >= final_depth) { printf("%d\n", current_time); return; } } while (heap_size > 0 && max_depth < final_depth) { int refill_time = deleteMin(heap, &heap_size); current_time = current_time + refill_time; max_depth =max_depth + additional_oxygen; } if (max_depth >= final_depth) { printf("%d", current_time); } else { printf("-1 %d", max_depth); }}
int main() { int num_stations, ini_oxygen, final_depth, additional_oxygen; scanf("%d %d %d %d", &num_stations, &ini_oxygen, &final_depth, &additional_oxygen);
Station stations[num_stations]; for (int i = 0; i < num_stations; ++i) { scanf("%d", &stations[i].depth); } for (int i = 0; i < num_stations; ++i) { scanf("%d", &stations[i].time); } func(stations, num_stations, ini_oxygen, final_depth, additional_oxygen); return 0;}
change only func function
- Assistant