Submission #1168604

#TimeUsernameProblemLanguageResultExecution timeMemory
1168604zh_hLightning Rod (NOI18_lightningrod)C++20
80 / 100
1102 ms263240 KiB
#include <bits/stdc++.h>
#define pb push_back
using namespace std;

inline int readInt() {
    int x = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') ch = getchar_unlocked();
    while (ch >= '0' && ch <= '9'){
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar_unlocked();
	}
    return x;
}

int X[10000000], Y[10000000];

int main() {

    int n = readInt();

    vector<pair<int, int>> building; // building[id] = (xi, yi)
    vector<pair<int, int>> building_mapper; // building_mapper => sort

    for (int i = 0; i < n; i ++) {
		X[i] = readInt();
		Y[i] = readInt();

        building.pb({X[i], Y[i]});
        building_mapper.pb({Y[i], i});
    }

    vector<bool> is_covered(n+1, false);

    sort(building_mapper.begin(), building_mapper.end(), [](pair<int, int> a, pair<int, int> b){
        return a.first > b.first;
    });

    int ans = 0;
    for (int i = 0; i < n; i ++) {
        
        if (is_covered[building_mapper[i].second]) {continue;}

        ans ++;
        pair<int, int> cur_building = building[building_mapper[i].second];

        is_covered[building_mapper[i].second] = true;
        for (int j = building_mapper[i].second-1; j >= 0; j --) { // check the left side of cur_building
            if ( abs(building[j].first - cur_building.first) <= abs(building[j].second - cur_building.second) ) {
                is_covered[j] = true;
            } else {
                break;
            }
        }

        for (int j = building_mapper[i].second+1; j < n; j ++) { // check the right side of cur_building
            if ( abs(building[j].first - cur_building.first) <= abs(building[j].second - cur_building.second) ) {
                is_covered[j] = true;
            } else {
                break;
            }
        }

    }

    cout << ans << endl;
}

// key idea:
// 1. find the tallest building that is not covered yet
// 2. plant a lightning rod there
// 3. check the left and right side of the building
//     => if the building is under cover, mark it under cover and continue check
//     => if meet a building is not under cover, stop (think why !)
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...