제출 #1325247

#제출 시각아이디문제언어결과실행 시간메모리
1325247eri16메기 농장 (IOI22_fish)C++20
70 / 100
207 ms219332 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

ll dp[3005][3005][2];
ll pref[3005][3005];

ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W){
    bool evn=true;
    bool lwr=true;
    bool sb3=true;
    
    ll sm1=0;
    ll sm2=0;
    
    int sub_2[N][2];
    
    for (int i=0; i<N; i++){
        for (int j=0; j<2; j++){
            sub_2[i][j]=0;
        }
    }
    
    for (int i=0; i<M; i++){
        if (Y[i]!=0){sb3=false;}
        if (X[i]%2){evn=false;sm1+=W[i];sub_2[Y[i]][X[i]%2]=W[i];}
        else{sm2+=W[i];sub_2[Y[i]][X[i]%2]=W[i];}
        if (X[i]>1){lwr=false;}
    }
    
    if (evn){return (sm1+sm2);}
    if (lwr && N==2){return max(sm1,sm2);}
    if (lwr){
        vector<ll> A(N+1,0), B(N+1,0);
        for (int i=0; i<N; i++){
            A[i+1]=A[i]+sub_2[i][0];
            B[i+1]=B[i]+sub_2[i][1];
        }
        ll ans=0;
        for (int i=0; i<=N; i++) {
            ans=max(ans, B[N]-B[i]+A[i]);
        }
        return ans;        
    }
    if (sb3){
        vector <int> w(N,0);
        
        for (int i=0; i<M; i++){w[X[i]]=W[i];}
        
        ll dp[N][2];
        
        dp[0][0]=0;
        dp[0][1]=0;
        dp[1][0]=w[1];
        dp[1][1]=w[0];
        
        for (int i=2; i<N; i++){
            dp[i][0]=max(dp[i-1][0],dp[i-1][1]+w[i]);
            
            dp[i][1]=max({dp[i-1][0],dp[i-1][1],dp[i-2][0]+w[i-1]});
        }
        
        return max(dp[N-1][0],dp[N-1][1]);
    }
    
    //actual o(n^2 solution);
    
    for (int i=0; i<M; i++){pref[X[i]+1][Y[i]+1]+=W[i];}
    
    for (int i=0; i<N+1; i++){
        for (int j=1; j<N+1; j++){
            pref[i][j]+=pref[i][j-1];
        }    
    }
    
    for (int i=0; i<N+2; i++){
        for (int j=0; j<N+2; j++){
            for (int z=0; z<2; z++){
                dp[i][j][z]=LLONG_MIN;
            }
        }
    }
    
    ll ans=0;
    dp[0][0][1]=0;
    
    for (int i=1; i<N+1; i++){
		ll ndp=LLONG_MIN;
		
		vector<ll>prefMax(N+2,LLONG_MIN),sufMax(N+2, LLONG_MIN);
		for(int x=0; x<N+1; x++){
			ndp=max({ndp,dp[i-1][x][0],dp[i-1][x][1]});
            if (x>0)prefMax[x]=max(prefMax[x-1], dp[i-1][x][1]-pref[i-1][x]);
            else prefMax[x]=dp[i-1][x][1]-pref[i-1][x];
		}
		for(int x=N; x>=0; x--){
			sufMax[x]=max(sufMax[x+1],max(dp[i-1][x][0],dp[i-1][x][1])+pref[i][x]);
		}
		for(int x=0; x<N+1; x++){
			if(x==0){
				dp[i][x][0]=dp[i][x][1]=ndp;
			}
			dp[i][x][1]=max(dp[i][x][1],pref[i-1][x]+prefMax[x]);
			dp[i][x][0]=max(dp[i][x][0],sufMax[x]-pref[i][x]);
			ans=max({ans,dp[i][x][1],dp[i][x][0]});
		}        
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...