답안 #638171

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
638171 2022-09-04T20:18:41 Z blue 메기 농장 (IOI22_fish) C++17
61 / 100
1000 ms 60332 KB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vi = vector<int>;
using vvi = vector<vi>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
#define sz(x) int(x.size())
 
const int lg = 18;
 
void selfmax(ll& a, ll b)
{
	a = max(a, b);
}
 
const int maxN = 100'000;
 
vpll fish[1 + maxN];
vll fishpref[1 + maxN];
vll tot(1 + maxN, 0);
 
int maxind(int i, int h)
{
	//return max ind of col i so that corresponding height is <= h
	int b = -1;
	for(int e = lg; e >= 0; e--)
	{
		if(b + (1<<e) < sz(fish[i]) && fish[i][b + (1<<e)].first <= h)
			b += (1<<e);
	}
	return b;
}
 
ll htwt(int i, int h)
{
	int j = maxind(i, h);
	if(j == -1)
		return 0;
	else
		return fishpref[i][j];
}
 
ll invhtwt(int i, int h)
{
	return tot[i] - htwt(i, h-1);
}
 
ll max_weights(int N, int M, vi X, vi Y, vi W)
{
	for(int j = 0; j < M; j++)
	{
		X[j]++;
		Y[j]++;
	}
 
	// vpll fish[1+N];
	// vll fishpref[1+N];
 
	for(int j = 0; j < M; j++)
	{
		fish[X[j]].push_back({Y[j], W[j]});
		tot[X[j]] += W[j];
	}
 
	for(int r = 1; r <= N; r++)
	{
		fish[r].push_back({N+1, 0});
		sort(fish[r].begin(), fish[r].end());
 
		if(fish[r][0].first != 1)
		{
			fish[r].insert(fish[r].begin(), {1, 0});
		}
 
		fishpref[r] = vll(sz(fish[r]));
		for(int i = 0; i < sz(fishpref[r]); i++)
		{
			fishpref[r][i] = (i == 0 ? 0 : fishpref[r][i-1]) + fish[r][i].second;
		}
	}
 
	fish[0] = vpll{{0, 0}};
	fishpref[0] = vll{0};
 
	vvll inc(1+N), dec(1+N);
	vvll incpref(1+N), decpref(1+N); //pref inc dec mx
	inc[0] = dec[0] = vll{0};
 
	// cerr << "done\n";
 
	for(int r = 1; r <= N; r++)
	{
		incpref[r-1] = inc[r-1];
		decpref[r-1] = dec[r-1];
		for(int i = 1; i < sz(fish[r-1]); i++)
		{
			incpref[r-1][i] = max(incpref[r-1][i-1], inc[r-1][i]);
			decpref[r-1][i] = max(decpref[r-1][i-1], dec[r-1][i]);
		}
 
 
		inc[r] = dec[r] = vll(sz(fish[r]), 0);
 
 
 
 
 
 
		vll Csuff;
		if(r >= 2)
		{
			Csuff = vll(sz(fish[r-2]));
			for(int j = sz(fish[r-2])-1; j >= 0; j--)
			{
				Csuff[j] = max(inc[r-2][j], dec[r-2][j]) + htwt(r-1, fish[r-2][j].first - 1);
				if(j+1 < sz(fish[r-2]))
					selfmax(Csuff[j], Csuff[j+1]);
			}
		}


		ll Dmx = 0;
		ll Did = -1;

 
		for(int i = 0; i < sz(fish[r]); i++)
		{
			ll basic = 0;
 
			ll Bcatch = 0;
			int Bk = -1;

			ll constD = -invhtwt(r-1, fish[r][i].first) + tot[r-1];
 
			if(r >= 2)
			{
				//A
				selfmax(basic, max(inc[r-2][0], dec[r-2][0]) + htwt(r-1, fish[r][i].first - 1));
 
 
 
 
 
				int ploci = maxind(r-2, fish[r][i].first);
 
				//B
				/*for(int j = 0; j <= ploci; j++)
				{
					// cerr << "j = " << j << " out of (exc) " << sz(fish[r-2]) << '\n';
					ll medcatch = 0;
					for(int k = 0; k < sz(fish[r-1]); k++)
						if(fish[r-1][k].first <= fish[r][i].first - 1)
							medcatch += fish[r-1][k].second;
 
					selfmax(basic, max(inc[r-2][j], dec[r-2][j]) + medcatch);
				}*/
				while(Bk+1 < sz(fish[r-1]) && fish[r-1][Bk+1].first <= fish[r][i].first - 1)
				{
					Bk++;
					Bcatch += fish[r-1][Bk].second;
				}
				selfmax(basic, Bcatch + max(incpref[r-2][ploci], decpref[r-2][ploci]));
				// for(int j = 0; j <= ploci; j++)
				// {
				// 	// cerr << "j = " << j << " out of (exc) " << sz(fish[r-2]) << '\n';
					
 
				// 	// selfmax(basic, max(inc[r-2][j], dec[r-2][j]) + Bcatch);
				// 	selfmax(basic, )
				// }
 
 
				//C
				// for(int j = sz(fish[r-2])-1; j > ploci; j--)
				// {
				// 	// cerr << "j = " << j << " out of (exc) " << sz(fish[r-2]) << '\n';
				// 	ll medcatch = 0;
				// 	for(int k = 0; k < sz(fish[r-1]); k++)
				// 		if(fish[r-1][k].first <= fish[r-2][j].first - 1)
				// 			medcatch += fish[r-1][k].second;
 
				// 	selfmax(basic, max(inc[r-2][j], dec[r-2][j]) + medcatch);
				// }
 
				if(ploci+1 < sz(fish[r-2]))
				{
					// cerr << ploci << " : " << sz(fish[r-2]) << '\n';
					selfmax(basic, Csuff[ploci+1]);
				}
			}
 
			selfmax(inc[r][i], basic);
			selfmax(dec[r][i], basic);

			//type D transition

			while(Did+1 < sz(fish[r-1]) && fish[r-1][Did+1].first <= fish[r][i].first)
			{
				Did++;
				Dmx = max(Dmx, inc[r-1][Did] - (Did >= 1 ? fishpref[r-1][Did-1] : 0));
			}
 
			// for(int j = 0; j < sz(fish[r-1]) && fish[r-1][j].first <= fish[r][i].first; j++)
			// {
			// 	// cerr << "j = " << j << " , " << fish[r-1][j].first-1 << '\n';
			// 	// ll ext = 0;
				
			// 	// cerr << "case 1\n";
			// 	// for(int k = j; k < sz(fish[r-1]); k++)
			// 	// {
			// 	// 	if(fish[r-1][k].first <= fish[r][i].first - 1)
			// 	// 		ext += fish[r-1][k].second;
			// 	// }
			// 	// if(j >= 1)
			// 	// 	ext -= fishpref[r-1][j-1];
			// 	// ll ext = -invhtwt(r-1, fish[r][i].first);
 
			// 	selfmax(inc[r][i], inc[r-1][j] - (j >= 1 ? fishpref[r-1][j-1] : 0) + constD);
			// 	selfmax(dec[r][i], inc[r-1][j] - (j >= 1 ? fishpref[r-1][j-1] : 0) + constD);
			// }
			selfmax(inc[r][i], Dmx + constD);
			selfmax(dec[r][i], Dmx + constD);

		}

		for(int i = sz(fish[r])-1; i >= 0; i--)
		{
 
			for(int j = sz(fish[r-1])-1; j >= 0 && fish[r-1][j].first > fish[r][i].first; j--)
			{
				
				// ll ext = 0;
				// // cerr << "case 2\n";
				// for(int k = i; k < sz(fish[r]); k++)
				// {
				// 	if(fish[r][k].first <= fish[r-1][j].first - 1)
				// 		ext += fish[r][k].second;
				// }
 
				ll ext = tot[r];
				if(i >= 1)
					ext -= fishpref[r][i-1];
				ext -= invhtwt(r, fish[r-1][j].first);
 
				selfmax(dec[r][i], dec[r-1][j] + ext);
				// cerr << r << ' ' << fish[r][i].first-1 << " : " << inc[r][i] << ' ' << dec[r][i] << '\n';		
		
			}
		}
	}
 
	ll res = 0;
	for(vll x : inc)
		for(ll y : x)
			res = max(res, y);
	for(vll x : dec)
		for(ll y : x)
			res = max(res, y);
 
	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 39484 KB Output is correct
2 Correct 128 ms 44096 KB Output is correct
3 Correct 91 ms 35464 KB Output is correct
4 Correct 97 ms 35512 KB Output is correct
5 Correct 238 ms 59704 KB Output is correct
6 Correct 343 ms 60332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Execution timed out 1097 ms 32628 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 35524 KB Output is correct
2 Correct 88 ms 35432 KB Output is correct
3 Correct 97 ms 33780 KB Output is correct
4 Correct 106 ms 36320 KB Output is correct
5 Correct 158 ms 37848 KB Output is correct
6 Correct 120 ms 37804 KB Output is correct
7 Correct 140 ms 37760 KB Output is correct
8 Correct 125 ms 37800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 5 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 4 ms 5716 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 4 ms 5844 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 3 ms 5844 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 4 ms 5716 KB Output is correct
14 Correct 4 ms 5844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 5 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 4 ms 5716 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 4 ms 5844 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 3 ms 5844 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 4 ms 5716 KB Output is correct
14 Correct 4 ms 5844 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 6 ms 5972 KB Output is correct
17 Correct 163 ms 9536 KB Output is correct
18 Correct 185 ms 10040 KB Output is correct
19 Correct 107 ms 9820 KB Output is correct
20 Correct 111 ms 9952 KB Output is correct
21 Correct 110 ms 9904 KB Output is correct
22 Correct 406 ms 14008 KB Output is correct
23 Correct 11 ms 6612 KB Output is correct
24 Correct 60 ms 8252 KB Output is correct
25 Correct 3 ms 5932 KB Output is correct
26 Correct 11 ms 6504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5716 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 5 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 4 ms 5716 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 4 ms 5844 KB Output is correct
10 Correct 4 ms 5972 KB Output is correct
11 Correct 3 ms 5844 KB Output is correct
12 Correct 3 ms 5972 KB Output is correct
13 Correct 4 ms 5716 KB Output is correct
14 Correct 4 ms 5844 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 6 ms 5972 KB Output is correct
17 Correct 163 ms 9536 KB Output is correct
18 Correct 185 ms 10040 KB Output is correct
19 Correct 107 ms 9820 KB Output is correct
20 Correct 111 ms 9952 KB Output is correct
21 Correct 110 ms 9904 KB Output is correct
22 Correct 406 ms 14008 KB Output is correct
23 Correct 11 ms 6612 KB Output is correct
24 Correct 60 ms 8252 KB Output is correct
25 Correct 3 ms 5932 KB Output is correct
26 Correct 11 ms 6504 KB Output is correct
27 Correct 7 ms 6740 KB Output is correct
28 Execution timed out 1091 ms 21796 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 35524 KB Output is correct
2 Correct 88 ms 35432 KB Output is correct
3 Correct 97 ms 33780 KB Output is correct
4 Correct 106 ms 36320 KB Output is correct
5 Correct 158 ms 37848 KB Output is correct
6 Correct 120 ms 37804 KB Output is correct
7 Correct 140 ms 37760 KB Output is correct
8 Correct 125 ms 37800 KB Output is correct
9 Correct 148 ms 40904 KB Output is correct
10 Correct 107 ms 25132 KB Output is correct
11 Correct 190 ms 44544 KB Output is correct
12 Correct 3 ms 5716 KB Output is correct
13 Correct 3 ms 5716 KB Output is correct
14 Correct 3 ms 5716 KB Output is correct
15 Correct 3 ms 5716 KB Output is correct
16 Correct 4 ms 5716 KB Output is correct
17 Correct 4 ms 5716 KB Output is correct
18 Correct 107 ms 35456 KB Output is correct
19 Correct 89 ms 35448 KB Output is correct
20 Correct 89 ms 35516 KB Output is correct
21 Correct 91 ms 35400 KB Output is correct
22 Correct 200 ms 42496 KB Output is correct
23 Correct 217 ms 50864 KB Output is correct
24 Correct 255 ms 51408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 39484 KB Output is correct
2 Correct 128 ms 44096 KB Output is correct
3 Correct 91 ms 35464 KB Output is correct
4 Correct 97 ms 35512 KB Output is correct
5 Correct 238 ms 59704 KB Output is correct
6 Correct 343 ms 60332 KB Output is correct
7 Correct 3 ms 5716 KB Output is correct
8 Execution timed out 1097 ms 32628 KB Time limit exceeded
9 Halted 0 ms 0 KB -