当前位置:网站首页>P3052 [USACO12MAR]Cows in a Skyscraper G
P3052 [USACO12MAR]Cows in a Skyscraper G
2022-06-25 22:10:00 【AMjieker】
做题 OJ
题意
给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)
解法
dfs+小剪枝
1,如果当前的答案超过ans 显然不合理 剪掉
2,我们可以从大到小选择每一个放在那一个电梯中 缩小枚举空间 (从小到大枚举的空间更多 耗时更长)
代码
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
const int N = 22;
int p[N];
int g[N];
int n,m,ans;
void dfs(int k,int t){
if(t>ans) return;
if(k<1){
ans=min(ans,t);return;
}
for(int i=0;i<t;i++){
if(g[k]+p[i]>m) continue;
p[i]+=g[k];
dfs(k-1,t);
p[i]-=g[k];
}
p[t]=g[k];
dfs(k-1,t+1);
p[t]=0;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>g[i];
sort(g+1,g+1+n);
ans = N*N;
dfs(n,1);
cout<<ans<<endl;
return 0;
}
边栏推荐
- 如何配置SQL Server 2008管理器_过路老熊_新浪博客
- One article explains R & D efficiency! Your concerns are
- 虚析构和纯虚析构及C ++实现
- Jenkins 发布PHP项目代码
- Wireshark对IMAP抓包分析
- 关于运行scrapy项目时提示 ModuleNotFoundError: No module named 'pymongo‘的解决方案
- 数组常用的一些操作方法
- 用ES5的方式实现const
- util. Collection and encapsulation of JS tool functions
- 剑指 Offer 48. 最长不含重复字符的子字符串
猜你喜欢
![mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动](/img/b2/2b87b3cea1422e2a860f5e0e7dcc40.png)
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动

Alipay payment interface sandbox environment test and integration into an SSM e-commerce project

debezium

SSM integrated learning notes (mainly ideas)

Blob

ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided解决

在win10下使用visual studio2015链接mysql数据库

为什么Integer的比较最好使用equals

Database - mongodb

CXF
随机推荐
Unsigned and signed vernacular
为什么Integer的比较最好使用equals
手工制作 pl-2303hx 的USB转TTL电平串口的电路_过路老熊_新浪博客
Megacli常用命令整理
Talk about singleton mode!
SSM integrated learning notes (mainly ideas)
The role of iomanip header file in actual combat
Linking MySQL database with visual studio2015 under win10
SVN
剑指 Offer 48. 最长不含重复字符的子字符串
Today's 61 Fu
中序线索二叉树
Apache Doris1.0版本集群搭建、负载均衡与参数调优
php socket通信中stream_select方法的理解
Px4 multi computer simulation (gazebo)
关于Swoole协程容器
Tensorflow中CSV文件数据读取
STL教程5-STL基本概念及String和vector使用
proxy
Format the number. If the number is not enough, fill in 0, for example, 1:0001,25:0025