#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef unsigned int uint; const int INF = 1e9 + 9; struct Partial { int lost = 0, tentative = 0, free = 0; }; ostream &operator<<(ostream &os, const Partial &p) { os << "Partial(Lost: " << p.lost << ", Tentative: " << p.tentative << ", Free: " << p.free << ")"; return os; } string s; vi prefix_sum_office = {0}; vi prefix_sum_remote = {0}; vi prefix_sum_free = {0}; void compute_prefix_sums(int n) { REP(i, n) { if (s[i] == '1') { prefix_sum_office.PB(prefix_sum_office.back() + 1); prefix_sum_remote.PB(prefix_sum_remote.back()); prefix_sum_free.PB(prefix_sum_free.back()); } else if (s[i] == '2') { prefix_sum_office.PB(prefix_sum_office.back()); prefix_sum_remote.PB(prefix_sum_remote.back() + 1); prefix_sum_free.PB(prefix_sum_free.back()); } else if (s[i] == '3') { prefix_sum_office.PB(prefix_sum_office.back()); prefix_sum_remote.PB(prefix_sum_remote.back()); prefix_sum_free.PB(prefix_sum_free.back() + 1); } else { throw "Unsupported char"; } } } Partial analyze_home(int start, int end) { Partial partial; partial.lost = prefix_sum_office[end] - prefix_sum_office[start]; partial.tentative = prefix_sum_remote[end] - prefix_sum_remote[start]; partial.free = (prefix_sum_office[end] - prefix_sum_office[start]) + (prefix_sum_free[end] - prefix_sum_free[start]); return partial; } Partial analyze_office(int start, int end, int t) { Partial partial; partial.lost = (prefix_sum_office[start + t] - prefix_sum_office[start]) + (prefix_sum_remote[start + t] - prefix_sum_remote[start]) + (prefix_sum_office[end] - prefix_sum_office[end - t]) + (prefix_sum_remote[end] - prefix_sum_remote[end - t]); return partial; } int solve(int n, int k, int t, int start, int end) { int result = -1; Partial left = analyze_home(0, start); Partial right = analyze_home(end, n); Partial office = analyze_office(start, end, t); int sum_lost = left.lost + right.lost + office.lost; if (sum_lost > k) { return result; } int lost_left = k - sum_lost; int sum_free = left.free + right.free + office.free; int extra = min(lost_left, left.tentative + right.tentative); return sum_free + extra; } void inline one() { int n, k, t; cin >> n >> k >> t; cin >> s; compute_prefix_sums(n); int result = -1; Partial home_only = analyze_home(0, n); if (home_only.lost <= k) { int lost_left = k - home_only.lost; result = max(result, home_only.free + min(lost_left, home_only.tentative)); } DEB("home_only=" << home_only << "\n"); REP(start, n) { FOR(end, start + 2 * t, n) { int temp_result = solve(n, k, t, start, end); DEB("start=" << start << " end=" << end << " temp_result=" << temp_result << "\n"); result = max(result, temp_result); } } cout << result << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); }
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef unsigned int uint; const int INF = 1e9 + 9; struct Partial { int lost = 0, tentative = 0, free = 0; }; ostream &operator<<(ostream &os, const Partial &p) { os << "Partial(Lost: " << p.lost << ", Tentative: " << p.tentative << ", Free: " << p.free << ")"; return os; } string s; vi prefix_sum_office = {0}; vi prefix_sum_remote = {0}; vi prefix_sum_free = {0}; void compute_prefix_sums(int n) { REP(i, n) { if (s[i] == '1') { prefix_sum_office.PB(prefix_sum_office.back() + 1); prefix_sum_remote.PB(prefix_sum_remote.back()); prefix_sum_free.PB(prefix_sum_free.back()); } else if (s[i] == '2') { prefix_sum_office.PB(prefix_sum_office.back()); prefix_sum_remote.PB(prefix_sum_remote.back() + 1); prefix_sum_free.PB(prefix_sum_free.back()); } else if (s[i] == '3') { prefix_sum_office.PB(prefix_sum_office.back()); prefix_sum_remote.PB(prefix_sum_remote.back()); prefix_sum_free.PB(prefix_sum_free.back() + 1); } else { throw "Unsupported char"; } } } Partial analyze_home(int start, int end) { Partial partial; partial.lost = prefix_sum_office[end] - prefix_sum_office[start]; partial.tentative = prefix_sum_remote[end] - prefix_sum_remote[start]; partial.free = (prefix_sum_office[end] - prefix_sum_office[start]) + (prefix_sum_free[end] - prefix_sum_free[start]); return partial; } Partial analyze_office(int start, int end, int t) { Partial partial; partial.lost = (prefix_sum_office[start + t] - prefix_sum_office[start]) + (prefix_sum_remote[start + t] - prefix_sum_remote[start]) + (prefix_sum_office[end] - prefix_sum_office[end - t]) + (prefix_sum_remote[end] - prefix_sum_remote[end - t]); return partial; } int solve(int n, int k, int t, int start, int end) { int result = -1; Partial left = analyze_home(0, start); Partial right = analyze_home(end, n); Partial office = analyze_office(start, end, t); int sum_lost = left.lost + right.lost + office.lost; if (sum_lost > k) { return result; } int lost_left = k - sum_lost; int sum_free = left.free + right.free + office.free; int extra = min(lost_left, left.tentative + right.tentative); return sum_free + extra; } void inline one() { int n, k, t; cin >> n >> k >> t; cin >> s; compute_prefix_sums(n); int result = -1; Partial home_only = analyze_home(0, n); if (home_only.lost <= k) { int lost_left = k - home_only.lost; result = max(result, home_only.free + min(lost_left, home_only.tentative)); } DEB("home_only=" << home_only << "\n"); REP(start, n) { FOR(end, start + 2 * t, n) { int temp_result = solve(n, k, t, start, end); DEB("start=" << start << " end=" << end << " temp_result=" << temp_result << "\n"); result = max(result, temp_result); } } cout << result << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |