Gym 101061F Fairness【dfs+剪枝】

探讨如何在分配一组数值的过程中,通过合理的策略最小化两方持有数值之差的最大绝对值,使用深度优先搜索算法并结合剪枝优化求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


传送门:Gym 101061F


Fairness
time limit per test2.0 s memory limit per test64 MB inputstandard input outputstandard output

Problem Description
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?

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.

Sample Input
2
5
1 2 1 4 3
7
4 5 6 1 1 3 4

Sample 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




题意:

给定n个权值,按序做n次选择,
每次选择把该权值分配给A或B,
其最终的权值既是在决策过程中A和B差值绝对值的最大值,
我们的目标是使这个函数最小化.输出最小权值.

题解:

当时做的时候想用dp做,但是状态怎么转移我一直想不出来,就看到他的n的范围也不是很大,就想有dfs做。
直接做交上去后超时了。然后就想到了剪枝,每次只要该行的差大于当前最大值了,就直接退出。




AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=110000;
int t;
int n;
int a[110];
int maxn;
void dfs(int x,int m,int s,int d)
{
  if(m>=maxn)
    return ;
  if(x>=n)
  {
    maxn=m;
    return;
  }
  if(abs(s-d)>m) m=abs(s-d);
  dfs(x+1,m,s+a[x],d);
  dfs(x+1,m,s,d+a[x]);
}
int main()
{
  scanf("%d",&t);
  while(t--)
  {
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
      scanf("%d",&a[i]);
    }
    maxn=inf;
    dfs(1,0,a[0],0);
    printf("%d\n",maxn);
  }
  return 0;
}

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
### 配置和使用ns-3与ns-3-gym在VSCode中的网络仿真 #### 安装依赖项 为了确保能够在VSCode中顺利运行ns-3以及ns-3-gym,需要先安装一系列必要的软件包。这包括但不限于CMake、Python3及其pip工具等基础构建组件。 对于Ubuntu 20.04而言,可以通过如下命令来完成这些前置条件的部署: ```bash sudo apt update && sudo apt install -y build-essential cmake python3-dev python3-pip git libboost-all-dev autoconf automake bison flex g++ gcc libffi-dev libgmp-dev libmpc-dev libmpfr-dev libtool make pkg-config texinfo zlib1g-dev qtbase5-dev libcwiid-dev wiimote-tools ``` 接着利用`pip3`安装额外所需的Python库文件,特别是针对ns-3-gym的支持部分: ```bash pip3 install gym numpy matplotlib pybind11==2.6.2 ``` #### 下载并编译ns-3源码 获取最新的ns-3版本(此处假设为v3.34),解压后进入对应目录执行编译操作。考虑到后续可能涉及到频繁修改测试的需求,建议采用调试模式(Debug)来进行编译工作以便于排查错误信息。 ```bash git clone https://github.com/nsnam/ns-3-dev.git ns-3.34 cd ns-3.34 ./waf configure --build-profile=debug ./waf ``` #### 整合ns-3-gym模块至ns-3项目内 下载ns-3-gym仓库,并将其放置到ns-3项目的顶层路径下作为子模块管理;随后按照官方文档指示调整配置参数使得两者能够协同运作[^1]。 ```bash git submodule add https://github.com/tkn-tub/ns3-gym.git src/gym-api ./waf clean ./waf configure --with-gym-api=./src/gym-api/ ./waf ``` #### 设置VSCode开发环境 打开Visual Studio Code编辑器,在目标工程根目录创建`.vscode/launch.json`用于定义启动选项,同时编写相应的任务脚本(`tasks.json`)辅助日常编码流程自动化处理。下面给出了一组基本样例供参考: **launch.json** ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/debug/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "compile" } ] } ``` **tasks.json** ```json { "version": "2.0.0", "tasks": [ { "label": "compile", "command": "./waf", "group": { "kind": "build", "isDefault": true }, "detail": "Compile the project using waf.", "problemMatcher": ["$gcc"], "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" } } ] } ``` 通过上述步骤即可实现在VSCode环境下对ns-3及ns-3-gym的有效支持,方便开发者开展基于强化学习算法驱动下的无线通信场景模拟实验研究工作[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值