当前位置:网站首页>洛谷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;
}
边栏推荐
- How to build a data index system is the most effective. From 0 to 1, we will take you a quick start - 02 live review
- Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source
- Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
- Cancer DDD
- Chunjun supports DDL conversion and automatic execution of heterogeneous data sources - dtmo 02 review (including course playback + courseware)
- The influence of the number of non-zero values in the picture on Classification
- 8 find subsequences with a maximum length of K
- Detailed explanation of status code meaning
- Compete for the key battle of stock users and help enterprises build a perfect labeling system - 01 live review
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
猜你喜欢

How to build a data index system is the most effective. From 0 to 1, we will take you a quick start - 02 live review

KEPServer配置

How to modify the strict mode under MySQL so that adding new users by inserting user table is successful

涌现与形态的局部差异和整体差异

YonBuilder赋能创新,用友第四届开发者大赛“金键盘奖”开启竞逐!

图片中非0值的数量对分类的影响

Play with the cluster configuration center and learn about the Taier console

An article reveals the NFT strategy of traditional game manufacturers such as Ubisoft

JVM judges that the object is dead, and practices verify GC recycling

熵与形态的非递进现象
随机推荐
TensorFlow笔记——基本函数及概念
Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change
Analysis of new communication security risks brought by quantum computer and Countermeasures
What is the mystery of the gate of the meta universe?
Background color style modification on table hover in antd
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
Google browser screenshot tips
推导STO双中心动能积分的详细展开式
Recruit top talents! The "megeagle creator program" of Kuangshi technology was officially launched
Ansible
One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
一次跨域问题的记录
JVM judges that the object is dead, and practices verify GC recycling
Time and power allocation method to ensure fairness in sensor fusion system
Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
ASP. Net core dependency injection journey: 1. Theoretical concepts
Yiwen counts NFT projects valued at more than US $100million
Customized modification based on jira7.9.2
Record of a cross domain problem
Application scenarios, key technologies and network architecture of communication perception integration