当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

平衡二叉树AVL

解析產品開發失敗的5個根本原因

Leetcode-1528- rearrange string - hash table - string

SSM整合学习笔记(主要是思路)

7.常用指令(下)v-on,v-bind,v-model的常见操作

Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or

兆欧表电压档位选择_过路老熊_新浪博客

权限设计=功能权限+数据权限

关于运行scrapy项目时提示 ModuleNotFoundError: No module named 'pymongo‘的解决方案

使用百度地图API在地图中设置一个覆盖物(InfoWindow),可自定义窗口内容
随机推荐
php性能优化
JS中常用知识点
php中使用google protobuf协议环境配置
Leetcode-1528- rearrange string - hash table - string
WordPress
Uniapp -- list page of multi header tabs
实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式
IDEA中如何生成get/set方法
一文讲透研发效能!您关心的问题都在
The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
WINCC与STEP7的仿真连接_过路老熊_新浪博客
DPVS-FullNAT模式部署篇
解析产品开发失败的5个根本原因
line-height小用
idea 查看单元测试覆盖率
用ES5的方式实现const
Record a simple question with ideas at the moment of brushing leetcode - Sword finger offer 09 Implementing queues with two stacks
Analyse des cinq causes profondes de l'échec du développement de produits
About the swoole coroutine container
Uniapp -- the use of document collation and push of unipush