Submission #659472

# Submission time Handle Problem Language Result Execution time Memory
659472 2022-11-17T20:39:06 Z pere_gil Catfish Farm (IOI22_fish) C++17
37 / 100
948 ms 2097152 KB
#include "fish.h"
#include "bits/stdc++.h"
using namespace std;
             
typedef long long ll;
#define all(x) x.begin(),x.end()

bool deb_inf_col=false;
bool deb_inf_state=false;

void pri(string s, vector<ll> v){
	printf("%s: ",s.c_str());
	for(ll i: v) printf("%lld ",i);
	printf("\n");
}

void pri(string s, vector<ll> a, vector<ll> b){
	printf("%s: ",s.c_str());
	for(ll i: a) printf("%lld ",b[i]);
	printf("\n");
}

void fill(vector<ll> &pier, vector<ll> &fish, vector<ll> &v){
	for(int i=0,j=0;i<(int)pier.size();)
		if(j==(int)fish.size()-1 || (pier[i]>=fish[j] && pier[i]<fish[j+1])){
			v[i]=j;
			i++;
		}
		else j++;
}

ll max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w){

	vector<ll> sum[n],fish[n],temp_pier[n],pier[n];
	vector<pair<ll,ll>> inf_fish[n];
	for(int i=0;i<m;i++) inf_fish[x[i]].push_back({y[i]+1,w[i]});
	for(int i=0;i<n;i++){
		sum[i].push_back(0); fish[i].push_back(0); temp_pier[i].push_back(0);

		sort(all(inf_fish[i]));
		
		for(int j=0;j<(int)inf_fish[i].size();j++){
			fish[i].push_back(inf_fish[i][j].first);
			sum[i].push_back(inf_fish[i][j].second);
			if(i) temp_pier[i-1].push_back(inf_fish[i][j].first);
			if(i!=n-1) temp_pier[i+1].push_back(inf_fish[i][j].first);
		}
	}
	for(int i=0;i<n;i++){

		sort(all(temp_pier[i]));
		pier[i].push_back(temp_pier[i][0]);
		for(int j=1;j<(int)temp_pier[i].size();j++)
			if(temp_pier[i][j]!=temp_pier[i][j-1])
				pier[i].push_back(temp_pier[i][j]);

		for(int j=1;j<(int)fish[i].size();j++)
			sum[i][j]+=sum[i][j-1];
	}
	vector<ll> left[n],me[n],right[n];
	for(int i=0;i<n;i++){
		left[i].assign(pier[i].size(),0);
		me[i].assign(pier[i].size(),0);
		right[i].assign(pier[i].size(),0);

		if(i) fill(pier[i],fish[i-1],left[i]);
		fill(pier[i],fish[i],me[i]);
		if(i!=n-1) fill(pier[i],fish[i+1],right[i]);
	}

	if(deb_inf_col){
		for(int i=0;i<n;i++){
			printf("%d:\n",i);
			pri("fish:",fish[i]);
			pri("sum:",sum[i]);
			pri("pier:",pier[i]);
			if(i) pri("left:",left[i],fish[i-1]);
			pri("me:",me[i],fish[i]);
			if(i!=n-1) pri("right:",right[i],fish[i+1]);
			printf("\n");
		}
	}
	
	vector<vector<vector<ll>>> dp[n];
	for(int i=0;i<n;i++){
		int j=(i>=2) ? pier[i-2].size() : 1;
		int k=(i) ? pier[i-1].size() : 1;
		int l=pier[i].size();
		dp[i].assign(j,vector<vector<ll>>(k,vector<ll>(l,0)));
	}
	
	for(int i=1;i<n;i++){
		int size_j=(i>=2) ? pier[i-2].size() : 1;
		for(int j=0;j<size_j;j++){
			for(int k=0;k<(int)pier[i-1].size();k++){
				
				ll prev=0;
				int size_l=(i>=3) ? pier[i-3].size() : 1;
				int pos=0;
				for(int l=0;l<size_l;l++){
					ll aux=dp[i-1][l][j][k];
					if(aux>=prev){
						prev=aux;
						pos=l;
					}
				}
				
				for(int l=0;l<(int)pier[i].size();l++){
					dp[i][j][k][l]+=prev;

					if(deb_inf_state){
						if(i>=2) printf("%d: %lld\n",i-2,pier[i-2][j]);
						printf("%d: %lld\n",i-1,pier[i-1][k]);
						printf("%d: %lld\n",i,pier[i][l]);
						printf("prev: %lld at %d %d %d\n",prev,pos,j,k);
					}
					
					if(pier[i-1][k]>=pier[i][l]){
						ll up=sum[i][right[i-1][k]];
						ll down=sum[i][me[i][l]];

						if(deb_inf_state){
							printf("sum in %d\n",i);
							printf("up: %lld\n",fish[i][right[i-1][k]]);
							printf("down: %lld\n",fish[i][me[i][l]]);
							printf("-> %lld - %lld = %lld\n",up,down,up-down);
						}
						
						dp[i][j][k][l]+=sum[i][right[i-1][k]]-sum[i][me[i][l]];
					}
					else{
						if(deb_inf_state) printf("sum in %d\n",i-1);
						if(i>=2 && pier[i-2][j]>=pier[i-1][k]){
							ll up=sum[i-1][left[i][l]];
							ll down=sum[i-1][right[i-2][j]];

							if(deb_inf_state){
								printf("tallest in middle: %d\n",i-2);
								printf("up: %lld\n",fish[i-1][left[i][l]]);
								printf("down: %lld\n",fish[i-1][right[i-2][j]]);
								printf("-> %lld - %lld = %lld\n",up,down,up-down);
							}
							
							dp[i][j][k][l]+=sum[i-1][left[i][l]]-sum[i-1][right[i-2][j]];
						}
						else{
							ll up=sum[i-1][left[i][l]];
							ll down=sum[i-1][me[i-1][k]];
							
							if(deb_inf_state){
								printf("tallest in back: %d\n",i-1);
								printf("up: %lld\n",fish[i-1][left[i][l]]);
								printf("down: %lld\n",fish[i-1][me[i-1][k]]);
								printf("-> %lld - %lld = %lld\n",up,down,up-down);
							}
							
							dp[i][j][k][l]+=sum[i-1][left[i][l]]-sum[i-1][me[i-1][k]];
						}
					}
					if(deb_inf_state){
						printf("----> %lld\n",dp[i][j][k][l]);
						printf("\n");
					}
				}
			}
		}
	}
	
	ll res=0;
	int size_i=(n>=3) ? pier[n-3].size() : 1;
	for(int i=0;i<size_i;i++)
		for(int j=0;j<(int)pier[n-2].size();j++)
			for(int k=0;k<(int)pier[n-1].size();k++)
				res=max(res,dp[n-1][i][j][k]);
	
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 135 ms 67436 KB Output is correct
2 Correct 164 ms 79016 KB Output is correct
3 Correct 79 ms 52764 KB Output is correct
4 Correct 80 ms 52684 KB Output is correct
5 Runtime error 933 ms 2097152 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 216 KB Output is correct
2 Runtime error 831 ms 2097152 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 88 ms 52684 KB Output is correct
2 Correct 90 ms 52712 KB Output is correct
3 Correct 145 ms 64808 KB Output is correct
4 Correct 145 ms 63656 KB Output is correct
5 Correct 219 ms 81808 KB Output is correct
6 Correct 193 ms 81696 KB Output is correct
7 Correct 233 ms 81612 KB Output is correct
8 Correct 214 ms 81732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 9 ms 3252 KB Output is correct
11 Correct 4 ms 1464 KB Output is correct
12 Correct 5 ms 1992 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 4 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 9 ms 3252 KB Output is correct
11 Correct 4 ms 1464 KB Output is correct
12 Correct 5 ms 1992 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 4 ms 1236 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 321 ms 107672 KB Output is correct
17 Runtime error 948 ms 2097152 KB Execution killed with signal 9
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 9 ms 3252 KB Output is correct
11 Correct 4 ms 1464 KB Output is correct
12 Correct 5 ms 1992 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 4 ms 1236 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 321 ms 107672 KB Output is correct
17 Runtime error 948 ms 2097152 KB Execution killed with signal 9
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 88 ms 52684 KB Output is correct
2 Correct 90 ms 52712 KB Output is correct
3 Correct 145 ms 64808 KB Output is correct
4 Correct 145 ms 63656 KB Output is correct
5 Correct 219 ms 81808 KB Output is correct
6 Correct 193 ms 81696 KB Output is correct
7 Correct 233 ms 81612 KB Output is correct
8 Correct 214 ms 81732 KB Output is correct
9 Correct 309 ms 111368 KB Output is correct
10 Correct 220 ms 61076 KB Output is correct
11 Correct 377 ms 125344 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 82 ms 52644 KB Output is correct
19 Correct 84 ms 52732 KB Output is correct
20 Correct 91 ms 52728 KB Output is correct
21 Correct 82 ms 52652 KB Output is correct
22 Correct 383 ms 122060 KB Output is correct
23 Correct 659 ms 221500 KB Output is correct
24 Correct 732 ms 249420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 67436 KB Output is correct
2 Correct 164 ms 79016 KB Output is correct
3 Correct 79 ms 52764 KB Output is correct
4 Correct 80 ms 52684 KB Output is correct
5 Runtime error 933 ms 2097152 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -