Submission #504136

# Submission time Handle Problem Language Result Execution time Memory
504136 2022-01-09T22:17:25 Z gavgav Fountain (eJOI20_fountain) C++17
100 / 100
99 ms 6724 KB
// This is Lucallie's solving but with some optimizations and explanations, 'cause when I solved this problem with own code I wondered why this solving is so fast and try to understand it, but he's code is weird and strange, but super fast.
#include <bits/stdc++.h>
using namespace std;
struct reservoir{
    int diameter;
    short capacity; // 1 <= diameter <= 10^9 | 1 <= capacity <= 1000
};
struct query{
    int level, volume, answer; // 1 <= volume <= 10^9
};
int main() {
    int height, queries_number, necessary = 0, top = 0, i, j; // 2 <= height <= 10^5 | 1 <= queries 2 * 10^5
    scanf( "%d%d", &height, &queries_number);
    int previous_request[queries_number + 1], // i-th number of previous_request keeps index of previous request that opened tap of this request
        last_request[height + 1] {}, // i-th number of last_request keeps index of last query that open tap at i-th reservoir
        necessary_water[height + 1], // i-th number of necessary_water keeps volume of water which need to reach waterways from i-th + 1 reservoir
        stream[height + 1];
    reservoir reservoirs[height + 1];
    query queries[queries_number + 1];
    reservoirs[0].diameter = 1000000001; // maximum possible diameter + 1
    for (i = 1; i <= height; ++i)
        scanf( "%d%hd", &reservoirs[i].diameter, &reservoirs[i].capacity);
    for (i = 1; i <= queries_number; ++i) {
        scanf( "%d%d", &queries[i].level, &queries[i].volume);
        previous_request[i] = last_request[queries[i].level];
        last_request[queries[i].level] = i;
    }
    stream[0] = 0;
    int left, middle, right;
    for (i = height; i > 0; --i) {
        while (reservoirs[i].diameter >= reservoirs[stream[top]].diameter){
            necessary -= reservoirs[stream[top]].capacity;
            --top;
        }
        ++top;
        stream[top] = i;
        necessary_water[top] = necessary;
        necessary += reservoirs[i].capacity;
        j = last_request[i];
        while (j != 0) {
            left = 0;
            right = top + 1;
            while (right - left > 1 ) {
                middle = (right + left) / 2;
                if (necessary - necessary_water[middle] >= queries[j].volume)
                    left = middle;
                else
                    right = middle;
            }
            queries[j].answer = stream[left];
            j = previous_request[j];
        }
    }
    for (i = 1; i <= queries_number; ++i){
        printf("%d\n", queries[i].answer);
    }
}

Compilation message

fountain.cpp: In function 'int main()':
fountain.cpp:13:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     scanf( "%d%d", &height, &queries_number);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:22:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         scanf( "%d%hd", &reservoirs[i].diameter, &reservoirs[i].capacity);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         scanf( "%d%d", &queries[i].level, &queries[i].volume);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 4832 KB Output is correct
2 Correct 70 ms 5124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 63 ms 4832 KB Output is correct
9 Correct 70 ms 5124 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 45 ms 3424 KB Output is correct
12 Correct 99 ms 6724 KB Output is correct
13 Correct 87 ms 6596 KB Output is correct
14 Correct 71 ms 6632 KB Output is correct
15 Correct 69 ms 6588 KB Output is correct