Submission #1269733

#TimeUsernameProblemLanguageResultExecution timeMemory
1269733ducdevLightning Rod (NOI18_lightningrod)C++17
7 / 100
1103 ms248824 KiB
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> ii;

const int MAX_N = 1e7;
const int MOD = 1e9 + 7;
const int INF = 1e9;

template <class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x <= y) return false;
    x = y;
    return true;
};

struct FenwickTree {
    int n;
    vector<int> bit;

    void update(int pos, int val) {
        for (; pos > 0; pos -= pos & (-pos))
            minimize(bit[pos], val);
    };

    int get(int pos) {
        int ret = INF;
        for (; pos <= n; pos += pos & (-pos))
            minimize(ret, bit[pos]);
        return ret;
    };

    FenwickTree(int n) : n(n) {
        bit.assign(n + 5, INF);
    };
};

int n;
ii a[MAX_N + 5];

namespace SUBTASK_1 {
    const int N = MAX_N;
    const int V = 1e9;

    bitset<V + 1> mark;

    bool checkSubtask() {
        for (int i = 1; i <= n; i++) {
            if (a[i].second != 1) return false;
        };
        return true;
    };

    void Solve() {
        for (int i = 1; i <= n; i++) mark.set(a[i].first);
        cout << mark.count() << '\n';
    };
};  // namespace SUBTASK_1

namespace SUBTASK_2345 {
    const int N = 2e5;

    int dp[N + 5], sum[N + 5], diff[N + 5];

    void Solve() {
        for (int i = 1; i <= n; i++) dp[i] = INF;

        vector<int> v;
        for (int i = 1; i <= n; i++) {
            int x = a[i].first, y = a[i].second;
            v.push_back(x + y);
            v.push_back(x - y);
        };

        sort(all(v));
        v.resize(unique(all(v)) - v.begin());

        for (int i = 1; i <= n; i++) {
            int x = a[i].first, y = a[i].second;
            sum[i] = lower_bound(all(v), x + y) - v.begin() + 1;
            diff[i] = lower_bound(all(v), x - y) - v.begin() + 1;
        };

        int sz = v.size();

        FenwickTree prevDp(sz), bit(sz);

        dp[0] = 0;
        for (int i = 1; i <= n; i++) {
            // turn on previous lightning
            minimize(dp[i], bit.get(sum[i]));

            // turn on ith lightning
            minimize(dp[i], dp[i - 1] + 1);
            minimize(dp[i], prevDp.get(diff[i]) + 1);

            bit.update(sum[i], dp[i]);
            prevDp.update(diff[i], dp[i - 1]);
        };

        cout << dp[n] << '\n';
    };
};  // namespace SUBTASK_2345

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen("MAIN.INP", "r")) {
        freopen("MAIN.INP", "r", stdin);
        freopen("MAIN.OUT", "w", stdout);
    };

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].first >> a[i].second;
    };
  
    if (SUBTASK_1::checkSubtask())
      return SUBTASK_1::Solve(), 0;
    SUBTASK_2345::Solve();
};

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen("MAIN.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |         freopen("MAIN.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...