#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define int long long
#define ff first
#define ss second
#define tobit(n) bitset<20>(n) //выводит 20 элементов в битовую систему
#define all(v) (v).begin(), (v).end()
#define rtt(v, k) rotate(v.begin(), v.begin() + k, v.end()); //move k elements back
const int MOD = 1e9 + 7;
int binpownm(int n, int m){
if(m == 1) return n;
if(m % 2 == 0){
int t = binpownm(n, m / 2); return t * t;
} else return binpownm(n, m - 1) * n;
}
bool sortt(pair<int, double> a, pair<int, double> b){
return a.ss < b.ss;
}
struct sturc_t {
int ff_, ss_, tt_;
};
int knaps(vector<int>& cur, vector<int>& cst, int S){
vector<vector<int>> dp(S + 1, vector<int> (cur.size() + 1, 0));
for(int j = 1; j <= cur.size(); j++){
for(int w = 1; w <= S; w++){
if(cur[j-1] <= w){
dp[w][j] = max(dp[w][j - 1], dp[w - cur[j - 1]][j - 1] + cst[j - 1]);
} else dp[w][j] = dp[w][j - 1];
}
} return dp[S][cur.size()];
}
int knapsack2(vector<sturc_t>& v, int S){
vector<int> dp(S + 1, 0);
for(auto& to : v){
for(int q = 0; q < to.tt_; q++){
for(int w = S; w >= to.ss_; w--){
dp[w] = max(dp[w], dp[w - to.ss_] + to.ff_);
}
}
} return dp[S];
}
signed main(){
int n, k, res = -1, cst = 0; cin >> n >> k;
string s; cin >> s;
vector<int> a(n), b(n), c(n);
a[0] = (s[0] == 'O') - 1;
c[0] = (s[0] == 'O');
for(int i = 1; i < n; i++){
a[i] = (s[i] == 'O') ? i : a[i - 1];
c[i] = c[i - 1] + (s[i] == 'O');
} b[n - 1] = (s[n - 1] == 'O') ? n - 1 : -1;
for(int i = n - 2; i >= 0; i--){
b[i] = (s[i] == 'O') ? i : b[i + 1];
} vector<int> res1(n, -1);
deque<int> d;
// for(auto to : a) cout << to << " "; cout << "\n";
// for(auto to : b) cout << to << " "; cout << "\n";
// for(auto to : c) cout << to << " "; cout << "\n\n\n";
for(int i = 0; i < n; i++){
if(s[i] != 'J') continue;
if(!d.empty()) cst += i - d.back() - 1;
d.push_back(i);
if((int)d.size() == k+1){
int f = d.front(); d.pop_front();
cst -= d.front() - f - 1;
} if ((int)d.size() == k) res1[i] = cst;
} vector<int> res2(n, -1);
d.clear(); cst = 0;
for(int i = n - 1; i >= 0; i--){
if(s[i] != 'I') continue;
if(!d.empty())
cst += d.back() - i - 1;
d.push_back(i);
if((int)d.size() == k + 1){
int f = d.front(); d.pop_front();
cst -= f - d.front() - 1;
}
if((int)d.size() == k) res2[i] = cst;
} //for(auto to : res1) cout << to << " "; cout << "\n";
// for(auto to : res2) cout << to << " "; cout << "\n";
for(int i = 0; i < n; i++){
if(res1[i] == -1) continue;
for (int j = i + 2; j < n; j++){
if(res2[j] == -1 || c[j - 1] - c[i] < k) continue;
int cst = res1[i] + res2[j] + (j - i - 1 - k);
if(res == -1 || res > cst) res = cst;
}
} cout << res;
}
// NEED TO FAST CIN && COUT //
const int fastIO = [](){
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
return 0;
}();
Compilation message
ho_t2.cpp: In function 'long long int knaps(std::vector<long long int>&, std::vector<long long int>&, long long int)':
ho_t2.cpp:25:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int j = 1; j <= cur.size(); j++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
408 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
408 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Execution timed out |
2062 ms |
7912 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |