当前位置:网站首页>[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;
}边栏推荐
- [MySQL 12] MySQL 8.0.18 reinitialization
- Huawei switches are configured with SSH login remote management switches
- haproxy+keepalived搭建01
- Install cross compiler arm none liunx gnueabihf
- [step on the pit series] MySQL failed to modify the root password
- Go language foundation ----- 06 ----- anonymous fields, fields with the same name
- An article for you to understand - Manchester code
- How does yarn link help developers debug NPM packages?
- Uniapp learning records
- Go language foundation ----- 02 ----- basic data types and operators
猜你喜欢

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

Docker installs MySQL and successfully uses Navicat connection

How to configure GDAL under idea

Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select

UA camouflage, get and post in requests carry parameters to obtain JSON format content

Redis batch startup and shutdown script

Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition

Research shows that breast cancer cells are more likely to enter the blood when patients sleep

Iterm2 setting

The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
随机推荐
2020-12-12
PIP uses image website to solve the problem of slow network speed
一个实习生的CnosDB之旅
Go language foundation ----- 04 ----- closure, array slice, map, package
密西根大学张阳教授受聘中国上海交通大学客座教授(图)
VMware virtual machine configuration static IP
E: 无法定位软件包 ros-melodic-desktop-full
Docker installs MySQL and successfully uses Navicat connection
Enter three times and guess a number
PAT甲级 1029 Median
一篇文章让你读懂-曼彻斯特编码
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
regular expression
Pat class a 1028 list sorting
opensips与对方tls sip trunk对接注意事项
Go language foundation ----- 05 ----- structure
[cocos creator] Click the button to switch the interface
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
[at] ABC 258g - Triangle triples reachable - violence
Technical dry goods | some thoughts on the future of AI architecture