#include <iostream>
#include <queue>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
struct State {
int time, skip, location, tasks;
bool wasInOffice;
State(int t, int s, int l, int ts, bool w) : time(t), skip(s), location(l), tasks(ts), wasInOffice(w) {}
State() : time(0), skip(0), location(0), tasks(0), wasInOffice(false) {}
bool operator==(const State &other) const {
return time == other.time && skip == other.skip && location == other.location &&
tasks == other.tasks && wasInOffice == other.wasInOffice;
}
bool operator!=(const State &other) const {
return !(*this == other);
}
};
struct StateHash {
size_t operator()(const State &s) const {
return hash<int>()(s.time) ^ hash<int>()(s.skip) ^ hash<int>()(s.location) ^
hash<int>()(s.tasks) ^ hash<bool>()(s.wasInOffice);
}
};
int countMissedMeetings(const string &s, int start, int duration) {
int missed = 0;
for (int i = start; i < min((int)s.size(), start + duration); i++) {
if (s[i] == '1' || s[i] == '2') missed++;
}
return missed;
}
int main() {
int n, k, t;
cin >> n >> k >> t;
string s;
cin >> s;
unordered_set<State, StateHash> visited;
queue<State> q;
State start(0, 0, 0, 0, false);
int missed = countMissedMeetings(s, 0, t);
State startInOffice(t, missed, 1, 0, true);
q.push(start);
q.push(startInOffice);
visited.insert(start);
visited.insert(startInOffice);
State best_state(0, 0, 0, -1, false);
while (!q.empty()) {
State state = q.front();
q.pop();
int curr_time = state.time, curr_skip = state.skip;
int curr_location = state.location, curr_tasks = state.tasks;
bool curr_wasInOffice = state.wasInOffice;
if (curr_time == n && curr_tasks > best_state.tasks && curr_skip <= k && curr_location == 0) {
best_state = state;
}
if (curr_time == n) continue;
char event = s[curr_time];
if (curr_location == 0) {
// Spotkanie online
State next(curr_time + 1, curr_skip, 0, curr_tasks, curr_wasInOffice);
if (event == '2' && visited.find(next) == visited.end()) {
visited.insert(next);
q.push(next);
}
// Zadanie
next = State(curr_time + 1, curr_skip, 0, curr_tasks + 1, curr_wasInOffice);
if (event == '3' && visited.find(next) == visited.end()) {
visited.insert(next);
q.push(next);
}
// Podroz
int missed = countMissedMeetings(s, curr_time, t);
if (curr_skip + missed <= k) {
next = State(curr_time + t, curr_skip + missed, 1, curr_tasks, true);
if (!curr_wasInOffice && visited.find(next) == visited.end()) {
visited.insert(next);
q.push(next);
}
}
// Pominięcie
next = State(curr_time + 1, curr_skip + 1, 0, curr_tasks + 1, curr_wasInOffice);
if ((event == '1' || event == '2') && curr_skip < k && visited.find(next) == visited.end()) {
visited.insert(next);
q.push(next);
}
} else {
// Spotkanie w biurze
State next(curr_time + 1, curr_skip, 1, curr_tasks, curr_wasInOffice);
if ((event == '1' || event == '2') && visited.find(next) == visited.end()) {
visited.insert(next);
q.push(next);
}
// Powrot do domu
int missed = countMissedMeetings(s, curr_time, t);
if (curr_skip + missed <= k) {
next = State(curr_time + t, curr_skip + missed, 0, curr_tasks, curr_wasInOffice);
if (visited.find(next) == visited.end() && curr_time + t <= n) {
visited.insert(next);
q.push(next);
}
}
// Pominiecie zadania
next = State(curr_time + 1, curr_skip, 1, curr_tasks, curr_wasInOffice);
if (event == '3' && visited.find(next) == visited.end()) {
visited.insert(next);
q.push(next);
}
}
}
if (best_state.tasks == -1) {
cout << -1 << endl;
} else {
cout << best_state.tasks << 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 | #include <iostream> #include <queue> #include <vector> #include <unordered_map> #include <unordered_set> using namespace std; struct State { int time, skip, location, tasks; bool wasInOffice; State(int t, int s, int l, int ts, bool w) : time(t), skip(s), location(l), tasks(ts), wasInOffice(w) {} State() : time(0), skip(0), location(0), tasks(0), wasInOffice(false) {} bool operator==(const State &other) const { return time == other.time && skip == other.skip && location == other.location && tasks == other.tasks && wasInOffice == other.wasInOffice; } bool operator!=(const State &other) const { return !(*this == other); } }; struct StateHash { size_t operator()(const State &s) const { return hash<int>()(s.time) ^ hash<int>()(s.skip) ^ hash<int>()(s.location) ^ hash<int>()(s.tasks) ^ hash<bool>()(s.wasInOffice); } }; int countMissedMeetings(const string &s, int start, int duration) { int missed = 0; for (int i = start; i < min((int)s.size(), start + duration); i++) { if (s[i] == '1' || s[i] == '2') missed++; } return missed; } int main() { int n, k, t; cin >> n >> k >> t; string s; cin >> s; unordered_set<State, StateHash> visited; queue<State> q; State start(0, 0, 0, 0, false); int missed = countMissedMeetings(s, 0, t); State startInOffice(t, missed, 1, 0, true); q.push(start); q.push(startInOffice); visited.insert(start); visited.insert(startInOffice); State best_state(0, 0, 0, -1, false); while (!q.empty()) { State state = q.front(); q.pop(); int curr_time = state.time, curr_skip = state.skip; int curr_location = state.location, curr_tasks = state.tasks; bool curr_wasInOffice = state.wasInOffice; if (curr_time == n && curr_tasks > best_state.tasks && curr_skip <= k && curr_location == 0) { best_state = state; } if (curr_time == n) continue; char event = s[curr_time]; if (curr_location == 0) { // Spotkanie online State next(curr_time + 1, curr_skip, 0, curr_tasks, curr_wasInOffice); if (event == '2' && visited.find(next) == visited.end()) { visited.insert(next); q.push(next); } // Zadanie next = State(curr_time + 1, curr_skip, 0, curr_tasks + 1, curr_wasInOffice); if (event == '3' && visited.find(next) == visited.end()) { visited.insert(next); q.push(next); } // Podroz int missed = countMissedMeetings(s, curr_time, t); if (curr_skip + missed <= k) { next = State(curr_time + t, curr_skip + missed, 1, curr_tasks, true); if (!curr_wasInOffice && visited.find(next) == visited.end()) { visited.insert(next); q.push(next); } } // Pominięcie next = State(curr_time + 1, curr_skip + 1, 0, curr_tasks + 1, curr_wasInOffice); if ((event == '1' || event == '2') && curr_skip < k && visited.find(next) == visited.end()) { visited.insert(next); q.push(next); } } else { // Spotkanie w biurze State next(curr_time + 1, curr_skip, 1, curr_tasks, curr_wasInOffice); if ((event == '1' || event == '2') && visited.find(next) == visited.end()) { visited.insert(next); q.push(next); } // Powrot do domu int missed = countMissedMeetings(s, curr_time, t); if (curr_skip + missed <= k) { next = State(curr_time + t, curr_skip + missed, 0, curr_tasks, curr_wasInOffice); if (visited.find(next) == visited.end() && curr_time + t <= n) { visited.insert(next); q.push(next); } } // Pominiecie zadania next = State(curr_time + 1, curr_skip, 1, curr_tasks, curr_wasInOffice); if (event == '3' && visited.find(next) == visited.end()) { visited.insert(next); q.push(next); } } } if (best_state.tasks == -1) { cout << -1 << endl; } else { cout << best_state.tasks << endl; } return 0; } |
English