#include <unistd.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <deque>
#include <iostream>
#include <algorithm>
// using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL;
typedef unsigned long long ULL;
using VINT = std::vector<int>;
using VLL = std::vector<LL>;
using VULL = std::vector<ULL>;
struct Uczestnik {
int n;
int wynik;
bool uczestniczy;
};
int checkAbsents(VINT& vint, int begin, int end, bool inHome){
int ret = 0;
for (int i = begin; i < end; i++) {
if (vint[i] == 3) continue; // wolne
if (vint[i] == 2 && inHome) continue; // w domu praca
ret++;
}
return ret;
}
int checkFree(VINT& vint, int begin, int end) {
int ret = 0;
for (int i = begin; i < end; i++) {
if (vint[i] == 3) ret++;
}
return ret;
}
int main() {
std::ios_base::sync_with_stdio(false);
int n, k, t;
char c;
std::string s;
std::cin >> n >> k >> t;
std::vector<Uczestnik> uczestnicy;
VINT vint;
vint.reserve(n);
REP(i, n) {
std::cin >> c;
int a;
if (c == '1') a = 1;
if (c == '2') a = 2;
if (c == '3') a = 3;
vint.push_back(a);
}
//
int max = -1;
int absents = checkAbsents(vint, 0, n, true);
if (absents <= k) {
// int tt = checkFree(vint, 0, n);
max = std::max(max, (checkFree(vint, 0, n) + k));
}
for (int i = 0; i < n - 2*t + 1; i++)
{
int a = checkAbsents(vint, 0, i, true);
int b = checkAbsents(vint, i, i + t, false);
int aa = checkFree(vint, 0, i);
int jazdaPowrotna = checkAbsents(vint, i + t + 1, i+ t + t + 1, false);
int poPowrocie = checkAbsents(vint, i + t + t + 1, n, true);
for (int j = i + t + 1; j < n - t + 1; j++) {
// int d = checkAbsents(vint, j + t, n, true);
if (a + b + jazdaPowrotna + poPowrocie <= k) {
int bb = checkFree(vint, j+t, n);
int tmpMax = aa + bb + k - b - jazdaPowrotna;
if (max < tmpMax)
max = tmpMax;
}
if (vint[j] == 3) jazdaPowrotna++;
if (vint[j+t] == 3) jazdaPowrotna--;
if (vint[j+t] == 1) poPowrocie--;
}
}
max = std::min(max, n);
std::cout << max << std::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 | #include <unistd.h> #include <string> #include <vector> #include <map> #include <set> #include <utility> #include <deque> #include <iostream> #include <algorithm> // using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; using VINT = std::vector<int>; using VLL = std::vector<LL>; using VULL = std::vector<ULL>; struct Uczestnik { int n; int wynik; bool uczestniczy; }; int checkAbsents(VINT& vint, int begin, int end, bool inHome){ int ret = 0; for (int i = begin; i < end; i++) { if (vint[i] == 3) continue; // wolne if (vint[i] == 2 && inHome) continue; // w domu praca ret++; } return ret; } int checkFree(VINT& vint, int begin, int end) { int ret = 0; for (int i = begin; i < end; i++) { if (vint[i] == 3) ret++; } return ret; } int main() { std::ios_base::sync_with_stdio(false); int n, k, t; char c; std::string s; std::cin >> n >> k >> t; std::vector<Uczestnik> uczestnicy; VINT vint; vint.reserve(n); REP(i, n) { std::cin >> c; int a; if (c == '1') a = 1; if (c == '2') a = 2; if (c == '3') a = 3; vint.push_back(a); } // int max = -1; int absents = checkAbsents(vint, 0, n, true); if (absents <= k) { // int tt = checkFree(vint, 0, n); max = std::max(max, (checkFree(vint, 0, n) + k)); } for (int i = 0; i < n - 2*t + 1; i++) { int a = checkAbsents(vint, 0, i, true); int b = checkAbsents(vint, i, i + t, false); int aa = checkFree(vint, 0, i); int jazdaPowrotna = checkAbsents(vint, i + t + 1, i+ t + t + 1, false); int poPowrocie = checkAbsents(vint, i + t + t + 1, n, true); for (int j = i + t + 1; j < n - t + 1; j++) { // int d = checkAbsents(vint, j + t, n, true); if (a + b + jazdaPowrotna + poPowrocie <= k) { int bb = checkFree(vint, j+t, n); int tmpMax = aa + bb + k - b - jazdaPowrotna; if (max < tmpMax) max = tmpMax; } if (vint[j] == 3) jazdaPowrotna++; if (vint[j+t] == 3) jazdaPowrotna--; if (vint[j+t] == 1) poPowrocie--; } } max = std::min(max, n); std::cout << max << std::endl; return 0; } |
English