Submission #1073511

#TimeUsernameProblemLanguageResultExecution timeMemory
1073511RiverFlowBali Sculptures (APIO15_sculpture)C++14
100 / 100
78 ms1296 KiB
#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 (stderr)

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 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...