ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#196650 | #3434. Shopping | FAT | 100 | 2060ms | 182756kb | C++11 | 2.5kb | 2023-10-29 10:31:51 | 2023-10-29 13:28:00 |
answer
#include <bits/stdc++.h>
#define fi first
#define se second
#define PB push_back
#define EB emplace_back
using namespace std;
typedef long long i64;
struct IO {
char buf[1 << 23], *p1 = buf, *p2 = buf;
char nc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 23, stdin), p1 == p2) ? EOF : *p1++; }
template <typename T = int>
T read() {
char ch = nc();
T sum = 0;
while (ch < '0' || ch > '9') ch = nc();
while (ch >= '0' && ch <= '9') sum = sum * 10 + ch - '0', ch = nc();
return sum;
}
} IO;
const int maxn = 1000000;
int w[maxn + 5], v[maxn + 5];
int nw[maxn + 5];
pair<int, int> mn[maxn + 5][20];
int lg2[maxn + 5];
inline int qMn(int l, int r) {
if (l > r) return 0;
int t = lg2[r - l + 1];
return min(mn[l][t], mn[r - (1 << t) + 1][t]).se;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n = IO.read(); i64 S = IO.read<i64>();
for (int i = 1; i <= n; i++) w[i] = IO.read();
for (int i = 1; i <= n; i++) v[i] = IO.read();
i64 t = max(S - 1000000, 0LL), sum = 0, c = 0;
vector<pair<int, i64>> st;
st.EB(0, 0);
for (int i = 1; i <= n; i++)
if (sum + w[i] <= t) {
sum += w[i], c += v[i];
st.EB(i, sum);
}
for (int i = n; i; i--) {
mn[i][0] = {w[i], i};
for (int j = 1; i + (1 << j) - 1 <= n; j++) mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
}
for (int i = 2; i <= n; i++) lg2[i] = lg2[i >> 1] + 1;
memset(nw, 63, sizeof(nw));
for (int i = 0; i < st.size(); i++) {
int id = qMn(st[i].fi + 1, i == st.size() - 1 ? n : st[i + 1].fi - 1);
if (id) nw[w[id] + st[i].se - t] = min(nw[w[id] + st[i].se - t], id);
}
i64 ans = c;
i64 tt = t + 1;
for (int i = 1; tt <= S; i++, tt++)
if (nw[i] <= 1E6) {
int id = nw[i];
int lst = n;
while (!st.empty() && st.back().fi > id) {
int x = qMn(st.back().fi + 1, lst);
if (x && nw[w[x] + st.back().se - t] == x) nw[w[x] + st.back().se - t] = 1E9;
c -= v[st.back().fi];
lst = st.back().fi - 1;
st.pop_back();
}
int x = qMn(st.back().fi + 1, lst - 1);
if (x && nw[w[x] + st.back().se - t] == x) nw[w[x] + st.back().se - t] = 1E9;
c += v[id];
x = qMn(st.back().fi + 1, id - 1);
if (x) nw[w[x] + st.back().se - t] = min(nw[w[x] + st.back().se - t], x);
st.EB(id, st.back().se + w[id]);
x = qMn(id + 1, n);
if (x) nw[w[x] + st.back().se - t] = min(nw[w[x] + st.back().se - t], x);
ans = max(ans, c);
}
printf("%lld\n", ans);
}
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 36ms
memory: 161452kb
input:
5 677 1000 7 6 13 10 13 3 10 14 2 19 10 18 7 19 14 18 6 20 17 4 6 1 8 3 13 10 2 18 20 18 1 19 8 15 1...
output:
60221588 11161611 11992143 5987570 5494136
result:
ok 5 number(s): "60221588 11161611 11992143 5987570 5494136"
Test #2:
score: 10
Accepted
time: 28ms
memory: 161456kb
input:
5 797 1000 1 16 6 13 4 7 16 8 19 15 10 7 18 17 6 2 15 5 3 20 1 11 16 9 12 6 11 5 5 6 11 20 15 13 11 ...
output:
45064938 11130695 9065532 7962523 6271979
result:
ok 5 number(s): "45064938 11130695 9065532 7962523 6271979"
Test #3:
score: 10
Accepted
time: 309ms
memory: 171948kb
input:
5 199409 901716 18256 49 14998 346 3405 16882 3510 5570 253 9999 12628 2971 1486 6791 17237 1159 102...
output:
49088364 24257996 15728896 13906680 16392123
result:
ok 5 number(s): "49088364 24257996 15728896 13906680 16392123"
Test #4:
score: 10
Accepted
time: 327ms
memory: 171944kb
input:
5 199048 966483 12932 6049 9150 7955 6647 16710 11570 19933 4791 797 310 9970 5661 18558 7392 3303 3...
output:
56731000 24267529 18966985 17482979 16821002
result:
ok 5 number(s): "56731000 24267529 18966985 17482979 16821002"
Test #5:
score: 10
Accepted
time: 327ms
memory: 175208kb
input:
5 199143 835266 16 17 15 15 16 20 14 18 10 19 4 19 4 16 15 17 6 3 7 7 12 17 17 9 11 1 17 14 13 4 16 ...
output:
39744287649 44478571836 47633170803 42887366916 42103566624
result:
ok 5 number(s): "39744287649 44478571836 47633170803 42887366916 42103566624"
Test #6:
score: 10
Accepted
time: 335ms
memory: 175212kb
input:
5 199728 868552 6 7 18 4 8 13 4 12 14 10 10 8 4 3 1 19 4 2 10 19 13 9 20 11 9 7 13 5 20 11 8 11 5 20...
output:
41622366847 39943210567 46861514918 45277132457 44300737370
result:
ok 5 number(s): "41622366847 39943210567 46861514918 45277132457 44300737370"
Test #7:
score: 10
Accepted
time: 63ms
memory: 167000kb
input:
5 49504 40639801648 82819 532587 456964 723647 391931 179392 552835 900301 958811 402819 207198 6572...
output:
24780846078 24971710621 24884912415 24833256101 24433921808
result:
ok 5 number(s): "24780846078 24971710621 24884912415 24833256101 24433921808"
Test #8:
score: 10
Accepted
time: 184ms
memory: 175224kb
input:
5 199424 47394878950 406715 898079 588541 753140 231491 205798 68907 599680 491070 9431 869648 34129...
output:
47439477903 44224724703 46670238431 38973933565 42809389320
result:
ok 5 number(s): "47439477903 44224724703 46670238431 38973933565 42809389320"
Test #9:
score: 10
Accepted
time: 223ms
memory: 182756kb
input:
5 100 5313560 215540 329139 994205 880796 233881 162800 235425 276724 569856 112319 107199 113108 69...
output:
7591961 8257546 5140193 7826846 48254787099
result:
ok 5 number(s): "7591961 8257546 5140193 7826846 48254787099"
Test #10:
score: 10
Accepted
time: 228ms
memory: 182756kb
input:
5 100 4526822 894245 761303 485719 704383 507168 97192 465982 104090 133956 10138 583167 931907 2512...
output:
7227188 9298197 8161080 5066482 51293669446
result:
ok 5 number(s): "7227188 9298197 8161080 5066482 51293669446"
Extra Test:
score: 0
Extra Test Passed