#include <iostream> using namespace std; const int MAX_N = 10005; int n, k, t; int officeCount, remoteCount, allCount, maxx = -1; int officePref[MAX_N], remotePref[MAX_N], duties[MAX_N]; string day; int calcMissedInTraffic(int officeStart, int officeEnd, int* prefSum) { int missedInGo = prefSum[officeStart - 1]; if (officeStart - t - 1 >= 0) { missedInGo -= prefSum[officeStart - t - 1]; } int missedInReturn = prefSum[officeEnd + t] - prefSum[officeEnd]; return missedInGo + missedInReturn; } int calcMissedRemoteInTraffic(int officeStart, int officeEnd) { return calcMissedInTraffic(officeStart, officeEnd, remotePref); } int calcMissedOfficeInTraffic(int officeStart, int officeEnd) { return calcMissedInTraffic(officeStart, officeEnd, officePref); } int calcMissedInTraffic(int officeStart, int officeEnd) { return calcMissedOfficeInTraffic(officeStart, officeEnd) + calcMissedRemoteInTraffic(officeStart, officeEnd); } int calcDutiesAtHome(int officeStart, int officeEnd, int* prefSum) { int res = 0; int homeEnd = officeStart - t - 1; if (homeEnd >= 0) { res += prefSum[homeEnd]; } int homeStart = officeEnd + t + 1; if (homeStart < n) { res += prefSum[n - 1] - prefSum[homeStart - 1]; } return res; } int calcMissedOfficeAtHome(int officeStart, int officeEnd) { return calcDutiesAtHome(officeStart, officeEnd, officePref); } int compute(int officeStart, int officeEnd) { int missedInTraffic = calcMissedInTraffic(officeStart, officeEnd); int missedOfficeAtHome = calcMissedOfficeAtHome(officeStart, officeEnd); int alreadyMissed = missedInTraffic + missedOfficeAtHome; if (alreadyMissed > k) { return -1; } int timeAtHome = n - 2 * t - (officeEnd - officeStart + 1); int remoteAtHome = calcDutiesAtHome(officeStart, officeEnd, remotePref); int toMiss = k - alreadyMissed; remoteAtHome = max(0, remoteAtHome - toMiss); return timeAtHome - remoteAtHome; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> k >> t; cin >> day; for (int i = 0; i < day.size(); ++i) { duties[i] = day[i] - '0'; if (duties[i] == 1) { ++officePref[i]; ++officeCount; } else if (duties[i] == 2) { ++remotePref[i]; ++remoteCount; } if (i > 0) { officePref[i] += officePref[i - 1]; remotePref[i] += remotePref[i - 1]; } } allCount = officeCount + remoteCount; if (k >= officeCount) { int mustBeOn = max(0, allCount - k); cout << n - mustBeOn << endl; return 0; } for (int i = t; i < n - t; ++i) { for (int j = i; j < n - t; ++j) { int curr = compute(i, j); if (curr > maxx) { maxx = curr; } } } cout << maxx << endl; 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 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 | #include <iostream> using namespace std; const int MAX_N = 10005; int n, k, t; int officeCount, remoteCount, allCount, maxx = -1; int officePref[MAX_N], remotePref[MAX_N], duties[MAX_N]; string day; int calcMissedInTraffic(int officeStart, int officeEnd, int* prefSum) { int missedInGo = prefSum[officeStart - 1]; if (officeStart - t - 1 >= 0) { missedInGo -= prefSum[officeStart - t - 1]; } int missedInReturn = prefSum[officeEnd + t] - prefSum[officeEnd]; return missedInGo + missedInReturn; } int calcMissedRemoteInTraffic(int officeStart, int officeEnd) { return calcMissedInTraffic(officeStart, officeEnd, remotePref); } int calcMissedOfficeInTraffic(int officeStart, int officeEnd) { return calcMissedInTraffic(officeStart, officeEnd, officePref); } int calcMissedInTraffic(int officeStart, int officeEnd) { return calcMissedOfficeInTraffic(officeStart, officeEnd) + calcMissedRemoteInTraffic(officeStart, officeEnd); } int calcDutiesAtHome(int officeStart, int officeEnd, int* prefSum) { int res = 0; int homeEnd = officeStart - t - 1; if (homeEnd >= 0) { res += prefSum[homeEnd]; } int homeStart = officeEnd + t + 1; if (homeStart < n) { res += prefSum[n - 1] - prefSum[homeStart - 1]; } return res; } int calcMissedOfficeAtHome(int officeStart, int officeEnd) { return calcDutiesAtHome(officeStart, officeEnd, officePref); } int compute(int officeStart, int officeEnd) { int missedInTraffic = calcMissedInTraffic(officeStart, officeEnd); int missedOfficeAtHome = calcMissedOfficeAtHome(officeStart, officeEnd); int alreadyMissed = missedInTraffic + missedOfficeAtHome; if (alreadyMissed > k) { return -1; } int timeAtHome = n - 2 * t - (officeEnd - officeStart + 1); int remoteAtHome = calcDutiesAtHome(officeStart, officeEnd, remotePref); int toMiss = k - alreadyMissed; remoteAtHome = max(0, remoteAtHome - toMiss); return timeAtHome - remoteAtHome; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> k >> t; cin >> day; for (int i = 0; i < day.size(); ++i) { duties[i] = day[i] - '0'; if (duties[i] == 1) { ++officePref[i]; ++officeCount; } else if (duties[i] == 2) { ++remotePref[i]; ++remoteCount; } if (i > 0) { officePref[i] += officePref[i - 1]; remotePref[i] += remotePref[i - 1]; } } allCount = officeCount + remoteCount; if (k >= officeCount) { int mustBeOn = max(0, allCount - k); cout << n - mustBeOn << endl; return 0; } for (int i = t; i < n - t; ++i) { for (int j = i; j < n - t; ++j) { int curr = compute(i, j); if (curr > maxx) { maxx = curr; } } } cout << maxx << endl; return 0; } |