#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <map>
using namespace std;
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair
const int mxn = 5050;
int a[mxn][mxn], n, m;
pii pos[mxn];
void build() {
map<int, int> mp;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
mp[a[i][j]];
int s = 0;
for(auto &p : mp)
p.second = s++;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) {
a[i][j] = mp[a[i][j]];
pos[a[i][j]] = {i, j};
}
}
bool neighbour(int x, int y) {
const pii &px = pos[x];
const pii &py = pos[y];
pii p;
p.ff = px.ff - py.ff;
p.ss = px.ss - py.ss;
return (p.ff * p.ss == 0) && (abs(p.ff + p.ss) == 1);
}
bool can(int i1, int i2, int j1, int j2) {
vector<int> v;
for(int i = i1; i <= i2; i++)
for(int j = j1; j <= j2; j++)
v.push_back(a[i][j]);
sort(v.begin(), v.end());
for(int i = 1; i < v.size(); i++)
if(!neighbour(v[i], v[i - 1]))
return 0;
return 1;
}
int main() {
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> a[i][j];
build();
int ans = 0;
for(int s = 0; s < n; s++)
for(int e = s; e < n; e++)
for(int l = 0; l < m; l++)
for(int r = l; r < m; r++) {
if(can(s, e, l, r)) {
ans++;
}
}
cout << ans << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'bool can(int, int, int, int)':
Main.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i = 1; i < v.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
5075 ms |
5212 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Execution timed out |
5038 ms |
31324 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Execution timed out |
5038 ms |
31324 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Execution timed out |
5038 ms |
31324 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |