답안 #755228

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
755228 2023-06-09T15:43:30 Z Red_Inside 메기 농장 (IOI22_fish) C++17
100 / 100
482 ms 101872 KB
#include "fish.h"
#include <bits/stdc++.h>

//#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")

#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define o cout<<"BUG"<<endl;
#define FOR(i, j, n) for(int j = i; j < n; ++j)
#define forn(i, j, n) for(int j = i; j <= n; ++j)
#define nfor(i, j, n) for(int j = n; j >= i; --j)
#define all(v) v.begin(), v.end()
//#define ld long double
#define ull unsigned long long
 
using namespace std;
const int maxn=3e5+10,LOG=17,mod=1e9+7;
int block = 650, timer = 0;
const double eps = 1e-9;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 
#define bt(i) (1 << (i))
//#define int ll
const long long inf=2e18;
#define y1 yy
#define pii pair <int, int>

ll n, m, x[300100], y[300100], w[300100];
ll Dp[100100], ans;
vector <pii> vec[100100];
vector <ll> dp[maxn], dp2[maxn], pref[maxn], pos[maxn];

long long max_weights(int N, int M, vector <int> X, vector <int> Y, vector <int> W)
{
	n = N;
	m = M;
	forn(1, i, m)
	{
		x[i] = X[i-1]+1;
		y[i] = Y[i-1]+1;
		w[i] = W[i-1];
		vec[x[i]].pb({y[i], w[i]});
	}
	forn(1, i, n) sort(all(vec[i]));
	forn(1, i, n)
	{
		int u1 = 0;
		int u2 = 0;
		int u3 = 0;
		while(u1 < (int)vec[i-1].size() || u2 < (int)vec[i].size() || 
		u3 < (int)vec[i+1].size())
		{
			int f1 = (u1 < (int)vec[i-1].size() ? vec[i-1][u1].f : n+1);
			int f2 = (u2 < (int)vec[i].size() ? vec[i][u2].f : n+1);
			int f3 = (u3 < (int)vec[i+1].size() ? vec[i+1][u3].f : n+1);
			pos[i].pb(min({f1, f2, f3}));
			pref[i].pb((int)pref[i].size() ? pref[i].back() : 0);
			dp[i].pb(0);
			dp2[i].pb(0);
			if(min({f1, f2, f3}) == f1)
				u1++;
			if(min({f1, f2, f3}) == f2)
			{
				pref[i].back() += vec[i][u2].s;
				u2++;
			}
			if(min({f1, f2, f3}) == f3)
				u3++;
		}
	}
	FOR(0, i, (int)dp[0].size())
	{
		dp[0][i] = -inf;
	}
	forn(1, i, n)
	{
		ll tek = 0;
		if(i >= 2)
		{
			int uk = -1;
			tek = 0;
			FOR(0, j, (int)pos[i].size())
			{
				//pointer <= j
				while(uk+1<(int)pos[i-1].size() && pos[i-1][uk+1] <= pos[i][j])
				{
					++uk;
					tek = max(tek, dp[i-1][uk] - pref[i-1][uk]);
				}
				int temp = upper_bound(all(pos[i-1]), pos[i][j]) - pos[i-1].begin()-1;
				dp[i][j] = max(dp[i][j], (temp>=0?pref[i-1][temp]:0) + tek);
			}
		}
		if(i >= 2)
		{
			int uk = -1;
			tek = 0;
			FOR(0, j, (int)pos[i].size())
			{
				//pointer <= j
				while(uk+1 < (int)pos[i-2].size() && pos[i-2][uk+1] <= pos[i][j])
				{
					++uk;
					tek = max(tek, dp[i-2][uk]);
				}
				int temp = upper_bound(all(pos[i-1]), pos[i][j]) - pos[i-1].begin()-1;
				dp[i][j] = max(dp[i][j], (temp>=0?pref[i-1][temp]:0) + tek);
			}
		}
		if(i >= 3)
		{
			tek = 0;
			int uk = (int)pos[i-2].size();
			for(int j = (int)pos[i].size() - 1; j >= 0; --j)
			{
				//pointer > j
				while(uk-1 >= 0 && pos[i-2][uk-1] > pos[i][j])
				{
					uk--;
					int temp = upper_bound(all(pos[i-1]), pos[i-2][uk])-pos[i-1].begin()-1;
					tek = max(tek, dp[i-2][uk] + (temp>=0?pref[i-1][temp]:0));
				}
				dp[i][j] = max(dp[i][j], tek);
			}
		}
		if(i >= 3)
		{
			FOR(0, j, (int)pos[i].size())
			{
				int temp = upper_bound(all(pos[i-1]), pos[i][j])-pos[i-1].begin()-1;
				dp[i][j] = max(dp[i][j], Dp[i-3]+(temp>=0?pref[i-1][temp]:0));
			}
		}
		if(i >= 2)
		{
			tek = -inf;
			int uk = (int)pos[i-1].size();
			for(int j = (int)pos[i].size()-1; j >= 0; --j)
			{
				//pointer > j
				while(uk-1>=0 && pos[i-1][uk-1] > pos[i][j])
				{
					uk--;
					int temp = upper_bound(all(pos[i]), pos[i-1][uk]) - pos[i].begin()-1;
					tek = max(tek, max(dp2[i-1][uk],dp[i-1][uk])+(temp>=0?pref[i][temp]:0));
				}
				dp2[i][j] = max(dp2[i][j], tek - pref[i][j]);
				int temp = upper_bound(all(pos[i+1]), pos[i][j])-pos[i+1].begin()-1;
				Dp[i] = max(Dp[i], dp2[i][j] + (temp>=0?pref[i+1][temp]:0));
			}
		}
		FOR(0, j, pos[i].size())
		{
			int temp = upper_bound(all(pos[i+1]), pos[i][j])-pos[i+1].begin()-1;
			Dp[i] = max(Dp[i], dp[i][j] + (temp>=0?pref[i+1][temp]:0));
		}
		Dp[i] = max(Dp[i-1], Dp[i]);
		ans = max(ans, Dp[i]);
/*		cout << "FOR " << i << endl;
		FOR(0, j, pos[i].size())
			cout << dp[i][j] << " ";
		cout << endl;*/
//		cout << Dp[i] << " " << i << endl;
	}
	return ans;
}

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:12:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define FOR(i, j, n) for(int j = i; j < n; ++j)
......
  156 |   FOR(0, j, pos[i].size())
      |          ~~~~~~~~~~~~~~~~              
fish.cpp:156:3: note: in expansion of macro 'FOR'
  156 |   FOR(0, j, pos[i].size())
      |   ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 43132 KB Output is correct
2 Correct 85 ms 45824 KB Output is correct
3 Correct 19 ms 31552 KB Output is correct
4 Correct 21 ms 31624 KB Output is correct
5 Correct 351 ms 81516 KB Output is correct
6 Correct 412 ms 100644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 30952 KB Output is correct
2 Correct 136 ms 50540 KB Output is correct
3 Correct 156 ms 54544 KB Output is correct
4 Correct 73 ms 43744 KB Output is correct
5 Correct 81 ms 45832 KB Output is correct
6 Correct 21 ms 30800 KB Output is correct
7 Correct 18 ms 30836 KB Output is correct
8 Correct 17 ms 30756 KB Output is correct
9 Correct 17 ms 30804 KB Output is correct
10 Correct 18 ms 31640 KB Output is correct
11 Correct 18 ms 31600 KB Output is correct
12 Correct 100 ms 46348 KB Output is correct
13 Correct 133 ms 49016 KB Output is correct
14 Correct 93 ms 44496 KB Output is correct
15 Correct 100 ms 42780 KB Output is correct
16 Correct 94 ms 44532 KB Output is correct
17 Correct 96 ms 45664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 31572 KB Output is correct
2 Correct 23 ms 31620 KB Output is correct
3 Correct 57 ms 43708 KB Output is correct
4 Correct 45 ms 40648 KB Output is correct
5 Correct 96 ms 53520 KB Output is correct
6 Correct 90 ms 52856 KB Output is correct
7 Correct 91 ms 53496 KB Output is correct
8 Correct 92 ms 53496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 30824 KB Output is correct
2 Correct 16 ms 30804 KB Output is correct
3 Correct 15 ms 30804 KB Output is correct
4 Correct 16 ms 30804 KB Output is correct
5 Correct 16 ms 30748 KB Output is correct
6 Correct 18 ms 30804 KB Output is correct
7 Correct 19 ms 30804 KB Output is correct
8 Correct 21 ms 30804 KB Output is correct
9 Correct 17 ms 30804 KB Output is correct
10 Correct 17 ms 31036 KB Output is correct
11 Correct 21 ms 30940 KB Output is correct
12 Correct 19 ms 30932 KB Output is correct
13 Correct 19 ms 30868 KB Output is correct
14 Correct 17 ms 30940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 30824 KB Output is correct
2 Correct 16 ms 30804 KB Output is correct
3 Correct 15 ms 30804 KB Output is correct
4 Correct 16 ms 30804 KB Output is correct
5 Correct 16 ms 30748 KB Output is correct
6 Correct 18 ms 30804 KB Output is correct
7 Correct 19 ms 30804 KB Output is correct
8 Correct 21 ms 30804 KB Output is correct
9 Correct 17 ms 30804 KB Output is correct
10 Correct 17 ms 31036 KB Output is correct
11 Correct 21 ms 30940 KB Output is correct
12 Correct 19 ms 30932 KB Output is correct
13 Correct 19 ms 30868 KB Output is correct
14 Correct 17 ms 30940 KB Output is correct
15 Correct 15 ms 30932 KB Output is correct
16 Correct 16 ms 31060 KB Output is correct
17 Correct 56 ms 36684 KB Output is correct
18 Correct 47 ms 36496 KB Output is correct
19 Correct 48 ms 36472 KB Output is correct
20 Correct 44 ms 36052 KB Output is correct
21 Correct 42 ms 35956 KB Output is correct
22 Correct 76 ms 41296 KB Output is correct
23 Correct 30 ms 32588 KB Output is correct
24 Correct 48 ms 35032 KB Output is correct
25 Correct 18 ms 31060 KB Output is correct
26 Correct 26 ms 32432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 30824 KB Output is correct
2 Correct 16 ms 30804 KB Output is correct
3 Correct 15 ms 30804 KB Output is correct
4 Correct 16 ms 30804 KB Output is correct
5 Correct 16 ms 30748 KB Output is correct
6 Correct 18 ms 30804 KB Output is correct
7 Correct 19 ms 30804 KB Output is correct
8 Correct 21 ms 30804 KB Output is correct
9 Correct 17 ms 30804 KB Output is correct
10 Correct 17 ms 31036 KB Output is correct
11 Correct 21 ms 30940 KB Output is correct
12 Correct 19 ms 30932 KB Output is correct
13 Correct 19 ms 30868 KB Output is correct
14 Correct 17 ms 30940 KB Output is correct
15 Correct 15 ms 30932 KB Output is correct
16 Correct 16 ms 31060 KB Output is correct
17 Correct 56 ms 36684 KB Output is correct
18 Correct 47 ms 36496 KB Output is correct
19 Correct 48 ms 36472 KB Output is correct
20 Correct 44 ms 36052 KB Output is correct
21 Correct 42 ms 35956 KB Output is correct
22 Correct 76 ms 41296 KB Output is correct
23 Correct 30 ms 32588 KB Output is correct
24 Correct 48 ms 35032 KB Output is correct
25 Correct 18 ms 31060 KB Output is correct
26 Correct 26 ms 32432 KB Output is correct
27 Correct 20 ms 31620 KB Output is correct
28 Correct 177 ms 59644 KB Output is correct
29 Correct 285 ms 72380 KB Output is correct
30 Correct 482 ms 101852 KB Output is correct
31 Correct 396 ms 101028 KB Output is correct
32 Correct 204 ms 72492 KB Output is correct
33 Correct 404 ms 101868 KB Output is correct
34 Correct 416 ms 101872 KB Output is correct
35 Correct 123 ms 51856 KB Output is correct
36 Correct 385 ms 91616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 31572 KB Output is correct
2 Correct 23 ms 31620 KB Output is correct
3 Correct 57 ms 43708 KB Output is correct
4 Correct 45 ms 40648 KB Output is correct
5 Correct 96 ms 53520 KB Output is correct
6 Correct 90 ms 52856 KB Output is correct
7 Correct 91 ms 53496 KB Output is correct
8 Correct 92 ms 53496 KB Output is correct
9 Correct 122 ms 59576 KB Output is correct
10 Correct 73 ms 45436 KB Output is correct
11 Correct 149 ms 60088 KB Output is correct
12 Correct 17 ms 30804 KB Output is correct
13 Correct 18 ms 30804 KB Output is correct
14 Correct 17 ms 30804 KB Output is correct
15 Correct 16 ms 30804 KB Output is correct
16 Correct 17 ms 30796 KB Output is correct
17 Correct 18 ms 30836 KB Output is correct
18 Correct 18 ms 31696 KB Output is correct
19 Correct 19 ms 31572 KB Output is correct
20 Correct 18 ms 31608 KB Output is correct
21 Correct 18 ms 31636 KB Output is correct
22 Correct 157 ms 58232 KB Output is correct
23 Correct 197 ms 68508 KB Output is correct
24 Correct 212 ms 70120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 43132 KB Output is correct
2 Correct 85 ms 45824 KB Output is correct
3 Correct 19 ms 31552 KB Output is correct
4 Correct 21 ms 31624 KB Output is correct
5 Correct 351 ms 81516 KB Output is correct
6 Correct 412 ms 100644 KB Output is correct
7 Correct 16 ms 30952 KB Output is correct
8 Correct 136 ms 50540 KB Output is correct
9 Correct 156 ms 54544 KB Output is correct
10 Correct 73 ms 43744 KB Output is correct
11 Correct 81 ms 45832 KB Output is correct
12 Correct 21 ms 30800 KB Output is correct
13 Correct 18 ms 30836 KB Output is correct
14 Correct 17 ms 30756 KB Output is correct
15 Correct 17 ms 30804 KB Output is correct
16 Correct 18 ms 31640 KB Output is correct
17 Correct 18 ms 31600 KB Output is correct
18 Correct 100 ms 46348 KB Output is correct
19 Correct 133 ms 49016 KB Output is correct
20 Correct 93 ms 44496 KB Output is correct
21 Correct 100 ms 42780 KB Output is correct
22 Correct 94 ms 44532 KB Output is correct
23 Correct 96 ms 45664 KB Output is correct
24 Correct 18 ms 31572 KB Output is correct
25 Correct 23 ms 31620 KB Output is correct
26 Correct 57 ms 43708 KB Output is correct
27 Correct 45 ms 40648 KB Output is correct
28 Correct 96 ms 53520 KB Output is correct
29 Correct 90 ms 52856 KB Output is correct
30 Correct 91 ms 53496 KB Output is correct
31 Correct 92 ms 53496 KB Output is correct
32 Correct 19 ms 30824 KB Output is correct
33 Correct 16 ms 30804 KB Output is correct
34 Correct 15 ms 30804 KB Output is correct
35 Correct 16 ms 30804 KB Output is correct
36 Correct 16 ms 30748 KB Output is correct
37 Correct 18 ms 30804 KB Output is correct
38 Correct 19 ms 30804 KB Output is correct
39 Correct 21 ms 30804 KB Output is correct
40 Correct 17 ms 30804 KB Output is correct
41 Correct 17 ms 31036 KB Output is correct
42 Correct 21 ms 30940 KB Output is correct
43 Correct 19 ms 30932 KB Output is correct
44 Correct 19 ms 30868 KB Output is correct
45 Correct 17 ms 30940 KB Output is correct
46 Correct 15 ms 30932 KB Output is correct
47 Correct 16 ms 31060 KB Output is correct
48 Correct 56 ms 36684 KB Output is correct
49 Correct 47 ms 36496 KB Output is correct
50 Correct 48 ms 36472 KB Output is correct
51 Correct 44 ms 36052 KB Output is correct
52 Correct 42 ms 35956 KB Output is correct
53 Correct 76 ms 41296 KB Output is correct
54 Correct 30 ms 32588 KB Output is correct
55 Correct 48 ms 35032 KB Output is correct
56 Correct 18 ms 31060 KB Output is correct
57 Correct 26 ms 32432 KB Output is correct
58 Correct 20 ms 31620 KB Output is correct
59 Correct 177 ms 59644 KB Output is correct
60 Correct 285 ms 72380 KB Output is correct
61 Correct 482 ms 101852 KB Output is correct
62 Correct 396 ms 101028 KB Output is correct
63 Correct 204 ms 72492 KB Output is correct
64 Correct 404 ms 101868 KB Output is correct
65 Correct 416 ms 101872 KB Output is correct
66 Correct 123 ms 51856 KB Output is correct
67 Correct 385 ms 91616 KB Output is correct
68 Correct 122 ms 59576 KB Output is correct
69 Correct 73 ms 45436 KB Output is correct
70 Correct 149 ms 60088 KB Output is correct
71 Correct 17 ms 30804 KB Output is correct
72 Correct 18 ms 30804 KB Output is correct
73 Correct 17 ms 30804 KB Output is correct
74 Correct 16 ms 30804 KB Output is correct
75 Correct 17 ms 30796 KB Output is correct
76 Correct 18 ms 30836 KB Output is correct
77 Correct 18 ms 31696 KB Output is correct
78 Correct 19 ms 31572 KB Output is correct
79 Correct 18 ms 31608 KB Output is correct
80 Correct 18 ms 31636 KB Output is correct
81 Correct 157 ms 58232 KB Output is correct
82 Correct 197 ms 68508 KB Output is correct
83 Correct 212 ms 70120 KB Output is correct
84 Correct 400 ms 81840 KB Output is correct
85 Correct 403 ms 81808 KB Output is correct
86 Correct 384 ms 100556 KB Output is correct
87 Correct 403 ms 101676 KB Output is correct
88 Correct 17 ms 30804 KB Output is correct
89 Correct 402 ms 101648 KB Output is correct
90 Correct 293 ms 86860 KB Output is correct
91 Correct 200 ms 74152 KB Output is correct