/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Wrong Answer 0ms 284.0 KiB
#2 Accepted 12ms 1.555 MiB
#3 Accepted 15ms 1.586 MiB
#4 Accepted 13ms 1.512 MiB
#5 Accepted 17ms 1.77 MiB
#6 Accepted 18ms 1.77 MiB
#7 Accepted 20ms 1.891 MiB
#8 Accepted 52ms 1.855 MiB
#9 Accepted 54ms 1.914 MiB
#10 Accepted 57ms 1.816 MiB
#11 Accepted 60ms 1.926 MiB
#12 Accepted 60ms 1.746 MiB
#13 Accepted 29ms 1.773 MiB
#14 Accepted 328ms 1.855 MiB
#15 Accepted 3033ms 1.871 MiB
#16 Time Exceeded ≥4077ms ≥1.797 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:55:24
Judged At
2024-11-11 03:33:40
Judged By
Score
70
Total Time
≥4077ms
Peak Memory
≥1.926 MiB