当前位置:网站首页>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;
} 边栏推荐
- In the second half of 2022, the system integration project management engineer certification starts on August 20
- adb remount of the / superblock failed: Permission denied
- Wildcard ssl/tls certificate
- Why is customer support important to SaaS?
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- mmo及时战斗游戏中的场景线程分配
- local/chain/run_ tdnn.sh:
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
- JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs
- Edge detection and connection of image segmentation realized by MATLAB
猜你喜欢
![[C language] Fibonacci sequence [recursion and iteration]](/img/02/6cff776db583f1b149686e15649d41.png)
[C language] Fibonacci sequence [recursion and iteration]

English translation Arabic - batch English translation Arabic tools free of charge

Getting started with enterprise distributed crawler framework

JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

Idea properties file display \u solution of not displaying Chinese

What is the process of swing event processing?

【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)

通信网络基础知识01
![[C language] step jumping problem [recursion]](/img/0c/32870484e89b494e41068f7c38b08e.png)
[C language] step jumping problem [recursion]
随机推荐
数字滤波器设计——Matlab
How can Plato obtain premium income through elephant swap in a bear market?
8. Compilation errors of C language and Chinese explanation
[C language] Pointer advanced knowledge points
local/chain/run_ tdnn.sh:
Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
为什么客户支持对SaaS公司很重要?
9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
Store and guarantee rancher data based on Minio objects
[C language] initial C language reflection and summary
Labelme (I)
How to use pycharm to quickly create a flask project
Multi-Modal Knowledge Graph Construction and Application: A Survey
Const pointer of C language and parameter passing of main function
Implementation of strcat in C language
[C language] header file of complex number four operations and complex number operations
Richpedia: A Large-Scale, Comprehensive Multi-Modal Knowledge Graph
C language - control statement
English Translation Spanish - batch English Translation Spanish tools free of charge
C language array and bubble sort