当前位置:网站首页>Method number problem for solving sum of numbers (knapsack problem)
Method number problem for solving sum of numbers (knapsack problem)
2022-07-28 20:14:00 【Tiredd】
Title Description :
Problem description : Given a n An array of positive integers a And an integer sum, Find selection array a The sum of the middle part of the figure is sum Number of alternatives . If the subscript of a number is different between the two selection schemes , It is considered to be a different scheme .
Input description : Input two lines , The first 1 Two positive integers n(1≤n≤1000)、sum(1≤sum≤1000), The first 2 Behavior n A positive integer a[i](32 An integer ), Space off .
Output description : Output the number of solutions .
sample input :
5 15
5 5 10 2 3
sample output :
4
Ideas : The classical knapsack problem is to find the number of solutions , Definition f[i][j] For the past i Number selected , And for j Number of schemes . Then the state transfer equation is
No second choice i Number :f[i][j] += f[i - 1][j]
Choose the first i Number :f[i][j] += f[i - 1][j - w[i]
Here we use rolling array optimization , See code for details :
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1010;
int n, m, w[N], f[N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) cin >> w[i];
f[0] = 1;
for(int i = 1; i <= n; i ++ )
for(int j = m; j >= w[i]; j -- )
f[j] += f[j - w[i]];
cout << f[m] << endl;
return 0;
}边栏推荐
- 河北邯郸:拓展基层就业空间 助力高校毕业生就业
- Richpedia: A Large-Scale, Comprehensive Multi-Modal Knowledge Graph
- NEIL: Extracting Visual Knowledge from Web Data
- Source code analysis of scripy spider
- Basic usage of docker
- Wildcard ssl/tls certificate
- C language - data type
- Basic knowledge of communication network 01
- robobrowser的简单使用
- Deploy ZABBIX automatically with saltstack
猜你喜欢

JVM(二十四) -- 性能监控与调优(五) -- 分析GC日志

2. Floating point number, the difference between float and double in C language and how to choose them

基于 MinIO 对象存储保障 Rancher 数据

河北:稳粮扩豆助力粮油生产提质增效
![[C language] print pattern summary](/img/48/d8ff17453e810fcd9269f56eda4d47.png)
[C language] print pattern summary
![[C language] header file of complex number four operations and complex number operations](/img/f9/b389fe5367f1fa6cd18aaac856bc0d.png)
[C language] header file of complex number four operations and complex number operations

83. (cesium home) how the cesium example works

数字图像理论知识(一)(个人浅析)

Const pointer of C language and parameter passing of main function

English translation Arabic - batch English translation Arabic tools free of charge
随机推荐
[C language] guessing numbers game [function]
Function fitting based on MATLAB
Return and job management of saltstack
How to automatically store email attachments in SharePoint
5. Difference between break and continue (easy to understand version)
English Translation Spanish - batch English Translation Spanish tools free of charge
Cdga | how can the industrial Internet industry do a good job in data governance?
中国能否在元宇宙的未来发展中取得突破,占领高地?
Prometheus deployment
lattice
3、 Are formal and actual parameters in a programming language variables?
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
New fruit naming (DP is similar to the longest common subsequence)
[C language] Pointer elementary knowledge points
plt. What does it mean when linestyle, marker, color equals none in plot()
Digital filter design matlab
Use of strtok and strError
Labelme (I)
C language - control statement
跨区域网络的通信学习静态路由