#41627: 這還有辦法變快ㄇ


19552108 (七年九班34號)

學校 : 臺北市立麗山國中
編號 : 278352
來源 : [220.129.198.237]
最後登入時間 :
2024-11-19 19:41:38
a014. 夾娃娃 | From: [36.226.23.66] | 發表日期 : 2024-08-11 12:38

#pragma GCC optimize("Ofast", "unroll-loops", "no-stack-protector", "fast-math", "prefetch-loop-arrays")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_INPUT_SIZE 100001  // Example limit for input size

int main() {
    // Allocate memory dynamically based on the maximum possible range
    int* e = (int*)calloc(MAX_INPUT_SIZE + 1, sizeof(int));

    if (e == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }

    while (1) {
        int n, y;
        if (scanf("%d %d", &n, &y) != 2) {
            break;
        }

        int max_count = 0, count = 0;
        int min_index = MAX_INPUT_SIZE, max_index = 0;

        for (int i = 0; i < n; i++) {
            int a, b;
            scanf("%d %d", &a, &b);

            int start = (b - y < 0) ? 0 : b - y;
            int end = a;

            // Track the active range to minimize the reset operation later
            if (start < min_index) min_index = start;
            if (end > max_index) max_index = end;

            for (int j = start; j <= end; j++) {
                int value = ++e[j];
                if (value > max_count) {
                    max_count = value;
                    count = 1;
                } else if (value == max_count) {
                    count++;
                }
            }
        }

        printf("%d %d\n", count, max_count);

        // Reset only the used portion of the array
        for (int i = min_index; i <= max_index; i++) {
            e[i] = 0;
        }
    }

    // Free allocated memory
    free(e);

    return 0;
}
 
#41628: Re: 這還有辦法變快ㄇ


19552108 (七年九班34號)

學校 : 臺北市立麗山國中
編號 : 278352
來源 : [220.129.198.237]
最後登入時間 :
2024-11-19 19:41:38
a014. 夾娃娃 | From: [36.226.23.66] | 發表日期 : 2024-08-11 12:39

#pragma GCC optimize("Ofast", "unroll-loops", "no-stack-protector", "fast-math", "prefetch-loop-arrays")
#include 
#include 
#include 

#define MAX_INPUT_SIZE 100001  // Example limit for input size

int main() {
    // Allocate memory dynamically based on the maximum possible range
    int* e = (int*)calloc(MAX_INPUT_SIZE + 1, sizeof(int));

    if (e == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }

    while (1) {
        int n, y;
        if (scanf("%d %d", &n, &y) != 2) {
            break;
        }

        int max_count = 0, count = 0;
        int min_index = MAX_INPUT_SIZE, max_index = 0;

        for (int i = 0; i < n; i++) {
            int a, b;
            scanf("%d %d", &a, &b);

            int start = (b - y < 0) ? 0 : b - y;
            int end = a;

            // Track the active range to minimize the reset operation later
            if (start < min_index) min_index = start;
            if (end > max_index) max_index = end;

            for (int j = start; j <= end; j++) {
                int value = ++e[j];
                if (value > max_count) {
                    max_count = value;
                    count = 1;
                } else if (value == max_count) {
                    count++;
                }
            }
        }

        printf("%d %d\n", count, max_count);

        // Reset only the used portion of the array
        for (int i = min_index; i <= max_index; i++) {
            e[i] = 0;
        }
    }

    // Free allocated memory
    free(e);

    return 0;
}
通過檢測
通過檢測

#2: 30% AC (6ms, 448KB)

通過檢測

#3: 30% TLE (1s)

Killed



 
ZeroJudge Forum