# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
981715 |
2024-05-13T13:43:54 Z |
12345678 |
Nafta (COI15_nafta) |
C++17 |
|
3 ms |
4956 KB |
#include <bits/stdc++.h>
using namespace std;
const int nx=305;
int n, m, mp[nx][nx], dp[nx][nx], mn[nx][nx], cnt[nx][nx], g[nx][nx], t, s[nx][nx],vs[nx][nx], dx[4]={0, 0, 1, -1}, dy[4]={1, -1, 0, 0}, ans[nx];
char ch;
queue<pair<int, int>> q;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) cin>>ch, mp[i][j]=(ch=='.')?-1:ch-'0';
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
if (vs[i][j]||mp[i][j]==-1) continue;
q.push({i, j});
vs[i][j]=1;
int k=INT_MAX, sm=0;
vector<pair<int, int>> v;
while (!q.empty())
{
v.push_back(q.front());
auto [x, y]=q.front();
q.pop();
k=min(k, y);
sm+=mp[x][y];
for (int d=0; d<4; d++)
{
int nwx=x+dx[d], nwy=y+dy[d];
if (nwx<1||nwx>n||nwy<1||nwy>m||mp[nwx][nwy]==-1||vs[nwx][nwy]) continue;
q.push({nwx, nwy});
vs[nwx][nwy]=1;
}
}
++t;
for (auto [x, y]:v) g[x][y]=t, mn[x][y]=k, s[x][y]=sm; //cout<<"group "<<t<<' '<<x<<' '<<y<<'\n';
}
}
for (int i=1; i<=m; i++)
{
vector<int> v(nx);
for (int j=1; j<=n; j++)
{
if (mp[j][i]==-1||v[g[j][i]]) continue;
v[g[j][i]]=1;
cnt[i][mn[j][i]-1]+=s[j][i];
}
for (int j=i; j>=0; j--) cnt[i][j]+=cnt[i][j+1]; //cout<<"debug "<<i<<' '<<j<<' '<<cnt[i][j]<<'\n';
}
for (int i=1; i<=m; i++)
{
for (int j=1; j<=m; j++)
{
for (int k=0; k<i; k++)
{
dp[i][j]=max(dp[i][j], dp[k][j-1]+cnt[i][k]);
}
ans[j]=max(ans[j], dp[i][j]);
}
}
for (int i=1; i<=m; i++) cout<<ans[i]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
4956 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
4956 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
4956 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |