当前位置:网站首页>[usaco12mar]cows in a skyscraper g (state compression DP)
[usaco12mar]cows in a skyscraper g (state compression DP)
2022-07-03 07:58:00 【eva_ can(not)survive】
[USACO12MAR]Cows in a Skyscraper G - Luogu https://www.luogu.com.cn/problem/P3052 Continue to learn my state pressure dp, A good question for getting started with shape pressing is to continue to consider the state and the quantity in that state , I found that many questions of state pressure need to record the quantity of this state .
This problem is to record the current state of cattle taking the elevator , Then record the remaining of the last elevator in this state , Then start to think about whether other cows can come in , Or go to the next elevator .
#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;
}边栏推荐
- PHP common sorting algorithm
- [MySQL 12] MySQL 8.0.18 reinitialization
- Install cross compiler arm none liunx gnueabihf
- Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task
- 华为S5700交换机初始化和配置telnet,ssh用户方法
- P1896 [SCOI2005] 互不侵犯(状压dp)
- Getting started with minicom
- Go language - loop statement
- Zohocrm deluge function application time verification
- 使用 FileChannel 进行文件的复制拷贝
猜你喜欢

Pat grade a 1029 median

EtherCAT state machine transition (ESM)

What to do after the browser enters the URL

Pat grade a 1027 colors in Mars

Install cross compiler arm none liunx gnueabihf

IP production stream is so close to me

Oracle queries grouped by time

Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“

Unity performance optimization

the installer has encountered an unexpected error installing this package
随机推荐
Unity dotween sequence animation replay problem.
Lua hot update basic grammar
Zohocrm deluge function application time verification
Precautions for opensips and TLS SIP trunk docking
Pat class a 1031 Hello world for u
Generate video using clipout in viz engine
What is a data type? What is the use of data types?
Redis profile
haproxy+keepalived搭建01
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
【LeetCode】4. Best time to buy and sell stock
Install cross compiler arm none liunx gnueabihf
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
Uniapp learning records
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
微软安全响应中心
Screenshot tool snipaste