Editorial for Đọng nước

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <utility>
#include <queue>
#include <cstring>
using namespace std;
const int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

int m,n,h[1010][1010],a[1010][1010];
vector < pair<int,int> > p[1000100];

void bfs(int x,int y,int val)
{
queue < pair<int,int> > q;
h[x][y]=val;
q.push(make_pair(x,y));
while (!q.empty())
{
x=q.front().first; y=q.front().second;
q.pop();
for (int i=0;i<4;i++)
{
int xx=x+dx[i],yy=y+dy[i];
if (!h[xx][yy] && a[xx][yy]<=val)
{
h[xx][yy]=val;
q.push(make_pair(xx,yy));
}
}
}
}

int isBorder(int x,int y)
{
for (int i=0;i<4;i++)
if (h[x+dx[i]][y+dy[i]]) return 1;
return 0;
}

int main()
{
memset(h,-1,sizeof(h));
cin >> m >> n;
for (int i=1;i<=m;i++)
for (int j=1;j<=n;j++)
scanf("%d",&a[i][j]), p[a[i][j]].push_back(make_pair(i,j)), h[i][j]=0;
for (int i=1;i<=1000000;i++)
for (int j=0;j<int(p[i].size());j++)
{
int x=p[i][j].first,y=p[i][j].second;
if (!h[x][y] && isBorder(x,y)) bfs(x,y,a[x][y]);
}
long long ans=0;
for (int i=2;i<m;i++)
for (int j=2;j<n;j++)
ans+=max(0,h[i][j]-a[i][j]);
cout << ans << endl;
}


Code mẫu của ladpro98

#include <bits/stdc++.h>
#define ii pair<int, int>
#define iii pair<int, ii>
#define X first
#define Y second
const int N = 1010;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int oo = 1000000009;
using namespace std;
int d[N][N], a[N][N];
int m, n;

int main() {
scanf("%d %d", &m, &n);
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &a[i][j]), d[i][j] = oo;
priority_queue<iii, vector<iii>, greater<iii> > Q;
for(int i = 1; i <= m; i++) {
d[i][1] = a[i][1]; d[i][n] = a[i][n];
Q.push(iii(a[i][1], ii(i, 1)));
Q.push(iii(a[i][n], ii(i, n)));
}
for(int i = 2; i < n; i++) {
d[1][i] = a[1][i]; d[m][i] = a[m][i];
Q.push(iii(a[1][i], ii(1, i)));
Q.push(iii(a[m][i], ii(m, i)));
}
while (!Q.empty()) {
ii u = Q.top().Y; int du = Q.top().X; Q.pop();
if (du != d[u.X][u.Y]) continue;
for(int i = 0; i < 4; i++) {
ii v (u.X + dx[i], u.Y + dy[i]);
if (v.X < 1 || v.Y < 1 || v.X > m || v.Y > n) continue;
if (d[v.X][v.Y] > max(d[u.X][u.Y], a[v.X][v.Y])) {
d[v.X][v.Y] = max(d[u.X][u.Y], a[v.X][v.Y]);
Q.push(iii(d[v.X][v.Y], v));
}
}
}
long long res = 0;
for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++)
if (d[i][j] > a[i][j]) res += d[i][j] - a[i][j];
printf("%lld", res);
return 0;
}


Code mẫu của RR

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
if(!*inp) { \
if (REACHEOF) return 0;\
memset(BUF,0,sizeof BUF);\
int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
if (inpzzz != BUFSIZE) REACHEOF = true;\
inp=BUF; \
} \
INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
AM=0;\
GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
if (INP=='-') {AM=1;GETCHAR(INP);} \
j=INP-'0'; GETCHAR(INP); \
while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);

const int MN = 1011;
const int MN2 = MN * MN;

int id[MN][MN], h[MN][MN], m, n;
int lab[MN2], height[MN2];
bool out[MN2];
vector< pair<int,int> > ls[MN2];

int getRoot(int u) {
if (lab[u] < 0) return u;
return lab[u] = getRoot(lab[u]);
}

void merge(int u, int v) {
lab[u] = lab[u] + lab[v];
lab[v] = u;

height[u] = max(height[u], height[v]);
out[u] = out[u] | out[v];
}

const int di[] = {-1,1,0,0};
const int dj[] = {0,0,-1,1};

int main() {
scanf("%d%d", &m, &n);
int now = 0;
FOR(i,2,m+1) FOR(j,2,n+1)
scanf("%d", &h[i][j]);

m += 2; n += 2;

FOR(i,1,m) FOR(j,1,n) {
id[i][j] = ++now;
lab[now] = -1;
height[now] = h[i][j];
if (h[i][j] == 0) out[now] = true;
else out[now] = false;

ls[h[i][j]].PB(MP(i,j));
}

long long res = 0;

FOR(t,1,1000000) REP(tt,ls[t].size()) {
int u = ls[t][tt].F, v = ls[t][tt].S, uu, vv;

REP(dir,4) {
uu = u + di[dir], vv = v + dj[dir];
if (uu < 1 || uu > m || vv < 1 || vv > n) continue;

int id2 = getRoot(id[uu][vv]);
int id1 = getRoot(id[u][v]);

if (height[id1] >= height[id2] && id2 != id1) {
if (!out[id2]) {
res += -lab[id2] * (height[id1] - height[id2]);
}
merge(id2, id1);
}
}
}
cout << res << endl;

return 0;
}