답안 #829455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
829455 2023-08-18T10:57:09 Z caganyanmaz 메기 농장 (IOI22_fish) C++17
18 / 100
138 ms 36752 KB
#include <bits/stdc++.h>
#define pb push_back
#define int int64_t
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

using namespace std;
#ifdef DEBUGGING
#include "../debug.h"
#else
#define debug(x...) void(42)
#endif 

int n;
int m;
constexpr static int INF = 1e17;
constexpr static int MXN = 1e5 + 5;
constexpr static int INC = 0;
constexpr static int DEC = 1;
constexpr static int ZER = 2;
vector<array<int, 3>> dp[MXN]; // Y position, height (in fish units), state (increasing, decreasing, zeroed (returns previous height)
vector<int> lpf[MXN];
vector<int> rpf[MXN];
vector<array<int, 2>> f[MXN]; // X pos, Y pos, height/weight

int max_weights(int32_t N, int32_t M, vector<int32_t> x, vector<int32_t> y, vector<int32_t> w) 
{
	n = N;
	m = M;
	for (int i = 0; i < m; i++)
		f[x[i]].pb({y[i], w[i]});
	for (int i = 0; i < n; i++)
		sort(f[i].begin(), f[i].end());
	dp[0]= vector<array<int,3>>(f[0].size()+1, array<int, 3>({0, 0, 0}));
	for (int i = 0; i <= f[0].size(); i++)
	{ dp[0][i][INC] = 0; dp[0][i][DEC] = 0;
		dp[0][i][ZER] = -INF;
	}
	for (int i = 1; i < n; i++)
	{
		dp[i]= vector<array<int,3>>(f[i].size()+1, array<int, 3>({0, 0, 0}));
		// Calculate prefixes
		// Calculate INC -> INC
		int k = 0; // Previous level
		for (int j = 0; j <= f[i].size(); j++) // Current level
		{
			if (j > 0)
				dp[i][j][INC] = max(dp[i][j][INC], dp[i][j-1][INC]);
			while (k <= f[i-1].size() && (j == f[i].size() || (k < f[i-1].size() && f[i][j][0] > f[i-1][k][0])))
			{
				dp[i][j][INC] = max(dp[i][j][INC], dp[i-1][k][INC]);
				if (k < f[i-1].size())
					dp[i][j][INC] += f[i-1][k][1];
				k++;
			}
		}
		// Calculating DEC -> DEC
		k = f[i-1].size(); // Previous level
		for (int j = f[i].size(); j >= 0; j--)
		{
			
			if (j < f[i].size())
				dp[i][j][DEC] = max(dp[i][j][DEC], dp[i][j+1][DEC]);
			while (k >= 0 && (k == f[i-1].size() || (j != f[i].size() && f[i-1][k][0] > f[i][j][0])))
			{
				dp[i][j][DEC] = max(dp[i][j][DEC], dp[i-1][k][DEC]);
				k--;
			}
			if (j < f[i].size())
				dp[i][j][DEC] += f[i][j][1];
		}

		// Calculating INC -> DEC (it's only reasonable at the peak
		// Calculating DEC -> ZER
		k = 0;
		for (int j = 0; j <= f[i].size(); j++)
		{
			while (k <= f[i-1].size() && (k == f[i-1].size() || (j != f[i].size() && f[i-1][k][0] > f[i][j][0])))
			{
				debug(i, j, k);
				dp[i][j][ZER] = max(dp[i][j][ZER], dp[i-1][k][DEC]);
				k++;
			}
		}
		dp[i][f[i].size()][ZER] = max(dp[i][f[i].size()][ZER], dp[i-1][f[i-1].size()][DEC]);
		// Calculating ZER -> INC
		vector<int> best_upper(f[i].size()+1);
		k = f[i-1].size();
		int change = 0;
		for (int j = 0; j < f[i-1].size(); j++)
			change += f[i-1][j][1];
		for (int j = f[i].size(); j>= 0; j--)
		{
			if (j < f[i].size())
				best_upper[j] = max(best_upper[j], best_upper[j+1]);
			while (k >= 0 && (k == f[i-1].size() || f[i-1][k][0] > f[i-1][j][0]))
			{
				best_upper[j] = max(best_upper[j], dp[i-1][k][ZER] + change);
				k--;
				if (k >= 0)
					change -= f[i-1][k][1];
			}
		}
		k = 0;
		int sum = 0;
		int prev_val = 0;
		for (int j = 0; j <= f[i].size(); j++)
		{
			dp[i][j][INC] = max(dp[i][j][INC], best_upper[j]);
			while (k < f[i-1].size() && (j == f[i].size() || f[i-1][k][0] < f[i][j][0]))
			{
				prev_val = max(prev_val, dp[i-1][k][ZER]);
				sum += f[i-1][k][1];
				dp[i][j][INC] = max({dp[i][j][INC], prev_val + sum});
				k++;
			}
		}
		// Calcaulating INC to DEC (max only)
		dp[i][f[i].size()][DEC] = max(dp[i][f[i].size()][DEC], dp[i][f[i].size()][INC]);
		debug(i, f[i].size(), dp[i].size(), dp[i]);
	}
	int best = 0;
	for (int i = 0; i < dp[n-1].size(); i++)
	{
		debug(i, dp[n-1][i][DEC], dp[n-1][i][INC], dp[n-1][i][ZER]);
		best = max({best, dp[n-1][i][DEC], dp[n-1][i][INC], dp[n-1][i][ZER]});
	}
	return best;



}

Compilation message

fish.cpp: In function 'int64_t max_weights(int32_t, int32_t, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for (int i = 0; i <= f[0].size(); i++)
      |                  ~~^~~~~~~~~~~~~~
fish.cpp:45:21: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |   for (int j = 0; j <= f[i].size(); j++) // Current level
      |                   ~~^~~~~~~~~~~~~~
fish.cpp:49:13: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |    while (k <= f[i-1].size() && (j == f[i].size() || (k < f[i-1].size() && f[i][j][0] > f[i-1][k][0])))
      |           ~~^~~~~~~~~~~~~~~~
fish.cpp:49:36: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |    while (k <= f[i-1].size() && (j == f[i].size() || (k < f[i-1].size() && f[i][j][0] > f[i-1][k][0])))
      |                                  ~~^~~~~~~~~~~~~~
fish.cpp:49:57: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |    while (k <= f[i-1].size() && (j == f[i].size() || (k < f[i-1].size() && f[i][j][0] > f[i-1][k][0])))
      |                                                       ~~^~~~~~~~~~~~~~~
fish.cpp:52:11: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     if (k < f[i-1].size())
      |         ~~^~~~~~~~~~~~~~~
fish.cpp:62:10: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |    if (j < f[i].size())
      |        ~~^~~~~~~~~~~~~
fish.cpp:64:24: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |    while (k >= 0 && (k == f[i-1].size() || (j != f[i].size() && f[i-1][k][0] > f[i][j][0])))
      |                      ~~^~~~~~~~~~~~~~~~
fish.cpp:64:47: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |    while (k >= 0 && (k == f[i-1].size() || (j != f[i].size() && f[i-1][k][0] > f[i][j][0])))
      |                                             ~~^~~~~~~~~~~~~~
fish.cpp:69:10: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |    if (j < f[i].size())
      |        ~~^~~~~~~~~~~~~
fish.cpp:76:21: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |   for (int j = 0; j <= f[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~
fish.cpp:78:13: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |    while (k <= f[i-1].size() && (k == f[i-1].size() || (j != f[i].size() && f[i-1][k][0] > f[i][j][0])))
      |           ~~^~~~~~~~~~~~~~~~
fish.cpp:78:36: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |    while (k <= f[i-1].size() && (k == f[i-1].size() || (j != f[i].size() && f[i-1][k][0] > f[i][j][0])))
      |                                  ~~^~~~~~~~~~~~~~~~
fish.cpp:78:59: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |    while (k <= f[i-1].size() && (k == f[i-1].size() || (j != f[i].size() && f[i-1][k][0] > f[i][j][0])))
      |                                                         ~~^~~~~~~~~~~~~~
fish.cpp:90:21: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |   for (int j = 0; j < f[i-1].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~
fish.cpp:94:10: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |    if (j < f[i].size())
      |        ~~^~~~~~~~~~~~~
fish.cpp:96:24: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |    while (k >= 0 && (k == f[i-1].size() || f[i-1][k][0] > f[i-1][j][0]))
      |                      ~~^~~~~~~~~~~~~~~~
fish.cpp:107:21: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |   for (int j = 0; j <= f[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~
fish.cpp:110:13: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |    while (k < f[i-1].size() && (j == f[i].size() || f[i-1][k][0] < f[i][j][0]))
      |           ~~^~~~~~~~~~~~~~~
fish.cpp:110:35: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |    while (k < f[i-1].size() && (j == f[i].size() || f[i-1][k][0] < f[i][j][0]))
      |                                 ~~^~~~~~~~~~~~~~
fish.cpp:123:20: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::array<long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |  for (int i = 0; i < dp[n-1].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 18840 KB Output is correct
2 Correct 46 ms 20252 KB Output is correct
3 Correct 13 ms 12768 KB Output is correct
4 Correct 15 ms 12768 KB Output is correct
5 Correct 138 ms 34484 KB Output is correct
6 Correct 138 ms 36752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9692 KB Output is correct
2 Correct 69 ms 25456 KB Output is correct
3 Correct 79 ms 26596 KB Output is correct
4 Correct 47 ms 18844 KB Output is correct
5 Correct 63 ms 20344 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 4 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 12 ms 12756 KB Output is correct
11 Correct 17 ms 12772 KB Output is correct
12 Correct 39 ms 18784 KB Output is correct
13 Correct 50 ms 20268 KB Output is correct
14 Correct 39 ms 18780 KB Output is correct
15 Correct 42 ms 19768 KB Output is correct
16 Correct 41 ms 18836 KB Output is correct
17 Correct 41 ms 19732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12764 KB Output is correct
2 Correct 13 ms 12768 KB Output is correct
3 Correct 32 ms 17952 KB Output is correct
4 Correct 27 ms 16492 KB Output is correct
5 Correct 42 ms 22660 KB Output is correct
6 Correct 42 ms 22328 KB Output is correct
7 Correct 48 ms 22588 KB Output is correct
8 Correct 47 ms 22664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9696 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 4 ms 9688 KB Output is correct
6 Correct 4 ms 9588 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9692 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Incorrect 5 ms 9812 KB 1st lines differ - on the 1st token, expected: '799839985182', found: '799098388912'
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9696 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 4 ms 9688 KB Output is correct
6 Correct 4 ms 9588 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9692 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Incorrect 5 ms 9812 KB 1st lines differ - on the 1st token, expected: '799839985182', found: '799098388912'
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9696 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 4 ms 9688 KB Output is correct
6 Correct 4 ms 9588 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 5 ms 9692 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Incorrect 5 ms 9812 KB 1st lines differ - on the 1st token, expected: '799839985182', found: '799098388912'
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12764 KB Output is correct
2 Correct 13 ms 12768 KB Output is correct
3 Correct 32 ms 17952 KB Output is correct
4 Correct 27 ms 16492 KB Output is correct
5 Correct 42 ms 22660 KB Output is correct
6 Correct 42 ms 22328 KB Output is correct
7 Correct 48 ms 22588 KB Output is correct
8 Correct 47 ms 22664 KB Output is correct
9 Correct 45 ms 22668 KB Output is correct
10 Incorrect 41 ms 19728 KB 1st lines differ - on the 1st token, expected: '36454348383152', found: '36364983059693'
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 18840 KB Output is correct
2 Correct 46 ms 20252 KB Output is correct
3 Correct 13 ms 12768 KB Output is correct
4 Correct 15 ms 12768 KB Output is correct
5 Correct 138 ms 34484 KB Output is correct
6 Correct 138 ms 36752 KB Output is correct
7 Correct 6 ms 9692 KB Output is correct
8 Correct 69 ms 25456 KB Output is correct
9 Correct 79 ms 26596 KB Output is correct
10 Correct 47 ms 18844 KB Output is correct
11 Correct 63 ms 20344 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 12 ms 12756 KB Output is correct
17 Correct 17 ms 12772 KB Output is correct
18 Correct 39 ms 18784 KB Output is correct
19 Correct 50 ms 20268 KB Output is correct
20 Correct 39 ms 18780 KB Output is correct
21 Correct 42 ms 19768 KB Output is correct
22 Correct 41 ms 18836 KB Output is correct
23 Correct 41 ms 19732 KB Output is correct
24 Correct 16 ms 12764 KB Output is correct
25 Correct 13 ms 12768 KB Output is correct
26 Correct 32 ms 17952 KB Output is correct
27 Correct 27 ms 16492 KB Output is correct
28 Correct 42 ms 22660 KB Output is correct
29 Correct 42 ms 22328 KB Output is correct
30 Correct 48 ms 22588 KB Output is correct
31 Correct 47 ms 22664 KB Output is correct
32 Correct 5 ms 9684 KB Output is correct
33 Correct 5 ms 9696 KB Output is correct
34 Correct 6 ms 9684 KB Output is correct
35 Correct 5 ms 9684 KB Output is correct
36 Correct 4 ms 9688 KB Output is correct
37 Correct 4 ms 9588 KB Output is correct
38 Correct 6 ms 9684 KB Output is correct
39 Correct 5 ms 9692 KB Output is correct
40 Correct 4 ms 9684 KB Output is correct
41 Incorrect 5 ms 9812 KB 1st lines differ - on the 1st token, expected: '799839985182', found: '799098388912'
42 Halted 0 ms 0 KB -