답안 #885168

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885168 2023-12-09T06:36:50 Z AnhPham Bali Sculptures (APIO15_sculpture) C++17
100 / 100
82 ms 4952 KB
#include <bits/stdc++.h>

#ifdef OP_DEBUG
    #include <algo/debug.h>
#else
    #define debug(...) 26
#endif

using namespace std;

#define int 	long long // maybe tle?
#define sz(v)   (int)(v).size()
#define all(v)  (v).begin(), (v).end()
#define TcT     template <class T

const   int     MOD = (int)1e9 + 7, INF = (int)2e9 + 9, INF64 = (int)4e18 + 18;

TcT>            bool minimize(T &lhs, const T &rhs) { return rhs < lhs ? lhs = rhs, 1 : 0; }
TcT>            bool maximize(T &lhs, const T &rhs) { return rhs > lhs ? lhs = rhs, 1 : 0; }

TcT, class S>   istream &operator >> (istream &scanf, pair <T, S> &u) { return scanf >> u.first >> u.second;        }
TcT, class S>   ostream &operator << (ostream &print, pair <T, S> &u) { return print << u.first << ' ' << u.second; }

void solve();

int32_t main() {
    cin.tie(nullptr), cout.tie(nullptr) -> sync_with_stdio(0);
    int testcases = 1;

#define TC 0
    if (TC) { cin >> testcases; } for (; testcases--;) { solve(); }
}

/* [Pham Hung Anh - 11I - Tran Hung Dao High School for Gifted Student] */

const int N = 2002;

int n, a, b;
int y[N];

namespace sub1 {
    int ans = INF64;

    void Try(int k, int groups, int sum, int sum_or) {
        if (k > n) {
            if (a <= groups && groups <= b)
                minimize(ans, sum_or | sum);

            return;
        }

        Try(k + 1, groups, sum + y[k], sum_or);
        Try(k + 1, groups + 1, y[k], sum_or | sum);
    }
    
    void solve() {
        Try(2, 1, y[1], 0);

        cout << ans << '\n';
    }
}

namespace sub2 {
    bool dp[55][22][505];

    void solve() {
        dp[0][0][0] = 1;
        for (int i = 1; i <= n; ++i)
            for (int groups = 0; groups <= b; ++groups)
                for (int sum_or = 0; sum_or <= 500; ++sum_or) {
                    int sum = 0;
                    for (int j = i; j >= 1; --j)
                        dp[i][groups + 1][sum_or | (sum += y[j])] |= dp[j - 1][groups][sum_or];
                }

        int ans = INF64;
        for (int groups = a; groups <= b; ++groups)
            for (int sum_or = 0; sum_or <= 500; ++sum_or)
                if (dp[n][groups][sum_or])
                    minimize(ans, sum_or);

        cout << ans << '\n';
    }
}

namespace sub3 {
    int dp[101][2002];

    void solve() {
        memset(dp, 0x3f, sizeof (dp));

        dp[0][0] = 0;
        for (int i = 1; i <= n; ++i)
            for (int sum_or = 0; sum_or <= 2000; ++sum_or) {
                int sum = 0;
                for (int j = i; j >= 1; --j)
                    minimize(dp[i][sum_or | (sum += y[j])], dp[j - 1][sum_or] + 1);
            }

        int ans = INF64;
        for (int sum_or = 0; sum_or <= 2000; ++sum_or)
            if (dp[n][sum_or] <= b)
                minimize(ans, sum_or);
        
        cout << ans << '\n';
    }
}

namespace sub4 {
    bool dp[N][N];

    bool can(int mask) {
        memset(dp, 0, sizeof (dp));

        dp[0][0] = 1;
        for (int i = 1; i <= n; ++i) {
            memset(dp[i], 0, sizeof (dp[i]));

            int sum = 0;
            for (int j = i; j >= 1; --j) {
                sum += y[j];
                if ((~mask & sum) == 0)
                    for (int groups = 1; groups <= b; ++groups)
                        dp[i][groups] |= dp[j - 1][groups - 1];
            }
        }

        for (int groups = a; groups <= b; ++groups)
            if (dp[n][groups])
                return 1;
        
        return 0;
    }

    void solve() {
        int ans = (1ll << 61) - 1;
        for (int bit = 60; bit >= 0; --bit)
            if (can(ans ^ (1ll << bit)))
                ans ^= (1ll << bit);
        
        cout << ans << '\n';
    }
}

namespace sub5 {
    int dp[N];

    bool can(int mask) {
        memset(dp, 0x3f, sizeof (dp));

        dp[0] = 0;
        for (int i = 1; i <= n; ++i) {
            int sum = 0;
            for (int j = i; j >= 1; --j) {
                sum += y[j];
                if ((~mask & sum) == 0)
                    minimize(dp[i], dp[j - 1] + 1);
            }
        }

        return dp[n] <= b;
    }

    void solve() {
        int ans = (1ll << 61) - 1;
        for (int bit = 60; bit >= 0; --bit)
            if (can(ans ^ (1ll << bit)))
                ans ^= (1ll << bit);
        
        cout << ans << '\n';
    }
}

void solve() {
    cin >> n >> a >> b;

    for (int i = 1; i <= n; ++i)
        cin >> y[i];
    
    if (n <= 20)
        sub1 :: solve();
    else if (n <= 50)
        sub2 :: solve();
    else if (n <= 100 && a == 1 && *max_element(y + 1, y + 1 + n) <= 20)
        sub3 :: solve();
    else if (n <= 100)
        sub4 :: solve();
    else if (n <= 2000 && a == 1)
        sub5 :: solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 472 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 464 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 344 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 2 ms 344 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 2 ms 348 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 464 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 600 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 3 ms 604 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 3 ms 604 KB Output is correct
30 Correct 10 ms 860 KB Output is correct
31 Correct 14 ms 784 KB Output is correct
32 Correct 14 ms 828 KB Output is correct
33 Correct 2 ms 600 KB Output is correct
34 Correct 17 ms 1008 KB Output is correct
35 Correct 15 ms 860 KB Output is correct
36 Correct 8 ms 788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 10 ms 868 KB Output is correct
18 Correct 15 ms 860 KB Output is correct
19 Correct 15 ms 860 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 15 ms 868 KB Output is correct
22 Correct 13 ms 860 KB Output is correct
23 Correct 8 ms 860 KB Output is correct
24 Correct 4 ms 3672 KB Output is correct
25 Correct 6 ms 3676 KB Output is correct
26 Correct 8 ms 3676 KB Output is correct
27 Correct 10 ms 3676 KB Output is correct
28 Correct 16 ms 3676 KB Output is correct
29 Correct 12 ms 3760 KB Output is correct
30 Correct 13 ms 3676 KB Output is correct
31 Correct 12 ms 3676 KB Output is correct
32 Correct 13 ms 3760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 464 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 344 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 3 ms 344 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 2 ms 348 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 3 ms 604 KB Output is correct
40 Correct 10 ms 860 KB Output is correct
41 Correct 14 ms 856 KB Output is correct
42 Correct 15 ms 996 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 15 ms 1016 KB Output is correct
45 Correct 13 ms 856 KB Output is correct
46 Correct 8 ms 856 KB Output is correct
47 Correct 4 ms 3676 KB Output is correct
48 Correct 6 ms 3676 KB Output is correct
49 Correct 8 ms 3672 KB Output is correct
50 Correct 10 ms 3676 KB Output is correct
51 Correct 13 ms 3676 KB Output is correct
52 Correct 12 ms 3676 KB Output is correct
53 Correct 12 ms 3672 KB Output is correct
54 Correct 12 ms 3676 KB Output is correct
55 Correct 12 ms 3676 KB Output is correct
56 Correct 5 ms 4696 KB Output is correct
57 Correct 6 ms 4744 KB Output is correct
58 Correct 9 ms 4700 KB Output is correct
59 Correct 15 ms 4700 KB Output is correct
60 Correct 15 ms 4576 KB Output is correct
61 Correct 6 ms 4700 KB Output is correct
62 Correct 17 ms 4744 KB Output is correct
63 Correct 16 ms 4736 KB Output is correct
64 Correct 18 ms 4696 KB Output is correct
65 Correct 5 ms 4700 KB Output is correct
66 Correct 5 ms 4736 KB Output is correct
67 Correct 6 ms 4736 KB Output is correct
68 Correct 10 ms 4696 KB Output is correct
69 Correct 15 ms 4696 KB Output is correct
70 Correct 16 ms 4696 KB Output is correct
71 Correct 6 ms 4700 KB Output is correct
72 Correct 17 ms 4740 KB Output is correct
73 Correct 14 ms 4732 KB Output is correct
74 Correct 12 ms 4696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 356 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 344 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 2 ms 604 KB Output is correct
26 Correct 3 ms 600 KB Output is correct
27 Correct 10 ms 860 KB Output is correct
28 Correct 15 ms 796 KB Output is correct
29 Correct 15 ms 1112 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 15 ms 1024 KB Output is correct
32 Correct 15 ms 856 KB Output is correct
33 Correct 8 ms 856 KB Output is correct
34 Correct 4 ms 3672 KB Output is correct
35 Correct 6 ms 3676 KB Output is correct
36 Correct 8 ms 3676 KB Output is correct
37 Correct 10 ms 3676 KB Output is correct
38 Correct 12 ms 3764 KB Output is correct
39 Correct 12 ms 3676 KB Output is correct
40 Correct 12 ms 3756 KB Output is correct
41 Correct 15 ms 3676 KB Output is correct
42 Correct 16 ms 3676 KB Output is correct
43 Correct 7 ms 4740 KB Output is correct
44 Correct 7 ms 4496 KB Output is correct
45 Correct 11 ms 4952 KB Output is correct
46 Correct 15 ms 4696 KB Output is correct
47 Correct 16 ms 4700 KB Output is correct
48 Correct 6 ms 4700 KB Output is correct
49 Correct 17 ms 4700 KB Output is correct
50 Correct 17 ms 4696 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 7 ms 504 KB Output is correct
53 Correct 12 ms 348 KB Output is correct
54 Correct 22 ms 504 KB Output is correct
55 Correct 21 ms 348 KB Output is correct
56 Correct 82 ms 596 KB Output is correct
57 Correct 77 ms 344 KB Output is correct
58 Correct 78 ms 348 KB Output is correct
59 Correct 77 ms 348 KB Output is correct
60 Correct 80 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 13 ms 344 KB Output is correct
63 Correct 22 ms 348 KB Output is correct
64 Correct 23 ms 348 KB Output is correct
65 Correct 39 ms 348 KB Output is correct
66 Correct 59 ms 348 KB Output is correct
67 Correct 78 ms 492 KB Output is correct
68 Correct 82 ms 344 KB Output is correct
69 Correct 77 ms 348 KB Output is correct