当前位置:网站首页>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;
}
边栏推荐
- 助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- 冒烟测试怎么做
- R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
- 驼峰式与下划线命名规则(Camel case With hungarian notation)
- The second day of rhcsa study
- R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
- Zero foundation entry polardb-x: build a highly available system and link the big data screen
- MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
猜你喜欢
应用使用Druid连接池经常性断链问题分析
Mysql Information Schema 学习(二)--Innodb表
Word如何显示修改痕迹
冒烟测试怎么做
[translation] a GPU approach to particle physics
Benefit a lot, Android interview questions
业务与应用同步发展:应用现代化的策略建议
Digital "new" operation and maintenance of energy industry
安装Mysql报错:Could not create or access the registry key needed for the...
10 schemes to ensure interface data security
随机推荐
反射及在运用过程中出现的IllegalAccessException异常
MATLAB中deg2rad和rad2deg函数的使用
R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
Pytorch common loss function
A method of removing text blur based on pixel repair
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
Tensorflow and torch code verify whether CUDA is successfully installed
ZABBIX proxy server and ZABBIX SNMP monitoring
安装Mysql报错:Could not create or access the registry key needed for the...
ROS自定义消息发布订阅示例
Take a look at how cabloyjs workflow engine implements activiti boundary events
R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组均值(mean)
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
[translation] a GPU approach to particle physics
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
About image reading and processing, etc
How to type multiple spaces when editing CSDN articles