题目描述

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:

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

3
2 1 3
1 2
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.

题解:迭代加深搜索

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int n,w,flag,v[],bag[]; bool cmp(int x,int y){
return x>y;
} void dfs(int deep,int now){
if(flag)return;
if(now==n+){
flag=true;
return;
}
for(int i=;i<=min(deep,now);i++){
if(bag[i]+v[now]<=w){
bag[i]+=v[now];
dfs(deep,now+);
bag[i]-=v[now];
}
}
} int main(){
scanf("%d%d",&n,&w);
for(int i=;i<=n;i++)scanf("%d",&v[i]);
sort(v+,v+n+,cmp);
for(int deep=;deep;deep++){
dfs(deep,);
if(flag){
printf("%d\n",deep);
return ;
}
}
return ;
}

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

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

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

  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. “中兴捧月”比赛之——二叉查找树(BST)树的最短路径Java求解

    问题描述: BST树,又称二叉查找树,求其到所有叶子节点路径的最小值 测试用例一:  10 5 20 返回15: 测试用例二: 100 20 70 110 120 10 null null 89 nu ...

  2. linux命令详解之df(6/19)

    df命令作用是列出文件系统的整体磁盘空间使用情况.可以用来查看磁盘已被使用多少空间和还剩余多少空间. df命令显示系统中包含每个文件名参数的磁盘使用情况,如果没有文件名参数,则显示所有当前已挂载文件系 ...

  3. Ubuntu系统常用操作命令

    1.基本命令: sudo 提升用户权限为root用户 ls 显示文件内容 cd 进入指定路径,后接路径参数 如cd /进入根目录 cd -进入用户目录 cd ..返回上一级目录 mv xx.txt x ...

  4. Struts2笔记02——Struts2 概述(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Struts2是基于MVC设计模式的一种流行.成熟的We ...

  5. matplotlib模块之子图画法

    一般化的子图布局 首先要创建各个子图的坐标轴,传入一个四元列表参数:[x,y,width,height],用来表示这个子图坐标轴原点的x坐标.y坐标,以及宽和高.值得注意的是,这四个值的取值范围都是[ ...

  6. Kubernetes lxcfs

    容器实现的基础是NameSpace和Cgroups. NameSpace实现了对容器(进程)的隔离,NameSpace技术实际上修改了应用进程看待整个计算机“视图”,也就是作用域,即它的“视线”被操作 ...

  7. Go 语言defer用法

    defer延迟调用: 1.确保调用在函数结束时发生: 2.defer列表为先进后出: 3.通常在Open/Close  Lock/Unlock中使用. defer调用顺序示例: package mai ...

  8. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812 ...

  9. jquery开发js插件

    1.需要掌握的知识点 1)(function($){...}(jQuery)):实际上就是匿名函数并且函数用()阔起来,形成闭包,外界对其内部函数没有影响 $(function(){…});   jQ ...

  10. 泛型学习第一天:List与IList的区别 (一)

    先看代码: using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace L ...