Submission #1023380

# Submission time Handle Problem Language Result Execution time Memory
1023380 2024-07-14T17:25:52 Z Shadxwed Fountain (eJOI20_fountain) C
0 / 100
1500 ms 2104 KB
#include <stdio.h>
#include <stdlib.h>

// Structure to keep track of the original index after sorting
typedef struct {
    int value;
    int index;
} Element;

// Comparison function for qsort
int compare(const void *a, const void *b) {
    return ((Element *)a)->value - ((Element *)b)->value;
}

int main() {
    int n, q;

    // Reading number of elements and queries
    scanf("%d %d", &n, &q);

    Element *d = malloc(n * sizeof(Element));
    int *v = malloc(n * sizeof(int));
    int *prefixSum = malloc((n + 1) * sizeof(int));

    // Reading values into arrays
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &d[i].value, &v[i]);
        d[i].index = i;  // Store original index
    }

    // Compute prefix sums
    prefixSum[0] = 0;
    for (int i = 1; i <= n; i++) {
        prefixSum[i] = prefixSum[i - 1] + v[i - 1];
    }

    // Sort the elements based on their values in d
    qsort(d, n, sizeof(Element), compare);

    int q1, q2;
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &q1, &q2);
        q1--;

        int prev = -1;
        int remaining = q2;

        // Process the query using the sorted array
        for (int j = 0; j < n; j++) {
            int idx = d[j].index;
            if (idx >= q1 && d[j].value > prev) {
                remaining -= v[idx];
                prev = d[j].value;
                if (remaining <= 0) {
                    printf("%d\n", idx + 1);
                    break;
                }
            }
        }

        if (remaining > 0) {
            printf("0\n");
        }
    }

    // Free allocated memory
    free(d);
    free(v);
    free(prefixSum);

    return 0;
}

Compilation message

fountain.c: In function 'main':
fountain.c:19:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     scanf("%d %d", &n, &q);
      |     ^~~~~~~~~~~~~~~~~~~~~~
fountain.c:27:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         scanf("%d %d", &d[i].value, &v[i]);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.c:42:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         scanf("%d %d", &q1, &q2);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1588 ms 2104 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -