1197E - Culture Code - CodeForces Solution


binary search combinatorics data structures dp shortest paths sortings *2300

Please click on ads to support us..

C++ Code:

/*
Problem: 1197E
Date: 29-02-2024 01:07 PM
*/


#include <bits/stdc++.h>

#define ll long long
#define P 1000000007
using namespace std;

const int MAX_N = 2e5 + 5;
int n;
ll out, in;
vector<pair<ll, ll> > dolloi;
vector<tuple<ll, ll, int> > dollio;
int J[MAX_N];
ll dp[MAX_N];
ll num[MAX_N];

int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> out >> in;
        dolloi.push_back({out, in});
    }
    sort(dolloi.begin(), dolloi.end());
    for(int i = 0; i < n; i++) {
        dollio.push_back({dolloi[i].second, dolloi[i].first, i});
    }
    sort(dollio.begin(), dollio.end());

    for(int i = 0; i < n; i++) {
        J[i] = n;
    }
    int i = 0, j = 0;
    while(i < n && j < n) {
        ll in = get<0>(dollio[i]);
        int idx = get<2>(dollio[i]);
        ll out = dolloi[j].first;
        if(out > in) {
            J[idx] = j;
            i++;
        }else {
            j++;
        }
    }

    dp[0] = 0;
    num[0] = 1;
    for(int i = 0; i < n; i++) {
        ll val1 = dp[i];
        ll val2 = dolloi[i].first - dolloi[i].second + dp[J[i]];
        if(val1 < val2) {
            num[i + 1] = num[J[i]];
        }else if(val1 > val2) {
            num[i + 1] = num[i];
        }else {
            num[i + 1] = (num[i] + num[J[i]]) % P;
        }
        dp[i + 1] = max(val1, val2);
    }

    ll M = INT_MAX;
    ll tot = 0;
    ll maxsize = get<0>(dollio[n - 1]);
    for(int i = 0; i < n; i++) {
        if(dolloi[i].first > maxsize) {
            ll val = dolloi[i].second - dp[J[i]];
            M = min(M, val);
        }
    }

    for(int i = 0; i < n; i++) {
        ll val = dolloi[i].second - dp[J[i]];
        if(val == M && dolloi[i].first > maxsize) {
            tot += num[J[i]];
            tot %= P;
        }
    }
    cout << tot << endl;
}


Comments

Submit
0 Comments
More Questions

1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping
57C - Array
1713D - Tournament Countdown
33A - What is for dinner
810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement