답안 #468492

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
468492 2021-08-28T14:14:46 Z gavgav Fountain (eJOI20_fountain) C++17
30 / 100
104 ms 3004 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();
    }
    /*
     * 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:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         scanf("%d %d", &level, &volume);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
fountain.cpp:81:15: warning: 'answer' may be used uninitialized in this function [-Wmaybe-uninitialized]
   81 |         printf("%d\n", answer % (height + 1));
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 3004 KB Output is correct
2 Correct 104 ms 2952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -