Submission #629579

# Submission time Handle Problem Language Result Execution time Memory
629579 2022-08-14T16:25:08 Z jh05013 Catfish Farm (IOI22_fish) C++17
52 / 100
800 ms 2097152 KB
#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, prefm, sufm;
	vector<bool> written;
	vector<int> nonempty;
	SparseArray(int N): n{N}, arr(N), sum(N),
		prefm(N), sufm(N),
		written(N){}

	void write(int i, T x){arr[i] = x; written[i] = true;}
	void init(){
		sum[0] = prefm[0] = arr[0];
		for(int i=1; i<n; i++){
			sum[i] = sum[i-1]+arr[i];
			prefm[i] = max(prefm[i-1], arr[i]);
		}
		for(int i=0; i<n; i++) if(written[i])
			nonempty.push_back(i);
		sufm[n-1] = arr[n-1];
		for(int i=n-2; i>=0; i--)
			sufm[i] = max(sufm[i+1], arr[i]);
	}

	T operator[](int i){return arr[i];}
	T prefsum(int i){return i>=0? sum[i] : (T)0;}
	T getsum(int l, int r){return prefsum(r) - prefsum(l);}
	T allmax(){return *max_element(entire(arr));}
	T prefmax(int i){return i>=0? prefm[i] : (T)0;}
	T sufmax(int i){return i<n? sufm[i] : (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<L; i++)
		grid[i].write(0, 0), grid[i].write(L, 0);
	for(int i=0; i<n; i++) grid[X[i]].write(Y[i], (ll)W[i]);
	for(auto &A: grid) A.init();

	SparseArray<ll> dinc(L+1), ddec(L+1);
	dinc.init(); ddec.init();
	// column _,
	// last column height j,
	// next will increase/decrease
	for(int i=1; i<L; i++){
		SparseArray<ll> ndinc(L+1), nddec(L+1);
		SparseArray<ll> incmaxer(L+1), decmaxer(L+1);
		for(int y0: grid[i-1].nonempty){
			incmaxer.write(y0, dinc[y0] - grid[i-1].prefsum(y0-1));
			decmaxer.write(y0, ddec[y0] + grid[i].prefsum(y0-1));
		}
		incmaxer.init(), decmaxer.init();
		for(int y: grid[i].nonempty){
			ll incy = 0, decy = 0;
			if(y == 0) incy = decy = ddec[0];
			if(y == L) incy = decy = max(ddec[0], dinc[L]);
			
			ll incmax = grid[i-1].prefsum(y-1) + incmaxer.prefmax(y-1);
			incy = max(incy, incmax);
			if(y == L) decy = max(decy, incmax);
			ll decmax = -grid[i].prefsum(y-1) + decmaxer.sufmax(y+1);
			decy = max(decy, decmax);
			ndinc.write(y, incy);
			nddec.write(y, decy);
		}
		ndinc.init(); ddec.init();
		dinc = ndinc, ddec = nddec;
	}

	return max(dinc.allmax(), ddec.allmax());
}

#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
1 Runtime error 770 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 800 ms 2097152 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 777 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 9 ms 3284 KB Output is correct
11 Correct 2 ms 980 KB Output is correct
12 Correct 7 ms 3252 KB Output is correct
13 Correct 1 ms 428 KB Output is correct
14 Correct 8 ms 3304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 9 ms 3284 KB Output is correct
11 Correct 2 ms 980 KB Output is correct
12 Correct 7 ms 3252 KB Output is correct
13 Correct 1 ms 428 KB Output is correct
14 Correct 8 ms 3304 KB Output is correct
15 Correct 7 ms 3280 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 20 ms 4436 KB Output is correct
18 Correct 20 ms 4676 KB Output is correct
19 Correct 20 ms 4524 KB Output is correct
20 Correct 19 ms 4564 KB Output is correct
21 Correct 20 ms 4632 KB Output is correct
22 Correct 37 ms 5968 KB Output is correct
23 Correct 10 ms 3524 KB Output is correct
24 Correct 17 ms 4052 KB Output is correct
25 Correct 8 ms 3412 KB Output is correct
26 Correct 12 ms 3508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 9 ms 3284 KB Output is correct
11 Correct 2 ms 980 KB Output is correct
12 Correct 7 ms 3252 KB Output is correct
13 Correct 1 ms 428 KB Output is correct
14 Correct 8 ms 3304 KB Output is correct
15 Correct 7 ms 3280 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 20 ms 4436 KB Output is correct
18 Correct 20 ms 4676 KB Output is correct
19 Correct 20 ms 4524 KB Output is correct
20 Correct 19 ms 4564 KB Output is correct
21 Correct 20 ms 4632 KB Output is correct
22 Correct 37 ms 5968 KB Output is correct
23 Correct 10 ms 3524 KB Output is correct
24 Correct 17 ms 4052 KB Output is correct
25 Correct 8 ms 3412 KB Output is correct
26 Correct 12 ms 3508 KB Output is correct
27 Correct 599 ms 284576 KB Output is correct
28 Correct 110 ms 25224 KB Output is correct
29 Correct 722 ms 293852 KB Output is correct
30 Correct 757 ms 298700 KB Output is correct
31 Correct 736 ms 298684 KB Output is correct
32 Correct 107 ms 24140 KB Output is correct
33 Correct 743 ms 298584 KB Output is correct
34 Correct 746 ms 298144 KB Output is correct
35 Correct 653 ms 289472 KB Output is correct
36 Correct 686 ms 296912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 777 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 770 ms 2097152 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -