当前位置:网站首页>Solve the brick stacking problem (DP)
Solve the brick stacking problem (DP)
2022-07-28 20:15:00 【Tiredd】
Problem description :

Ideas 1: First, let's talk about a simple idea , But the complexity is slightly higher .
Define the State f[i][j][k] Show consideration i Items , At this time, put it on the left or on the right or not , At this time, the height of the left border is j, The height on the right is k Whether the state of exists .
In this way, a complete state can be described , The answer is f[n][j][j] In Chinese, it means true, And j maximal .
At this time, the state is transferred
1 Don't put :f[i][j][k] = f[i - 1][j][k]
2 Put it on the left side. :f[i][j][k] |= f[i - 1][j - h[i][k]
3 Put it on the right side. :f[i][j][k] |= f[i - 1][j][k - h[i]
In this way, the current state can be calculated ijk Whether it can be transferred from the previous three states , Because space will explode , So use scrolling and array optimization .
The code is as follows :
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, h[N], m;
bool f[2][N][N];
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ ) cin >> h[i], m += h[i];// Input
memset(f, false, sizeof f);// initialization , Initialize the illegal status as false
f[0][0][0] = true;// This is the starting point of the topic
for(int i = 1; i <= n; i ++ )
for(int j = 0; j <= m; j ++ )
for(int k = 0; k <= m; k ++ )
{
f[i&1][j][k] = f[(i - 1)&1][j][k]; // State shift
if(j - h[i] >= 0) f[i&1][j][k] |= f[(i - 1)&1][j - h[i]][k];
if(k - h[i] >= 0) f[i&1][j][k] |= f[(i - 1)&1][j][k - h[i]];
}
int ans = 0;
for(int j = m; j >= 0; j -- )
if(f[n&1][j][j])
{
ans = j;
break;
}
if(ans != 0) cout << ans << endl;
else cout << "impossible" << endl;
return 0;
}
Ideas 2: If the state is described by height difference , Then there is no need to enumerate two heights , Just enumerate a height difference . So definition f[i][j] Before the election i individual , At this time, the height difference is j The height of the lower tower . Then it needs to be right f The state is max. Yes, place No i Bricks need to be classified and discussed .
1 Don't put :f[i][j] = f[i - 1][j]
2 Put it on a shorter brick , At this time, the brick is still shorter than the high one .
f[i][j] = f[i - 1][j + h[i]] + h[i]
3 Put it on a shorter brick , At this time, the brick is higher than the high one .
The original height difference :h[i] - j, So the equation is :f[i][j] = f[i - 1][h[i] - j] + h[i] - j]
4 Put on high bricks
f[i][j] = f[i - 1][j - h[i]] - (j - h[i])
See code for details :
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500001;
int f[51][N], n, h[51], m;
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++ ) scanf("%d", &h[i]), m += h[i];
memset(f, -1, sizeof f);
f[0][0] = 0;
for(int i = 1; i <= n; i ++ )
for(int j = 0; j <= m; j ++ )
{
f[i][j] = f[i - 1][j];// Don't put
if(j + h[i] <= m && f[i - 1][j + h[i]] != -1) f[i][j] = max(f[i][j], f[i - 1][j + h[i]] + h[i]);// Lower the tower , Shorter than the tall tower
if(h[i] - j >= 0 && f[i - 1][h[i] - j] != -1) f[i][j] = max(f[i][j], f[i - 1][h[i] - j] + h[i] - j);// Lower the tower , Higher than the tower
if(j - h[i] >= 0 && f[i - 1][j - h[i]] != -1) f[i][j] = max(f[i][j], f[i - 1][j - h[i]]);// Heighten the tower
}
if(f[n][0] == 0) puts("-1");
else cout << f[n][0] << endl;
return 0;
} 边栏推荐
- HSETNX KEY_ Name field value usage
- [C language] simulation implementation of strlen (recursive and non recursive)
- Deploy LNMP automatically with saltstack
- Andorid system layout, values, drawable adaptation
- C language operators and input and output
- [C language] step jumping problem [recursion]
- 9. Pointer of C language (4) pointer and one-dimensional array, pointer operation
- 基于 MinIO 对象存储保障 Rancher 数据
- 党员故事|李青艾用漫画带动农民增收致富
- C language pointer and two-dimensional array
猜你喜欢

Can China make a breakthrough in the future development of the meta universe and occupy the highland?

Prometheus deployment
![[C language] function](/img/81/c185e2bb5eccc13433483f9558f52a.png)
[C language] function

A chip company fell in round B

In the second half of 2022, the system integration project management engineer certification starts on August 20

9. Pointer of C language (2) wild pointer, what is wild pointer, and the disadvantages of wild pointer

The privatized instant messaging platform protects the security of enterprise mobile business

9. Pointer of C language (4) pointer and one-dimensional array, pointer operation

XOR operation and its usage

9. Pointer of C language (1) what is pointer and how to define pointer variables
随机推荐
Zfoo adds routes similar to mydog
Saltstack advanced
一文读懂如何部署具有外部数据库的高可用 K3s
Source insight project import and use tutorial
local/chain/run_ tdnn.sh:
Common modules of saltstack
[C language] header file of complex number four operations and complex number operations
[NPP installation plug-in]
MySQL command statement (personal summary)
Use of strtok and strError
Theoretical knowledge of digital image (I) (personal analysis)
Simple use of robobrowser
Basic knowledge of C language
83.(cesium之家)cesium示例如何运行
2、 Relationship between software operation and memory
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
Maximum exchange [greedy thought & monotonic stack implementation]
[C language] summary of methods for solving the greatest common divisor
Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
C language - control statement