Submission #67630246
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define int ll
#define fast_io cin.tie(0)->sync_with_stdio(0);
#define endl '\n'
typedef long long ll;
const int INF = 1e18;
vector<int> maxplus(vector<int>& a, vector<int>& b) {
int n = a.size(), m = b.size();
vector<int> ans(n + m - 1);
int i = 0, j = 0;
while (i < n - 1 || j < m - 1) {
if (i == n - 1) {
j++;
} else if (j == m - 1) {
i++;
} else {
if (a[i + 1] + b[j] > a[i] + b[j + 1]) {
i++;
} else {
j++;
}
}
ans[i + j] = max(-INF, a[i] + b[j]);
}
//cout << "conv" << endl;
//for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
//cout << endl;
//for (int i = 0; i < b.size(); i++) cout << b[i] << " ";
//cout << endl;
//for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
//cout << endl;
return ans;
}
int32_t main() {
fast_io;
int t; cin >> t;
while (t--) {
int n; cin >> n;
int w; cin >> w;
const int MX = __lg(w) + 1;
vector<vector<int>> items(MX);
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
if (x < MX) items[x].push_back(y);
}
vector<int> dp(n + 1, -INF);
dp[0] = 0;
for (int x = 0; x < MX; x++) {
sort(items[x].rbegin(), items[x].rend());
for (int i = 1; i < (int)items[x].size(); i++) items[x][i] += items[x][i - 1];
items[x].insert(items[x].begin(), 0);
auto ndp = maxplus(dp, items[x]);
for (int i = 0; i < ndp.size(); i++) {
if (w >> x & 1) {
dp[i/2] = max(dp[i/2], ndp[i]);
} else {
dp[(i + 1)/2] = max(dp[(i + 1)/2], ndp[i]);
}
}
}
cout << dp[0] << endl;
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | B - Binary Knapsack |
| User | MvKaio |
| Language | C++ 20 (gcc 12.2) |
| Score | 500 |
| Code Size | 1659 Byte |
| Status | AC |
| Exec Time | 267 ms |
| Memory | 10948 KiB |
Compile Error
Main.cpp: In function ‘int32_t main()’:
Main.cpp:67:43: warning: comparison of integer expressions of different signedness: ‘ll’ {aka ‘long long int’} and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
67 | for (int i = 0; i < ndp.size(); i++) {
| ~~^~~~~~~~~~~~
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 500 / 500 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample-01.txt |
| All | 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 03-01.txt, 03-02.txt, 03-03.txt, 03-04.txt, 03-05.txt, 03-06.txt, 03-07.txt, hand-01.txt, sample-01.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01-01.txt | AC | 267 ms | 3536 KiB |
| 01-02.txt | AC | 82 ms | 3480 KiB |
| 01-03.txt | AC | 50 ms | 3448 KiB |
| 01-04.txt | AC | 43 ms | 3440 KiB |
| 01-05.txt | AC | 44 ms | 3700 KiB |
| 01-06.txt | AC | 49 ms | 8332 KiB |
| 01-07.txt | AC | 49 ms | 8172 KiB |
| 01-08.txt | AC | 153 ms | 3364 KiB |
| 01-09.txt | AC | 56 ms | 3476 KiB |
| 01-10.txt | AC | 42 ms | 3432 KiB |
| 01-11.txt | AC | 43 ms | 3708 KiB |
| 01-12.txt | AC | 48 ms | 8376 KiB |
| 01-13.txt | AC | 44 ms | 8396 KiB |
| 01-14.txt | AC | 46 ms | 3692 KiB |
| 01-15.txt | AC | 50 ms | 8440 KiB |
| 01-16.txt | AC | 50 ms | 8372 KiB |
| 02-01.txt | AC | 48 ms | 3524 KiB |
| 02-02.txt | AC | 44 ms | 3856 KiB |
| 02-03.txt | AC | 51 ms | 8556 KiB |
| 02-04.txt | AC | 48 ms | 9724 KiB |
| 02-05.txt | AC | 39 ms | 3780 KiB |
| 02-06.txt | AC | 48 ms | 9652 KiB |
| 02-07.txt | AC | 39 ms | 8592 KiB |
| 02-08.txt | AC | 43 ms | 3832 KiB |
| 02-09.txt | AC | 51 ms | 8496 KiB |
| 02-10.txt | AC | 48 ms | 9616 KiB |
| 03-01.txt | AC | 45 ms | 10948 KiB |
| 03-02.txt | AC | 43 ms | 9292 KiB |
| 03-03.txt | AC | 28 ms | 9252 KiB |
| 03-04.txt | AC | 44 ms | 8280 KiB |
| 03-05.txt | AC | 49 ms | 8392 KiB |
| 03-06.txt | AC | 40 ms | 8544 KiB |
| 03-07.txt | AC | 50 ms | 8456 KiB |
| hand-01.txt | AC | 1 ms | 3440 KiB |
| sample-01.txt | AC | 1 ms | 3300 KiB |