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>
using namespace std;
/*
suppose we are trying to satisfy prefix j
let dp[i][0] be the minimum number of segments required to finish at i
and dp[i][1] be the maximum
we can do transitions in O(n^2) and check if dp[n][0] or dp[n][1] is good enough
then we can build from the highest bit down
overall: O(n^2 log y)?
*/
const int MAXN = 2005;
int n;
int a, b;
int nums[MAXN];
int dp[MAXN][2];
int badmask;
bool check() {
for (int i = 0; i <= n; i ++)
dp[i][0] = 1e9, dp[i][1] = -1;
dp[0][0] = dp[0][1] = 0;
for (int i = 0; i < n; i ++) {
int cursum = 0;
for (int j = i + 1; j <= n; j ++) {
cursum += nums[j];
// exceeded limit
if (cursum > (1 << 30))
break;
if ((cursum & badmask) == 0) {
dp[j][0] = min(dp[j][0], dp[i][0] + 1);
dp[j][1] = max(dp[j][1], dp[i][1] + 1);
}
}
}
return (dp[n][0] <= b) && (dp[n][1] >= a);
}
int main() {
cin >> n >> a >> b;
for (int i = 1; i <= n; i ++)
cin >> nums[i];
int curbit = (1 << 29);
badmask = 0;
int ans = 0;
while (curbit) {
badmask += curbit;
if (!check()) {
badmask -= curbit;
ans += curbit;
}
curbit /= 2;
}
cout << ans << endl;
}
# | 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... |