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 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.

状压Dp

见代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int inf=0x3f3f3f3f;
 6 int n,v,w[18],end;
 7 int dp[20][1<<20];
 8 int main() {
 9     /*
10         此处状态为前i个电梯,(bit)j的最小重量
11         dp[i][j]=>dp[i][j|(1<<k)]||dp[i+1][j|(1<<k)] ,!(k&(1<<k))
12     */
13     scanf("%d%d",&n,&v),end=1<<n;
14     for(int i=0;i<n;i++) scanf("%d",&w[i]);
15     for(int i=0;i<n;i++) for(int j=0;j<end;j++) dp[i][j]=inf;
16     for(int i=0;i<n;i++) dp[1][1<<i]=w[i];
17     for(int i=0;i<=n;i++) for(int j=0;j<end;j++) if(dp[i][j]!=inf)//存在
18     for(int k=0;k<n;k++) if(!(j&(1<<k)))//不存在
19     if(dp[i][j]+w[k]<=v) dp[i][j|(1<<k)]=min(dp[i][j|(1<<k)],dp[i][j]+w[k]);
20     else dp[i+1][j|(1<<k)]=min(dp[i][j|(1<<k)],w[k]);
21     for(int i=0;i<=n;i++) if(dp[i][end-1]!=inf) {
22         printf("%d\n",i);
23         break;
24     }
25     return 0;
26 }
时间: 2024-08-28 02:30:20

P3052 [USACO12MAR]摩天大楼里的奶牛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\),那么就转移一下.利用前缀和并不难写. 像这道题这种无序分组的问题,其实可以通过各种打乱顺序化

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

BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper

首先比较容易想到是状态压缩DP 令$f[S]$表示选取了集合$S$以后,已经送了最少次数cnt且当前电梯剩下的体积rest最大(即$f[S]$是一个二元组(cnt, rest)) 于是$f[S] = min_{i \in S} f[S - {i}] + v[i]$ $<$和$+$运算详情就请看程序好了,反正就是一个贪心思想,总复杂度$O(n * 2 ^ {n - 1})$ 1 /***********************************************************

动态规划之子集枚举

子集枚举DP P3959 宝藏 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了\(n\)个深埋在地下的宝藏屋, 也给出了这\(n\)个宝藏屋之间可供开发的\(m\)条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋中的宝藏.但是,每个宝藏屋距离地面都很远, 也就是说,从地面打通一条到某个宝藏屋的道路是很困难的,而开发宝藏屋之间的道路 则相对容易很多. 小明的决心感动了考古挖掘的赞助商,赞助商决定免费赞助他打通一条从地面到某 个宝藏屋的通道,通往哪个宝藏屋则由小明来决定. 在此基础

bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set

Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1..10^9]:Xi,Yi∈整数.当满足下列两个条件之一,两只奶牛i和j是属于同一个群的: 1.两只奶牛的曼哈顿距离不超过C(1≤C≤10^9),即lXi - xil+IYi - Yil≤C. 2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群.

[poj2181]jumping cows

poj 2181 Jumping Cows Description Farmer John's cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump. The local witch doctor has mixed up P (1 <= P <= 150,000) potions to aid the

【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 并查集+Treap/STL-set

题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1..10^9]:Xi,Yi∈整数.当满足下列两个条件之一,两只奶牛i和j是属于同一个群的: 1.两只奶牛的曼哈顿距离不超过C(1≤C≤10^9),即lXi - xil+IYi - Yil≤C. 2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群. 给出奶牛们的位置,请计算