QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#85092 | #5690. 背包 | zlxFTH | TL | 0ms | 0kb | C++14 | 1.2kb | 2023-03-06 22:43:04 | 2023-03-06 22:43:04 |
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
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...