当前位置:网站首页>[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
2022-07-03 07:54:00 【eva_can(not)survive】
[USACO12MAR]Cows in a Skyscraper G - 洛谷https://www.luogu.com.cn/problem/P3052继续学习我的状压dp,一道状压入门好题还是继续考虑状态和该状态下的量,我发现状压的很多题都要记录该状态的量。
这道题就是记录目前牛的坐上电梯的状态,然后记录该状态下最后一班电梯的剩余,然后开始考虑其他牛牛能不能进来,或者是去下一班电梯。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
using P = pair<int, int>;
int n, W;
int a[MAXN];
int f[MAXN];
int g[MAXN];
int main() {
int n, W;
scanf("%d %d", &n, &W);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
memset(f, 0x3f, sizeof(f));
f[0] = 1;
g[0] = W;
for (int i = 0; i < (1 << n); i++) {
for (int j = 1; j <= n; j++) {
if (i & 1 << (j - 1))
continue;
if (g[i] >= a[j] && f[i | (1 << (j - 1))] >= f[i]) {
f[i | (1 << (j - 1))] = f[i];
if (f[i | (1 << (j - 1))] == f[i])
g[i | (1 << (j - 1))] = max(g[i | 1 << (j - 1)], g[i] - a[j]);
else
g[i | (1 << (j - 1))] = g[i] - a[j];
} else if (g[i] < a[j] && f[i | (1 << (j - 1))] >= f[i] + 1) {
f[i | (1 << (j - 1))] = f[i] + 1;
if (f[i | (1 << (j - 1))] == f[i] + 1)
g[i | (1 << (j - 1))] = max(g[i | (1 << (j - 1))], W - a[j]);
else
g[i | (1 << (j - 1))] = W - a[j];
}
}
}
printf("%d\n", f[(1 << n) - 1]);
return 0;
}边栏推荐
- Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition
- [cocos creator] get the resource UUID
- Huawei switch: configure Telnet, SSH and web access
- idea取消引用显示效果
- Redis配置文件
- 多旅行商问题——公式和求解过程概述
- Go language foundation ----- 08 ----- interface
- [step on the pit series] MySQL failed to modify the root password
- Iterm2 setting
- Register keyword
猜你喜欢

【LeetCode】2. Valid parentheses · valid parentheses

Go language foundation ------ 14 ------ gotest

Install cross compiler arm none liunx gnueabihf

Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!

JS common basic case sorting (continuous update)

Go language foundation ----- 13 ----- file

Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does

Go language foundation ----- 04 ----- closure, array slice, map, package

Go language foundation ----- 10 ----- string related operations (operation function, string conversion)

Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
随机推荐
华为交换机:配置telnet和ssh、web访问
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
C2-关于VCF文件合并的几种方法
PAT甲级 1027 Colors in Mars
Usage of requests module
Go language foundation ----- 06 ----- anonymous fields, fields with the same name
What is a data type? What is the use of data types?
Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
An intern's journey to cnosdb
static关键字
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
*p++、*++p、++*p、(*p)++
Go language foundation ----- 15 ----- reflection
Pat grade a 1027 colors in Mars
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
[cocos creator] get the resource UUID
Redis批量启停脚本
E: 无法定位软件包 ros-melodic-desktop-full
什么是数据类型?数据类型有什么用?
regular expression