#include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <set> using lli = long long int; int main() { int n; lli c; scanf("%d %lld\n", &n, &c); std::vector<lli> best_per_end_pattern; best_per_end_pattern.resize(500 * 1000, 0); lli best_overall = 0; // Size of the previously/currently processed block lli current_size = 0; // Contains the pattern IDs for each block seen so far that has the same // size as the current block std::set<int> current_group; auto flush_group = [&] () { const lli best = best_overall; for (int w : current_group) { best_per_end_pattern[w] = std::max( best_per_end_pattern[w] + current_size, best + current_size - c); best_overall = std::max(best_overall, best_per_end_pattern[w]); } current_group.clear(); }; for (int i = 0; i < n; i++) { lli a; int w; scanf("%lld %d\n", &a, &w); w--; // We use zero-based indexing if (current_size != a) { flush_group(); current_size = a; } current_group.insert(w); } flush_group(); printf("%lld\n", best_overall); 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 | #include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <set> using lli = long long int; int main() { int n; lli c; scanf("%d %lld\n", &n, &c); std::vector<lli> best_per_end_pattern; best_per_end_pattern.resize(500 * 1000, 0); lli best_overall = 0; // Size of the previously/currently processed block lli current_size = 0; // Contains the pattern IDs for each block seen so far that has the same // size as the current block std::set<int> current_group; auto flush_group = [&] () { const lli best = best_overall; for (int w : current_group) { best_per_end_pattern[w] = std::max( best_per_end_pattern[w] + current_size, best + current_size - c); best_overall = std::max(best_overall, best_per_end_pattern[w]); } current_group.clear(); }; for (int i = 0; i < n; i++) { lli a; int w; scanf("%lld %d\n", &a, &w); w--; // We use zero-based indexing if (current_size != a) { flush_group(); current_size = a; } current_group.insert(w); } flush_group(); printf("%lld\n", best_overall); return 0; } |