Submission #1073511

# Submission time Handle Problem Language Result Execution time Memory
1073511 2024-08-24T15:34:16 Z RiverFlow Bali Sculptures (APIO15_sculpture) C++14
100 / 100
78 ms 1296 KB
#include <bits/stdc++.h>

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b or a == -1) { a = b; return 1; } return 0;
}

const int N = (int)2e3 + 9;
const int mod = (int)1e9 + 7;

void prepare(); void main_code();

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0; prepare();
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); }
}

void prepare() {};

int n, l, r;

int a[N];
long long s[N];

namespace SUB1 {
void sol() {
    long long ans = (long long)1e18;
    for(int mask = 0; mask < (1 << n); ++mask) {
        if (!(1 & mask >> (n - 1))) continue ;
        int X = __builtin_popcount(mask);
        if (X < l or X > r) continue ;
        long long OR = 0, sum = 0;
        for(int i = 0; i < n; ++i) {
            sum += a[i + 1];
            if (1 & mask >> i) {
                OR |= sum;
                sum = 0;
            }
        }
        ans = min(ans, OR);
    }
    cout << ans;
}
};

namespace SUB2 {
const int N = 51;
const int MAX = (1 << 9);

bool dp[N][N][MAX];
void sol() {
    dp[0][0][0] = 1;
    for(int i = 0; i < n; ++i)
    for(int j = 0; j < r; ++j)
    for(int z = 0; z < MAX; ++z) if (dp[i][j][z]) {
        for(int k = i + 1; k <= n; ++k) {
            dp[k][j + 1][z | (s[k] - s[i])] = 1;
        }
    }
    int res = MAX;
    FOR(i, l, r)
    FOR(v, 0, MAX - 1)
        if (dp[n][i][v])
            res = min(res, v);
    cout << res;
}
};

namespace SUB3 {
const int N = 102;
const int MAX = (1 << 11);
int dp[N][MAX];

void sol() {
    memset(dp, -1, sizeof dp);
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i)
    for(int z = 0; z < MAX; ++z) if (dp[i][z] != -1) {
        for(int k = i + 1; k <= n; ++k) {
            mini(dp[k][z | (s[k] - s[i])], dp[i][z] + 1);
        }
    }
    int res = MAX;
    FOR(i, l, r)
    FOR(v, 0, MAX - 1)
        if (dp[n][v] != -1 and dp[n][v] <= r)
            res = min(res, v);
    cout << res;
}
};

namespace SUB4 {
const int N = 107;
bool dp[N][N];

bool check(long long res, int tot) {
    memset(dp, 0, sizeof dp);
    dp[0][0] = 1;
    for(int i = 0; i < n; ++i)
    for(int j = 0; j < r; ++j) if (dp[i][j]) {
        for(int k = i + 1; k <= n; ++k) {
            long long x = s[k] - s[i];
            long long y = x ^ res;

            y >>= (long long)tot;
            y <<= (long long)tot;

            if ( (y & res) == y ) {
                dp[k][j + 1] = 1;
            }
        }
    }
    FOR(i, l, r) if (dp[n][i]) return 1;
    return 0;
}
void sol() {
    long long res = 0;
    for(int i = 41; i >= 0; --i) {
        bool set0 = check(res, i);
        if (!set0) res += (1LL << i);
    }
    cout << res;
}
};

namespace SUB5 {

int dp[N];

bool check(long long res, int tot) {
    dp[0] = 0;
    for(int i = 1; i <= n; ++i) {
        dp[i] = -1;
        for(int j = 1; j <= i; ++j) if (dp[j - 1] != -1) {
            long long x = s[i] - s[j - 1];
            long long y = x ^ res;

            y >>= (long long)tot;
            y <<= (long long)tot;

            if ( (y & res) == y ) {
                mini(dp[i], dp[j - 1] + 1);
            }
        }
    }
    return (dp[n] != -1 and dp[n] <= r);
}
void sol() {
    long long res = 0;
    for(int i = 41; i >= 0; --i) {
        bool set0 = check(res, i);
        if (!set0) res += (1LL << i);
    }
    cout << res;
}
};

void main_code() {
    cin >> n >> l >> r;
    for(int i = 1; i <= n; ++i) cin >> a[i], s[i] = s[i - 1] + a[i];

    if (n <= 20) return SUB1::sol(), void();

    if (n <= 50 and *max_element(a + 1, a + n + 1) <= 10) {
        SUB2::sol();
        return ;
    }

    if (n <= 100 and l == 1 and *max_element(a + 1, a + n + 1) <= 20) {
        SUB3::sol();
        return ;
    }
    if (n <= 100) {
        SUB4::sol();
        return ;
    }

    if (l == 1) {
        SUB5::sol();
    }
}


/*     Let the river flows naturally     */

Compilation message

sculpture.cpp: In function 'int main()':
sculpture.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sculpture.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory 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 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 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 464 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 30 ms 468 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 428 KB Output is correct
17 Correct 1 ms 604 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 4 ms 468 KB Output is correct
22 Correct 5 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 4 ms 348 KB Output is correct
26 Correct 26 ms 472 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 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 4 ms 348 KB Output is correct
32 Correct 2 ms 344 KB Output is correct
33 Correct 26 ms 348 KB Output is correct
34 Correct 30 ms 344 KB Output is correct
35 Correct 2 ms 344 KB Output is correct
36 Correct 30 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 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 348 KB Output is correct
11 Correct 3 ms 604 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 30 ms 472 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 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 344 KB Output is correct
21 Correct 4 ms 344 KB Output is correct
22 Correct 4 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 4 ms 348 KB Output is correct
26 Correct 27 ms 472 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 0 ms 604 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
31 Correct 1 ms 860 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 0 ms 464 KB Output is correct
34 Correct 2 ms 860 KB Output is correct
35 Correct 1 ms 860 KB Output is correct
36 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 2 ms 344 KB Output is correct
9 Correct 2 ms 600 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 468 KB Output is correct
13 Correct 32 ms 348 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 856 KB Output is correct
19 Correct 1 ms 976 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 1 ms 872 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 1116 KB Output is correct
25 Correct 1 ms 1112 KB Output is correct
26 Correct 1 ms 1116 KB Output is correct
27 Correct 2 ms 1160 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
29 Correct 2 ms 1116 KB Output is correct
30 Correct 1 ms 1292 KB Output is correct
31 Correct 2 ms 1116 KB Output is correct
32 Correct 2 ms 1232 KB Output is correct
# Verdict Execution time Memory 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 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 344 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 30 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 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 0 ms 348 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 4 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 5 ms 476 KB Output is correct
26 Correct 27 ms 348 KB Output is correct
27 Correct 1 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 4 ms 468 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 26 ms 604 KB Output is correct
34 Correct 30 ms 344 KB Output is correct
35 Correct 2 ms 460 KB Output is correct
36 Correct 33 ms 348 KB Output is correct
37 Correct 1 ms 856 KB Output is correct
38 Correct 0 ms 604 KB Output is correct
39 Correct 1 ms 600 KB Output is correct
40 Correct 1 ms 860 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 1 ms 860 KB Output is correct
43 Correct 0 ms 604 KB Output is correct
44 Correct 1 ms 860 KB Output is correct
45 Correct 1 ms 860 KB Output is correct
46 Correct 1 ms 860 KB Output is correct
47 Correct 1 ms 1212 KB Output is correct
48 Correct 1 ms 1116 KB Output is correct
49 Correct 1 ms 1112 KB Output is correct
50 Correct 3 ms 1112 KB Output is correct
51 Correct 2 ms 1116 KB Output is correct
52 Correct 2 ms 1116 KB Output is correct
53 Correct 2 ms 1116 KB Output is correct
54 Correct 2 ms 1292 KB Output is correct
55 Correct 2 ms 1116 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 2 ms 348 KB Output is correct
59 Correct 2 ms 348 KB Output is correct
60 Correct 2 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 3 ms 348 KB Output is correct
63 Correct 3 ms 348 KB Output is correct
64 Correct 5 ms 348 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 1 ms 348 KB Output is correct
67 Correct 1 ms 348 KB Output is correct
68 Correct 3 ms 484 KB Output is correct
69 Correct 2 ms 348 KB Output is correct
70 Correct 2 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 3 ms 348 KB Output is correct
73 Correct 5 ms 600 KB Output is correct
74 Correct 3 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 600 KB Output is correct
7 Correct 0 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 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 30 ms 472 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 4 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 26 ms 348 KB Output is correct
21 Correct 30 ms 600 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 30 ms 452 KB Output is correct
24 Correct 0 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 2 ms 860 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 0 ms 460 KB Output is correct
31 Correct 1 ms 856 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 1 ms 1116 KB Output is correct
35 Correct 1 ms 1116 KB Output is correct
36 Correct 1 ms 1116 KB Output is correct
37 Correct 1 ms 1228 KB Output is correct
38 Correct 2 ms 1112 KB Output is correct
39 Correct 1 ms 1252 KB Output is correct
40 Correct 1 ms 1116 KB Output is correct
41 Correct 2 ms 1116 KB Output is correct
42 Correct 2 ms 1296 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 2 ms 348 KB Output is correct
46 Correct 2 ms 348 KB Output is correct
47 Correct 3 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 3 ms 348 KB Output is correct
50 Correct 3 ms 484 KB Output is correct
51 Correct 0 ms 344 KB Output is correct
52 Correct 6 ms 344 KB Output is correct
53 Correct 7 ms 344 KB Output is correct
54 Correct 22 ms 496 KB Output is correct
55 Correct 19 ms 344 KB Output is correct
56 Correct 51 ms 348 KB Output is correct
57 Correct 44 ms 348 KB Output is correct
58 Correct 51 ms 348 KB Output is correct
59 Correct 64 ms 348 KB Output is correct
60 Correct 54 ms 344 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 7 ms 488 KB Output is correct
63 Correct 22 ms 348 KB Output is correct
64 Correct 14 ms 496 KB Output is correct
65 Correct 24 ms 344 KB Output is correct
66 Correct 37 ms 344 KB Output is correct
67 Correct 49 ms 596 KB Output is correct
68 Correct 78 ms 348 KB Output is correct
69 Correct 43 ms 348 KB Output is correct