【无标题】

7-13

#include 
using namespace std;
int beginn[10005];//存开始时间
int endd[10005];//存结束时间
int main()
{
    int n;//总活动个数
    int cnt = 0;//需要的最少会场数
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> beginn[i] >> endd[i];
    //按开始时间升序排列
    sort(beginn, beginn + n);
    //按结束时间升序排列
     sort(endd, endd + n);
     int j = 0;
     for (int i = 0; i < n; i++)
     {
      if (beginn[i] < endd[j])
       cnt++;
      else
       j++;
     }
     cout << cnt;
     return 0; 
}


7-14

#include 
#include 

void minHeapify(int arr[], int n, int i) {
    int smallest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;

    if (l < n && arr[l] < arr[smallest])
        smallest = l;
    if (r < n && arr[r] < arr[smallest])
        smallest = r;

    if (smallest != i) {
        int temp = arr[i];
        arr[i] = arr[smallest];
        arr[smallest] = temp;
        minHeapify(arr, n, smallest);
    }
}

void maxHeapify(int arr[], int n, int i) {
    int largest = i;
    int l = 2 * i + 1;
    int r = 2 * i + 2;

    if (l < n && arr[l] > arr[largest])
        largest = l;
    if (r < n && arr[r] > arr[largest])
        largest = r;

    if (largest != i) {
        int temp = arr[i];
        arr[i] = arr[largest];
        arr[largest] = temp;
        maxHeapify(arr, n, largest);
    }
}

int calculateComparisons(int arr[], int n, int minHeap) {
    int totalComparisons = 0;
    int size = n;

    while (size > 1) {
        int first = arr[0];
        arr[0] = arr[size - 1];
        size--;
        if (minHeap)
            minHeapify(arr, size, 0);
        else
            maxHeapify(arr, size, 0);

        int second = arr[0];
        int merged = first + second;
        totalComparisons += merged - 1;

        arr[0] = merged;
        if (minHeap)
            minHeapify(arr, size, 0);
        else
            maxHeapify(arr, size, 0);
    }

    return totalComparisons;
}

int main() {
    int k;
    scanf("%d", &k);

    int *lengthsMin = (int *)malloc(k * sizeof(int));
    int *lengthsMax = (int *)malloc(k * sizeof(int));

    for (int i = 0; i < k; i++) {
        scanf("%d", &lengthsMin[i]);
        lengthsMax[i] = lengthsMin[i];
    }

    // 构建最小堆
    for (int i = k / 2 - 1; i >= 0; i--)
        minHeapify(lengthsMin, k, i);

    // 构建最大堆
    for (int i = k / 2 - 1; i >= 0; i--)
        maxHeapify(lengthsMax, k, i);

    int minComparisons = calculateComparisons(lengthsMin, k, 1);
    int maxComparisons = calculateComparisons(lengthsMax, k, 0);

    printf("%d ", maxComparisons);
    printf("%d", minComparisons);

    free(lengthsMin);
    free(lengthsMax);
    return 0;
}


7-15

#include 

int min_refueling_stops(int n, int k, int stations[]) {
    int stops = 0, current_fuel = n, i;

    for (i = 0; i < k; i++) {
        // 距离超过n, 无法到达
        if (stations[i] > n) {
            return -1;
        }

        // 检查是否需要在当前加油站加油
        if (current_fuel < stations[i]) {
            stops++;        // 增加加油次数
            current_fuel = n; // 重新加满油
        }

        // 消耗油量以到达下一个加油站
        current_fuel -= stations[i];
    }

    return stops;
}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    int stations[k + 1]; // 加油站数组
    for (int i = 0; i < k + 1; i++) {
        scanf("%d", &stations[i]);
    }

    int result = min_refueling_stops(n, k + 1, stations);
    if (result == -1) {
        printf("No Solution!\n");
    } else {
        printf("%d\n", result);
    }

    return 0;
}




你可能感兴趣的:(算法,数据结构,排序算法)