当前位置:网站首页>洛谷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;
}
边栏推荐
- Why is the data service API the standard configuration of the data midrange when we take the last mile of the data midrange?
- 推导STO双中心动能积分的详细展开式
- img src为空或者src不存在,图片出现白色边框
- C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
- Study notes Minio
- 开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
- antd table中排序th阻止悬停变色+table悬停行变色+table表头变色
- tensorflow运行报错解决方法
- Influence of black and white pixel distribution on iteration times
- What is the mystery of the gate of the meta universe?
猜你喜欢

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

MySQL index, transaction and storage engine

ASP. Net core dependency injection journey: 1. Theoretical concepts

Tdengine business ecosystem partner recruitment starts

IMG SRC is empty or SRC does not exist, and the picture has a white border

Redis+caffeine two-level cache enables smooth access speed

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

antd table+checkbox 默认值显示

Tcp/ip protocol

FAQs of "relay chain" and "dot" in Poka ecosystem
随机推荐
计算重叠积分的第二种方法
pyquery 的使用
Learning notes - wechat payment
The open source project Taier version 1.1 was officially released, and the list of new functions is fast
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
How to modify the strict mode under MySQL so that adding new users by inserting user table is successful
十年架构五年生活-07 年轻气盛的蜕变
Application scenarios, key technologies and network architecture of communication perception integration
DNS principle and resolution process
Use of beautifulsoup
8 find subsequences with a maximum length of K
Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change
Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education
树形数据转换
深度解析:什么是Diffusion Model?
学习笔记-uni-app
Derivation of the detailed expansion sto overlap integrals
Thank you for your likes and attention
Local and overall differences between emergence and morphology