Submission #468488

# Submission time Handle Problem Language Result Execution time Memory
468488 2021-08-28T14:08:43 Z gavgav Fountain (eJOI20_fountain) C++17
0 / 100
29 ms 9796 KB
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main(){
    /*
     * Get dates about fountain
     * Creating array in witch every number denotes index of the nearest number witch greater than this reservoir.
     * Indexes in this array means number of reservoir.
     */
    int height, queries;
    scanf("%d%d", &height, &queries);
    int diameters[height], next_grater[height + 1], capacities[height + 1];
    capacities[height] = 0;
    fill(next_grater, next_grater + height + 1, -1);
    stack <int> meowmeow;
    for (int i = 0; i < height; i++){
        scanf("%d%d", &diameters[i], &capacities[i]);
        while (!meowmeow.empty() && diameters[i] > diameters[meowmeow.top()]){
            next_grater[meowmeow.top()] = i;
            meowmeow.pop();
        }
        meowmeow.push(i);
    }
    while (!meowmeow.empty()){
        next_grater[meowmeow.top()] = height;
        meowmeow.pop();
    }
    cout << 1;
    /*
     * Creating of any possible water path.
     */

    int indexes[height + 1], last = 0;
    fill(indexes, indexes + height + 1, -1);
    vector <int> lengths;
    vector <vector <int>> ways;
    for (int i = 0; i < height;){
        ways.push_back(vector <int> {});
        lengths.push_back(-1);
        for (int j = i; j == -1;){
            ways[last].push_back(j);
            lengths[last]++;
            indexes[j] = last;
            if (j + 1 == next_grater[j]){
                i++;
            }
            j = next_grater[j];
        }
        i++;
        while (i < height && indexes[i] != - 1){
            i++;
        }
        last++;
    }
    /*
     * Creating postfix sums array.
     */
    for (int i = height; i > -1; i--){
        if (next_grater[i] != -1){
            capacities[i] += capacities[next_grater[i]];
        }
    }
    /*
     * Get queries and by binary search print answer.
     */
    int volume, level, left, right, middle, answer;
    while (queries--){
        scanf("%d %d", &level, &volume);
        level--;
        left = 0, right = lengths[indexes[level]];
        while (left - right < 1){
            middle = (left + right) / 2;
            if (volume > capacities[level] - capacities[ways[indexes[level]][middle]]){
                answer = ways[indexes[level]][middle] + 1;
                left = middle + 1;
            }
            else {
                right = middle - 1;
            }
        }
        printf("%d\n", answer % (height + 1));
    }
}

Compilation message

fountain.cpp: In function 'int main()':
fountain.cpp:12:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     scanf("%d%d", &height, &queries);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:18:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         scanf("%d%d", &diameters[i], &capacities[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         scanf("%d %d", &level, &volume);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:82:15: warning: 'answer' may be used uninitialized in this function [-Wmaybe-uninitialized]
   82 |         printf("%d\n", answer % (height + 1));
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 9796 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -