This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true;
return false;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, M;
cin >> N >> M;
vector<vector<char>> a(N, vector<char>(M));
for(int i = 0; i < N; ++i){
for(int j = 0; j < M; ++j){
cin >> a[i][j];
}
}
auto check_vertical = [&](int i, int j){
return i - 1 >= 0 && i + 1 < N && a[i - 1][j] == 'R' && a[i][j] == 'G' && a[i + 1][j] == 'W';
};
auto check_horizontal = [&](int i, int j){
return j - 1 >= 0 && j + 1 < M && a[i][j - 1] == 'R' && a[i][j] == 'G' && a[i][j + 1] == 'W';
};
int ans = 0;
for(int s = 0; s <= N + M - 1; ++s){
vector<tuple<int, int, int, int>> cells; //[x, y, v, h]
for(int i = max(0, s - M + 1), j = s - i; 0 <= i && i < N && 0 <= j && j < M; ++i, --j){
cells.emplace_back(i, j, check_vertical(i, j), check_horizontal(i, j));
}
int k = (int)cells.size();
vector<array<int, 3>> dp(k + 1, {0, 0, 0});
for(int j = 0; j < k; ++j){
int x, y, v, h; tie(x, y, v, h) = cells[j];
maximize(dp[j + 1][0], dp[j][0]);
maximize(dp[j + 1][0], dp[j][1]);
maximize(dp[j + 1][0], dp[j][2]);
maximize(dp[j + 1][1], dp[j][0]);
maximize(dp[j + 1][1], dp[j][1]);
maximize(dp[j + 1][2], dp[j][0]);
maximize(dp[j + 1][2], dp[j][2]);
if(v) maximize(dp[j + 1][1], max(dp[j][0], dp[j][1]) + 1);
if(h) maximize(dp[j + 1][2], max(dp[j][0], dp[j][2]) + 1);
}
ans += max({dp[k][0], dp[k][1], dp[k][2]});
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |