#include <iostream> #include <string> #include <algorithm> using namespace std; static const int16_t UNK = -2; static int n, k, t; static string s; static int aMeetingAggregatedCnt[8002]; static int16_t aDP[3][8192][8192]; static int RecursiveAtHome(int iLevel, int iHowManyICanSkip, bool fWasInTheOffice); static int RecursiveInTheOffice(int iLevel, int iHowManyICanSkip); static void InitDP() { for (int i = 0; i < 3; ++i) { for (int j = 0; j < n; ++j) { for (int m = 0; m <= k; ++m) { aDP[i][j][m] = UNK; } } } } static void InitMeetingAggregatedCnt() { for (int i = 0; i < n; ++i) { if (i == 0) { aMeetingAggregatedCnt[i] = 0; } else { aMeetingAggregatedCnt[i] = aMeetingAggregatedCnt[i - 1]; } if (s[i] != '3') { aMeetingAggregatedCnt[i]++; } } } static inline int GetMeetingsDuringCommute(int iLevel) { if (iLevel - t < 0) { return aMeetingAggregatedCnt[iLevel]; } return aMeetingAggregatedCnt[iLevel] - aMeetingAggregatedCnt[iLevel - t]; } static inline void AppendResult(int16_t & iRet, int iResult) { if (iResult > iRet) { iRet = iResult; } } static inline void AppendResultIncremented(int16_t & iRet, int iResult) { if (iResult == -1) return; iResult++; if (iResult > iRet) { iRet = iResult; } } static int RecursiveAtHome(int iLevel, int iHowManyICanSkip, bool fWasInTheOffice) { if (iLevel == -1) // fajrant { return 0; } int i = fWasInTheOffice ? 1 : 0; int16_t & iResult = aDP[i][iLevel][iHowManyICanSkip]; if (iResult != UNK) return iResult; int iRemainingSlots = iLevel + 1; if (iHowManyICanSkip >= iRemainingSlots) { return iResult = iRemainingSlots; } int16_t iRet = -1; char c = s[iLevel]; if (c == '1' && iHowManyICanSkip > 0) // office meeting { AppendResultIncremented(iRet, RecursiveAtHome(iLevel - 1, iHowManyICanSkip - 1, fWasInTheOffice)); // skip office meeting and solve } if (c == '2') // remote meeting { AppendResult(iRet, RecursiveAtHome(iLevel - 1, iHowManyICanSkip, fWasInTheOffice)); // take part in remote meeting if (iHowManyICanSkip > 0) AppendResultIncremented(iRet, RecursiveAtHome(iLevel - 1, iHowManyICanSkip - 1, fWasInTheOffice)); // skip remote meeting and solve } if (c == '3') // no duties { AppendResultIncremented(iRet, RecursiveAtHome(iLevel - 1, iHowManyICanSkip, fWasInTheOffice)); // solve safely } if (!fWasInTheOffice && iRemainingSlots > t * 2) { int iMeetingsDuringCommute = GetMeetingsDuringCommute(iLevel); if (iHowManyICanSkip >= iMeetingsDuringCommute) { AppendResult(iRet, RecursiveInTheOffice(iLevel - t, iHowManyICanSkip - iMeetingsDuringCommute)); // go to the office } } return iResult = iRet; } static int RecursiveInTheOffice(int iLevel, int iHowManyICanSkip) { int16_t & iResult = aDP[2][iLevel][iHowManyICanSkip]; if (iResult != UNK) return iResult; int iRemainingSlots = iLevel + 1; if (iRemainingSlots < t) return iResult = -1; int16_t iRet = -1; int iMeetingsDuringCommute = GetMeetingsDuringCommute(iLevel); if (iHowManyICanSkip >= iMeetingsDuringCommute) { AppendResult(iRet, RecursiveAtHome(iLevel - t, iHowManyICanSkip - iMeetingsDuringCommute, true)); // go back home } if (iRemainingSlots == t) { return iResult = iRet; } char c = s[iLevel]; if (c == '1') // office meeting { AppendResult(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip)); // take part in office meeting if (iHowManyICanSkip > 0) AppendResultIncremented(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip - 1)); // skip office meeting and solve } if (c == '2') // remote meeting { AppendResult(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip)); // take part in remote meeting if (iHowManyICanSkip > 0) AppendResultIncremented(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip - 1)); // skip remote meeting and solve } if (c == '3') // no meetings { AppendResult(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip)); // do your job } return iResult = iRet; } int main() { cin >> n >> k >> t >> s; InitDP(); InitMeetingAggregatedCnt(); int iAnswer = RecursiveAtHome(n - 1, k, false); cout << iAnswer; }
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | #include <iostream> #include <string> #include <algorithm> using namespace std; static const int16_t UNK = -2; static int n, k, t; static string s; static int aMeetingAggregatedCnt[8002]; static int16_t aDP[3][8192][8192]; static int RecursiveAtHome(int iLevel, int iHowManyICanSkip, bool fWasInTheOffice); static int RecursiveInTheOffice(int iLevel, int iHowManyICanSkip); static void InitDP() { for (int i = 0; i < 3; ++i) { for (int j = 0; j < n; ++j) { for (int m = 0; m <= k; ++m) { aDP[i][j][m] = UNK; } } } } static void InitMeetingAggregatedCnt() { for (int i = 0; i < n; ++i) { if (i == 0) { aMeetingAggregatedCnt[i] = 0; } else { aMeetingAggregatedCnt[i] = aMeetingAggregatedCnt[i - 1]; } if (s[i] != '3') { aMeetingAggregatedCnt[i]++; } } } static inline int GetMeetingsDuringCommute(int iLevel) { if (iLevel - t < 0) { return aMeetingAggregatedCnt[iLevel]; } return aMeetingAggregatedCnt[iLevel] - aMeetingAggregatedCnt[iLevel - t]; } static inline void AppendResult(int16_t & iRet, int iResult) { if (iResult > iRet) { iRet = iResult; } } static inline void AppendResultIncremented(int16_t & iRet, int iResult) { if (iResult == -1) return; iResult++; if (iResult > iRet) { iRet = iResult; } } static int RecursiveAtHome(int iLevel, int iHowManyICanSkip, bool fWasInTheOffice) { if (iLevel == -1) // fajrant { return 0; } int i = fWasInTheOffice ? 1 : 0; int16_t & iResult = aDP[i][iLevel][iHowManyICanSkip]; if (iResult != UNK) return iResult; int iRemainingSlots = iLevel + 1; if (iHowManyICanSkip >= iRemainingSlots) { return iResult = iRemainingSlots; } int16_t iRet = -1; char c = s[iLevel]; if (c == '1' && iHowManyICanSkip > 0) // office meeting { AppendResultIncremented(iRet, RecursiveAtHome(iLevel - 1, iHowManyICanSkip - 1, fWasInTheOffice)); // skip office meeting and solve } if (c == '2') // remote meeting { AppendResult(iRet, RecursiveAtHome(iLevel - 1, iHowManyICanSkip, fWasInTheOffice)); // take part in remote meeting if (iHowManyICanSkip > 0) AppendResultIncremented(iRet, RecursiveAtHome(iLevel - 1, iHowManyICanSkip - 1, fWasInTheOffice)); // skip remote meeting and solve } if (c == '3') // no duties { AppendResultIncremented(iRet, RecursiveAtHome(iLevel - 1, iHowManyICanSkip, fWasInTheOffice)); // solve safely } if (!fWasInTheOffice && iRemainingSlots > t * 2) { int iMeetingsDuringCommute = GetMeetingsDuringCommute(iLevel); if (iHowManyICanSkip >= iMeetingsDuringCommute) { AppendResult(iRet, RecursiveInTheOffice(iLevel - t, iHowManyICanSkip - iMeetingsDuringCommute)); // go to the office } } return iResult = iRet; } static int RecursiveInTheOffice(int iLevel, int iHowManyICanSkip) { int16_t & iResult = aDP[2][iLevel][iHowManyICanSkip]; if (iResult != UNK) return iResult; int iRemainingSlots = iLevel + 1; if (iRemainingSlots < t) return iResult = -1; int16_t iRet = -1; int iMeetingsDuringCommute = GetMeetingsDuringCommute(iLevel); if (iHowManyICanSkip >= iMeetingsDuringCommute) { AppendResult(iRet, RecursiveAtHome(iLevel - t, iHowManyICanSkip - iMeetingsDuringCommute, true)); // go back home } if (iRemainingSlots == t) { return iResult = iRet; } char c = s[iLevel]; if (c == '1') // office meeting { AppendResult(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip)); // take part in office meeting if (iHowManyICanSkip > 0) AppendResultIncremented(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip - 1)); // skip office meeting and solve } if (c == '2') // remote meeting { AppendResult(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip)); // take part in remote meeting if (iHowManyICanSkip > 0) AppendResultIncremented(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip - 1)); // skip remote meeting and solve } if (c == '3') // no meetings { AppendResult(iRet, RecursiveInTheOffice(iLevel - 1, iHowManyICanSkip)); // do your job } return iResult = iRet; } int main() { cin >> n >> k >> t >> s; InitDP(); InitMeetingAggregatedCnt(); int iAnswer = RecursiveAtHome(n - 1, k, false); cout << iAnswer; } |