F. Fairness 分硬币最大差值最小
F. Fairness
2.0 s
64 MB
standard input
standard output
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 ith coin 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?
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).
Print t lines, ith line containing a single integer, the answer to the ith test case.
2
5
1 2 1 4 3
7
4 5 6 1 1 3 4
2
5
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
题意:n个硬币分给a,b两个人,每分一个硬币对a,b当前硬币数量作差,分完之后,取分配过程中的最大差为最终权值。问在所有分配方法中,最终权值的最小值为多少
题解:数据量不大,暴力搜索每一种分配方法,求出每一种分配方法的最大差,取最小权值
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<math.h>
#define mod 998244353
#define ll long long
#define MAX 0x3f3f3f3f
using namespace std;
int p[];
int n,t,mx;
void dfs(int a,int b,int k,int now)
{
if(k>=n)//目标状态,更新分硬币过程中的最大差值
{
mx=now;
return;
}
if(now>=mx)//如果比上一种分硬币方法的最大差值还大,就返回,换一种分法
{
return;
}
else
{
if(abs(a-b)>now)//更新当前差
now=abs(a-b);
dfs(a+p[k],b,k+,now);
dfs(a,b+p[k],k+,now);
} }
int main()
{
cin>>t;
while(t--)
{
mx=;
cin>>n;
for(int i=;i<n;i++)
cin>>p[i];
dfs(p[],,,);//第一个给谁差都一样
cout<<mx<<endl;
}
}
F. Fairness 分硬币最大差值最小的更多相关文章
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- 【poj3522-苗条树】最大边与最小边差值最小的生成树,并查集
题意:求最大边与最小边差值最小的生成树.n<=100,m<=n*(n-1)/2,没有重边和自环. 题解: m^2的做法就不说了. 时间复杂度O(n*m)的做法: 按边排序,枚举当前最大的边 ...
- hdu Caocao's Bridges(无向图边双连通分量,找出权值最小的桥)
/* 题意:给出一个无向图,去掉一条权值最小边,使这个无向图不再连同! tm太坑了... 1,如果这个无向图开始就是一个非连通图,直接输出0 2,重边(两个节点存在多条边, 权值不一样) 3,如果找到 ...
- 从无序序列中求这个序列排序后邻点间最大差值的O(n)算法
标题可能比较绕口,简单点说就是给你一个无序数列A={a1,a2,a3……an},如果你把这个序列排序后变成序列B,求序列B中相邻两个元素之间相差数值的最大值. 注意:序列A的元素的大小在[1,2^31 ...
- BAT面试题 - 找一个无序实数数组中的最大差值
题目描写叙述: 一个无序的实数数组a[i].要求求里面大小相邻的实数的差的最大值.比方 double a[]={1,5,4,0.2,100} 这个无序的数组,相邻的数的最大差值为100-5=95. 题 ...
- 洛谷 P5146 最大差值 题解
P5146 最大差值 题目描述 HKE最近热衷于研究序列,有一次他发现了一个有趣的问题: 对于一个序列\(A_1,A_2\cdots A_n\),找出两个数\(i,j\),\(1\leq i< ...
- C# - 习题07_计算1分2分5分硬币各有多少枚
时间:2017-09-08 整理:byzqy 题目:现在有1分.2分.5分硬币共100个,总金额为2.46元,请用程序计算出1分.2分.5分各有多少枚,有多少种算法? 这是最近面试遇到的一个题目,刚开 ...
- 【LeetCode】1432. 改变一个整数能得到的最大差值 Max Difference You Can Get From Changing an Integer
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- hdu 1284 分硬币 && uva 147
#include<bits/stdc++.h> using namespace std; int main() { unsigned ]; memset(dp,,sizeof(dp)); ...
随机推荐
- 如何编写README.md
一.标题写法 1.在文本下方加上 =,文本变为大标题 2.在文本下方加上-,文本变为中标题 3.单独向输入 = ,则需要空一行 标题的另一种写法: # 一级标题 ## 二级标题 ### 三级标题 ## ...
- c++模板(翁恺c++公开课[34-35]学习笔记)
为什么要有模板(templates):当我们需要一个列表(list),列表中元素可能都为X类型也可能都为Y类型,怎么来实现呢? 定义基类?可以实现,很多情况下可能不够简明的表达设计思想 克隆代码(写一 ...
- Linux centosVMware LAMP php-fpm的pool、php-fpm慢执行日志、open_basedir、php-fpm进程管理
一.php-fpm的pool vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加 include = etc/php-fpm.d/*.conf mkd ...
- node.js绑定监听事件EventEmitter类
Node.js 有多个内置的事件,我们可以通过引入 events 模块,并通过实例化 EventEmitter 类来绑定和监听事件,如下: // 引入 events 模块 var events = r ...
- 题解 P3950 【部落冲突】
树链剖分吼啊 一看就看出是LCT模板题啦 前记 见这么多人写LCT,却很少人写树链剖分,于是我就来一发树链剖分(其实是因为自己不会LCT) 本蒟蒻的写法和诸位写树链剖分的大神有点不同 思路 树链剖分, ...
- SVM的使用
注意:数据结构的一致性,在高维度数据一般使用rbf核函数,使用网格搜索思想迭代求出gamma和c. 每行为一个样本,数据类型都围绕标黄代码而定义的. SVM训练如下坐标(左边一列为A类,右边为B类), ...
- sklearn实现决策树算法
1.决策树算法是一种非参数的决策算法,它根据数据的不同特征进行多层次的分类和判断,最终决策出所需要预测的结果.它既可以解决分类算法,也可以解决回归问题,具有很好的解释能力.另外,对于决策树的构建方法具 ...
- 如何安装第三方模块#Python
从书上看到用pip install XXX 就可以直接安装模块.下面就是具体操作 1.添加环境变量D:\Python\Scripts\pip.exe 2.打开cmd切换到python安装目录 3.pi ...
- SpringCloud学习之Zuul路由转发、拦截和熔断处理(七)
Spring Cloud Zuul 服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能. Sp ...
- 【MAVEN】maven项目下载更新pom jar包速度慢 解决方案
1·下载安装 最新版本的maven https://maven.apache.org/download.cgi 2·速度慢的主要原因是因为默认setting.xml里配置的国外的 maven 数据源 ...