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;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ff first
#define sc second
#define int ll
const int mod = 1e9 + 7, N = 1e6 + 1;
vector< vector<int> > x, s;
int n, m;
vector<int> v[N], vis(N), p;
int cnt = 0, cnt1 = 0;
void dfs(int u){
cnt++;
vis[u] = 1;
int a = u / m;
if(u % m != 0) a++;
int b = u % m;
if(b == 0) b = m;
if(s[a][b] == 0){
p.pb(x[a][b]);
}else{
cnt1++;
}
for(auto to : v[u]){
if(vis[to] == 0) dfs(to);
}
}
main(){
cin >> n >> m;
x.resize(n + 1);
s.resize(n + 1);
for(int i = 1; i <= n; i++){
x[i].resize(m + 1);
s[i].resize(m + 1);
for(int j = 1; j <= m; j++){
cin >> x[i][j];
}
}
int k;
cin >> k;
int ans = 0;
for(int i = 1; i <= k; i++){
int a, b;
cin >> a >> b;
a++;b++;
ans += x[a][b];
s[a][b] = 1;
vector< pair<int, int> > l = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
for(auto t : l){
int a1 = a + t.ff;
int b1 = b + t.sc;
if(a1 < 1 || a1 > n || b1 < 1 || b1 > m) continue;
v[(a - 1) * m + b].pb((a1 - 1) * m + b1);
v[(a1 - 1) * m + b1].pb((a - 1) * m + b);
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(s[i][j] == 0 || vis[(i - 1) * m + j] == 1) continue;
dfs((i - 1) * m + j);
if(cnt < cnt1 * 4){
cout << "No" << endl;
return 0;
}
sort(rall(p));
for(int i = 0; i < cnt1 * 3; i++) ans += p[i];
cnt = 0;
cnt1 = 0;
p.clear();
}
}
cout << ans << endl;
}
Compilation message (stderr)
covering.cpp:33:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
33 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |