当前位置:网站首页>Leshan normal programming competition 2020-e: divide stones [01 backpacks]
Leshan normal programming competition 2020-e: divide stones [01 backpacks]
2022-07-20 10:01:00 【Starry sky and bright moon】
Title Description
Do you have n Stones of known weight W1,…,Wn.
Your task is : Divide the stones into two piles , Minimize the difference between the sum of the two piles .
Input
first line , Enter the number of stones n(1≤n≤60)
The second line , Input n The weight of a stone W1,…,Wn( Positive integer ,1≤Wi≤100000).
Output
Output a number , Represents the minimum weight difference between the sum of the stones divided into two piles .
The sample input
5
5 8 13 27 14
Sample output
3
Tips
Only divided into such two piles , The sum of the first pile is 35, The sum of the second pile is 32, So the weight difference is 3
The first pile :8 13 14
The second pile :5 27
Ideas
Try to divide a pile into two piles equally , We can imagine it as a backpack , The capacity of this backpack is sum/2, Each weight is the weight in the title , The value and weight are the same . The title translates into 01 knapsack problem .
01 knapsack problem : Yes n Items , They have their own size and value , There are backpacks of given capacity , How to maximize the total value of the items in your backpack ?
AC Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 200;
int w[MAXN], v[MAXN], dp[6000000 + 50];
void solve() {
int n, sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &w[i]);
v[i] = w[i];
sum += w[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = sum / 2; j >= w[i]; j--) {
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
}
printf("%d\n", sum - 2 * dp[sum / 2]);
}
int main() {
solve();
return 0;
}
边栏推荐
- FPGA skimming P3: 4-bit numeric comparator circuit, 4bit carry ahead adder circuit, priority encoder circuit, priority encoder
- Detailed explanation of multiresolution decomposition and reconstruction of wavelet analysis
- Output PWM square wave with ls1028a development board
- FPGA skimming p1:1-out-of-4 multiplexer, asynchronous reset Series T trigger, parity check, shift splicing multiplication
- [markdown] about markdown, I want to say this~
- Hard core strength! Feiling Ti Sitara am62x series-335x classic continuation
- 离散数据(数组)的过零位置搜索
- C language data type and uint8 under typedef_ t / uint32_ t
- 电脑快捷方式变白原因及解决方法——血的教训呜呜呜
- Ktor 2.0?半香不香的尴尬
猜你喜欢
How to put a "platform" into a small box? (I) scheme comparison
FPGA skimming p2: multifunctional data processor, calculate the difference between two numbers, use generate For statement simplifies the code, uses sub modules to realize the size comparison of three
Least square linear fitting and its code implementation (C language)
个人博客网站文章添加目录导航
Detailed explanation of tunnel and agent usage in SSH protocol
Cible vulnhub jangow: 1.0.1
[Lora & nb IOT] current situation analysis
STM32 CubeIDE 断点失效的解决方法
When FPM generates packages, the associated Allegro cannot generate packages after it is opened. Solution to the problem
RDO deployment openstack single node
随机推荐
FPGA skimming P3: 4-bit numeric comparator circuit, 4bit carry ahead adder circuit, priority encoder circuit, priority encoder
Definition, calculation method and relationship among linear convolution, cyclic convolution and periodic convolution
First hand evaluation of Reza g2l core board and development board
How to test the availability and stability of EIM bus
2022-7-15 第八小组 顾宇佳 学习笔记
Leshan normal programming competition 2020-a: good pairs
皮尔逊相关系数及代码实现(C语言+MATLAB)
Generics of typescript
Stm32f4 uses advanced timer to output PWM wave (including code)
线性卷积、循环卷积、周期卷积的定义、计算方法及三者之间的关系
What about the software problem of Quanzhi a40i network card
Bing必应搜索用不了方法
乐山师范程序设计大赛2020-F: 我的魔法【模拟】
Output PWM square wave with ls1028a development board
Vulnhub target earth
解决问题phpstudy导入数据库失败
TypeScript 之泛型
川菜菜谱(一)
乐山师范程序设计大赛2020-D: 后缀语言【STL】
How to set the oil on the through hole cover when exporting the Gerber file of PCB