演算法訓練 最大的算式 線性DP

2021-09-01 14:43:58 字數 1116 閱讀 3548

時間限制:1.0s   記憶體限制:256.0mb

問題描述

題目很簡單,給出n個數字,不改變它們的相對位置,在中間加入k個乘號和n-k-1個加號,(括號隨便加)使最終結果盡量大。因為乘號和加號一共就是n-1個了,所以恰好每兩個相鄰數字之間都有乙個符號。例如:

n=5,k=2,5個數字分別為1、2、3、4、5,可以加成:

1*2*(3+4+5)=24

1*(2+3)*(4+5)=45

(1*2+3)*(4+5)=45

……輸入格式

輸入檔案共有二行,第一行為兩個有空格隔開的整數,表示n和k,其中(2<=n<=15, 0<=k<=n-1)。第二行為 n個用空格隔開的數字(每個數字在0到9之間)。

輸出格式

輸出檔案僅一行包含乙個整數,表示要求的最大的結果

樣例輸入

5 21 2 3 4 5

樣例輸出

樣例說明

(1+2+3)*4*5=120

#include#include#include#include#include#include#include#include#include#include#include#include#include#include#define mod 998244353

#define inf 0x3f3f3f3f

#define min 0xc0c0c0c0

#define mst(a) memset(a,0,sizeof(a))

#define f(i,a,b) for(int i=a;iusing namespace std;

typedef long long ll;

static const int max_n = 1e5 + 5;

static const double pi = acos(-1);

int v[20];

ll dp[20][20];

int main()

for(int i = 1; i <= n; i++)

for(int i = 2; i <= n; i++)}}

printf("%lld\n", dp[n][m]);

}return 0;

}

演算法訓練 最大的算式

問題描述 題目很簡單,給出n個數字,不改變它們的相對位置,在中間加入k個乘號和n k 1個加號,括號隨便加 使最終結果盡量大。因為乘號和加號一共就是n 1個了,所以恰好每兩個相鄰數字之間都有乙個符號。例如 n 5,k 2,5個數字分別為1 2 3 4 5,可以加成 1 2 3 4 5 24 1 2 ...

演算法訓練 最大的算式

問題描述 題目很簡單,給出n個數字,不改變它們的相對位置,在中間加入k個乘號和n k 1個加號,括號隨便加 使最終結果盡量大。因為乘號和加號一共就是n 1個了,所以恰好每兩個相鄰數字之間都有乙個符號。例如 n 5,k 2,5個數字分別為1 2 3 4 5,可以加成 1 2 3 4 5 24 1 2 ...

藍橋網 演算法訓練 最大的算式

問題描述 題目很簡單,給出n個數字,不改變它們的相對位置,在中間加入k個乘號和n k 1個加號,括號隨便加 使最終結果盡量大。因為乘號和加號一共就是n 1個了,所以恰好每兩個相鄰數字之間都有乙個符號。例如 n 5,k 2,5個數字分別為1 2 3 4 5,可以加成 1 2 3 4 5 24 1 2 ...