当前位置:网站首页>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;
}
边栏推荐
- Use of strtok and strError
- JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
- Labelme (I)
- Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
- Read how to deploy highly available k3s with external database
- 8. Compilation errors of C language and Chinese explanation
- Communication learning static routing across regional networks
- 1、 Relationship among CPU, memory and hard disk
- 【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)
- Why is there no log output in the telnet login interface?
猜你喜欢
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
[C language] simulation implementation of strlen (recursive and non recursive)
数字滤波器设计——Matlab
Deploy LNMP automatically with saltstack
9. Pointer of C language (1) what is pointer and how to define pointer variables
How to automatically store email attachments in SharePoint
Basic usage of docker
Tencent cloud deployment lamp_ Experience of building a station
Merge sort template
跨区域网络的通信学习静态路由
随机推荐
WPF--实现WebSocket服务端
local/chain/run_ tdnn.sh:
软考中级(系统集成项目管理工程师)高频考点
How to automatically store email attachments in SharePoint
4. Const and difine and the problem of initializing arrays with const and define
Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Digital filter design matlab
“中国网事·感动2022”二季度网络感动人物评选结果揭晓
zfoo增加类似于mydog的路由
C language array and bubble sort
一文读懂如何部署具有外部数据库的高可用 K3s
[C language] Hanoi Tower problem [recursion]
Longest Palindromic Substring
[C language] guessing numbers game [function]
Solve the problem of adding the least number of parentheses (interval DP)
Array method added in ES6
河北邯郸:拓展基层就业空间 助力高校毕业生就业
Source insight project import and use tutorial
JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
How can Plato obtain premium income through elephant swap in a bear market?