/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 284.0 KiB
#2 Accepted 22ms 1.367 MiB
#3 Accepted 26ms 1.531 MiB
#4 Accepted 23ms 1.559 MiB
#5 Accepted 27ms 1.719 MiB
#6 Accepted 29ms 1.855 MiB
#7 Accepted 31ms 1.797 MiB
#8 Accepted 73ms 1.848 MiB
#9 Accepted 76ms 1.812 MiB
#10 Accepted 80ms 1.766 MiB
#11 Accepted 84ms 1.801 MiB
#12 Accepted 84ms 1.809 MiB
#13 Accepted 42ms 1.812 MiB
#14 Accepted 477ms 1.863 MiB
#15 Time Exceeded ≥4019ms ≥1.883 MiB

Code

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

typedef struct {
    int a;
    int idx;
} Pair;

Pair *pairs;
int n, x;

int compare(const void *a, const void *b) {
    Pair *pa = (Pair *)a;
    Pair *pb = (Pair *)b;
    return pa->a - pb->a;
}

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

    while (T--) {
        scanf("%d%d", &n, &x);
        pairs = (Pair *)malloc(n * sizeof(Pair));

        for (int i = 0; i < n; i++) {
            scanf("%d", &pairs[i].a);
            pairs[i].idx = i + 1;
        }

        qsort(pairs, n, sizeof(Pair), compare);

        int min_loss = INT_MAX;
        int optimal_k = 0;

        for (int k = 1; k <= x; k++) {
            int loss = 0;
            int prev_idx = -1;

            for (int i = 0; i < n; i++) {
                if (pairs[i].a % k == 0 && (prev_idx == -1 || pairs[i].idx != pairs[prev_idx].idx + 1)) {
                    loss += pairs[i].a;
                    prev_idx = i;
                }
            }

            if (loss < min_loss) {
                min_loss = loss;
                optimal_k = k;
            }
        }

        printf("%d\n", optimal_k);
        free(pairs);
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1039 Prince Roy, the undisputed ruler of the world
Contest
Brain Booster #3
Language
C++17 (G++ 13.2.0)
Submit At
2024-05-06 15:54:32
Judged At
2024-11-11 03:33:41
Judged By
Score
65
Total Time
≥4019ms
Peak Memory
≥1.883 MiB