제출 #922356

#제출 시각아이디문제언어결과실행 시간메모리
922356Ludissey메기 농장 (IOI22_fish)C++17
35 / 100
1115 ms2097152 KiB
#include "fish.h" #include <bits/stdc++.h> #define int long long using namespace std; vector<vector<int>> a; vector<vector<int>> fsh; vector<vector<int>> prfx; vector<vector<int>> possprfx; vector<vector<vector<int>>> memo; int N,M; //x N possible //last N possible negatif et pas negatif //big 3 possible int dp(int x, int last, int big){ if(memo[x][last][big]!=-1) return memo[x][last][big]; if(x==N) { return 0; } int mx = 0; for (auto i:fsh[x]) //O(3) { if(i>last&&big==0) continue; int c=0; if(i==0) { if(big!=2) c=dp(x+1, last, 1+(last>0)); else c=dp(x+1, 0, 1); } else c+=dp(x+1, i, (i>last)); if(i==0&&last>0) { if(big<2) c+=prfx[x][last-1]; } else if(last>i&&big<2) { c+=prfx[x][last-1]-prfx[x][i-1]; } else if(i>last&&x>0){ int ng=0; if(last>0) ng=prfx[x-1][last-1]; c+=prfx[x-1][i-1]-ng; } mx=max(c, mx); } //cerr<<x << " " << last << " " << big << " = " << mx << "\n"; return memo[x][last][big]=mx; } long long max_weights(signed n, signed m, std::vector<signed> X, std::vector<signed> Y, std::vector<signed> W) { N=n; M=m; fsh.resize(N+1,vector<int>(1,0)); prfx.resize(N+1, vector<int>(N+1,0)); possprfx.resize(N+1, vector<int>(N+1,0)); memo.resize(N+1, vector<vector<int>>(2*(N+1), vector<int>(3,-1))); a.resize(N+1, vector<int>((N+1), 0)); for (int i = 0; i < M; i++){ a[X[i]][Y[i]] = W[i]; fsh[X[i]+1].push_back(Y[i]+1); if(X[i]>0) fsh[X[i]-1].push_back(Y[i]+1); } for (int i = 0; i < N; i++) sort(fsh[i].begin(),fsh[i].end()); // N log N for (int i = 0; i < N; i++) //N*N { for (int j = 0; j < N; j++) { prfx[i][j]=a[i][j]; if(j>0) prfx[i][j]+=prfx[i][j-1]; } } for (int i = 0; i < N; i++) //N*N { int mxIndx=-1; int mx=0; //decrease for (int j = 0; j < N; j++) { if(j>0) possprfx[i][j]=possprfx[i][j-1]; possprfx[i][j]-=a[i][j]; if(i<N-1) possprfx[i][j]+=a[i+1][j]; if(possprfx[i][j]>=mx) { mxIndx=j; mx=possprfx[i][j]; } } fsh[i].push_back(mxIndx+1); mxIndx=-1; mx=0; //increase for (int j = 0; j < N; j++) { if(j>0) possprfx[i][j]=possprfx[i][j-1]; if(i>0) possprfx[i][j]+=a[i-1][j]; if(possprfx[i][j]>mx) { mxIndx=j; mx=possprfx[i][j]; } } fsh[i].push_back(mxIndx+1); } int p=dp(0,0,1); //(N*2N*2*N) (300*600*300*2) (108.000.000) return p; }
#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...