P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

题目描述

A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.

The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.

给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)

输入输出格式

输入格式:

  • Line 1: N and W separated by a space.

  • Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.

输出格式:

  • Line 1: A single integer, R, indicating the minimum number of elevator rides needed.

  • Lines 2..1+R: Each line describes the set of cows taking

one of the R trips down the elevator. Each line starts with an integer giving the number of cows in the set, followed by the indices of the individual cows in the set.

输入输出样例

输入样例#1:

  1. 4 10
  2. 5
  3. 6
  4. 3
  5. 7
输出样例#1:

  1. 3
  2. 2 1 3
  3. 1 2
  4. 1 4

说明

There are four cows weighing 5, 6, 3, and 7 pounds. The elevator has a maximum weight capacity of 10 pounds.

We can put the cow weighing 3 on the same elevator as any other cow but the other three cows are too heavy to be combined. For the solution above, elevator ride 1 involves cow #1 and #3, elevator ride 2 involves cow #2, and elevator ride 3 involves cow #4. Several other solutions are possible for this input.

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. #define maxn 20
  6. int n,m,a[maxn],b[maxn],mid;
  7. bool flag=;
  8. void dfs(int pos,int num){
  9. if(pos==n+){
  10. flag=;
  11. return;
  12. }
  13. if(flag)return;
  14. for(int i=;i<=num;i++){
  15. if(m-b[i]>=a[pos]){
  16. b[i]+=a[pos];
  17. dfs(pos+,num);
  18. b[i]-=a[pos];
  19. }
  20. }
  21. if(num==mid)return;
  22. b[num+]=a[pos];
  23. dfs(pos+,num+);
  24. b[num+]=;
  25. }
  26. int main(){
  27. scanf("%d%d",&n,&m);
  28. for(int i=;i<=n;i++)scanf("%d",&a[i]);
  29. int ans=n,l=,r=n;
  30. while(l<=r){
  31. mid=(l+r)>>;
  32. memset(b,,sizeof(b));
  33. flag=;
  34. dfs(,);
  35. if(flag)ans=mid,r=mid-;
  36. else l=mid+;
  37. }
  38. printf("%d",ans);
  39. }

100分 迭代加深搜索(二分深度)

  1. /*
  2. f 数组为结构体
  3. f[S].cnt 表示集合 S 最少的分组数
  4. f[S].v 表示集合 S 最少分组数下当前组所用的最少容量
  5. f[S] = min(f[S], f[S - i] + a[i]) (i ∈ S)
  6. 运算重载一下即可。
  7. */
  8. #include<cstdio>
  9. #include<iostream>
  10. int n,m,w;
  11. int a[];
  12. struct node{
  13. int cnt,v;
  14. node operator + (const int b)const{
  15. node res;
  16. if(v+b<=w)res.cnt=cnt,res.v=v+b;
  17. else res.cnt=cnt+,res.v=b;
  18. return res;
  19. }
  20. bool operator < (const node b)const{
  21. if(cnt==b.cnt)return v<b.v;
  22. return cnt<b.cnt;
  23. }
  24. }f[<<];
  25. node min(node x,node y){
  26. if(x<y)return x;
  27. return y;
  28. }
  29. node make_node(int x,int y){
  30. node res;
  31. res.cnt=x;res.v=y;
  32. return res;
  33. }
  34. int main(){
  35. int sta;
  36. scanf("%d%d",&n,&w);
  37. m=(<<n)-;
  38. for(int i=;i<=n;i++)scanf("%d",&a[i]);
  39. for(sta=;sta<=m;sta++){
  40. f[sta]=make_node(1e9,w);
  41. for(int i=;i<=n;i++){
  42. if(!((<<i-)&sta))continue;
  43. f[sta]=min(f[sta],f[(<<i-)^sta]+a[i]);
  44. }
  45. }
  46. printf("%d",f[m].cnt+);
  47. return ;
  48. }

100分 状压dp

洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper的更多相关文章

  1. 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  2. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]

    题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...

  3. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...

  4. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp

    这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...

  5. LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  6. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  7. [USACO12MAR] 摩天大楼里的奶牛 Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  8. [bzoj2621] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta] ...

  9. [luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)

    传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f ...

随机推荐

  1. MFC实现COM组件

    一般而言,ATL实现了对COM组件最好的支持,所以不用MFC实现COM组件.但是MFC实际上也是可以实现COM组件的. 一.MFC DLL优点: MFC com组件可以将MFC的类型作为参数进行传递, ...

  2. bootstrap 左右框多项选择示例

    bootstrap 左右选择框,左边框是未选项,右边框是已选择项,提供单选,全选按钮,以及取消已选项,如图示:

  3. POJ2442:Sequence

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=2442 我们先简化题意,假设只有两 ...

  4. VS Code:快捷方式

    转于:vscode: Visual Studio Code 常用快捷键 博主:魚魚 更多操作参见官网:https://code.visualstudio.com/docs/getstarted/key ...

  5. makefile 基础知识

    $@    目标文件名 $< 第一个依赖文件名 $^ 规则所有依赖文件列表 如果不想让执行语句被打印出来,就在语句前面加上@符号 模式规则 %.o:%.c 后缀规则 .c.o 生成单进程的Mak ...

  6. 用python做的windows和linx文件夹同步。解决自动同步、加快传输大量小文件的速度、更丰富的文件上传过滤设置。

    现在工具不好用,用的pycharm自动同步,但对于git拉下来的新文件不能自动上传到linux,只有自己编辑过或者手动ctrl + s的文件才会自动同步.导致为了不遗漏文件,经常需要全量上传,速度非常 ...

  7. oracle中创建sequence指定起始值

    oracle中创建sequence指定起始值 DECLARE V_Area_Id NUMBER; BEGIN SELECT MAX(T.Area_Id)+10 INTO V_Area_Id FROM ...

  8. DataTable列查询加排序

    DataTable列查询加排序 DataRow[] drArray = dt.Select("ANLYCOM_ID='" + chSPrdtStblAnly.AnlyComId + ...

  9. #np.random.normal,产生制定分布的数集(默认是标准正态分布)

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html #np.random.normal,产生制定分 ...

  10. zabbix 系列 (1)安装

    安装server http://blog.csdn.net/xiegh2014/article/details/54988548 安装 agent http://m.blog.csdn.net/wu2 ...