【题解】Luogu P3052 【USACO12】摩天大楼里的奶牛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: 复制

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.

思路

  • 迭代加深搜索:这是一种类似广搜的深搜,但是它不需要广搜如此大的空间,它的空间与深搜的空间一样
    可以看做带深度限制的DFS。
    首先设置一个搜索深度,然后进行DFS,当目前深度达到限制深度后验证当前方案的合理性,更新答案。
    不断调整搜索深度,直到找到最优解。
    本题中枚举电梯数num,就是搜索的深度
  • 剪枝: 可证第i奶牛放到i后车厢没有意义

代码

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
int n, m, c[19], tot(0), ans(0), v[19];
bool dfs(int x, int num){
    for(re i=1;i<=x&&i<=num;++i)
        if(v[i]+c[x]<=m){
            v[i]+=c[x];
            if(x==n) return 1;
            if(dfs(x+1,num)) return 1;
            v[i]-=c[x];
        }
    return 0;
}
int main(){
    scanf("%d%d",&n,&m);
    for(re i=1;i<=n;++i) scanf("%d", &c[i]);
    for(re i=1;i<=n;++i){//枚举厢数
        memset(v,0,sizeof(v));
        if (dfs(1,i)){
            printf("%d\n",i);
            break;
        }
    }
    return 0;
}

?

原文地址:https://www.cnblogs.com/bbqub/p/8469587.html

时间: 2024-08-07 04:06:41

【题解】Luogu P3052 【USACO12】摩天大楼里的奶牛Cows in a Skyscraper的相关文章

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

P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 想状压dp正解是不可能的,这辈子都不可能的 如果物品的分组是连续的,那么就是个小学组问题了:直接遍历贪心搞即可. 据说还有dp方法:\(dp[i]\)为前\(i\)个物品的最小分组,如果某段区间的和大于\(W\),那么就转移一下.利用前缀和并不难写. 像这道题这种无序分组的问题,其实可以通过各种打乱顺序化

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

题目描述 给出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

[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 proble

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

传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f[S], f[S - i] + a[i]) (i ∈ S) 运算重载一下即可. ——代码 1 #include <cstdio> 2 #include <iostream> 3 4 int n, m, w; 5 int a[19]; 6 struct qwq 7 { 8 int cnt

题解 luogu P1850 【换教室】

题解 luogu P1850 [换教室] 时间:2019.8.6 一晚上(约 3.5h 写完) 题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有 \(2n\) 节课程安排在 \(n\) 个时间段上.在第 \(i\)(\(1 \leq i \leq n\))个时间段上,两节内容相同的课程同时在不同的地点进行,其中,牛牛预先被安排在教室 \(c_i\) 上课,而另一节课程在教室 \(d_i\) 进行. 在不提交任何申请的情况下,学生们需要

题解 luogu P5021 【赛道修建】

题解 luogu P5021 [赛道修建] 时间:2019.8.9 20:40 时间:2019.8.12 题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 \(m\) 条赛道. C 城一共有 \(n\) 个路口,这些路口编号为 \(1,2,\dots,n\),有 \(n-1\) 条适合于修建赛道的双向通行的道路,每条道路连接着两个路口.其中,第 \(i\) 条道路连接的两个路口编号为 \(a_i\) 和 \(b_i\),该道路的长度为 \(l_i\).借助这 \(n-1\) 条

题解 Luogu P2499: [SDOI2012]象棋

关于这道题, 我们可以发现移动顺序不会改变答案, 具体来说, 我们有以下引理成立: 对于一个移动过程中的任意一个移动, 若其到达的位置上有一个棋子, 则该方案要么不能将所有棋子移动到最终位置, 要么可以通过改变顺序使这一次移动合法 证明: 考虑到达位置上的那个棋子, 如果它没有到达最终位置, 则我们考虑将该棋子移至下一步, 如果下一步还有没有到达最终位置的棋子, 则也移动它 否则直接调换这两个棋子的移动顺序即可 好的我们去除了题目中的要求: 「移动过程中不能出现多颗棋子同时在某一格的情况」, 接

题解 Luogu P3370

讲讲这题的几种做法: 暴力匹配法 rt,暴力匹配,即把字符串存起来一位一位判相等 时间复杂度$ O(n^2·m) $ 再看看数据范围 $n\le10^5,m\le10^3$ 当场爆炸.当然有暴力分 代码(20pts): #include <bits/stdc++.h> using namespace std; char c[100001][1001]; bool pd(int x, int y) { int l1 = strlen(c[x]), l2 = strlen(c[y]); if(l1

题解-luogu P3810三维偏序(陌上花开)

\(\rm三维偏序\) \(\rm一.定义:序列上每个点有三个权值~(x,y,z)~,求有多少个点对~(a,b)~同时满足~a_x \le b_x~,~a_y \le b_y~,~a_z \le b_z~\) \(\rm二.解法:~cdq~分治\) \(\rm\qquad首先,我们思考一下二维偏序的解法\) \(\rm\qquad\qquad我们先对整个序列按照~x~排一遍序,那么我们就已经解决了一个维度\) \(\rm\qquad\qquad即目前序列中所有后面的点的~x~值一定比前面的点小\