Submission #911048

# Submission time Handle Problem Language Result Execution time Memory
911048 2024-01-18T11:55:27 Z 8pete8 Nafta (COI15_nafta) C++17
34 / 100
1000 ms 45992 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<float.h>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,ll>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
using namespace std;
#define int long long
#define double long double
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
const int mod=1e9+7,mxn=3e5,lg=30,inf=1e18,minf=-1e9,Mxn=100000;
int n,m;
char grid[2002][2002];
int dp[2002][2002],val[2002],cost[2002][2002],on[2002];
bool vis[2002][2002];
int u[4]={0,0,1,-1},r[4]={1,-1,0,0};
int ans=0,sum=0;
int mn,mx;
void dfs(int cx,int cy){
    on[cy]=1;
    sum+=(grid[cx][cy]-'0');
    mn=min(mn,cy);
    mx=max(mx,cy);
    vis[cx][cy]=true;
    for(int i=0;i<4;i++){
        int nx=cx+u[i],ny=cy+r[i];
        if(nx<0||ny<0||nx>=n||ny>=m)continue;
        if(vis[nx][ny]||grid[nx][ny]=='.')continue;
        dfs(nx,ny);
    }
}
void solve(int l,int r,int opl,int opr,int k){
    if(l>r)return;
    int mid=l+(r-l)/2;
    int opt=mid;
    for(int j=0;j<mid;j++)if(dp[opt][k-1]-cost[opt][mid]<dp[j][k-1]-cost[j][mid])opt=j;
    dp[mid][k]=dp[opt][k-1]-cost[opt][mid]+val[mid];
    if(dp[mid][k]<val[mid])dp[mid][k]=val[mid],opt=mid;
    solve(l,mid-1,opl,opt,k);
    solve(mid+1,r,opt,opr,k);
}
int32_t main(){
    fastio
    int sub=0;
    cin>>n>>m;
    for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>grid[i][j];
    for(int i=0;i<n;i++)for(int j=0;j<m;j++){
        if(vis[i][j]||grid[i][j]=='.')continue;
        sum=0;
        mn=inf,mx=minf;
        dfs(i,j);
        for(int k=0;k<m;k++)if(on[k]){
            val[k]+=sum;
            for(int l=0;l<k;l++)if(on[l])cost[l][k]+=sum;
        }
        for(int k=0;k<m;k++)on[k]=0;
    }/*
    for(int i=m-1;i>0;i--)for(int l=0,r=i;r<m;r++,l++){
            cost[l][r]+=cost[l][r+1];
            if(l)cost[l][r]+=cost[l-1][r];
    }*/
    for(int i=1;i<=m;i++){
        for(int j=0;j<m;j++){
            dp[j][i]=val[j];
            if(j)dp[j][i]=max(dp[j][i],dp[j-1][i]);
            for(int l=0;l<j;l++)dp[j][i]=max(dp[j][i],dp[l][i-1]-cost[l][j]+val[j]);
        }
    }
    for(int i=1;i<=m;i++)cout<<dp[m-1][i]<<'\n';
    /*
    for(int i=1;i<=m;i++)solve(0,m-1,0,m-1,i);
    for(int i=1;i<=m;i++){
        int ans=0;
        for(int j=0;j<m;j++)ans=max(ans,dp[j][i]);
        cout<<ans<<'\n';
    }
    */
}

Compilation message

nafta.cpp: In function 'int32_t main()':
nafta.cpp:70:9: warning: unused variable 'sub' [-Wunused-variable]
   70 |     int sub=0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8536 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8652 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8536 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8652 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 29 ms 17340 KB Output is correct
8 Correct 23 ms 17500 KB Output is correct
9 Correct 21 ms 18524 KB Output is correct
10 Correct 24 ms 17536 KB Output is correct
11 Correct 20 ms 17500 KB Output is correct
12 Correct 22 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8536 KB Output is correct
2 Correct 2 ms 8536 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8652 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 29 ms 17340 KB Output is correct
8 Correct 23 ms 17500 KB Output is correct
9 Correct 21 ms 18524 KB Output is correct
10 Correct 24 ms 17536 KB Output is correct
11 Correct 20 ms 17500 KB Output is correct
12 Correct 22 ms 17500 KB Output is correct
13 Execution timed out 1014 ms 45992 KB Time limit exceeded
14 Halted 0 ms 0 KB -