题干:

Dwik and his brother Samir both received scholarships from a famous university in India. Their father, Besher, wants to send some money with each of them.

Besher has n coins, the ith coin has a value of ai. He will distribute these coins between his two sons in n steps. In the ith step, he chooses whether to give the ithcoin to Dwik or to Samir.

Let xi be the absolute difference between the sum of Dwik's and Samir's coins after the ith step. The unfairness factor of a distribution is max({x1, x2, ..., xn}). Besher wants to minimize the unfairness factor, can you help him?

Input

The first line of the input consists of a single integer t, the number of test cases. Each test case consists of 2 lines:

The first line contains an integer n (1 ≤ n ≤ 100).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100).

Output

Print t lines, ith line containing a single integer, the answer to the ith test case.

Example

Input

2
5
1 2 1 4 3
7
4 5 6 1 1 3 4

Output

2
5

Note

In the first sample test, besher has 5 coins (1, 2, 1, 4, 3), he can distribute them in the following way:

Step 1: Give the first coin to dwik , d = 1, s = 0  x1 = |1 - 0| = 1

Step 2: Give the second coin to samir, d = 1, s = 2  x2 = |1 - 2| = 1

Step 3: Give the third coin to samir, d = 1, s = 3  x3 = |1 - 3| = 2

Step 4: Give the fourth coin to dwik, d = 5, s = 3  x4 = |5 - 3| = 2

Step 5: Give the fifth coin to samir, d = 5, s = 6  x5 = |5 - 6| = 1

max({x1, x2, x3, x4, x5}) = 2

解题报告:

直接dp[i][j]代表前i个物品其中A这个人选择了总价值为j的容量的最小化最大差值。

注意代码逻辑,所以要算两个转移的时候分别取最大值,然后取个最小值赋值给dp[i][j],

而不能每次都dp[i][j]=min(dp[i][j],max())。其实如果用我为人人的写法的话就可以避免这个问题。

(然而好像也可以直接每次都更新dp[i][j]呀?)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e4 + 5;
int dp[105][MAX],a[MAX],sum[MAX];
int ZERO = 1e4,n;
int main()
{int t;cin>>t;while(t--) {scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i),sum[i] = sum[i-1] + a[i];memset(dp,0x3f,sizeof dp);int inf = dp[0][0];dp[0][0]=0;for(int i = 1; i<=n; i++) {for(int j = 0; j<=sum[n]; j++) {int curA = j,curB = sum[i] - j;int cA = j,cB = sum[i] - cA;int tmp1=0x3f3f3f3f,tmp2=0x3f3f3f3f;tmp1 = max(dp[i-1][j],abs(curA-curB));//把第i个放入B中 if(j >= a[i])tmp2 = max(dp[i-1][j-a[i]],abs(cA-cB));//把第i个放入A中 dp[i][j] = min(tmp1,tmp2);}}int ans = 0x3f3f3f3f;for(int i = 0; i<MAX; i++) ans = min(ans,dp[n][i]);printf("%d\n",ans);} return 0 ;
}

这题还可以用记忆化或者二分写,思路差不多,光贴个代码吧:

【Gym - 101061F】Fairness(dp,思维)相关推荐

  1. CSP-S 2019————Emiya 家今天的饭————DP+思维

    题解:本题主要考查DP+思维. 简要题意:一个矩阵,要求每行只选一个节点,每列选的节点不能超过所有选的节点的一半,不能不选,给出每个节点的选择方案数,求总方案数. 1.DP+思维: (1).维护每列已 ...

  2. Ivan the Fool and the Probability Theory-Codeforces Round #594 (Div. 2)-C题(dp+思维)

    Ivan the Fool and the Probability Theory-Codeforces Round #594 (Div. 2)-C题(dp+思维) time limit per tes ...

  3. HDU 4489 找出n!个全排列数中的“波浪数” dp 思维,全排列

    这题关键还是在于找出dp的转移方程,这题是从第n个数在前n-1个数的位置中入手,然后把"波浪数"分成两派.理解起来不困难,但是要自己想到的话,其中的思维一片也不能断掉,尤其是要找到 ...

  4. HDU多校10 - 6880 Permutation Counting(dp+思维)

    题目链接:点击查看 题目大意:给出一个长度为 n - 1 的 01 序列 b 用来表示排列 a 的相对大小关系,b[ i ] = 0 说明 a[ i ] < a[ i + 1 ] ,b[ i ] ...

  5. Lighting System Design UVA 11400 (dp+思维)

    题目大意:有一个照明系统需要用到n种灯,每种灯的电压为V,电源费用K,每个灯泡费用为C,需要该灯的数量为L.注意到,电压相同的灯泡只需要共享一个对应的电源即可,还有电压低的灯泡可以被电压高的灯泡替代. ...

  6. Codeforces Round #727 (Div. 2) E. Game with Cards dp + 思维

    传送门 文章目录 题意: 思路: 题意: 初始有左右手,上面各有一个数字为000的卡牌,每次都有一个新卡kik_iki​,你可以将其放在左手或者右手,使两只手上的卡片范围在[ll,i,rl,i][l_ ...

  7. CF946D Timetable 背包dp + 思维转换

    传送门 文章目录 题意: 思路: 题意: n,m,k≤500n,m,k\le500n,m,k≤500 思路: 将其转换成背包的模型,就可以想出来一个很明显的dpdpdp状态:f[i][j]f[i][j ...

  8. Codeforces Round #462 (Div. 2) C. A Twisty Movement dp + 思维转换

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的只包含1,21,21,2的序列aaa,你可以至多翻转一段区间,求翻转之后最长非递减子序列是多长. 思路: 考虑如果翻转的话,翻转的子区间 ...

  9. Codeforces Round #693 (Div. 3) G. Moving to the Capital dp + 思维

    传送门 题意: 给一个图,111号点为中心点,定义dis[i]dis[i]dis[i]表示111号点到iii的距离.现在有三种移动方式 (1)(1)(1)从iii移动到jjj且dis[i]<di ...

最新文章

  1. leetcode算法题--骑士拨号器
  2. 一天1个机器学习知识点(三)
  3. Nginx缓存引发的跨域惨案(转:https://www.baidu.com/home/news/data/newspage?nid=9966642810298490574n_type=0p_f)
  4. cocos2d-lua-win
  5. 洛谷——P2018 消息传递
  6. C# 谈谈Interface和通过Interface传递web页面数据
  7. 【文末福利】如何用精密算法解决未婚妻问题?
  8. kafka集群脚本启动失败,在kafkaServer.out中提示nohup: failed to run command `java’: No such file or directory
  9. php refcount,php变量引用和计数_refcount_gc和is_ref_gc
  10. 编译运行BSR/bench源码
  11. 信息检索:“众筹”专利分析
  12. php 连接 sybase,thinkphp连接sybase数据库
  13. python 菜鸟教程 xml-【读书】Django教程(菜鸟教程)
  14. Max函数、Min函数
  15. 防辐射门行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  16. PyQt5自定义图片组件:同时显示多张图片
  17. 【火炉炼AI】机器学习055-使用LBP直方图建立人脸识别器
  18. 小程序获取上一页的数据修改上一个页面的数据
  19. 找学习资料的网址/地方
  20. Pytorch2.0发布了,向下兼容,加一句代码,性能翻番

热门文章

  1. [Leetcode][第93题][JAVA][复原IP地址][剪枝][回溯]
  2. normalize函数_提取棋盘格角点函数解析
  3. 前台文件_欧木瑾怎么定制办公前台?
  4. 自动打包linux,Linux环境下Springboot自动打包发布功能
  5. RT-Thread设备框架学习感悟
  6. 查询成绩小于85且是计算机的一项应用,查询练习2
  7. java while do_java中while和do-while的总结
  8. 计算机网络关于封装成帧题目,上海第二工业大学-计算机网络通信期中试卷答案...
  9. 中职计算机说课稿三篇,精选中职计算机说课稿三篇-20210609060707.docx-原创力文档...
  10. OpenGL: 实现立体显示