当前位置:网站首页>[oiclass] maximum formula
[oiclass] maximum formula
2022-07-06 14:48:00 【Liujincheng LJC】
problem : The biggest formula
Title Description
The subject is very simple , give N A digital , Don't change their relative position , Add... In the middle K A multiplication sign and N-K-1 Plus sign ,( Put in parentheses ) Maximize the final result . Because the multiply sign and the plus sign are N-1 A the , So there's just a sign between every two adjacent numbers . for example :
N=5, K=2,5 The numbers are 1、2、3、4、5, Can add :
1*2*(3+4+5)=24
1*(2+3)*(4+5)=45
(1*2+3)*(4+5)=45
...
Input
The input file has two lines , The first line is two integers separated by spaces , Express N and K, among (2<=N<=15, 0<=K<=N-1). Second behavior N A number separated by a space ( Each number is in 0 To 9 Between ).
Output
The output file contains only one integer on one line , Represents the maximum result required
The final result <=maxlongint
Examples
#1
Input
5 2
1 2 3 4 5
Output
120
Previous ideas
Think f[i][j][k] It should be defined like this :
set up f[i][j][k] For in [i , j] Within the interval of k A multiple sign
State transition equation :
f[i][j][k] = max4(f[i+1][j][k]+a[i],f[i][j-1][k]+a[j],
f[i+1][j][k-1]*a[i],f[i][j-1][k-1]*a[j]);
Then the wrong code should be like this :
#include<bits/stdc++.h>
using namespace std;
const int N = 101;
int n,m,a[N],f[N][N][N*11];
int main(){
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=n;i>0;i--){
for(int j=i;j<=n;j++){
for(int k=0;k<=m;k++){
f[i][j][k] = max(f[i+1][j][k]+a[i],
max(f[i+1][j][k-1]*a[i],
max(f[i][j-1][k]+a[j],
f[i][j-1][k-1]*a[j])));
}
}
}
cout << f[1][n][m] << endl;
}
/************************************************************** Language: C++ Result: Wrong answer 50 ****************************************************************/
got 50 branch , Later, I found several error examples and found them by comparison :
This section can be cut from more than two sides , It can also be cut from any position in the middle , If you cycle the middle line , Then the number of multiplication signs can be more or less . At this time f[i][j][k] The initialization of will become -INF This is running max Function to find the optimal solution
On AC Code :
#include<bits/stdc++.h>
#define INF 2147483647
using namespace std;
const int N = 110;
int n,m,a[N],f[N][N][N];
int main(){
cin >> n >> m;
for(int i=1;i<=n;i++){
cin >> a[i];}
for(int i=1;i<=n;i++){
f[i][i][0] = a[i];
}
for(int i=n;i>0;i--){
for(int j=i+1;j<=n;j++){
for(int k=0;k<=min(j-i,m);k++){
if(m-(n-(j-i))>=k){
f[i][j][k]=-INF;
continue;
}
for(int z=i;z<j;z++){
for(int y=0;y<=k;y++){
f[i][j][k] = max(f[i][j][k],f[i][z][y]+f[z+1][j][k-y]);
if(y==0){
f[i][j][k] = max(f[i][z][y]*f[z+1][j][k-y-1],
f[i][j][k]);
}
else{
f[i][j][k] = max(f[i][z][y-1]*f[z+1][j][k-y],
f[i][j][k]);
}
}
}
}
}
}
// cout << f[3][5][1] <<endl;
cout << f[1][n][m] << endl;
}
/************************************************************** Language: C++ Result: correct 100 Time:0 ms Memory:6912 kb ****************************************************************/
边栏推荐
- Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
- Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
- Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
- 数字电路基础(二)逻辑代数
- 数字电路基础(五)算术运算电路
- 《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
- Matplotlib绘图快速入门
- Overview of LNMP architecture and construction of related services
- 《统计学》第八版贾俊平第五章概率与概率分布
- 【指针】数组逆序重新存放后并输出
猜你喜欢

150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview

后台登录系统,JDBC连接数据库,做小案例练习

《统计学》第八版贾俊平第七章知识点总结及课后习题答案

Based on authorized access, cross host, and permission allocation under sqlserver

Query method of database multi table link

High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters

数据库多表链接的查询方式

Intranet information collection of Intranet penetration (2)

Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers

数字电路基础(三)编码器和译码器
随机推荐
《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
Query method of database multi table link
指針:最大值、最小值和平均值
Overview of LNMP architecture and construction of related services
王爽汇编语言详细学习笔记二:寄存器
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
Intranet information collection of Intranet penetration (4)
"If life is just like the first sight" -- risc-v
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
《统计学》第八版贾俊平第一章课后习题及答案总结
Bing Dwen Dwen official NFT blind box will be sold for about 626 yuan each; JD home programmer was sentenced for deleting the library and running away; Laravel 9 officially released | Sifu weekly
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
[pointer] solve the last person left
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
后台登录系统,JDBC连接数据库,做小案例练习
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
“Hello IC World”
flask实现强制登陆
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
Proceedingjoinpoint API use