# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1038603 | Marco_Escandon | Catfish Farm (IOI22_fish) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
//#include "fish.h"
typedef long long ll;
#define x first
#define y second
vector<vector<pair<ll,ll>>> cad;
vector<vector<ll>> index;
/// @brief costo en la columna i
/// @param i columna
/// @param j valor del puente
/// @return
ll cost(ll i, ll j){
ll a=-1,b=cad[i].size();
while(abs(a-b)!=1){
ll m=(a+b)/2;
if(cad[i][m].x<=j) a=m;
else b=m;
}
if(b==0) return 0;
return cad[i][b-1].y;
}
/// @brief mayor k tal que index[i][k] <= j
/// @param i columna
/// @param j valor
/// @return posicion
ll idx(ll i, ll j){
ll a=-1,b=index[i].size();
while(abs(a-b)!=1){
ll m=(a+b)/2;
if(index[i][m]<=j) a=m;
else b=m;
}
return b-1;
}
long long max_weights(int n, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> w) {
cad.resize(n+1); index.resize(n+1);
vector<vector<vector<ll>>> dp(n+1);
for(int i=0; i<X.size(); i++){
cad[X[i]+1].push_back({Y[i]+1,w[i]});
if(X[i]!=0) index[X[i]].push_back(Y[i]+1);
if(X[i]+2<=n) index[X[i]+2].push_back(Y[i]+1);
}
for(int i=0; i<cad.size(); i++)
{
index[i].push_back(0);
sort(cad[i].begin(),cad[i].end());
sort(index[i].begin(),index[i].end());
dp[i].assign(index[i].size(),{0,0});
for(int j=1; j<cad[i].size(); j++)
cad[i][j].y+=cad[i][j-1].y;
}
ll bs=0;
for(int i=1; i<=n; i++)
{
for(int j=0; j<index[i].size(); j++)
{
for(int l=idx(i-1,index[i][j])+1; l<dp[i-1].size(); l++)/// 321 & 231
dp[i][j][0]=max(dp[i][j][0],(max(dp[i-1][l][0],dp[i-1][l][1])+cost(i,index[i-1][l]))-cost(i,index[i][j]));
for(int l=0; l<=idx(i-1,index[i][j]); l++)/// 123
dp[i][j][1]=max(dp[i][j][1],(dp[i-1][l][1]-cost(i,index[i-1][l]))+cost(i,index[i][j]));
if(i>=2)
for(int l=0; l<dp[i-2].size(); l++)/// 103
dp[i][j][1]=max(dp[i][j][1],max(dp[i-2][l][1],dp[i-2][l][1])+cost(i-1,max(index[i][j],index[i-2][l])) );
bs=max(bs,max(dp[i][j][0],dp[i][j][1]));// salvar la respuesta
}
}
return bs;
}