답안 #402829

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
402829 2021-05-12T12:02:12 Z yoavL Bali Sculptures (APIO15_sculpture) C++14
46 / 100
1000 ms 1932 KB
#include <iostream>
#include <vector>
#include <algorithm>

#define upmax(a, b) a = max(a, b);
#define upmin(a, b) a = min(a, b);
#define pr(x) cout << x << endl;

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

const ll inf = 1e18;
const ll maxn = 1026; // each one is smaller than 10. so sum is no more than 500. Which means maxn's last bit is no more than 1023.
const ll maxn2 = 2050;
const ll maxg = 21;
ll n, a, b;
vll arr;
ll ans;
vll s;

void brute(vll& div, ll ind)
{
	if (ind == n) {
		if (div.size() < a-1 || div.size() > b-1) return;
		ll res = 0;
		ll cursum = 0;
		ll p = 0;
		for (ll i = 0; i < n; i++) {
			cursum += arr[i];
			if (p < div.size() && div[p] == i) {
				res |= cursum;
				p++;
				cursum = 0;
			}
		}
		res |= cursum;
		upmin(ans, res);
		return;
	}

	div.push_back(ind);
	if(ind < n-1) brute(div, ind + 1);
	div.pop_back();
	brute(div, ind + 1);
}


ll sum(ll l, ll r)
{
	ll res = s[r];
	if (l) res -= s[l - 1];
	return res;
}


ll calc_dp()
{
	
	/*
	dp[i][j][k] = is it possible to use first i vals to form j groups with OR-SUM equals k.
	*/

	vvvll dp(n+1, vvll(maxg + 1, vll(maxn, false)));

	dp[0][0][0] = true;


	for (ll i = 0; i <= n; i++) {
		for (ll j = 0; j < maxg; j++) {
			for (ll k = 0; k < maxn; k++) {
				if (!dp[i][j][k]) continue;
				ll cursum = 0;
				for (ll m = i + 1; m <= n; m++) {
					cursum += arr[m - 1];
					if (cursum >= maxn) while (true);
					dp[m][j + 1][k | cursum] = 1;
				}
			}
		}
	}


	ll res = inf;
	for (ll j = a; j <= b; j++) {
		for (ll k = 0; k < maxn; k++) {
			if (dp[n][j][k]) upmin(res, k);
		}
	}
	return res;
}



ll calc_dp2()
{

	/*
	dp[i][j] = minimum number of groupos needed to make first i values have OR-SUM of jj
	*/

	vvll dp(n + 1, vll(maxn2, inf));
	dp[0][0] = 0;


	for (ll i = 0; i <= n; i++) {
		for (ll j = 0; j < maxn2; j++) {
			if (dp[i][j] == inf) continue;
			ll cursum = 0;
			for (ll m = i + 1; m <= n; m++) {
				cursum += arr[m - 1];
				if (cursum >= maxn2) while (true);
				upmin(dp[m][j | cursum], dp[i][j] + 1);
			}
		}
	}


	ll res = inf;
	for (ll k = 0; k < maxn2; k++) {
		if (dp[n][k] <= b) upmin(res, k);
	}
	
	return res;
}

int main()
{
	cin >> n >> a >> b;
	arr.resize(n);
	for (ll i = 0; i < n; i++) cin >> arr[i];
	s.resize(n);
	s[0] = arr[0];
	for (ll i = 1; i < n; i++) s[i] = arr[i] + s[i - 1];
	ans = inf;
	if (n <= 20) {

		vll div;
		brute(div, 0);
		pr(ans);
		return 0;
	}
	else if (a == 1) {
		ans = calc_dp2();
		pr(ans);
		return 0;
	}
	ans = calc_dp();
	pr(ans);
}

/*

6 1 3 
8 1 2 1 5 4


*/

Compilation message

sculpture.cpp: In function 'void brute(vll&, ll)':
sculpture.cpp:27:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   27 |   if (div.size() < a-1 || div.size() > b-1) return;
      |       ~~~~~~~~~~~^~~~~
sculpture.cpp:27:38: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   27 |   if (div.size() < a-1 || div.size() > b-1) return;
      |                           ~~~~~~~~~~~^~~~~
sculpture.cpp:33:10: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |    if (p < div.size() && div[p] == i) {
      |        ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 2 ms 204 KB Output is correct
7 Correct 2 ms 204 KB Output is correct
8 Correct 7 ms 204 KB Output is correct
9 Correct 7 ms 204 KB Output is correct
10 Correct 7 ms 204 KB Output is correct
11 Correct 7 ms 204 KB Output is correct
12 Correct 7 ms 204 KB Output is correct
13 Correct 35 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 10 ms 204 KB Output is correct
22 Correct 10 ms 204 KB Output is correct
23 Correct 10 ms 204 KB Output is correct
24 Correct 7 ms 288 KB Output is correct
25 Correct 10 ms 204 KB Output is correct
26 Correct 30 ms 268 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 4 ms 204 KB Output is correct
31 Correct 10 ms 292 KB Output is correct
32 Correct 7 ms 204 KB Output is correct
33 Correct 33 ms 204 KB Output is correct
34 Correct 34 ms 268 KB Output is correct
35 Correct 7 ms 204 KB Output is correct
36 Correct 33 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 2 ms 204 KB Output is correct
7 Correct 2 ms 204 KB Output is correct
8 Correct 7 ms 292 KB Output is correct
9 Correct 7 ms 204 KB Output is correct
10 Correct 7 ms 288 KB Output is correct
11 Correct 7 ms 204 KB Output is correct
12 Correct 7 ms 204 KB Output is correct
13 Correct 32 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 10 ms 288 KB Output is correct
22 Correct 13 ms 288 KB Output is correct
23 Correct 10 ms 284 KB Output is correct
24 Correct 7 ms 204 KB Output is correct
25 Correct 10 ms 204 KB Output is correct
26 Correct 30 ms 288 KB Output is correct
27 Correct 1 ms 588 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 844 KB Output is correct
30 Correct 1 ms 972 KB Output is correct
31 Correct 2 ms 1100 KB Output is correct
32 Correct 2 ms 1100 KB Output is correct
33 Correct 2 ms 1100 KB Output is correct
34 Correct 2 ms 1100 KB Output is correct
35 Correct 2 ms 1100 KB Output is correct
36 Correct 2 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 2 ms 204 KB Output is correct
7 Correct 2 ms 204 KB Output is correct
8 Correct 7 ms 204 KB Output is correct
9 Correct 7 ms 204 KB Output is correct
10 Correct 7 ms 284 KB Output is correct
11 Correct 7 ms 204 KB Output is correct
12 Correct 8 ms 204 KB Output is correct
13 Correct 32 ms 204 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 844 KB Output is correct
17 Correct 2 ms 972 KB Output is correct
18 Correct 2 ms 1100 KB Output is correct
19 Correct 2 ms 1100 KB Output is correct
20 Correct 2 ms 1100 KB Output is correct
21 Correct 2 ms 1100 KB Output is correct
22 Correct 2 ms 1100 KB Output is correct
23 Correct 2 ms 1100 KB Output is correct
24 Correct 2 ms 1100 KB Output is correct
25 Correct 2 ms 1356 KB Output is correct
26 Correct 3 ms 1484 KB Output is correct
27 Correct 4 ms 1740 KB Output is correct
28 Correct 4 ms 1868 KB Output is correct
29 Correct 4 ms 1868 KB Output is correct
30 Correct 4 ms 1932 KB Output is correct
31 Correct 5 ms 1868 KB Output is correct
32 Correct 6 ms 1824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 2 ms 204 KB Output is correct
7 Correct 2 ms 204 KB Output is correct
8 Correct 8 ms 284 KB Output is correct
9 Correct 7 ms 204 KB Output is correct
10 Correct 7 ms 284 KB Output is correct
11 Correct 7 ms 204 KB Output is correct
12 Correct 8 ms 204 KB Output is correct
13 Correct 33 ms 288 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 10 ms 204 KB Output is correct
22 Correct 10 ms 288 KB Output is correct
23 Correct 10 ms 288 KB Output is correct
24 Correct 7 ms 292 KB Output is correct
25 Correct 10 ms 204 KB Output is correct
26 Correct 34 ms 268 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 4 ms 204 KB Output is correct
31 Correct 10 ms 288 KB Output is correct
32 Correct 7 ms 292 KB Output is correct
33 Correct 37 ms 204 KB Output is correct
34 Correct 32 ms 272 KB Output is correct
35 Correct 7 ms 288 KB Output is correct
36 Correct 32 ms 264 KB Output is correct
37 Correct 1 ms 588 KB Output is correct
38 Correct 1 ms 716 KB Output is correct
39 Correct 1 ms 844 KB Output is correct
40 Correct 1 ms 908 KB Output is correct
41 Correct 2 ms 1100 KB Output is correct
42 Correct 2 ms 1100 KB Output is correct
43 Correct 2 ms 1100 KB Output is correct
44 Correct 2 ms 1100 KB Output is correct
45 Correct 2 ms 1100 KB Output is correct
46 Correct 2 ms 1100 KB Output is correct
47 Correct 2 ms 1100 KB Output is correct
48 Correct 2 ms 1356 KB Output is correct
49 Correct 3 ms 1484 KB Output is correct
50 Correct 4 ms 1740 KB Output is correct
51 Correct 4 ms 1868 KB Output is correct
52 Correct 4 ms 1868 KB Output is correct
53 Correct 4 ms 1892 KB Output is correct
54 Correct 5 ms 1868 KB Output is correct
55 Correct 5 ms 1868 KB Output is correct
56 Execution timed out 1089 ms 1100 KB Time limit exceeded
57 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 2 ms 204 KB Output is correct
7 Correct 2 ms 204 KB Output is correct
8 Correct 8 ms 204 KB Output is correct
9 Correct 7 ms 204 KB Output is correct
10 Correct 9 ms 204 KB Output is correct
11 Correct 7 ms 204 KB Output is correct
12 Correct 7 ms 204 KB Output is correct
13 Correct 32 ms 288 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 4 ms 204 KB Output is correct
18 Correct 10 ms 204 KB Output is correct
19 Correct 8 ms 204 KB Output is correct
20 Correct 30 ms 204 KB Output is correct
21 Correct 33 ms 204 KB Output is correct
22 Correct 7 ms 284 KB Output is correct
23 Correct 32 ms 204 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 2 ms 844 KB Output is correct
27 Correct 1 ms 972 KB Output is correct
28 Correct 2 ms 1100 KB Output is correct
29 Correct 2 ms 1100 KB Output is correct
30 Correct 2 ms 1100 KB Output is correct
31 Correct 2 ms 1100 KB Output is correct
32 Correct 2 ms 1100 KB Output is correct
33 Correct 2 ms 1100 KB Output is correct
34 Correct 2 ms 1100 KB Output is correct
35 Correct 2 ms 1356 KB Output is correct
36 Correct 3 ms 1484 KB Output is correct
37 Correct 3 ms 1740 KB Output is correct
38 Correct 4 ms 1868 KB Output is correct
39 Correct 4 ms 1868 KB Output is correct
40 Correct 4 ms 1868 KB Output is correct
41 Correct 4 ms 1928 KB Output is correct
42 Correct 5 ms 1824 KB Output is correct
43 Execution timed out 1087 ms 1100 KB Time limit exceeded
44 Halted 0 ms 0 KB -