Submission #799206

#TimeUsernameProblemLanguageResultExecution timeMemory
799206RecursiveCoBali Sculptures (APIO15_sculpture)C++14
100 / 100
142 ms1016 KiB
// CF template, version 3.0

#include <bits/stdc++.h>

using namespace std;

#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}

#define int long long int

signed main() {
    improvePerformance;

    get(n);
    get(A);
    get(B);
    getList(n, nums);
    
    // 100 points
    int ans = 0;
    int power = 1ll << 59ll;
    for (int b = 59; b >= 0; b--) {
        // DP with O(n2) states and O(n) transition per state.
        // so O(n3) DP
        // should be fine right?
        int lower = ans;
        int upper = ans + power - 1;
        vector<vector<bool>> dp(n, vector<bool>(n + 1, false));
        int pref = 0;
        bool can = false;
        if (n <= 100) {
            // Naive DP
            forto(n, i) {
                pref += nums[i];
                forto(n + 1, j) {
                    if (j == 0) continue;
                    if (j == 1) {
                        dp[i][j] = lower <= (pref | ans) && (pref | ans) <= upper;
                        continue;
                    }
                    if (j > i + 1) {
                        dp[i][j] = false;
                        break;
                    }
                    int sum = 0;
                    for (int k = i - 1; k >= 0; k--) {
                        sum += nums[k + 1];
                        if (dp[k][j - 1] && lower <= (sum | ans) && (sum | ans) <= upper) {
                            dp[i][j] = true;
                            break;
                        }
                    }
                }
            }
            for (int i = A; i <= B; i++) {
                if (dp[n - 1][i]) {
                    can = true;
                    break;
                }
            }
        } else {
            // A=1 since n is higher
            // we are interested in the "minimum number of groups we need."
            vector<int> dp(n, 1e18);
            int pref = 0;
            forto(n, i) {
                pref += nums[i];
                int sum = 0;
                for (int k = i - 1; k >= 0; k--) {
                    sum += nums[k + 1];
                    if (lower <= (sum | ans) && (sum | ans) <= upper) {
                        dp[i] = min(dp[i], dp[k] + 1);
                    }
                }
                if (lower <= (pref | ans) && (pref | ans) <= upper) dp[i] = 1;
            }
            if (dp[n - 1] <= B) can = true;
        }
        if (!can) ans += power;

        power >>= 1ll;
    }
    out(ans);
}

Compilation message (stderr)

sculpture.cpp: In function 'int main()':
sculpture.cpp:10:23: warning: unnecessary parentheses in declaration of 'n' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
sculpture.cpp:23:5: note: in expansion of macro 'get'
   23 |     get(n);
      |     ^~~
sculpture.cpp:10:23: warning: unnecessary parentheses in declaration of 'A' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
sculpture.cpp:24:5: note: in expansion of macro 'get'
   24 |     get(A);
      |     ^~~
sculpture.cpp:10:23: warning: unnecessary parentheses in declaration of 'B' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
sculpture.cpp:25:5: note: in expansion of macro 'get'
   25 |     get(B);
      |     ^~~
sculpture.cpp:12:40: warning: unnecessary parentheses in declaration of 'nums' [-Wparentheses]
   12 | #define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
      |                                        ^
sculpture.cpp:26:5: note: in expansion of macro 'getList'
   26 |     getList(n, nums);
      |     ^~~~~~~
sculpture.cpp:10:23: warning: unnecessary parentheses in declaration of 'a' [-Wparentheses]
   10 | #define get(name) int (name); cin >> (name)
      |                       ^
sculpture.cpp:12:76: note: in expansion of macro 'get'
   12 | #define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
      |                                                                            ^~~
sculpture.cpp:26:5: note: in expansion of macro 'getList'
   26 |     getList(n, nums);
      |     ^~~~~~~
sculpture.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
sculpture.cpp:42:13: note: in expansion of macro 'forto'
   42 |             forto(n, i) {
      |             ^~~~~
sculpture.cpp:15:35: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
sculpture.cpp:44:17: note: in expansion of macro 'forto'
   44 |                 forto(n + 1, j) {
      |                 ^~~~~
sculpture.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
sculpture.cpp:75:13: note: in expansion of macro 'forto'
   75 |             forto(n, i) {
      |             ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...