#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; struct Styl { vector<int> a_list; vector<long long> prefix_max; void add(int a, long long wynik) { a_list.push_back(a); if (prefix_max.empty()) { prefix_max.push_back(wynik); } else { prefix_max.push_back(max(prefix_max.back(), wynik)); } } long long query(int threshold) { int lewo = 0, prawo = a_list.size() - 1; int pos = a_list.size(); while (lewo <= prawo) { int mid = (lewo + prawo) / 2; if (a_list[mid] <= threshold) { pos = mid; prawo = mid - 1; } else { lewo = mid + 1; } } if (pos == 0) return 0; return prefix_max[pos - 1]; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, c; cin >> n >> c; vector<pair<int, int>> klocki(n); for (int i = 0; i < n; ++i) { cin >> klocki[i].first >> klocki[i].second; } unordered_map<int, Styl> styl_mapa; Styl global_list; long long max_wynik = 0; for (int i = n - 1; i >= 0; --i) { int a = klocki[i].first; int w = klocki[i].second; Styl &styl_lista = styl_mapa[w]; long long best_same = styl_lista.query(a); long long naj_glob = global_list.query(a); long long naj_inne = naj_glob > 0 ? naj_glob - c : 0; long long current = a + max(best_same, naj_inne); max_wynik = max(max_wynik, current); styl_lista.add(a, current); global_list.add(a, current); } cout << max_wynik << "\n"; //:p return 0; }
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 | #include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; struct Styl { vector<int> a_list; vector<long long> prefix_max; void add(int a, long long wynik) { a_list.push_back(a); if (prefix_max.empty()) { prefix_max.push_back(wynik); } else { prefix_max.push_back(max(prefix_max.back(), wynik)); } } long long query(int threshold) { int lewo = 0, prawo = a_list.size() - 1; int pos = a_list.size(); while (lewo <= prawo) { int mid = (lewo + prawo) / 2; if (a_list[mid] <= threshold) { pos = mid; prawo = mid - 1; } else { lewo = mid + 1; } } if (pos == 0) return 0; return prefix_max[pos - 1]; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, c; cin >> n >> c; vector<pair<int, int>> klocki(n); for (int i = 0; i < n; ++i) { cin >> klocki[i].first >> klocki[i].second; } unordered_map<int, Styl> styl_mapa; Styl global_list; long long max_wynik = 0; for (int i = n - 1; i >= 0; --i) { int a = klocki[i].first; int w = klocki[i].second; Styl &styl_lista = styl_mapa[w]; long long best_same = styl_lista.query(a); long long naj_glob = global_list.query(a); long long naj_inne = naj_glob > 0 ? naj_glob - c : 0; long long current = a + max(best_same, naj_inne); max_wynik = max(max_wynik, current); styl_lista.add(a, current); global_list.add(a, current); } cout << max_wynik << "\n"; //:p return 0; } |