[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, v;
 9     qwq(int cnt = 0, int v = 0) : cnt(cnt), v(v) {}
10 }f[1 << 19];
11
12 inline qwq operator + (const qwq x, const int d)
13 {
14     return x.v + d <= w ? qwq(x.cnt, x.v + d) : qwq(x.cnt + 1, d);
15 }
16
17 inline bool operator < (const qwq x, const qwq y)
18 {
19     return x.cnt == y.cnt ? x.v < y.v : x.cnt < y.cnt;
20 }
21
22 inline qwq min(qwq x, qwq y)
23 {
24     return x < y ? x : y;
25 }
26
27 inline int read()
28 {
29     int x = 0, f = 1;
30     char ch = getchar();
31     for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
32     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
33     return x * f;
34 }
35
36 int main()
37 {
38     int i, S;
39     n = read();
40     w = read();
41     m = (1 << n) - 1;
42     for(i = 1; i <= n; i++) a[i] = read();
43     for(S = 1; S <= m; S++)
44     {
45         f[S] = qwq(1e9, w);
46         for(i = 1; i <= n; i++)
47         {
48             if(!((1 << i - 1) & S)) continue;
49             f[S] = min(f[S], f[(1 << i - 1) ^ S] + a[i]);
50         }
51     }
52     printf("%d\n", f[m].cnt + 1);
53     return 0;
54 }

时间: 2024-12-05 11:24:50

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

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

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

【题解】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

[BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)

[BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given

P2986 [USACO10MAR]伟大的奶牛聚集(思维,dp)

题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of course, she would like to choose the most convenient location for the gathering to take place. Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会

TYVJ P1023 奶牛的锻炼 Label:dp

背景 USACO 描述 奶牛Bessie有N分钟时间跑步,每分钟她可以跑步或者休息.若她在第i分钟跑步,可以跑出D_i米,同时疲倦程度增加1(初始为0).若她在第i分钟休息,则疲倦程度减少1.无论何时,疲倦程度都不能超过M.另外,一旦她开始休息,只有当疲惫程度减为0时才能重新开始跑步.在第N分钟后,她的疲倦程度必须为0. 输入格式 第一行,两个整数,代表N和M.接下来N行,每行一个整数,代表D_i. 输出格式 Bessie想知道,她最多能跑的距离. 测试样例1 输入 5 2 5 3 4 2 10

HDU3045 Picnic Cows(斜率优化DP)

Picnic Cows Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2192    Accepted Submission(s): 675 Problem Description It’s summer vocation now. After tedious milking, cows are tired and wish to t

POJ 3186 Treats for the Cows (简单区间DP)

FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many reasons