This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
5 5
RGRGW
GRRGW
WGGWR
RWRGW
RGWGW
3 4
RGWR
GRGG
RGWW
4 4
RGWR
GRRG
WGGW
WWWR
3 4
RGWR
GRGG
RGWW
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
using vc = vector<char>;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N, M;
cin >> N >> M;
vector<vc> sk(N, vc(M));
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
cin >> sk[i][j];
}
}
vii dp;
// 0 - nieko
// 1 - vertical
// 2 - horizontal
ll ats = 0;
auto ver = [&](int i, int j) -> int {
return i - 1 >= 0 and i + 1 < N and sk[i - 1][j] == 'R'
and sk[i][j] == 'G' and sk[i + 1][j] == 'W';
};
auto hor = [&](int i, int j) -> int {
return j - 1 >= 0 and j + 1 < M and sk[i][j - 1] == 'R'
and sk[i][j] == 'G' and sk[i][j + 1] == 'W';
};
for (int d = 0; d < N + M - 1; ++d)
{
dp = vii(N, vi(3, 0));
int lst = max(N - d - 1, 0);
for (int i = max(N - d - 1, 0), j = max(d - N + 1, 0); i < N and j < M; i++, j++)
{
// printf("d = %d, i = %d, j = %d\n", d, i, j);
// if (!i) continue;
dp[i][0] = (i ? max({dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]}) : 0);
dp[i][1] = (i ? max(dp[i - 1][0], dp[i - 1][1]) : 0) + ver(i, j);
dp[i][2] = (i ? max(dp[i - 1][0], dp[i - 1][2]) : 0) + hor(i, j);
lst = i;
}
// printf("d = %d, dp:\n", d);
// for (int k = 0; k < 3; ++k)
// {
// for (int i = 0; i < N; ++i)
// printf("%d ", dp[i][k]);
// printf("\n");
// }
ats += max({dp[lst][0], dp[lst][1], dp[lst][2]});
}
printf("%lld\n", ats);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |