이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
void OJize(){cin.tie(NULL);ios_base::sync_with_stdio(false);}
typedef long long ll;
const int IINF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define sz(X) (int)((X).size())
#define entire(X) X.begin(),X.end()
template <class T1, class T2>ostream&operator<<(ostream &os,pair<T1,T2>const&x){os<<'('<<x.first<<", "<<x.second<<')';return os;}
template <class Ch, class Tr, class Container>basic_ostream<Ch,Tr>&operator<<(basic_ostream<Ch,Tr>&os,Container const&x){os<<"[ ";for(auto&y:x)os<<y<<" ";return os<<"]";}
// NOTE: this is not a "sparse table" related to
// binary lifting, it's literally an array which is sparse!!
template<typename T>
struct SparseArray{
// not actually sparse yet!!
int n;
vector<T> arr, sum;
SparseArray(int N): n{N}, arr(N), sum(N){}
void write(int i, T x){arr[i] = x;}
void init(){
sum[0] = arr[0];
for(int i=1; i<n; i++) sum[i] = sum[i-1]+arr[i];
}
T operator[](int i){return arr[i];}
T getsum(int l, int r){
return sum[r] - (l? sum[l-1] : (T)0);
}
};
ll max_weights(int L, int n,
vector<int> X, vector<int> Y, vector<int> W){
vector<SparseArray<ll>> grid(L, SparseArray<ll>(L+1));
for(int i=0; i<n; i++) grid[X[i]].write(Y[i], (ll)W[i]);
for(auto &A: grid) A.init();
vector<ll> dinc(L+1), ddec(L+1);
// column _,
// last column height j,
// next will increase/decrease
for(int i=1; i<L; i++){
vector<ll> ndinc(L+1), nddec(L+1);
ndinc[0] = nddec[0] = ddec[0];
ndinc[L] = nddec[L] = max(ddec[0], dinc[L]);
for(int y=0; y<=L; y++){
if(!(y == L || y == 0 || grid[i][y])) continue;
for(int y0=0; y0<y; y0++){ // inc
if(!(y0 == L || y0 == 0 || grid[i-1][y0])) continue;
// take [i-1][y0 to y-1]
ll val = dinc[y0] + grid[i-1].getsum(y0, y-1);
ndinc[y] = max(ndinc[y], val);
if(y == L) nddec[y] = max(nddec[y], val);
}
for(int y0=y+1; y0<=L; y0++){ // dec
if(!(y0 == L || y0 == 0 || grid[i-1][y0])) continue;
// take [i][y to y0-1]
ll val = ddec[y0] + grid[i].getsum(y, y0-1);
nddec[y] = max(nddec[y], val);
}
}
dinc = ndinc, ddec = nddec;
}
return max(
*max_element(entire(dinc)),
*max_element(entire(ddec))
);
}
#ifdef jh
int main(){OJize();
int n; cin>>n;
vector<int> X, Y, W;
for(int i=0; i<n; i++) for(int j=0; j<n; j++){
int x; cin>>x;
if(x){
X.push_back(j);
Y.push_back(n-1-i);
W.push_back(x);
}
}
cout << max_weights(n, sz(X), X, Y, W);
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |