NOIAC 30 candy
思路:
$90pts$:
显然要让最小的愉悦度最大,则维护最大的前缀和,枚举即可。
#include <bits/stdc++.h> const int INF = 1 << 30; const int MAXN = 100050; typedef int intt; #define int long long using namespace std; int n, w, ans = -INF, a[MAXN], b[MAXN], sum1[MAXN], sum2[MAXN]; int read() { int x = 0; bool sign = false; char alpha = 0; while(!isdigit(alpha)) { sign |= alpha == '-'; alpha = getchar(); } while(isdigit(alpha)) { x = (x << 1) + (x << 3) + (alpha ^ 48); alpha = getchar(); } return sign ? -x : x; } intt main() { n = read(); w = read(); for(int i = 1; i <= n; i++) { a[i] = read(); sum1[i] = sum1[i - 1] + a[i]; } for(int i = 1; i <= n; i++) { b[i] = read(); sum2[i] = sum2[i - 1] + b[i]; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) ans = max(ans, min(sum1[n] - sum1[n - i], sum2[n] - sum2[n - j]) - 1ll * (i + j) * w); } cout << ans << endl; return 0; }
$100pts$:
我们假设从第一组选,则可以二分那个大于第一组的第二组的最小值,统计答案。
第二组也要这样做一遍,找最大值即可。
#include <bits/stdc++.h> const int INF = 1 << 30; const int MAXN = 100050; typedef int intt; #define int long long using namespace std; int n, w, p1, p2, ans1 = -INF, ans2 = -INF, res, l, r, a[MAXN], b[MAXN], sum1[MAXN], sum2[MAXN]; int read() { int x = 0; bool sign = false; char alpha = 0; while(!isdigit(alpha)) { sign |= alpha == '-'; alpha = getchar(); } while(isdigit(alpha)) { x = (x << 1) + (x << 3) + (alpha ^ 48); alpha = getchar(); } return sign ? -x : x; } bool check1(int x, int y) { return sum2[n] - sum2[n - x] >= y; } bool check2(int x, int y) { return sum1[n] - sum1[n - x] >= y; } intt main() { n = read(); w = read(); for(int i = 1; i <= n; i++) { a[i] = read(); sum1[i] = sum1[i - 1] + a[i]; } for(int i = 1; i <= n; i++) { b[i] = read(); sum2[i] = sum2[i - 1] + b[i]; } for(int i = 1; i <= n; i++) { res = sum1[n] - sum1[n - i]; l = 1, r = n; while(l <= r) { int mid = (l + r) >> 1; if(check1(mid, res)) { ans1 = max(ans1, res - (i + mid) * w); r = mid - 1; } else l = mid + 1; } } for(int i = 1; i <= n; i++) { res = sum2[n] - sum2[n - i]; l = 1, r = n; while(l <= r) { int mid = (l + r) >> 1; if(check2(mid, res)) { ans2 = max(ans2, res - (i + mid) * w); r = mid - 1; } else l = mid + 1; } } cout << max(ans1, ans2) << endl; return 0; }