当前位置:网站首页>洛谷 P3052 [USACO12MAR]Cows in a Skyscraper G
洛谷 P3052 [USACO12MAR]Cows in a Skyscraper G
2022-07-27 10:33:00 【ThXe】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[20][300000];//dp[i][j]第i组 状态为j的电梯已经有的重量
int w[20];//每头牛的重量
int n, m;
int main()
{
cin >> n >> m;
int u = 1<<n;
for (int i = 0; i < n; i++)
cin >> w[i];
memset(dp, 0x3f,sizeof dp);//初始化为无穷大表示当前情况不存在
for (int i = 0; i < n; i++)
dp[1][1 << i] = w[i];//一开始初始化 每个牛都有可能在第一组,先放进去
for (int i = 0; i <= n; i++)
for (int j = 0; j < u; j++)//枚举每种状态000000-111111
if (dp[i][j] != 0x3f3f3f3f)//如果当前状态存在
for (int k = 0; k < n; k++)
{
if (j & (1 << k))continue;//枚举状态j对于每个状态j判断这次里面有没有第k头牛,如果已经有第k头牛不参与讨论
if (dp[i][j] + w[k] <= m)dp[i][j | (1 << k)] = min(dp[i][j] + w[k],dp[i][j|(1<<k)]);
//没有第k头牛,且放入重量小于上限m就可以分到第i组,直接放入
//j|(1<<k)就是把j的第k位变成1 例如j=100(2) k=(1<<1)=010(2) j|k=110
else dp[i + 1][j | (1 << k)] = min(dp[i][j | (1 << k)], w[k]);
//对于状态j放不进去,就只能放入第i+1组,且状态变成有k,第i+1组现在没有牛所以为w[i],要与第i组有j取个最小值,保证为最优解,
}
for (int i = 0; i <= n; i++)
{
if (dp[i][u - 1] != 0x3f3f3f3f)//第i组 j为全1 说明所有牛都被选中了,输出答案即可
{
cout << i; break;
}
}
}
边栏推荐
猜你喜欢

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

Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process

迭代次数和熵之间关系的一个验证试验

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

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

FAQs of "relay chain" and "dot" in Poka ecosystem

KEPServer配置

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

推导STO双中心动能积分的详细展开式

Kangaroo cloud stack based on CBO in spark SQL optimization
随机推荐
Overview of data security in fog computing
Symmetric encryption and asymmetric encryption
Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
Review and Prospect of encrypted traffic identification based on deep learning
The open source project Taier version 1.1 was officially released, and the list of new functions is fast
Learning notes uni app
最短移动距离和形态复合体的熵
9 UAV array
tensorflow运行报错解决方法
Taishan Office Technology Lecture: scaling and opening files
Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source
Verilog implementation of ECG signal acquisition, storage and transmission system based on FPGA
Learning notes - wechat payment
pyquery 的使用
Real time development platform construction practice, in-depth release of real-time data value - 04 live broadcast review
8 find subsequences with a maximum length of K
Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
Application of 5g private network in smart medicine
ASP.NET Core依赖注入之旅:1.理论概念
The second method of calculating overlapping integral