当前位置:网站首页>洛谷P1441 砝码称重
洛谷P1441 砝码称重
2022-07-27 10:33:00 【ThXe】
#include<bits/stdc++.h>
using namespace std;
int n, m, ans;
int a[25], st[25], f[2005];//a数组代表砝码重量, st[i]代表第i个砝码是否被舍弃 1为被舍弃 0为不被舍弃, f为存在性背包
int dp() {
memset(f, 0, sizeof f);
f[0] = 1;//重量为0一定存在作为起点
int res = 0, tot = 0;//res=此次dp答案,tot当前重量总和
for (int i = 0; i < n; i++)
{
// st[i]=0 表示不被舍弃,那么对它进行背包
if (!st[i]) {
for (int j = tot; j >= 0; j--)//当前最大可能组成的重量为tot
if (f[j] && !f[j + a[i]])f[j + a[i]] = 1, res++;//如果j存在那么j+a[i]也一定存在 记得不要重复计算
tot += a[i];//维护tot
}
}
return res;
}
void dfs(int i, int now)//第i个砝码是否舍弃 当前已经舍弃砝码数量
{
if (now > m)return;//舍弃大于m直接剪枝
if (i > n)return;//超过n个砝码也剪枝
if (i == n && now == m) {
ans = max(dp(), ans); return; }// 当满足条件时跑一遍01存在性背包
dfs(i + 1, now);//当前的砝码不舍弃,那么now不变
st[i] = 1;dfs(i + 1, now + 1);st[i] = 0;//当前的砝码舍弃 那么st[i]=1表示舍弃第i个砝码,now+1,结束后恢复原状
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)cin >> a[i];
dfs(0, 0);
cout << ans << endl;
}
边栏推荐
- 学习笔记-uni-app
- Overview of data security in fog computing
- What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
- How to create a.Net image with diagnostic tools
- A measurement method of 5g air interface one-way delay and its reliability
- Kangaroo cloud stack based on CBO in spark SQL optimization
- How to build a real-time development platform to deeply release the value of enterprise real-time data?
- 学习笔记-minio
- How to assemble a registry
- ASP. Net core dependency injection journey: 1. Theoretical concepts
猜你喜欢

No identifier specified for entity solution

Pyqt5 rapid development and practice 4.2 QWidget

Use of beautifulsoup

Iptables prevent nmap scanning and binlog explanation

Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change

Deep analysis: what is diffusion model?

Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?

tensorflow运行报错解决方法

Delete in MySQL: the difference between delete, drop and truncate

ASP. Net core dependency injection journey: 1. Theoretical concepts
随机推荐
8 find subsequences with a maximum length of K
Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?
Overview of data security in fog computing
Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education
Edata base, a secondary development project based on spark packaging, is introduced
Delete in MySQL: the difference between delete, drop and truncate
Deep analysis: what is diffusion model?
学习笔记-minio
Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
深度解析:什么是Diffusion Model?
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
Use__ slots__ And__ dict__ To save space (it's simply a qualitative leap, and leetcode's personal test is effective)
The difference of iteration number and information entropy
JVM judges that the object is dead, and practices verify GC recycling
How to build a real-time development platform to deeply release the value of enterprise real-time data?
The difference between scalar, vector, matrix and tensor in deep learning
Review and Prospect of encrypted traffic identification based on deep learning
发动机悬置系统冲击仿真-瞬时模态动态分析与响应谱分析
Problems and Countermeasures of minors' digital security protection
An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft