이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2000 + 7;
const ll oo = 1e18 + 69;
int n, m, id[maxn][maxn], num_gr = 0, cost[maxn][maxn], dp[maxn][maxn];
char a[maxn][maxn];
struct group {
int l, r, oil;
};
vector<group> cpn;
pa d[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool used[maxn];
void bfs(int sx, int sy) {
id[sx][sy] = ++num_gr;
queue<pa> q;
q.push({sx, sy});
int s = 0, l = m, r = 0;
while(!q.empty()) {
int u = q.front().fi, v = q.front().se;
q.pop();
s += a[u][v] - '0';
mini(l, v);
maxi(r, v);
for(int k = 0; k < 4; k++) {
int x = u + d[k].fi, y = v + d[k].se;
if(x < 1 || x > n || y < 1 || y > m || id[x][y] || a[x][y] == '.') continue;
id[x][y] = num_gr;
q.push({x, y});
}
}
cpn.pb({l, r, s});
}
void solve(int t, int l, int r, int optl, int optr) {
if(l > r) return;
int i = l + r >> 1;
pa res = {0, 0};
for(int j = optl; j <= min(i - 1, optr); j++)
if(maxi(res.fi, dp[t - 1][j] + cost[i][j])) res.se = j;
dp[t][i] = res.fi;
solve(t, l, i - 1, optl, res.se);
solve(t, i + 1, r, res.se, optr);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) cin>>a[i][j];
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(id[i][j] || a[i][j] == '.') continue;
bfs(i, j);
}
}
for(int i = 1; i <= m; i++) {
vector<pa> val;
fill_n(used, num_gr + 1, 0);
for(int j = 1; j <= n; j++) {
if(!id[j][i]) continue;
int idd = id[j][i] - 1;
if(used[idd]) continue;
used[idd] = 1;
val.pb({cpn[idd].l, idd});
cost[i][0] += cpn[idd].oil;
}
sort(all(val));
int k = 0;
for(int j = 1; j < i; j++) {
cost[i][j] = cost[i][j - 1];
while(k < Size(val) && val[k].fi == j) {
cost[i][j] -= cpn[val[k].se].oil;
k++;
}
}
}
for(int t = 1; t <= m; t++) {
int ans = 0;
if(t == 1) {
for(int i = 1; i <= m; i++) dp[t][i] = cost[i][0];
} else solve(t, 1, m, 1, m);
for(int i = 1; i <= m; i++) maxi(ans, dp[t][i]);
cout<<ans<<"\n";
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
nafta.cpp: In function 'void solve(int, int, int, int, int)':
nafta.cpp:57:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | int i = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |