当前位置:网站首页>Backpack template
Backpack template
2022-07-02 20:32:00 【. dye】
01 knapsack
int dp[10000];
int v[100];
int p[100];
int main()
{ int n,w;cin>>n>>w;
for(int i=1;i<=n;i++){
cin>>v[i]>>p[i];
}
for(int i=1;i<=n;i++){
for(int j=w;j>=0;j--){
if(j>=v[i])dp[j]=max(dp[j],dp[j-v[i]]+p[i]);
}
}
cout<<dp[w];
}
Completely backpack
ll dp[60000];
ll v[60000];
ll p[20000];
int main()
{ ll n,w;scanf("%lld%lld",&n,&w);
for(ll i=1;i<=n;i++){
scanf("%lld%lld",&v[i],&p[i]);
}
for(ll i=1;i<=n;i++){
for(ll j=v[i];j<=w;j++){
if(j>=v[i])dp[j]=max(dp[j],dp[j-v[i]]+p[i]);
}
}
cout<<dp[w];
}
Multiple backpack ( A monotonous queue )
int dp[60000],zu[60000], x[60000],n,m;
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
int v, w, s;
cin >> v >> w >> s;
memcpy(zu, dp, sizeof dp);
for (int j = 0; j < v; ++j) {
int a = 0, b = -1;
for (int k = j; k <= m; k += v) {
if (a <= b && x[a]<k-s* v) a ++;
while (a <= b && zu[x[b]]-(x[b] - j) / v * w <= zu[k] - (k - j) / v * w) b --;
if (a <= b) dp[k] = max(dp[k], zu[x[a]] + (k - x[a]) / v * w); x[++b] = k;
}
}
}
cout<<dp[m];
return 0;
}
边栏推荐
- Development skills of rxjs observable custom operator
- Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem
- Esp32c3 crash analysis
- Research Report on the overall scale, major manufacturers, major regions, products and applications of metal oxide arresters in the global market in 2022
- Implementation of online shopping mall system based on SSM
- [cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps
- Want to ask, is there any discount for opening an account now? Is it safe to open an account online?
- Conscience summary! Jupyter notebook from Xiaobai to master, the nanny tutorial is coming!
- Cron expression (seven subexpressions)
- In the era of consumer Internet, a few head platforms have been born
猜你喜欢
【每日一题】241. 为运算表达式设计优先级
台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
「 工业缺陷检测深度学习方法」最新2022研究综述
【Hot100】21. 合并两个有序链表
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
Resunnet - tensorrt8.2 Speed and Display record Sheet on Jetson Xavier NX (continuously supplemented)
笔记本安装TIA博途V17后出现蓝屏的解决办法
Data preparation for behavior scorecard modeling
Database schema notes - how to choose the right database in development + who invented relational database?
pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
随机推荐
[cloud native topic -49]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - basic processes and steps
CS5268完美代替AG9321MCQ Typec多合一扩展坞方案
AcWing 341. Optimal trade solution (shortest path, DP)
Istio deployment: quickly start microservices,
At compilation environment setup -win
「 工业缺陷检测深度学习方法」最新2022研究综述
In depth understanding of modern web browsers (I)
Implementation of online shopping mall system based on SSM
AcWing 1135. Happy New Year (shortest path + search)
[cloud native topic -50]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware MySQL microservice deployment process
Google Earth Engine(GEE)——Landsat 9影像全波段影像下载(北京市为例)
How to realize the function of detecting browser type in Web System
Research Report on the overall scale, major manufacturers, major regions, products and applications of capacitive voltage transformers in the global market in 2022
自动化制作视频
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of the inverted front fork of the global market in 2022
Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem
An analysis of the past and present life of the meta universe
Research and Analysis on the current situation of China's clamping device market and forecast report on its development prospect
Resunet tensorrt8.2 speed and video memory record table on Jetson Xavier NX (continuously supplemented later)
通信人的经典语录,第一条就扎心了……