#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#define ll long long
#define pb push_back
#define pll pair<ll ,ll>
#define F first
#define S second
#define pii pair<int ,int>
using namespace std;
const int N = (int)1e6 + 6;
int n, m, k, x, y, g[N], v, u, okt;
vector<ll int> vc, adj[N];
int hx[] = {-1, 1, 0, 0};
int hy[] = {0, 0, 1, -1};
bool vis[N], ok[N];
bool valid(int i,int j){return (i >= 0 && i < n && j >= 0 && j < m);}
void dfs(int v){
vis[v] = 1;
if(!ok[v])vc.pb(g[v]);
else okt ++;
for(auto u : adj[v])if(!vis[u])dfs(u);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i ++)for(int j = 0; j < m;j ++)cin >> g[i*m + j], ok[i*m + j] = vis[i*m + j] = 0;
cin >> k;
ll ans = 0;
for(int i = 0; i < k; i ++){
cin >> x >> y;
int v = x*m + y;
// cout << "V : " << v << endl;
ok[v] = 1, ans += g[v];
for(int r = 0; r < 4; r ++){
int xx = x + hx[r], yy = y + hy[r];
if(valid(xx, yy) == 0)continue;
int u = xx * m + yy;
adj[v].pb(u), adj[u].pb(v);
// cout << " adj : " <<u << " " << v << endl;
}
}
for(int i = 0; i < n; i ++){
for(int j = 0; j < m; j ++){
v = i*m + j;
if(!vis[v] && ok[v]){
okt = 0;
vc.clear();
dfs(v);
if(vc.size() < 3 * okt)return cout << "NO\n", 0;
sort(vc.begin(), vc.end());
// cout << i << " " << j << " : " ;
// for(auto x : vc)cout << x << " ";
// cout << endl;
for(int f = 1; f <= 3*okt; f ++)ans += vc[vc.size() - f];
}
}
}return cout << ans << "\n" , 0;
}
Compilation message
covering.cpp: In function 'int main()':
covering.cpp:60:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | if(vc.size() < 3 * okt)return cout << "NO\n", 0;
| ~~~~~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
24008 KB |
Output is correct |
3 |
Correct |
19 ms |
24296 KB |
Output is correct |
4 |
Correct |
31 ms |
25536 KB |
Output is correct |
5 |
Correct |
72 ms |
29812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
15 ms |
23892 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23912 KB |
Output is correct |
3 |
Correct |
19 ms |
24380 KB |
Output is correct |
4 |
Correct |
32 ms |
25608 KB |
Output is correct |
5 |
Correct |
75 ms |
29824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
16 ms |
24092 KB |
Output is correct |
4 |
Correct |
15 ms |
24084 KB |
Output is correct |
5 |
Correct |
20 ms |
24532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23828 KB |
Output is correct |
2 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
24008 KB |
Output is correct |
3 |
Correct |
19 ms |
24296 KB |
Output is correct |
4 |
Correct |
31 ms |
25536 KB |
Output is correct |
5 |
Correct |
72 ms |
29812 KB |
Output is correct |
6 |
Correct |
15 ms |
23764 KB |
Output is correct |
7 |
Incorrect |
15 ms |
23892 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
24008 KB |
Output is correct |
3 |
Correct |
19 ms |
24296 KB |
Output is correct |
4 |
Correct |
31 ms |
25536 KB |
Output is correct |
5 |
Correct |
72 ms |
29812 KB |
Output is correct |
6 |
Correct |
15 ms |
23764 KB |
Output is correct |
7 |
Incorrect |
15 ms |
23892 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |