2 条题解
-
4
另一种使用二分的方法:
// Written By Rosmarinus #include<algorithm> #include<iostream> #include<cstdio> #define int long long using namespace std; const int inf = 0x7f7f7f7f7f; struct Node { int v, p, rev, from; }A[1100000], B[1100000]; int alen, blen; inline int read() { int s = 0, w = 1; char ch = getchar(); while(ch < '0' || ch > '9') {if(ch == '-') w = -1; ch = getchar();} while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; } bool Sort(Node a, Node b) { if(a.p != b.p) return a.p < b.p; return a.v > b.v; } int Find_A(int v) { int l = 1, r = alen, ans = 0; while(l <= r) { int mid = (l + r) >> 1; if(A[mid].p <= v) ans = mid, l = mid + 1; else r = mid - 1; } return ans; } int Find_B(int v) { int l = 1, r = blen, ans = 0; while(l <= r) { int mid = (l + r) >> 1; if(B[mid].p <= v) ans = mid, l = mid + 1; else r = mid - 1; } return ans; } signed main() { int n, a, b, value, price, maxa = 0, maxb = 0, ans = 0; char s; n = read(), a = read(), b = read(); for(int i = 1; i <= n; i ++) { value = read(), price = read(); scanf(" %c", &s); if(s == 'A') { A[++ alen].v = value, A[alen].p = price, A[alen].rev = value; if(price <= a) maxa = max(maxa, value); } else { B[++ blen].v = value, B[blen].p = price, B[blen].rev = value; if(price <= b) maxb = max(maxb, value); } } ans = maxa + maxb; sort(A + 1, A + 1 + alen, Sort); sort(B + 1, B + 1 + blen, Sort); A[0].v = B[0].v = -inf; for(int i = 1; i <= alen; i ++) { if(A[i - 1].v > A[i].v) A[i].v = A[i - 1].v, A[i].from = A[i - 1].from; else A[i].from = i; } for(int i = 1; i <= blen; i ++) { if(B[i - 1].v > B[i].v) B[i].v = B[i - 1].v, B[i].from = B[i - 1].from; else B[i].from = i; } for(int i = 1; i <= alen; i ++) { int t = Find_A(a - A[i].p); if(A[t].from != i) ans = max(ans, A[i].rev + A[t].v); } for(int i = 1; i <= blen; i ++) { int t = Find_B(b - B[i].p); if(B[t].from != i) ans = max(ans, B[i].rev + B[t].v); } cout << ans << endl; return 0; }
信息
- ID
- 192
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 3
- 标签
- 递交数
- 215
- 已通过
- 35
- 上传者