답안 #155682

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
155682 2019-09-29T18:06:52 Z qkxwsm Bigger segments (IZhO19_segments) C++14
100 / 100
173 ms 28852 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 500013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N;
ll arr[MAXN], pref[MAXN];
pll dp[MAXN];
pll fen[MAXN];
int ans;

void update(int idx, pll p)
{
	for (int e = idx + 1; e <= N + 2; e += e & (-e))
	{
		ckmax(fen[e], p);
	}
	return;
}
pll query(int idx)
{
	pll res;
	for (int e = idx + 1; e; e -= e & (-e))
	{
		ckmax(res, fen[e]);
	}
	return res;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N;
	FOR(i, 0, N)
	{
		cin >> arr[i];
		pref[i + 1] = pref[i] + arr[i];
	}
	dp[0] = {0, 0}; //best, sum needed
	FOR(i, 0, N)
	{
		//you need pref to be >= dp[j].se
		int idx = LB(pref, pref + N + 1, pref[i] + dp[i].se) - pref;
		update(idx, {dp[i].fi + 1, pref[i]});
		pll p = query(i + 1);
		dp[i + 1] = {p.fi, pref[i + 1] - p.se};
	}
	// FOR(i, 1, N + 1)
	// {
	// 	FORD(j, i, 0)
	// 	{
	// 		if (pref[i] >= dp[j].se + pref[j])
	// 		{
	// 			dp[i].fi = dp[j].fi + 1;
	// 			dp[i].se = pref[i] - pref[j];
	// 			break;
	// 		}
	// 	}
	// }
	//ok cool so we only need an update suffix, query point
	ans = dp[N].fi;
	//choose the rightmost umber to go off of
	cout << ans << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 12 ms 504 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 12 ms 504 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 372 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 12 ms 504 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 372 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 3 ms 504 KB Output is correct
34 Correct 3 ms 504 KB Output is correct
35 Correct 3 ms 504 KB Output is correct
36 Correct 3 ms 504 KB Output is correct
37 Correct 3 ms 504 KB Output is correct
38 Correct 3 ms 504 KB Output is correct
39 Correct 3 ms 504 KB Output is correct
40 Correct 3 ms 504 KB Output is correct
41 Correct 3 ms 504 KB Output is correct
42 Correct 3 ms 504 KB Output is correct
43 Correct 3 ms 504 KB Output is correct
44 Correct 3 ms 424 KB Output is correct
45 Correct 3 ms 476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 12 ms 504 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 372 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 3 ms 504 KB Output is correct
34 Correct 3 ms 504 KB Output is correct
35 Correct 3 ms 504 KB Output is correct
36 Correct 3 ms 504 KB Output is correct
37 Correct 3 ms 504 KB Output is correct
38 Correct 3 ms 504 KB Output is correct
39 Correct 3 ms 504 KB Output is correct
40 Correct 3 ms 504 KB Output is correct
41 Correct 3 ms 504 KB Output is correct
42 Correct 3 ms 504 KB Output is correct
43 Correct 3 ms 504 KB Output is correct
44 Correct 3 ms 424 KB Output is correct
45 Correct 3 ms 476 KB Output is correct
46 Correct 18 ms 3576 KB Output is correct
47 Correct 26 ms 5264 KB Output is correct
48 Correct 32 ms 5880 KB Output is correct
49 Correct 31 ms 5880 KB Output is correct
50 Correct 34 ms 6008 KB Output is correct
51 Correct 33 ms 6008 KB Output is correct
52 Correct 17 ms 3192 KB Output is correct
53 Correct 11 ms 2040 KB Output is correct
54 Correct 25 ms 5240 KB Output is correct
55 Correct 35 ms 6100 KB Output is correct
56 Correct 29 ms 5496 KB Output is correct
57 Correct 24 ms 4856 KB Output is correct
58 Correct 25 ms 5240 KB Output is correct
59 Correct 28 ms 5656 KB Output is correct
60 Correct 29 ms 5624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 12 ms 504 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 372 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 3 ms 504 KB Output is correct
34 Correct 3 ms 504 KB Output is correct
35 Correct 3 ms 504 KB Output is correct
36 Correct 3 ms 504 KB Output is correct
37 Correct 3 ms 504 KB Output is correct
38 Correct 3 ms 504 KB Output is correct
39 Correct 3 ms 504 KB Output is correct
40 Correct 3 ms 504 KB Output is correct
41 Correct 3 ms 504 KB Output is correct
42 Correct 3 ms 504 KB Output is correct
43 Correct 3 ms 504 KB Output is correct
44 Correct 3 ms 424 KB Output is correct
45 Correct 3 ms 476 KB Output is correct
46 Correct 18 ms 3576 KB Output is correct
47 Correct 26 ms 5264 KB Output is correct
48 Correct 32 ms 5880 KB Output is correct
49 Correct 31 ms 5880 KB Output is correct
50 Correct 34 ms 6008 KB Output is correct
51 Correct 33 ms 6008 KB Output is correct
52 Correct 17 ms 3192 KB Output is correct
53 Correct 11 ms 2040 KB Output is correct
54 Correct 25 ms 5240 KB Output is correct
55 Correct 35 ms 6100 KB Output is correct
56 Correct 29 ms 5496 KB Output is correct
57 Correct 24 ms 4856 KB Output is correct
58 Correct 25 ms 5240 KB Output is correct
59 Correct 28 ms 5656 KB Output is correct
60 Correct 29 ms 5624 KB Output is correct
61 Correct 91 ms 18040 KB Output is correct
62 Correct 106 ms 21876 KB Output is correct
63 Correct 134 ms 24152 KB Output is correct
64 Correct 173 ms 28484 KB Output is correct
65 Correct 162 ms 28852 KB Output is correct
66 Correct 170 ms 28780 KB Output is correct
67 Correct 135 ms 23096 KB Output is correct
68 Correct 66 ms 11728 KB Output is correct
69 Correct 132 ms 24796 KB Output is correct
70 Correct 164 ms 28664 KB Output is correct
71 Correct 128 ms 25840 KB Output is correct
72 Correct 122 ms 22988 KB Output is correct
73 Correct 140 ms 25628 KB Output is correct
74 Correct 141 ms 27128 KB Output is correct
75 Correct 141 ms 27256 KB Output is correct