답안 #629589

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
629589 2022-08-14T17:13:21 Z jh05013 메기 농장 (IOI22_fish) C++17
84 / 100
1000 ms 142168 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<<"]";}

template <typename T>
struct Compress{
	int n; vector<T> arr;
	void add(T x){arr.push_back(x); n++;}
	void init(){sort(entire(arr));}
	int lb(T x){return lower_bound(entire(arr), x) - arr.begin();}
	int ub(T x){return upper_bound(entire(arr), x) - arr.begin();}
};

// 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, L;
	vector<int> idxs;
	Compress<int> C;
	vector<T> val, sum, prefm, sufm;
	
	SparseArray(int N): n{N} {}

	map<int, T> M;
	void write(int i, T x){M[i] = x;}
	void init(){
		L = sz(M);
		if(!L) return;
		sum.resize(L), prefm.resize(L), sufm.resize(L);
		for(auto [i, x]: M) C.add(i), val.push_back(x);
		C.init();
		sum[0] = prefm[0] = val[0];
		for(int i=1; i<L; i++){
			sum[i] = sum[i-1]+val[i];
			prefm[i] = max(prefm[i-1], val[i]);
		}
		idxs = C.arr;
		sufm[L-1] = val[L-1];
		for(int i=L-2; i>=0; i--)
			sufm[i] = max(sufm[i+1], val[i]);
	}

	T operator[](int i){
		int idx = C.lb(i);
		if(idx == L || C.arr[idx] != i) return 0;
		return val[idx];
	}
	T prefsum(int i){
		i = C.ub(i)-1;
		return i>=0? sum[i] : (T)0;
	}
	T getsum(int l, int r){return prefsum(r) - prefsum(l);}
	T allmax(){return prefm.back();}
	T prefmax(int i){
		i = C.ub(i)-1;
		return i>=0? prefm[i] : (T)0;
	}
	T sufmax(int i){
		i = C.lb(i);
		return i<L? 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].idxs){
			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].idxs){
			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(), nddec.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
# 결과 실행 시간 메모리 Grader output
1 Correct 341 ms 74304 KB Output is correct
2 Correct 378 ms 85604 KB Output is correct
3 Correct 243 ms 51928 KB Output is correct
4 Correct 235 ms 51864 KB Output is correct
5 Correct 962 ms 128480 KB Output is correct
6 Correct 800 ms 89804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 536 ms 117396 KB Output is correct
3 Correct 720 ms 142168 KB Output is correct
4 Correct 313 ms 75604 KB Output is correct
5 Correct 388 ms 87268 KB Output is correct
6 Correct 0 ms 300 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 230 ms 51912 KB Output is correct
11 Correct 244 ms 51888 KB Output is correct
12 Correct 384 ms 92500 KB Output is correct
13 Correct 481 ms 108120 KB Output is correct
14 Correct 354 ms 83152 KB Output is correct
15 Correct 409 ms 92572 KB Output is correct
16 Correct 347 ms 83228 KB Output is correct
17 Correct 404 ms 92380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 51944 KB Output is correct
2 Correct 260 ms 51944 KB Output is correct
3 Correct 238 ms 48976 KB Output is correct
4 Correct 299 ms 53264 KB Output is correct
5 Correct 264 ms 54392 KB Output is correct
6 Correct 272 ms 55076 KB Output is correct
7 Correct 273 ms 55660 KB Output is correct
8 Correct 293 ms 55608 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 3 ms 632 KB Output is correct
17 Correct 66 ms 6380 KB Output is correct
18 Correct 65 ms 6720 KB Output is correct
19 Correct 65 ms 6604 KB Output is correct
20 Correct 65 ms 6552 KB Output is correct
21 Correct 59 ms 6476 KB Output is correct
22 Correct 146 ms 12756 KB Output is correct
23 Correct 11 ms 1620 KB Output is correct
24 Correct 39 ms 4484 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 10 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 3 ms 632 KB Output is correct
17 Correct 66 ms 6380 KB Output is correct
18 Correct 65 ms 6720 KB Output is correct
19 Correct 65 ms 6604 KB Output is correct
20 Correct 65 ms 6552 KB Output is correct
21 Correct 59 ms 6476 KB Output is correct
22 Correct 146 ms 12756 KB Output is correct
23 Correct 11 ms 1620 KB Output is correct
24 Correct 39 ms 4484 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 10 ms 1492 KB Output is correct
27 Correct 10 ms 2132 KB Output is correct
28 Correct 406 ms 29944 KB Output is correct
29 Correct 606 ms 42152 KB Output is correct
30 Correct 485 ms 40516 KB Output is correct
31 Correct 527 ms 40416 KB Output is correct
32 Correct 515 ms 42212 KB Output is correct
33 Correct 513 ms 40416 KB Output is correct
34 Correct 524 ms 40548 KB Output is correct
35 Correct 154 ms 16984 KB Output is correct
36 Correct 559 ms 41796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 51944 KB Output is correct
2 Correct 260 ms 51944 KB Output is correct
3 Correct 238 ms 48976 KB Output is correct
4 Correct 299 ms 53264 KB Output is correct
5 Correct 264 ms 54392 KB Output is correct
6 Correct 272 ms 55076 KB Output is correct
7 Correct 273 ms 55660 KB Output is correct
8 Correct 293 ms 55608 KB Output is correct
9 Correct 360 ms 63180 KB Output is correct
10 Correct 216 ms 33668 KB Output is correct
11 Correct 399 ms 66000 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 234 ms 52024 KB Output is correct
19 Correct 229 ms 51928 KB Output is correct
20 Correct 237 ms 51872 KB Output is correct
21 Correct 238 ms 51896 KB Output is correct
22 Correct 417 ms 63372 KB Output is correct
23 Correct 501 ms 75164 KB Output is correct
24 Correct 628 ms 77496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 341 ms 74304 KB Output is correct
2 Correct 378 ms 85604 KB Output is correct
3 Correct 243 ms 51928 KB Output is correct
4 Correct 235 ms 51864 KB Output is correct
5 Correct 962 ms 128480 KB Output is correct
6 Correct 800 ms 89804 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 536 ms 117396 KB Output is correct
9 Correct 720 ms 142168 KB Output is correct
10 Correct 313 ms 75604 KB Output is correct
11 Correct 388 ms 87268 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 230 ms 51912 KB Output is correct
17 Correct 244 ms 51888 KB Output is correct
18 Correct 384 ms 92500 KB Output is correct
19 Correct 481 ms 108120 KB Output is correct
20 Correct 354 ms 83152 KB Output is correct
21 Correct 409 ms 92572 KB Output is correct
22 Correct 347 ms 83228 KB Output is correct
23 Correct 404 ms 92380 KB Output is correct
24 Correct 250 ms 51944 KB Output is correct
25 Correct 260 ms 51944 KB Output is correct
26 Correct 238 ms 48976 KB Output is correct
27 Correct 299 ms 53264 KB Output is correct
28 Correct 264 ms 54392 KB Output is correct
29 Correct 272 ms 55076 KB Output is correct
30 Correct 273 ms 55660 KB Output is correct
31 Correct 293 ms 55608 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 3 ms 724 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 3 ms 632 KB Output is correct
48 Correct 66 ms 6380 KB Output is correct
49 Correct 65 ms 6720 KB Output is correct
50 Correct 65 ms 6604 KB Output is correct
51 Correct 65 ms 6552 KB Output is correct
52 Correct 59 ms 6476 KB Output is correct
53 Correct 146 ms 12756 KB Output is correct
54 Correct 11 ms 1620 KB Output is correct
55 Correct 39 ms 4484 KB Output is correct
56 Correct 2 ms 596 KB Output is correct
57 Correct 10 ms 1492 KB Output is correct
58 Correct 10 ms 2132 KB Output is correct
59 Correct 406 ms 29944 KB Output is correct
60 Correct 606 ms 42152 KB Output is correct
61 Correct 485 ms 40516 KB Output is correct
62 Correct 527 ms 40416 KB Output is correct
63 Correct 515 ms 42212 KB Output is correct
64 Correct 513 ms 40416 KB Output is correct
65 Correct 524 ms 40548 KB Output is correct
66 Correct 154 ms 16984 KB Output is correct
67 Correct 559 ms 41796 KB Output is correct
68 Correct 360 ms 63180 KB Output is correct
69 Correct 216 ms 33668 KB Output is correct
70 Correct 399 ms 66000 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 0 ms 212 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 234 ms 52024 KB Output is correct
78 Correct 229 ms 51928 KB Output is correct
79 Correct 237 ms 51872 KB Output is correct
80 Correct 238 ms 51896 KB Output is correct
81 Correct 417 ms 63372 KB Output is correct
82 Correct 501 ms 75164 KB Output is correct
83 Correct 628 ms 77496 KB Output is correct
84 Execution timed out 1018 ms 100956 KB Time limit exceeded
85 Halted 0 ms 0 KB -