Submission #978765

#TimeUsernameProblemLanguageResultExecution timeMemory
978765SmuggingSpunBali Sculptures (APIO15_sculpture)C++14
100 / 100
70 ms860 KiB
#include<bits/stdc++.h> #define taskname "sculpture" using namespace std; typedef long long ll; const int INF = 1e9; template<class T>void minimize(T& a, T b){ if(a > b){ a = b; } } int n, a, b; vector<int>y; namespace sub1{ void solve(){ ll ans = LLONG_MAX; for(int mask = (1 << n) - 2; mask > -1; mask -= 2){ int cnt_1 = __builtin_popcount(mask) + 1; if(a <= cnt_1 && cnt_1 <= b){ ll value = 0, sum = 0; for(int i = 0; i < n; ){ if(1 << i & mask){ value |= sum; sum = 0; } sum += y[++i]; } minimize(ans, value | sum); } } cout << ans; } } namespace sub2{ const int LIM = 512; const int lim = 105; bitset<LIM>dp[2][lim]; void solve(){ bool cur = true, pre = false; int ans = LIM; for(int i = 0; i <= n; i++){ dp[cur][i].reset(); } dp[cur][n + 1].set(0); for(int i = 1; i <= b; i++){ swap(cur, pre); for(int j = 0; j <= n; j++){ dp[cur][j].reset(); } for(int j = 1; j <= n; j++){ for(int k = j, sum = 0; k <= n; k++){ sum += y[k]; for(int value = 0; value < LIM; value++){ if(dp[pre][k + 1].test(value)){ dp[cur][j].set(value | sum); } } } } if(i >= a){ for(int j = 0; j < ans; j++){ if(dp[cur][1].test(j)){ ans = j; break; } } } } cout << ans << "\n"; } } namespace sub3{ const int LIM = 2048; void solve(){ for(int i = 1; i < LIM; i++){ vector<int>dp(n + 2, INF); dp[n + 1] = 0; for(int j = n; j > 0; j--){ for(int k = j, sum = 0; k <= n; k++){ if(((sum += y[k]) & i) == sum){ minimize(dp[j], dp[k + 1] + 1); } } } if(dp[1] <= b){ cout << i; break; } } } } namespace sub4{ const int lim = 105; bitset<lim>dp[lim]; void solve(){ dp[n + 1].set(0); ll ans = 0; for(int i = 36; i > -1; i--){ for(int j = 1; j <= n; j++){ dp[j].reset(); } for(int j = n; j > 0; j--){ ll sum = 0; for(int k = j; k <= n; k++){ if(((sum += y[k]) & ~ans) < (1LL << i)){ for(int t = 0; t < n; t++){ if(dp[k + 1].test(t)){ dp[j].set(t + 1); } } } } } ans |= 1LL << i; for(int j = a; j <= b; j++){ if(dp[1].test(j)){ ans ^= 1LL << i; break; } } } cout << ans; } } namespace sub5{ const int lim = 2e3 + 5; int dp[lim]; void solve(){ ll ans = dp[n + 1] = 0; for(int i = 44; i > -1; i--){ for(int j = n; j > 0; j--){ ll sum = 0; dp[j] = INF; for(int k = j; k <= n; k++){ if(((sum += y[k]) & ~ans) < (1LL << i)){ minimize(dp[j], dp[k + 1] + 1); } } } if(dp[1] > b){ ans |= 1LL << i; } } cout << ans; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); } cin >> n >> a >> b; y.resize(n + 1); for(int i = 1; i <= n; i++){ cin >> y[i]; } if(n <= 20){ sub1::solve(); } else if(n <= 50 && *max_element(y.begin() + 1, y.end()) <= 10){ sub2::solve(); } else if(n <= 100 && *max_element(y.begin() + 1, y.end()) <= 20 && a == 1){ sub3::solve(); } else if(n <= 100){ sub4::solve(); } else{ sub5::solve(); } }

Compilation message (stderr)

sculpture.cpp: In function 'void sub3::solve()':
sculpture.cpp:79:15: warning: operation on 'sum' may be undefined [-Wsequence-point]
   79 |      if(((sum += y[k]) & i) == sum){
      |          ~~~~~^~~~~~~~
sculpture.cpp:79:15: warning: operation on 'sum' may be undefined [-Wsequence-point]
sculpture.cpp: In function 'int main()':
sculpture.cpp:149:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |   freopen(taskname".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...