feat(DP): new problem formated
This commit is contained in:
37
delete-and-earn/src/ac.cpp
Normal file
37
delete-and-earn/src/ac.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
typedef long long ll;
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
pick a number and gain n[i] points
|
||||
delete all numbers equal to n[i] - 1 and n[i] + 1
|
||||
the maximum that I can get in number n[i] is
|
||||
max (dp[i - 1], dp[i - 2] + points(i))
|
||||
*/
|
||||
|
||||
ll deleteAndEarn(vector<int>& nums) {
|
||||
unordered_map<int, int> freq;
|
||||
int maxN = INT_MIN;
|
||||
for (int i : nums) {
|
||||
maxN = max(maxN, i);
|
||||
freq[i]++;
|
||||
}
|
||||
|
||||
int N = maxN;
|
||||
vector<ll> dp(N + 1, 0);
|
||||
dp[1] = freq[1];
|
||||
for (int i = 2; i <= N; i++) {
|
||||
dp[i] = max(freq[i] * i + dp[i - 2], dp[i - 1]);
|
||||
}
|
||||
|
||||
return dp[N];
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n; cin >> n;
|
||||
vector<int> nums(n);
|
||||
for (int i = 0; i < n; i++) cin >> nums[i];
|
||||
cout << deleteAndEarn(nums) << endl;
|
||||
return 0;
|
||||
}
|
||||
18
delete-and-earn/src/checker.cpp
Normal file
18
delete-and-earn/src/checker.cpp
Normal 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;
|
||||
}
|
||||
99
delete-and-earn/src/generator.cpp
Normal file
99
delete-and-earn/src/generator.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MIN_N = 1;
|
||||
const int MAX_N = 20000;
|
||||
const int MIN_NI = 1;
|
||||
const int MAX_NI = 10000;
|
||||
|
||||
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<int>& nums) {
|
||||
ostringstream oss;
|
||||
oss << nums.size() << endl;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
if (i != 0) oss << " ";
|
||||
oss << nums[i];
|
||||
}
|
||||
oss << endl;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
vector<string> generate_sample_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc({3, 4, 5}));
|
||||
tests.push_back(output_tc({2, 2, 3, 3, 3, 4}));
|
||||
tests.push_back(output_tc({1, 2, 3, 4, 5}));
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<string> generate_manual_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc({1, 1, 1, 1, 1, 1, 1, 1, 1, 1}));
|
||||
tests.push_back(output_tc({2, 5, 9, 11, 17, 28, 100, 123, 1230}));
|
||||
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 = 5;
|
||||
}
|
||||
else if(i<rnd_test_n / 2){
|
||||
max_n = 20;
|
||||
}
|
||||
|
||||
int x = rnd.next(min_n, max_n);
|
||||
vector<int> nums(x);
|
||||
for (int i = 0; i < x; i++) {
|
||||
nums[i] = rnd.next(MIN_NI, MAX_NI);
|
||||
}
|
||||
|
||||
return(output_tc(nums));
|
||||
}
|
||||
|
||||
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(){
|
||||
vector<int> nums(MAX_N);
|
||||
for (int i = 0; i < MAX_N; i++) {
|
||||
nums[i] = MAX_NI;
|
||||
}
|
||||
|
||||
return(output_tc(nums));
|
||||
}
|
||||
|
||||
vector<string> generate_extreme_tests(){
|
||||
vector<string> tests;
|
||||
tests.push_back(extreme_test_1());
|
||||
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;
|
||||
}
|
||||
1
delete-and-earn/src/script.sh
Normal file
1
delete-and-earn/src/script.sh
Normal file
@@ -0,0 +1 @@
|
||||
generator
|
||||
5963
delete-and-earn/src/testlib.h
Normal file
5963
delete-and-earn/src/testlib.h
Normal file
File diff suppressed because it is too large
Load Diff
18
delete-and-earn/src/validator.cpp
Normal file
18
delete-and-earn/src/validator.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
registerValidation(argc, argv);
|
||||
int n = inf.readInt(1, 20000, "n");
|
||||
inf.readEoln();
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (i != 0) inf.readSpace();
|
||||
inf.readInt(1, 10000, "ni");
|
||||
}
|
||||
inf.readEoln();
|
||||
inf.readEof();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user