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;
|
||||
}
|
||||
Reference in New Issue
Block a user