QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#85093 | #5690. 背包 | zlxFTH | WA | 332ms | 42380kb | C++14 | 1.2kb | 2023-03-06 22:43:26 | 2023-03-06 22:43:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 55;
const int M = 5e6 + 5;
const double eps = 1e-8;
int dcmp(double x) {
return x < -eps ? -1 : x > eps;
}
LL v[N], c[N];
LL f[M];
void solve() {
int n, q;
cin >> n >> q;
double mx = 0;
int id = 0;
for (int i = 1; i <= n; ++i) {
cin >> v[i] >> c[i];
double w = double(c[i]) / v[i];
if (dcmp(w - mx) > 0) mx = w, id = i;
}
memset(f, -1, sizeof f);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = v[i]; j <= M - 5; ++j) {
if (f[j - v[i]] == -1) continue;
f[j] = max(f[j - v[i]] + c[i], f[j]);
}
}
for (int i = 1; i <= q; ++i) {
LL V;
cin >> V;
LL ti = V / v[id];
LL ans = -1;
int times = 0;
while (ti >= 0 && V - v[id] * ti <= M - 5) {
if (f[V - v[id] * ti] != -1) {
ans = max(ans, c[id] * ti + f[V - v[id] * ti]);
}
--ti;
if (++times > 500) break;
}
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 332ms
memory: 42380kb
input:
50 100000 51305 277520 85830 111973 14837 979296 64100 235055 72474 557263 36773 232129 62774 398759 70740 834677 25120 999531 81191 28056 90133 884467 16462 693203 27057 616092 34713 932782 89420 663734 16437 298828 91123 516501 24430 267003 85485 535000 54839 145786 28114 187135 43791 474768 71273...
output:
811136115447000000 130312175752000000 831238227959000000 711780431949000000 567014385469000000 176643060888000000 532602712803000000 228620751332000000 434289249665000000 964750609099000000 271686175594000000 300113128686000000 853267533241000000 291269801555000000 450299770506000000 440721497066000...
result:
ok 100000 lines
Test #2:
score: -100
Wrong Answer
time: 318ms
memory: 42376kb
input:
50 100000 97830 451823 37513 797833 61527 952574 98951 460952 9982 483106 39784 511945 98493 933482 90672 304557 73232 677587 81302 710167 50101 771645 1815 118770 32519 644183 79650 108103 13496 441661 46853 653992 17707 729081 40996 885010 30757 661427 36297 776622 63656 551419 58205 301706 50404 ...
output:
-1 -1 54200141913056370 -1 47783263639132800 39456586132249193 80866789029835593 63984560456175570 47041442747001970 31332172417359200 -1 43185914896957361 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 48849116397899593 52664474752684370 53888680945657170 -1 44786331213557970 61845196983401361 -1 492998012539965...
result:
wrong answer 1st lines differ - expected: '87402683733488322', found: '-1'