#include <cstdlib> #include <bitset> #include <functional> #include <utility> #include <chrono> #include <tuple> #include <new> #include <memory> #include <climits> #include <cfloat> #include <cinttypes> #include <exception> #include <string> #include <array> #include <vector> #include <deque> #include <list> #include <forward_list> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <stack> #include <queue> #include <algorithm> #include <iterator> #include <cmath> #include <complex> #include <valarray> #include <ios> #include <istream> #include <ostream> #include <iostream> #include <fstream> #include <streambuf> #include <cstdio> #include <thread> // #include <bits/stdc++.h> using namespace std; int n; long long c; long long wysokosc, maks_wysokosc; int akt_kolor; vector<pair<int, long long>> kolory[500005]; long long maksymalne[500005], ogolny_maks = 0; long long wyn = 0, akt_wyn; int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> c; for (int i = 1; i <= n; i++) { cin >> wysokosc >> akt_kolor; kolory[wysokosc].push_back({akt_kolor, 0}); maks_wysokosc = max(maks_wysokosc, wysokosc); } for (long long wys = maks_wysokosc; wys >= 1; wys--) { for (int i = 0; i < kolory[wys].size(); i++) { tie(akt_kolor, ignore) = kolory[wys][i]; akt_wyn = max(ogolny_maks - c, maksymalne[akt_kolor]); akt_wyn += wys; wyn = max(wyn, akt_wyn); kolory[wys][i].second = akt_wyn; } for (int i = 0; i < kolory[wys].size(); i++) { tie(akt_kolor, akt_wyn) = kolory[wys][i]; maksymalne[akt_kolor] = max(maksymalne[akt_kolor], akt_wyn); ogolny_maks = max(ogolny_maks, akt_wyn); } } cout << wyn; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #include <cstdlib> #include <bitset> #include <functional> #include <utility> #include <chrono> #include <tuple> #include <new> #include <memory> #include <climits> #include <cfloat> #include <cinttypes> #include <exception> #include <string> #include <array> #include <vector> #include <deque> #include <list> #include <forward_list> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <stack> #include <queue> #include <algorithm> #include <iterator> #include <cmath> #include <complex> #include <valarray> #include <ios> #include <istream> #include <ostream> #include <iostream> #include <fstream> #include <streambuf> #include <cstdio> #include <thread> // #include <bits/stdc++.h> using namespace std; int n; long long c; long long wysokosc, maks_wysokosc; int akt_kolor; vector<pair<int, long long>> kolory[500005]; long long maksymalne[500005], ogolny_maks = 0; long long wyn = 0, akt_wyn; int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> c; for (int i = 1; i <= n; i++) { cin >> wysokosc >> akt_kolor; kolory[wysokosc].push_back({akt_kolor, 0}); maks_wysokosc = max(maks_wysokosc, wysokosc); } for (long long wys = maks_wysokosc; wys >= 1; wys--) { for (int i = 0; i < kolory[wys].size(); i++) { tie(akt_kolor, ignore) = kolory[wys][i]; akt_wyn = max(ogolny_maks - c, maksymalne[akt_kolor]); akt_wyn += wys; wyn = max(wyn, akt_wyn); kolory[wys][i].second = akt_wyn; } for (int i = 0; i < kolory[wys].size(); i++) { tie(akt_kolor, akt_wyn) = kolory[wys][i]; maksymalne[akt_kolor] = max(maksymalne[akt_kolor], akt_wyn); ogolny_maks = max(ogolny_maks, akt_wyn); } } cout << wyn; } |