feat(DP): new dp problems formated

This commit is contained in:
2025-11-04 21:14:50 -03:00
parent 669c13147b
commit 3b03c32703
925 changed files with 30660 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int N, W; cin >> N >> W;
vector<pair<ll,ll>> items(N+1);
for (int i = 1; i <= N; i++) {
ll wi, vi; cin >> wi >> vi;
items[i] = {wi, vi};
}
ll ans = 0;
for (ll i = 0; i < (1<<N); i++) {
ll capacity = W, value = 0;
for (int j = 0; j < N; j++) {
auto [wj, vj] = items[j + 1];
if (((i >> j) & 1)) {
if (capacity >= wj) {
capacity -= wj;
value += vj;
} else {
value = -1;
break;
}
}
}
ans = max(ans, value);
}
cout << ans << endl;
return 0;
}

View File

@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool comp(pair<ll, ll>&a, pair<ll,ll>& b) {
auto [wa, va] = a;
auto [wb, vb] = b;
return ((double)va/wa > (double)vb/wb);
}
int main() {
int N, W; cin >> N >> W;
vector<pair<ll,ll>> items(N+1);
for (int i = 1; i <= N; i++) {
ll wi, vi; cin >> wi >> vi;
items[i] = {wi, vi};
}
sort(items.begin() + 1, items.end(), comp);
int item = 1, ans = 0;
while (W > 0 && item <= N) {
auto [w, v] = items[item++];
if (W >= w) {
W -= w;
ans += v;
}
}
cout << ans << endl;
return 0;
}

View File

@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int N, W; cin >> N >> W;
vector<pair<ll,ll>> items(N+1);
for (int i = 1; i <= N; i++) {
ll wi, vi; cin >> wi >> vi;
items[i] = {wi, vi};
}
vector<vector<ll>> dp(N+1, vector<ll>(W+1, 0));
for (int i = 1; i <= N; i++) {
auto [w,v] = items[i];
for (int j = 0; j <= W; j++) {
dp[i][j] = dp[i-1][j];
if (j >= w) dp[i][j] = max(dp[i][j], dp[i-1][j-w] + v);
}
}
cout << dp[N][W] << endl;
return 0;
}

View File

@@ -0,0 +1,18 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
setName("compare two numbers");
registerTestlibCmd(argc, argv);
long long ja = ans.readLong();
long long pa = ouf.readLong();
if (ja != pa)
quitf(_wa, "expected %lld, found %lld", ja, pa);
quitf(_ok, "answer is %lld", ja);
return 0;
}

View File

@@ -0,0 +1,108 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
const int MIN_N = 1;
const int MAX_N = 100;
const int MIN_W = 1;
const int MAX_W = 1e5;
const int MIN_V = 1;
const int MAX_V = 1e9;
const int rnd_test_n = 100;
template <typename T> void append(vector<T> &dest, const vector<T> &orig) {
dest.insert(dest.end(), orig.begin(), orig.end());
}
string output_tc(const vector<pair<int, int>> &nums, int W) {
ostringstream oss;
oss << nums.size() << " " << W << endl;
for (int i = 0; i < nums.size(); i++) {
auto [w, v] = nums[i];
oss << w << " " << v << endl;
}
return oss.str();
}
vector<string> generate_sample_tests() {
vector<string> tests;
tests.push_back(output_tc({{3, 30}, {4, 50}, {5, 60}}, 8));
tests.push_back(output_tc({{1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}}, 5));
tests.push_back(output_tc({{6, 5}, {5, 6}, {6, 4}, {6, 6}, {3, 5}, {7, 2}}, 15));
return tests;
}
vector<string> generate_manual_tests() {
vector<string> tests;
tests.push_back(output_tc({{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}, 15));
return tests;
}
string rnd_test(int i){
int min_n = MIN_N;
int max_n = MAX_N;
if(i<rnd_test_n / 3){
max_n = MAX_N/3;
}
else if(i<rnd_test_n / 2){
max_n = MAX_N/2;
}
int n = rnd.next(min_n, max_n), w = rnd.next(MIN_W, MAX_W);
vector<pair<int, int>> nums(n);
for (int i = 0; i < n; i++) {
nums[i] = {rnd.next(MIN_W, w), rnd.next(MIN_V, MAX_V)};
}
return(output_tc(nums, w));
}
vector<string> generate_random_tests() {
vector<string> tests;
for (int i = 0; i < rnd_test_n; i++){
tests.push_back(rnd_test(i));
}
return tests;
}
string extreme_test_1(){
int n = MAX_N, w = MAX_W;
vector<pair<int, int>> nums(n);
for (int i = 0; i < n; i++) {
nums[i] = {MIN_W, MAX_V};
}
return(output_tc(nums, w));
}
string extreme_test_2(){
int n = MAX_N, w = MAX_W;
vector<pair<int, int>> nums(n);
for (int i = 0; i < n; i++) {
nums[i] = {MIN_W, MIN_V};
}
return(output_tc(nums, w));
}
vector<string> generate_extreme_tests(){
vector<string> tests;
tests.push_back(extreme_test_1());
tests.push_back(extreme_test_2());
return tests;
}
int main(int argc, char *argv[]) {
registerGen(argc, argv, 1);
vector<string> tests;
size_t test = 0;
append(tests, generate_sample_tests());
append(tests, generate_manual_tests());
append(tests, generate_random_tests());
append(tests, generate_extreme_tests());
for (const auto &t : tests) {
startTest(++test);
cout << t;
}
return 0;
}

View File

@@ -0,0 +1 @@
generator

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,21 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
registerValidation(argc, argv);
int N = inf.readInt(1, 100, "N");
inf.readSpace();
int W = inf.readInt(1, 1e5, "W");
inf.readEoln();
for (int i = 0; i < N; i++) {
inf.readInt(1, W, "wi");
inf.readSpace();
inf.readInt(1, 1e9, "vi");
inf.readEoln();
}
inf.readEof();
return 0;
}