当前位置:网站首页>(POJ - 3186) treatments for the cows (interval DP)
(POJ - 3186) treatments for the cows (interval DP)
2022-07-06 16:23:00 【AC__ dream】
Topic link :3186 -- Treats for the Cows
The question : Here you are. n Numbers that have been arranged , Each time you can take out the leftmost number or the rightmost number , altogether n Take the next time . Suppose you i The number taken next is x, You can get i*x The value of . You need to plan the retrieval sequence , Maximize the total value obtained .
analysis : This problem is easy to be mistaken for greed , Select the minimum value that can be selected each time , Such greedy thinking is wrong .
The following is the analysis of the correct idea :
We set up dp[i][j] It means to take it out from the left i Take them out from the right j In all cases, the result is the maximum , This state can be transferred from two states , One is dp[i-1][j], That is, take it out from the left i-1 Take them out from the right j Circumstances , That is to say, the next number we want to take is i Number , The other is dp[i][j-1], That is, take it out from the left i Take them out from the right j-1 Circumstances , Then the next number we want to take is number n-j+1 Number. , It can only be transferred by these two situations , Then the state transition equation is easy to write , Namely
for(int i=0;i<=n;i++)
for(int j=0;i+j<=n;j++)
{
if(i>0) dp[i][j]=max(dp[i][j],dp[i-1][j]+a[i]*(i+j));// Currently selected No i Elements
if(j>0) dp[i][j]=max(dp[i][j],dp[i][j-1]+a[n-j+1]*(i+j));// Currently selected No n-j+1 Elements
}Attention should be paid to the boundary problem , Nothing else , The complete code is attached below :
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=2003;
int dp[N][N];//dp[i][j] It means taking from the left i The number is taken from the right j The maximum number of
int a[N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=0;i<=n;i++)
for(int j=0;i+j<=n;j++)
{
if(i>0) dp[i][j]=max(dp[i][j],dp[i-1][j]+a[i]*(i+j));// Currently selected No i Elements
if(j>0) dp[i][j]=max(dp[i][j],dp[i][j-1]+a[n-j+1]*(i+j));// Currently selected No n-j+1 Elements
}
int ans=0;
for(int i=0;i<=n;i++)
ans=max(ans,dp[i][n-i]);
printf("%d",ans);
return 0;
}边栏推荐
- 300th weekly match - leetcode
- Codeforces Round #803 (Div. 2)A~C
- Educational Codeforces Round 130 (Rated for Div. 2)A~C
- Codeforces Round #798 (Div. 2)A~D
- HDU - 6024 building shops (girls' competition)
- Codeforces Round #802(Div. 2)A~D
- Codeforces - 1526C1&&C2 - Potions
- Flask框架配置loguru日志庫
- 860. Lemonade change
- Programmers, what are your skills in code writing?
猜你喜欢

树莓派4B64位系统安装miniconda(折腾了几天终于解决)

Openwrt source code generation image

Flask框架配置loguru日志库

Data storage in memory & loading into memory to make the program run

去掉input聚焦时的边框

QT模拟鼠标事件,实现点击双击移动拖拽等

QWidget代码设置样式表探讨

Advancedinstaller安装包自定义操作打开文件

Sword finger offer II 019 Delete at most one character to get a palindrome

B - Code Party (girls' competition)
随机推荐
QWidget代码设置样式表探讨
Pyside6 signal, slot
2027. Minimum number of operations to convert strings
Kubernetes cluster deployment
去掉input聚焦时的边框
Openwrt source code generation image
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
Advancedinstaller安装包自定义操作打开文件
Codeforces Round #797 (Div. 3)无F
1855. Maximum distance of subscript alignment
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
MariaDB的安装与配置
Bidirectional linked list - all operations
QT模拟鼠标事件,实现点击双击移动拖拽等
QT实现窗口渐变消失QPropertyAnimation+进度条
Truck History
QT implementation fillet window
Borg maze (bfs+ minimum spanning tree) (problem solving report)
图图的学习笔记-进程
Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés