dividing coins 简单dp

本博客介绍了一个算法,用于解析输入中的硬币数量和价值,并计算将这些硬币分成两组时,两组之间的最小价值差异。通过迭代过程找到最接近总和一半的组合,进而计算差异。

Input

          A line with the number of problems n, followed by n times:

  • a line with a non negative integer m ($m \le 100$) indicating the number of coins in the bag
  • a line with m numbers separated by one space, each number indicates the value of a coin.

Output 

The output consists of n lines. Each line contains the minimal positive difference between the amount the two persons obtain when they divide the coins from the corresponding bag.

#include<stdio.h>
#include<string.h>
int n,a[110];
int dp[300010];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int sum=0;
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        int mid=sum/2;
        for(int i=0;i<n;i++)
        {
            for(int j=mid;j>=a[i];j--)  // for(int j=a[i];j<=mid;j++) 这是有本质区别!倒着循环a[i]只用了一次,正着循环表示可以使用mid-a[i] 次
            {
                if(dp[j-a[i]]) dp[j]=1;
            }
            if(dp[mid]) break;
        }
        for(;mid>=0;mid--)if(dp[mid])
        {
            printf("%d\n",sum-2*mid);
            break;
        }
    }
}

 


### Dividing 在编程和技术领域中的含义和用法 在编程和技术领域,“dividing”通常指代一种操作或过程,具体意义取决于其使用的场景。以下是几个常见的解释及其应用: #### 1. 数学运算中的除法 (Division) “Dividing”最常见的是表示数学上的除法操作,在大多数编程语言中通过 `/` 或 `//` 符号实现。例如,在 Python 中: ```python result = 10 / 3 # 结果为浮点数 3.333... integer_result = 10 // 3 # 结果为整数 3 ``` 这种操作广泛用于数值计算、比例分配以及单位转换等场合[^1]。 #### 2. 数据分割 (Data Partitioning) 在数据处理和机器学习领域,“dividing”可以指将数据集划分为训练集、验证集和测试集的过程。这一步骤对于模型评估至关重要。例如: ```python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) ``` 上述代码展示了如何将特征矩阵 `X` 和目标向量 `y` 划分到不同的子集中[^2]。 #### 3. 图像处理中的像素划分 在计算机视觉领域,“dividing”可能涉及对图像进行区域划分或逐像素访问的操作。例如,使用 OpenCV 可以遍历图像的每个像素并执行特定逻辑: ```python import cv2 image = cv2.imread('example.jpg') for i in range(image.shape[0]): for j in range(image.shape[1]): pixel = image[i, j] # 对像素进行某种变换 ``` 此方法适用于需要单独分析或修改每个像素的情况。 #### 4. 资源分配与负载均衡 在网络通信或分布式系统设计中,“dividing”也可能代表资源分配或任务分解的概念。比如,为了防止区块链交易中的溢出漏洞,开发者会仔细规划智能合约内的变量范围和边界条件[^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值