mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-08-21 18:52:07 +00:00
重构算法实现,移除未使用的 DFS 函数,优化 DP 函数逻辑,并更新输入输出处理
This commit is contained in:
parent
7a20684212
commit
73cea4d52e
@ -1,42 +1,34 @@
|
||||
/*
|
||||
2 10 7 11
|
||||
2 10
|
||||
6 1
|
||||
|
||||
n=2, W=10, B=7, X=11
|
||||
c[1]=2,c[2]=10
|
||||
cost[1]=6,cost[2]=1
|
||||
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <limits>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
using ll = int64_t;
|
||||
|
||||
#define il static inline
|
||||
ll n,w,b,x,ans=0;
|
||||
ll n,w,b,x;
|
||||
std::vector<ll> c,cost;
|
||||
constexpr ll inf = std::numeric_limits<ll>::min();
|
||||
|
||||
il void dfs(ll nn,ll nw,ll nb,ll nc){
|
||||
if(nn>n){
|
||||
ans=std::max(ans,nc);
|
||||
return;
|
||||
static inline ll dp(ll i,ll j){
|
||||
if(i==0){
|
||||
if(j==0)return w;
|
||||
else return inf;
|
||||
}
|
||||
for(ll i=0;i<=c[nn];i++){
|
||||
if(nw<cost[nn]*i)return;
|
||||
dfs(nn+1, std::min(nw-cost[nn]*i+x,nb+b*i), nb+b*i, nc+i);
|
||||
static std::vector<std::vector<ll>> vdp(n+1,std::vector<ll>(10000+1,inf));
|
||||
if(vdp[i][j]!=inf)return vdp[i][j];
|
||||
for(ll k=0;k<=c[i];k++){
|
||||
if(j-k<0||dp(i-1,j-k)<0)continue;
|
||||
vdp[i][j]=std::max(vdp[i][j],std::min(dp(i-1, j-k)+x,w+(j-k)*b)-cost[i]*k);
|
||||
}
|
||||
return vdp[i][j];
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
std::cout.tie(nullptr);
|
||||
|
||||
std::cin>>n>>w>>b>>x;
|
||||
c.resize(n+1);
|
||||
cost.resize(n+1);
|
||||
@ -46,6 +38,11 @@ int main(){
|
||||
for(ll i=1;i<=n;i++){
|
||||
std::cin>>cost[i];
|
||||
}
|
||||
dfs(1, w, b, 0);
|
||||
std::cout<<ans<<'\n';
|
||||
for(ll i=1000;i>=0;i--){
|
||||
if(dp(n, i)>=0){
|
||||
std::cout<<i<<std::endl;
|
||||
_Exit(0);
|
||||
}
|
||||
}
|
||||
throw;
|
||||
}
|
Loading…
Reference in New Issue
Block a user