#include <iostream> #include <climits> #define OFFICE 1 #define REMOTE 2 #define FREE 3 using namespace std; int countActivity(int* day, int n, int activity); void runAlgorithm(int* day, int n, int leaves, int t); void initNegative(int* arr, int n); int initMissed(int* day, int start, int n, int activity); void printTab(string name, int* tab, int n); pair<int, int> findShortestRange(int* a,int* b,int n, int leaves, int t); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, leaves, t; cin >> n >> leaves >> t; int* day = new int[n]; string in; cin >> in; for (int i = 0; i < n; i++) { day[i] = in[i] - '0'; } int offices = countActivity(day, n, OFFICE); int meetings = offices + countActivity(day, n, REMOTE); if (meetings <= leaves) { cout << n; delete[] day; return 0; } else if (offices <= leaves) { int hours = n - min(meetings, leaves); cout << hours; delete[] day; return 0; } else { runAlgorithm(day, n, leaves, t); delete[] day; } } int countActivity(int* day, int n, int activity) { int count = 0; for (int i = 0; i < n; i++) { if (day[i] == activity) count++; } return count; } void initNegative(int* arr, int n) { for (int i = 0; i < n; i++) { arr[i] = INT_MAX; } } int initMissed(int* day, int start, int n, int activity) { return countActivity(day + start, n, activity); } void printTab(string name, int* tab, int n) { cout << "\n " << name << "\n"; for (int i = 0; i < n; i++) cout << tab[i] << " "; cout << "\n"; } void runAlgorithm(int* day, int n, int leaves, int t) { int* a = new int[n]; int* b = new int[n]; initNegative(a, n); initNegative(b, n); // count missed meetings (office + possibly remotely) int missedOffice = initMissed(day, 0, t, OFFICE); int missedRemote = initMissed(day, 0, t, REMOTE); a[0] = missedOffice + missedRemote; for (int i = 1; i < n - 2 * t; i++) { if (day[i - 1] == REMOTE) missedRemote--; if (day[i + t - 1] == REMOTE) missedRemote++; if (day[i + t - 1] == OFFICE) missedOffice++; a[i] = missedOffice + missedRemote; } // count missed meetings (office + possibly remotely) missedOffice = initMissed(day, n-t, t, OFFICE); missedRemote = initMissed(day, n-t, t, REMOTE); b[n - t] = missedOffice + missedRemote; for (int i = n - t - 1; i > t; i--) { if (day[i + t] == REMOTE) missedRemote--; if (day[i] == REMOTE) missedRemote++; if (day[i] == OFFICE) missedOffice++; b[i] = missedOffice + missedRemote; } pair<int, int> startEnd = findShortestRange(a, b, n, leaves, t); if (startEnd.first == -1'000'000) { cout << -1; delete[] a; delete[] b; return; } else { int missed = a[startEnd.first] + b[startEnd.second]; int remoteBefore = countActivity(day, startEnd.first, REMOTE); int remoteAfter = countActivity(day + startEnd.second + t, n - startEnd.second - t, REMOTE); int remoteOutside = remoteBefore + remoteAfter; int missedLeft = leaves - missed; int outdoor = (startEnd.second - startEnd.first + t); int worked = n - outdoor - remoteOutside + missedLeft; cout << worked; } delete[] a; delete[] b; } pair<int, int> findShortestRange(int* a, int* b, int n, int leaves, int t) { int start = -1'000'000; int end = 0; for (int i = 0; i < n; i++) { if (start != -1'000'000 && a[i-1] < a[i]) continue; for (int j = i + t + 1; j <= n - t; j++) { int cost = a[i] + b[j]; if (cost <= leaves && j - i <= end - start) { start = i; end = j; break; } } } return { start, end }; }
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 | #include <iostream> #include <climits> #define OFFICE 1 #define REMOTE 2 #define FREE 3 using namespace std; int countActivity(int* day, int n, int activity); void runAlgorithm(int* day, int n, int leaves, int t); void initNegative(int* arr, int n); int initMissed(int* day, int start, int n, int activity); void printTab(string name, int* tab, int n); pair<int, int> findShortestRange(int* a,int* b,int n, int leaves, int t); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, leaves, t; cin >> n >> leaves >> t; int* day = new int[n]; string in; cin >> in; for (int i = 0; i < n; i++) { day[i] = in[i] - '0'; } int offices = countActivity(day, n, OFFICE); int meetings = offices + countActivity(day, n, REMOTE); if (meetings <= leaves) { cout << n; delete[] day; return 0; } else if (offices <= leaves) { int hours = n - min(meetings, leaves); cout << hours; delete[] day; return 0; } else { runAlgorithm(day, n, leaves, t); delete[] day; } } int countActivity(int* day, int n, int activity) { int count = 0; for (int i = 0; i < n; i++) { if (day[i] == activity) count++; } return count; } void initNegative(int* arr, int n) { for (int i = 0; i < n; i++) { arr[i] = INT_MAX; } } int initMissed(int* day, int start, int n, int activity) { return countActivity(day + start, n, activity); } void printTab(string name, int* tab, int n) { cout << "\n " << name << "\n"; for (int i = 0; i < n; i++) cout << tab[i] << " "; cout << "\n"; } void runAlgorithm(int* day, int n, int leaves, int t) { int* a = new int[n]; int* b = new int[n]; initNegative(a, n); initNegative(b, n); // count missed meetings (office + possibly remotely) int missedOffice = initMissed(day, 0, t, OFFICE); int missedRemote = initMissed(day, 0, t, REMOTE); a[0] = missedOffice + missedRemote; for (int i = 1; i < n - 2 * t; i++) { if (day[i - 1] == REMOTE) missedRemote--; if (day[i + t - 1] == REMOTE) missedRemote++; if (day[i + t - 1] == OFFICE) missedOffice++; a[i] = missedOffice + missedRemote; } // count missed meetings (office + possibly remotely) missedOffice = initMissed(day, n-t, t, OFFICE); missedRemote = initMissed(day, n-t, t, REMOTE); b[n - t] = missedOffice + missedRemote; for (int i = n - t - 1; i > t; i--) { if (day[i + t] == REMOTE) missedRemote--; if (day[i] == REMOTE) missedRemote++; if (day[i] == OFFICE) missedOffice++; b[i] = missedOffice + missedRemote; } pair<int, int> startEnd = findShortestRange(a, b, n, leaves, t); if (startEnd.first == -1'000'000) { cout << -1; delete[] a; delete[] b; return; } else { int missed = a[startEnd.first] + b[startEnd.second]; int remoteBefore = countActivity(day, startEnd.first, REMOTE); int remoteAfter = countActivity(day + startEnd.second + t, n - startEnd.second - t, REMOTE); int remoteOutside = remoteBefore + remoteAfter; int missedLeft = leaves - missed; int outdoor = (startEnd.second - startEnd.first + t); int worked = n - outdoor - remoteOutside + missedLeft; cout << worked; } delete[] a; delete[] b; } pair<int, int> findShortestRange(int* a, int* b, int n, int leaves, int t) { int start = -1'000'000; int end = 0; for (int i = 0; i < n; i++) { if (start != -1'000'000 && a[i-1] < a[i]) continue; for (int j = i + t + 1; j <= n - t; j++) { int cost = a[i] + b[j]; if (cost <= leaves && j - i <= end - start) { start = i; end = j; break; } } } return { start, end }; } |