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>
#define ve vector
#define vi vector<int>
#define vii vector<ii>
#define ii pair<int,int>
#define fi first
#define se second
#define ll long long
#define INF 1e9+7
#define pb push_back
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
const int MOD = 1e9+7;
const int nax = 800+5;
void readio(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int grid[nax][nax], vis[nax][nax];
int mx[16];
int ad[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
map<char,int> mp;
int m,r,c;
int dfs(int x, int y){
vis[x][y] = 1;
int res = 1;
for(int k = 0; k < 4; k++){
int nx = x + ad[k][0], ny = y + ad[k][1];
if(nx < 0 || ny < 0 || nx >= r || ny >= c || vis[nx][ny] || !grid[nx][ny]) continue;
int cur = 0;
for(int i = 0; i < 4; i++){
int vx = nx + ad[i][0], vy = ny + ad[i][1];
if(vx < 0 || vy < 0 || vx >= r || vy >= c || grid[vx][vy] == 0) continue;
if(vis[vx][vy]) cur += (1 << i);
}
//cout << nx << " " << ny<< " " << cur << " " << mx[cur] << endl;
if(grid[nx][ny] > mx[cur]) continue;
res += dfs(nx, ny);
}
return res;
}
int main()
{
optimise;
readio();
cin >> m >> r >> c;
string wind;
cin >> wind;
wind = wind + wind;
mp['S'] = 0, mp['N'] = 1, mp['W'] = 2, mp['E'] = 3;
for (int i = 0; i < 16; ++i)
{
int cur = 0;
for (int k = 0; k < wind.size(); ++k)
{
bool test = false;
for (int j = 0; j < 4; ++j)
{
if(i & (1 << j)){
if(mp[wind[k]] == j) test = true;
}
}
if(test) cur++;
else cur = 0;
mx[i] = max(mx[i], cur);
}
//cout << mx[i] << " ";
}
//cout << endl;
if(mx[1] == wind.size()) mx[1] = INF;
if(mx[2] == wind.size()) mx[2] = INF;
if(mx[4] == wind.size()) mx[4] = INF;
if(mx[8] == wind.size()) mx[8] = INF;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
cin >> grid[i][j];
}
}
int mn = INF, aff = 0;
if(r <= 50 && c <= 50){
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
if(!grid[i][j]) continue;
memset(vis, 0, sizeof vis);
int cur = dfs(i, j);
//cout << i << " " << j << " " << cur << endl;
if(mn > cur) mn = cur, aff = 1;
else if(mn == cur) aff++;
}
}
} else {
for (int i = 0; i < r; ++i)
{
int low = 0, high = 1;
for (int j = 0; j < c; ++j)
{
low = min(low, j);
high = max(high, j+1);
if(!grid[i][j]) {
low = j+1;
continue;
}
if(grid[i][j-1] > mx[8]) low = j;
while(high < c && grid[i][high] <= mx[4] && grid[i][high] != 0) high++;
int cur = high - low;
//cout << j << " " << low << " " << high << endl;
if(mn > cur) mn = cur, aff = 1;
else if(mn == cur) aff++;
}
}
}
cout << mn << " " << aff << endl;
}
Compilation message (stderr)
virus.cpp: In function 'int main()':
virus.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int k = 0; k < wind.size(); ++k)
| ~~^~~~~~~~~~~~~
virus.cpp:78:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | if(mx[1] == wind.size()) mx[1] = INF;
| ~~~~~~^~~~~~~~~~~~~~
virus.cpp:79:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | if(mx[2] == wind.size()) mx[2] = INF;
| ~~~~~~^~~~~~~~~~~~~~
virus.cpp:80:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | if(mx[4] == wind.size()) mx[4] = INF;
| ~~~~~~^~~~~~~~~~~~~~
virus.cpp:81:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | if(mx[8] == wind.size()) mx[8] = INF;
| ~~~~~~^~~~~~~~~~~~~~
virus.cpp: In function 'void readio()':
virus.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
virus.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |