【Gym - 101061F】Fairness(dp,思维)

题干:

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 ;
}

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440085.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

(2)连续存储数组的方法

目录 连续存储的代表应用&#xff1a;数组 1&#xff09;结构体的定义&#xff1a; 2&#xff09;基本操作 对数据进行初始化 判断数组是否为空 输出数组 判断数组是否满 追加元素 插入数组元素 删除数组元素 逆序 对数组进行排序 这篇笔记是根据郝斌老师的上课讲义…

什么是欧拉角/姿态角?

用一句话说&#xff0c;欧拉角就是物体绕坐标系三个坐标轴(x,y,z轴&#xff09;的旋转角度。 在这里&#xff0c;坐标系可以是世界坐标系&#xff0c;也可以是物体坐标系&#xff0c;旋转顺序也是任意的&#xff0c;可以是xyz,xzy,yxz,zxy,yzx,zyx中的任何一种&#xff0c;甚至…

机器学习笔记(十二):聚类

目录 1&#xff09;Unsupervised learning introduction 2&#xff09;K-means algorithm 3&#xff09;Optimization objective 4&#xff09;Random initialization 5&#xff09;Choosing the number of clusters 1&#xff09;Unsupervised learning introduction 下…

Linux下root登陆mysql

错误如下&#xff1a; 1.停止mysql服务 #service mysql stop2.进入到skip-grant-tables模式&#xff1a; #mysqld_safe --skip-grant-tables3.root连接mysql数据库&#xff1a; #mysql -uroot -p如出现如下错误&#xff1a; 其实&#xff0c;原本就没有这个目录&#xff1…

机器学习笔记(十三):降维

目录 1&#xff09;Motivation 1:Data Compression 2&#xff09;Motivation 2: Data Visualization 3&#xff09;Principal Component Analysis problem formulation 4&#xff09;Principal Component Analysis algorithm 5&#xff09;Advice for applying PCA 1&…

Django框架(展示图书信息简易版)

Linux环境下 创建虚拟环境 在python3中&#xff0c;创建虚拟环境 mkvirtualenv -p python3 虚拟机名称 mkvirtualenv -p python3 py_django查看创建的虚拟环境 workon退出当前的虚拟环境 deactivate 删除虚拟环境&#xff08;不要做&#xff09; rmvirtualenv 虚拟机名称 …

吴恩达机器学习作业(五):支持向量机

目录 1&#xff09;数据预处理 2&#xff09;Scikit-learn支持向量机 3&#xff09;决策边界比较 4&#xff09;非线性SVM 5&#xff09;最优超参数 6&#xff09;垃圾邮件过滤器 在本练习中&#xff0c;我们将使用支持向量机&#xff08;SVM&#xff09;来构建垃圾邮件分…

一些关于ROS中move_base的理解

move_base是ROS下关于机器人路径规划的中心枢纽。它通过订阅激光雷达、map地图、amcl的定位等数据&#xff0c;然后规划出全局和局部路径&#xff0c;再将路径转化为机器人的速度信息&#xff0c;最终实现机器人导航。这里又要盗官网的图了。 上面这个图很好的展示了move_base的…

机器学习笔记(十四):异常检测

目录 1&#xff09;Problem motivation 2&#xff09;Gaussian distribution 3&#xff09;Algorithm 4&#xff09;Developing and evaluating an anomaly detection system 5&#xff09;Anomaly detection vs. supervised learning 6&#xff09;Choosing what featur…

【Gym - 101606F】Flipping Coins(概率dp)

题干&#xff1a; Here’s a jolly and simple game: line up a row of N identical coins, all with the heads facing down onto the table and the tails upwards, and for exactly K times take one of the coins, toss it into the air, and replace it as it lands eith…

ROS actionlib学习(一)

actionlib是ROS中一个很重要的功能包集合&#xff0c;尽管在ROS中已经提供了srevice机制来满足请求—响应式的使用场景&#xff0c;但是假如某个请求执行时间很长&#xff0c;在此期间用户想查看执行的进度或者取消这个请求的话&#xff0c;service机制就不能满足了&#xff0c…

机器学习笔记(十五):推荐系统

目录 1&#xff09;Problem formulation 2&#xff09;Content-based recommendations 3&#xff09;Collaborative filtering 4&#xff09;Collaborative filtering algorithm 5&#xff09;Vectorization: Low rank matrix factorization 6&#xff09;Implementation…

*【CodeForces - 280C】Game on Tree(期望模型,期望的线性性)

题干&#xff1a; Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree. The game consists of several steps. On each step, Momiji chooses…

武侠风云(基础版)

基本任务&#xff1a; 1 建立角色类&#xff0c;角色拥有生命值的属性和攻击的方法&#xff0c;攻击值是随机的。 2 建立职业子类&#xff0c;刀客&#xff0c;&#xff08;伤害少&#xff0c;血量多&#xff09;剑客&#xff08;伤害正常&#xff0c;血量正常&#xff0c;有几…

机器学习笔记(十六):大规模机器学习

目录 1&#xff09;Learning with large datasets 2&#xff09;Stochastic gradient descent 3&#xff09;Mini-batch gradient descent 4&#xff09;Stochastic gradient descent convergence 1&#xff09;Learning with large datasets 回顾一下我们之前提到的这句…

【ZOJ - 3329】One Person Game(带循环的概率dp,数学期望,高斯消元,数学)

题干&#xff1a; There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the dice are fair dice, so the probability of rolling each value, 1 to K1, K…

智能聊天机器人系统

# 智能聊天机器人系统 # 1.系统简介 # 随着社会的各个公司以及大学对人工智能技术的深入研究和快速发展&#xff0c;人工智能技术将逐步应用到 # 方方面面。智能聊天机器人系统是基于各类传感器收集人类语音数据&#xff08;智能电视、智能空调&#xff0c; # 智能冰箱、智能音…

机器学习笔记:总结

下面是我们本课程学到的要点&#xff1a; 1&#xff09;监督学习&#xff1a;线性回归&#xff0c;逻辑回归&#xff0c;神经网络&#xff0c;SVM&#xff1b; 2&#xff09;无监督学习&#xff1a;k均值&#xff0c;PCA&#xff0c;异常检测 3&#xff09;特别应用&#xf…

ROS探索总结(十二)——坐标系统

在机器人的控制中&#xff0c;坐标系统是非常重要的&#xff0c;在ROS使用tf软件库进行坐标转换。 相关链接&#xff1a;http://www.ros.org/wiki/tf/Tutorials#Learning_tf 一、tf简介 我们通过一个小小的实例来介绍tf的作用。 1、安装turtle包 <span>$ rosdep instal…

【BZOJ - 3036】绿豆蛙的归宿(概率DAG图dp,拓扑排序,概率dp,期望的线性性)

题干&#xff1a; 随着新版百度空间的下线&#xff0c;Blog宠物绿豆蛙完成了它的使命&#xff0c;去寻找它新的归宿。 给出一个有向无环的连通图&#xff0c;起点为1终点为N&#xff0c;每条边都有一个长度。绿豆蛙从起点出发&#xff0c;走向终点。 到达每一个顶点时&#x…