当前位置:网站首页>Sequence maximum return
Sequence maximum return
2022-06-12 23:22:00 【Star University】
Title Description
Given a length of m Integer sequence of a1,a2,…,am.
The value of each element in the sequence ai All meet 1≤ai≤n.
When a value is i The element and a value of j When elements of are adjacent , The benefits that can be generated are wi,j.
Now? , We can delete up to from the sequence k Elements , After deleting some elements , Elements that are not adjacent may become adjacent .
The sum of the proceeds of the sequence is the sum of the proceeds generated by all adjacent element pairs , For example, a length of 3 Integer sequence of 1,3,2 The income and w1,3+w3,2.
Excuse me, , By using the delete operation , What is the maximum return sum of the sequence that can be obtained ?
Input format
The first line contains three integers n,k,m.
The second line contains m It's an integer a1,a2,…,am.
Next n That's ok , Each row contains n It's an integer , Among them the first i Xing di j The number of columns represents wi,j.
Output format
The maximum return and of the output sequence .
Data range
about 30% The data of ,1≤n,k,m≤20.
about 100% The data of ,1≤n,k,m≤200,0≤wi,j≤107,1≤ai≤n.
Data assurance wi,j=wj,i,wi,i=0.
Examples
sample input :
4 1 3
1 4 2
0 3 0 1
3 0 0 0
0 0 0 0
1 0 0 0
sample output :
3
Sample explanation
The sum of the initial sequence returns is w1,4+w4,2=1+0=1.
Delete the middle 4 after , Sequence 1,2 The income and w1,2=3. Train of thought : DP
Think back to finding the longest ascending subsequence :
use f[j] Before presentation j Number , And take the second j The longest ascending subsequence with the ending number .
This topic :
f[i][j] To express consideration in the form of i The number is the end , Delete j The maximum benefit of the number .
Suppose you delete j After the count , and i The adjacent number is the u Number .
u and i adjacent , u + 1 To i - 1 Be deleted , Deleted i - 1 - u Number , Had in 1 To u Delete again in j - (i - 1 - u) Number .
therefore :f[i][j] = max (f[u, j - (i - 1 - u)] + w[a[u], a[i]]), // State transition equation
among :j - (i - 1 - u) >= 0 And u <= i - 1 And j - (i - 1 - u) <= u.
Explanation of value range :
j - (i - 1 - u) >= 0: front u In number , The number of deleted numbers must be greater than or equal to 0 .
u <= i - 1 : u The biggest is i - 1.
j - (i - 1 - u) <= u : front u In number , The maximum number of deleted is u individual .
Time complexity O(n^3)
reference
C++ Code
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 240;
int n,m,k,res;
int a[N],w[N][N],f[N][N];
int main()
{
cin>>n>>k>>m;
for (int i = 1; i <= m; i ++ )cin>>a[i];
for (int i = 1; i <= n; i ++ )
for (int j = 1;j <= n; j ++ )
cin>>w[i][j];
f[1][0]=0;
for (int i = 1; i <= m; i ++ )
for (int j = 0; j <= k ; j ++ )
for (int u = 1; u < i; u ++ )
if(j>=i - u -1)
f[i][j]=max(f[i][j],f[u][j - (i - u - 1)] + w[a[u]][a[i]]);
for (int i = 0; i <= k; i ++ )
res=max(res,f[m][i]);
return cout<<res,0;
}
Java Code
import java.util.*;
import java.io.*;
public class Main{
static int res;
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int n=cin.nextInt(),k=cin.nextInt(),m=cin.nextInt();
int a[]=new int [240];
int f[][]=new int [240][240];
int w[][]=new int [240][240];
for(int i = 1; i <= m; i++ )a[i]=cin.nextInt();
for(int i = 1; i<= n; i ++ )
for(int j = 1; j<= n; j ++)
w[i][j]=cin.nextInt();
f[1][0]=0;
for (int i = 1; i <= m; i ++ )
for (int j = 0; j <= k ; j ++ )
for (int u = 1; u < i; u ++ )
if(j>=i - u -1)
f[i][j]=Math.max(f[i][j],f[u][j - (i - u - 1)] + w[a[u]][a[i]]);
for (int i = 0; i <= k; i ++ )
res=Math.max(res,f[m][i]);
System.out.println(res);
return ;
}
}GO Code
package main
import "fmt"
const N=240
var (
a[N]int
w[N][N]int
f[N][N]int
)
func main(){
var n,k,m,res int
fmt.Scanf("%d %d %d",&n,&k,&m)
for i:= 1;i <= m; i ++ {
fmt.Scanf("%d",&a[i])
}
for i:= 1; i <= n; i ++ {
for j:= 1; j <= n; j ++ {
fmt.Scanf("%d",&w[i][j])
}
}
f[1][0]=0
for i:= 1; i <=m; i ++ {
for j:= 0; j<=k; j ++ {
for u:= 1; u < i; u ++ {
if j>= i - u - 1{
f[i][j] = max(f[i][j], f[u][j - (i - u - 1)] + w[a[u]][a[i]])
}
}
}
}
for i:= 0; i <= k; i ++ {
res = max(res,f[m][i])
}
fmt.Println(res)
}
func max(a,b int)int{
if a > b{
return a
}
return b
} Train of thought two : DP
Another way of thinking :f[i][j] Said to i Bit end , Delete at most j The maximum benefit of the number
Define an enumeration variable p Express j
from i The subscript of the first undeleted number is i-p-1
The rest can be deleted at most j-p Number
The weight becomes w[a[i-p-1]][a[i]]
State transition equation :f[i][j]=max(f[i][j],f[i-p-1][j-p]+w[a[i-p-1]a[i]]);
The answer to this question is f[m][k];
Time complexity O(n^3)
reference
C++ Code
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int n,m,k;
int w[N][N],f[N][N],a[N];
int main()
{
cin>>n>>k>>m;
for (int i = 1; i <= m; i ++ )cin>>a[i];
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
cin>>w[i][j];
for (int i = 1; i <= m; i ++ )
for (int j = 0; j <= k; j ++ )
for(int p = 0; p <=j; p ++ )
if(i-p-1>=0)
f[i][j]=max(f[i][j],f[i-p-1][j-p]+w[a[i-p-1]][a[i]]);
return cout<<f[m][k],0;
}Java Code
import java.util.*;
import java.io.*;
public class Main{
static int res;
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int n=cin.nextInt(),k=cin.nextInt(),m=cin.nextInt();
int a[]=new int [240];
int f[][]=new int [240][240];
int w[][]=new int [240][240];
for(int i = 1; i <= m; i++ )a[i]=cin.nextInt();
for(int i = 1; i<= n; i ++ )
for(int j = 1; j<= n; j ++)
w[i][j]=cin.nextInt();
for (int i = 1; i <= m; i ++ )
for (int j = 0; j <= k ; j ++ )
for(int p = 0; p <=j; p ++ )
if(i-p-1>=0)
f[i][j]=Math.max(f[i][j],f[i-p-1][j-p]+w[a[i-p-1]][a[i]]);
System.out.println(f[m][k]);
return ;
}
}
GO Code
package main
import "fmt"
const N=240
var (
a[N]int
w[N][N]int
f[N][N]int
)
func main(){
var n,k,m int
fmt.Scanf("%d %d %d",&n,&k,&m)
for i:= 1;i <= m; i ++ {
fmt.Scanf("%d",&a[i])
}
for i:= 1; i <= n; i ++ {
for j:= 1; j <= n; j ++ {
fmt.Scanf("%d",&w[i][j])
}
}
f[1][0]=0
for i:= 1; i <=m; i ++ {
for j:= 0; j<=k; j ++ {
for p:= 0; p <= j; p ++ {
if i - p - 1>= 0{
f[i][j]=max(f[i][j],f[i-p-1][j-p]+w[a[i-p-1]][a[i]])
}
}
}
}
fmt.Println(f[m][k])
}
func max(a,b int)int{
if a > b{
return a
}
return b
}
Welcome to leave a message like
边栏推荐
- CST learning: four element array design of circular patch antenna (III) array formation and parallel excitation
- Is there any risk in opening a securities account? How to open an account safely?
- 深度学习-神经网络:卷积的实现方法【直接法(精度没损失)、GEMM(矩阵乘法,精度没损失)、FFT(傅里叶变换,精度有损失)、Winograd(精度有损失)】
- Report on the "fourteenth five year plan" and strategic strategy recommendations for China's intellectual property protection industry 2022 ~ 2028
- CST learning: four element array design of circular patch antenna (II) array formation and combination results
- Anti aliasing / anti aliasing Technology
- Leetcode1601: the maximum number of building change requests that can be reached (difficult)
- [opencv learning] perspective transformation matrix
- 細數攻防演練中十大關鍵防守點
- Analysis report on production and marketing demand and investment forecast of China's Melamine Industry from 2022 to 2028
猜你喜欢

Design a MySQL table for message queue to store message data
![[web technology] 1348- talk about several ways to implement watermarking](/img/5f/c4f6ba6799202c79d1e9cb7a083952.png)
[web technology] 1348- talk about several ways to implement watermarking

MYSQL 行转列、列转行、多列转一行、一行转多列

ASP. Net core Middleware

Anti aliasing / anti aliasing Technology

Shardingsphere-proxy-5.0.0 deployment table implementation (I)

Alien skin exposure X7 color filter plug-in, raw post-processing tool

Colab教程(超级详细版)及Colab Pro/Colab Pro+使用评测
![[Part 8] semaphore source code analysis and application details [key points]](/img/e2/05c08435d60564aaa1172d2d574675.jpg)
[Part 8] semaphore source code analysis and application details [key points]

数字藏品的发展趋势!
随机推荐
[Part 7] source code analysis and application details of cyclicbarrier [key]
lua 日期时间
Industry reshuffle, a large number of programmers are going to lose their jobs? How can we break the current workplace dilemma
LeetCode —— 26. Remove duplicates from an ordered array
Several Tsinghua students I know have left
Heilongjiang Branch and Liaoning Branch of PostgreSQL Chinese community have been established!
Pytorch中的梯度累加【在实验时,由于GPU显存限制,遇到batch_size不能再增大的情况。为解决该问题,使用梯度累加方法】
MYSQL 行转列、列转行、多列转一行、一行转多列
Pytorch common parameter initialization methods: [uniform distribution, normal (Gaussian) distribution, Xavier, Kaiming, orthogonal matrix, sparse matrix, constant, identity matrix, zero filling]
細數攻防演練中十大關鍵防守點
Flutter库推荐Sizer 可帮助您轻松创建响应式 UI
About three-tier architecture and MVC
Analysis report on the 14th five year development plan and operation mode of China's hazardous waste treatment industry from 2022 to 2028
Analysis report on business model innovation path and operation status of China's app store industry from 2022 to 2028
Abstract methods and abstract classes
PostgreSQL 中文社区黑龙江分会和辽宁分会成立啦!
Opencv source code compilation
House raiding 2
[recommended collection] easy to understand graphic network knowledge - Part 1
Face detection: mtcnn