Submission #1315246

#TimeUsernameProblemLanguageResultExecution timeMemory
1315246benightGlobal Warming (NOI13_gw)C++17
30 / 40
530 ms39316 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    if (!(cin >> n)) return 0;

    vector<int> raw(n);
    for (int i = 0; i < n; i++) cin >> raw[i];

    // 1. Remove consecutive duplicates
    vector<int> h;
    h.push_back(0); // Boundary
    for (int i = 0; i < n; i++) {
        if (h.empty() || raw[i] != h.back()) {
            h.push_back(raw[i]);
        }
    }
    h.push_back(0); // Boundary

    // 2. Identify local extrema and their impact
    // We use a map to store the net change in island count at each altitude
    map<int, int> changes;

    for (int i = 1; i < h.size() - 1; i++) {
        if (h[i-1] < h[i] && h[i+1] < h[i]) {
            // Peak: At sea level h[i], an island disappears
            changes[h[i]]--;
        } else if (h[i-1] > h[i] && h[i+1] > h[i]) {
            // Valley: At sea level h[i], an island splits (new island created)
            changes[h[i]]++;
        }
    }

    // 3. Sweep through altitudes from highest to lowest
    int max_islands = 0;
    int current_islands = 0;

    // We process from high altitude to low altitude.
    // If we start with sea level very high, islands = 0.
    // As sea level drops below a peak, current_islands++
    // As sea level drops below a valley, current_islands--
    for (auto it = changes.rbegin(); it != changes.rend(); ++it) {
        // Note: The logic in the loop is inverted because we are going 
        // from top to bottom. A peak encountered from the top increases islands.
        current_islands -= it->second; 
        max_islands = max(max_islands, current_islands);
    }

    // If all altitudes were 0, the answer is 0. Otherwise, at least 1.
    if (h.size() <= 2) cout << 0 << endl;
    else cout << max(1, max_islands) << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...