// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<"\n";}(X)
#else
#define debug(...) {}
#endif
// clang-format on
int
main()
{
cin.tie(0)->sync_with_stdio(0);
int N, K, T;
string seq;
cin >> N >> K >> T >> seq;
// Initial dp values
const short NINF = numeric_limits<short>::min();
vector<vector<vector<short>>> dp(
N + 1, vector<vector<short>>(2, vector<short>(K + 1, NINF)));
dp[0][0][0] = 0;
// Calculate dp
int drive_penalty = 0;
FOR (n, 1, N) {
const auto& ch = seq[n - 1];
bool some_meeting = ch != '3';
// Update penalty for finishing a drive at n
drive_penalty += some_meeting;
if (n - T >= 1) drive_penalty -= seq[n - T - 1] != '3';
// Logging
auto region = seq.substr(max(0, n - T), min(T, n));
debug(n, region.c_str(), drive_penalty);
// Maybe use car
if (n - T >= 0) {
FOR (k, drive_penalty, K) {
dp[n][0][k] = dp[n - T][1][k - drive_penalty];
dp[n][1][k] = dp[n - T][0][k - drive_penalty];
}
}
switch (seq[n - 1]) {
case '1': // meeting at work
// Politely work at work
FOR (k, 0, K) dp[n][1][k] = max(dp[n][1][k], dp[n - 1][1][k]);
// Ignore the meeting and do potyczki at home
FOR (k, 1, K)
dp[n][0][k]
= max<short>(dp[n][0][k], dp[n - 1][0][k - 1] + 1);
break;
case '2':
// Politely work at work or home
FOR (k, 0, K) dp[n][0][k] = max(dp[n][0][k], dp[n - 1][0][k]);
FOR (k, 0, K) dp[n][1][k] = max(dp[n][1][k], dp[n - 1][1][k]);
// Ignore the meeting and do potyczki at home
FOR (k, 1, K)
dp[n][0][k]
= max<short>(dp[n][0][k], dp[n - 1][0][k - 1] + 1);
break;
default:
// Do potyczki at home in free time
FOR (k, 0, K) dp[n][0][k] = max<short>(dp[n][0][k], dp[n - 1][0][k] + 1);
// Politely work at work
FOR (k, 0, K) dp[n][1][k] = max(dp[n][1][k], dp[n - 1][1][k]);
break;
}
debug(n, dp[n]);
}
auto score = max<short>(-1, *max_element(dp.back()[0].begin(), dp.back()[0].end()));
cout << score << "\n";
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | // clang-format off #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(auto i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";} #ifdef DEBUG #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<"\n";}(X) #else #define debug(...) {} #endif // clang-format on int main() { cin.tie(0)->sync_with_stdio(0); int N, K, T; string seq; cin >> N >> K >> T >> seq; // Initial dp values const short NINF = numeric_limits<short>::min(); vector<vector<vector<short>>> dp( N + 1, vector<vector<short>>(2, vector<short>(K + 1, NINF))); dp[0][0][0] = 0; // Calculate dp int drive_penalty = 0; FOR (n, 1, N) { const auto& ch = seq[n - 1]; bool some_meeting = ch != '3'; // Update penalty for finishing a drive at n drive_penalty += some_meeting; if (n - T >= 1) drive_penalty -= seq[n - T - 1] != '3'; // Logging auto region = seq.substr(max(0, n - T), min(T, n)); debug(n, region.c_str(), drive_penalty); // Maybe use car if (n - T >= 0) { FOR (k, drive_penalty, K) { dp[n][0][k] = dp[n - T][1][k - drive_penalty]; dp[n][1][k] = dp[n - T][0][k - drive_penalty]; } } switch (seq[n - 1]) { case '1': // meeting at work // Politely work at work FOR (k, 0, K) dp[n][1][k] = max(dp[n][1][k], dp[n - 1][1][k]); // Ignore the meeting and do potyczki at home FOR (k, 1, K) dp[n][0][k] = max<short>(dp[n][0][k], dp[n - 1][0][k - 1] + 1); break; case '2': // Politely work at work or home FOR (k, 0, K) dp[n][0][k] = max(dp[n][0][k], dp[n - 1][0][k]); FOR (k, 0, K) dp[n][1][k] = max(dp[n][1][k], dp[n - 1][1][k]); // Ignore the meeting and do potyczki at home FOR (k, 1, K) dp[n][0][k] = max<short>(dp[n][0][k], dp[n - 1][0][k - 1] + 1); break; default: // Do potyczki at home in free time FOR (k, 0, K) dp[n][0][k] = max<short>(dp[n][0][k], dp[n - 1][0][k] + 1); // Politely work at work FOR (k, 0, K) dp[n][1][k] = max(dp[n][1][k], dp[n - 1][1][k]); break; } debug(n, dp[n]); } auto score = max<short>(-1, *max_element(dp.back()[0].begin(), dp.back()[0].end())); cout << score << "\n"; return 0; } |
English