This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)2e5 + 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() {
int ans = INT_MAX;
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 ;
int OR = 0;
int 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;
}
};
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 ;
}
}
/* 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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |