当前位置:网站首页>Unbalance balance (dynamic programming, DP)
Unbalance balance (dynamic programming, DP)
2022-07-06 19:25:00 【surowvv】
Title Description
finally Alice Out of the trap of the demon king , But now she forgot to bring her weapon , What should I do ??? This is the time , A mysterious old man came up to her and promised to give her weapons for free , But the old man has one condition , You need to put the selected weapons on both ends of the scale , If the balance is balanced, all weapons on the balance can be taken away , Fortunately, the scale is rusty , As long as the weight difference between the two ends is less than or equal to m Will maintain balance ,Alice Foolishly think that the heavier the weapon, the better , seek Alice The maximum total weight of weapons that can be taken .( Unlimited number of operations )
Input description :
first line 2 It's an integer n, m; The second line n It's an integer x, respectively n The weight of a weapon . 1 <= n <= 100; 0 <= m <= 100; 1 <= x <= 100;
Output description :
An integer , Express Alice The maximum total weight of weapons that can be taken .
Example 1
Input
5 4 1 5 61 65 100
Output
132
Example 2
Input
5 0 10 20 30 40 100
Output
200
analysis :
dp[i][j] , In the i The difference between the two sides of the balance is j Maximum weight of
When not selected dp[i][j] = max(dp[i][j], dp[i - 1][j]);
Ipsilateral :dp[i][j + a[i]] = max(dp[i][j + a[i]], dp[i - 1][j] + a[i]);
Opposite side :dp[i][j - a[i]] = max(dp[i][j - a[i]], dp[i - 1][j] + a[i]);
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
int n, m;
int a[110];
int dp[110][10010];
int sum = 0;
const int inf = 0x3f3f3f3f;
int main()
{
cin >> n >> m;
memset(dp, -inf, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= sum; j++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
dp[i][j + a[i]] = max(dp[i][j + a[i]], dp[i - 1][j] + a[i]);
dp[i][abs(j - a[i])] = max(dp[i][abs(j - a[i])], dp[i - 1][j] + a[i]);
}
}
int ans = 0;
for (int i = 0; i <= m; i++) ans = max(ans, dp[n][i]);
cout << ans;
return 0;
}边栏推荐
- Black Horse - - Redis Chapter
- Precautions for binding shortcut keys of QPushButton
- MATLAB中deg2rad和rad2deg函数的使用
- In depth analysis, Android interview real problem analysis is popular all over the network
- test about BinaryTree
- The nearest library of Qinglong panel
- PMP每日一练 | 考试不迷路-7.6
- [paper notes] transunet: transformers make strongencoders for medical image segmentation
- 深入分析,Android面试真题解析火爆全网
- Computer network: sorting out common network interview questions (I)
猜你喜欢

反射及在运用过程中出现的IllegalAccessException异常

黑馬--Redis篇
深入分析,Android面试真题解析火爆全网

中缀表达式转后缀表达式详细思路及代码实现

今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发

数学知识——高斯消元(初等行变换解方程组)代码实现

ACTF 2022圆满落幕,0ops战队二连冠!!

The list of people who passed the fifth phase of personal ability certification assessment was published

JDBC details

倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
随机推荐
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
Documents to be used in IC design process
深入分析,Android面试真题解析火爆全网
Detailed idea and code implementation of infix expression to suffix expression
R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
学习探索-函数防抖
The second day of rhcsa study
zabbix 代理服务器 与 zabbix-snmp 监控
It's super detailed in history. It's too late for you to read this information if you want to find a job
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
Mysql Information Schema 学习(二)--Innodb表
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
C language daily practice - day 22: Zero foundation learning dynamic planning
10 schemes to ensure interface data security
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!