mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-09-11 18:40:00 +00:00
feat: 添加P1616和P5020题解代码
实现动态规划解法,分别解决背包问题和货币系统问题
This commit is contained in:
parent
5746fd6999
commit
f8f53e255d
24
src/9/6/P1616.cpp
Normal file
24
src/9/6/P1616.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
#include <istream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using ll = int64_t;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
std::iostream::sync_with_stdio(false);
|
||||||
|
std::cin.tie(nullptr);
|
||||||
|
|
||||||
|
ll t,m;
|
||||||
|
std::cin>>t>>m;
|
||||||
|
std::vector<ll> dp(t+1);
|
||||||
|
for(ll i=1;i<=m;i++){
|
||||||
|
ll w,v;
|
||||||
|
std::cin>>w>>v;
|
||||||
|
for(ll j=w;j<=t;j++){
|
||||||
|
dp[j]=std::max(dp[j],dp[j-w]+v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<dp[t]<<"\n";
|
||||||
|
}
|
43
src/9/6/P5020.cpp
Normal file
43
src/9/6/P5020.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
#include <istream>
|
||||||
|
#include <vector>
|
||||||
|
using ll = int64_t;
|
||||||
|
|
||||||
|
ll t;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
std::iostream::sync_with_stdio(false);
|
||||||
|
std::cin.tie(nullptr);
|
||||||
|
|
||||||
|
std::cin>>t;
|
||||||
|
std::vector<ll> a;
|
||||||
|
std::vector<bool> dp;
|
||||||
|
while(t--){
|
||||||
|
ll n;
|
||||||
|
std::cin>>n;
|
||||||
|
ll ans=n;
|
||||||
|
a.clear();
|
||||||
|
dp.clear();
|
||||||
|
a.resize(n+1);
|
||||||
|
|
||||||
|
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
std::cin>>a[i];
|
||||||
|
}
|
||||||
|
std::sort(a.begin()+1,a.begin()+1+n);
|
||||||
|
dp.resize(a[n]+1);
|
||||||
|
dp[0]=true;
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
if(dp[a[i]]){
|
||||||
|
ans--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for(ll j=a[i];j<=a[n];j++){
|
||||||
|
dp[j]=dp[j]|dp[j-a[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<ans<<"\n";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user