POJ 2576 Tug of War 【二维01背包】

POJ2576 Tug of War (二维的01背包) Description A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number 阅读详情

 

Tug of War

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9236 Accepted: 2572

Description

A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more than 1; the total weight of the people on each team should be as nearly equal as possible.

 

 

Input

The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.

 

 

Output

Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers differ, give the lesser first.

 

 

Sample Input

3
100
90
200

 

Sample Output

190 200

Source

Waterloo local 2000.09.30

 

 

题意:将n个人分成两个部分,两边人数相等或者相差 1 ,并且两边体重总和相差最小;(这里要求的相差最小,其实就是两边的总重量尽可能的接近左右两边总重量的一半

思路:

做背包有关的题的时候,尽量把dp所表示的状态和dp的下标联系在一起,像一般的01背包问题,限制的条件就是背包的容量上限,所以一个一维背包dp【j】,j表示当前背包容量就可以解决问题了;这题有两个限制条件,一个是体重相差最小,一个是两边人数相差1或者相等,那么dp的状态尽可能的能够包含这两个限制条件,所以在这里设置一个二维dp【j】【k】,我们这里的dp可以只分析一边,因为知道了一边的总体重,另一半用sum 减去 求的这个总体重就是另一半的总体重了;这里的 j 表示的是左边的人数,k表示的左边的总体重;

下面代码的三重循环其实求的是,人数为 j 的时候的所有可能的重量组合,比如人数为 2的时候,可能有的组合有哪些,能组合得到的小于或者等于所有人数总重量一半的就记为 1 ,否则为0;最后得到人数为上限人数的dp后,再找最大的那个 k值,输出就是答案;

 

 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>

using namespace std;

#define Maxn 45010

int dp[105][Maxn],a[105],sum,book[Maxn];

int main(void)
{
    int n;
    while (scanf("%d",&n) != EOF) {
        sum = 0;
        for (int i = 1; i <= n; ++i) {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(n == 1)  { printf("0 %d\n",a[1]); continue; }
        memset(dp,0,sizeof(dp));

        dp[0][0] = 1;
        int limit_people = (n+1)/2,limit_weight = sum/2;
        // 这里的人数上限 是(n+1)/2,如果总人数是偶数,那么结果和不 + 1的结果是一样的,
        //如果是奇数,那么求得的人数是人数最多的一边,
        // 因为两边的界限主要是用总重量来限制,如果上限的人数少一个,如果少掉的这个人的重量加上去不会超过
        //限制的重量,那么结果就会不对,宁可多算一个人都不能少算一个;
         for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= limit_people; ++j) {
                for (int k = limit_weight; k >= a[i]; --k) {
                    if(!dp[j][k] && dp[j-1][k-a[i]] && !book[k - a[i]]) { 
                            dp[j][k] = 1; 
                            if(j != limit_people) book[k] = 1;  // book的作用是为了避免同一个人的总量重复加
                    }
                    else book[k] = 0; // 只考虑当前人数的上一层的人数是否重复计算了,所以,上上层的book要及时清0
                }
            }
        }

        int ans;
        for (int i = limit_weight; i >= 1; --i)
            // i 是所有可能的重量组合中,总量不大于限制总重量的值,取最大的那个值,而且在两边不相等的情况下
            // 这个最大值可能是少 1 的一边,也可能是不少1的那边
        if(dp[limit_people][i] || dp[limit_people-1][i]) { ans = i; break; }
        if(ans > sum-ans) ans = sum-ans;

        printf("%d ",ans);
        printf("%d\n",sum-ans);
    }
    return 0;
}

 

POJ 1578 Tug of War|背包问题|动态规划 问题描述 总时间限制:3000ms 内存限制:65536kB 描述 A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must no.. 阅读详情

相关推荐

poj 2576 Tug of War——背包问题

Tug of War Description A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more

five_east_west的博客 1027

07:有趣的跳跃http://www.noi.openjudge.cn/

07:有趣的跳跃 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 一个长度为n(n>0)的序列中存在“有趣的跳跃”当前仅当相邻元素的差的绝对值经过排序后正好是从1到(n-1)。例如,1 4 2 3存在“有趣的跳跃”,因为差的绝对值分别为3,2,1。当然,任何只包含单个元素的序列一定存在“有趣的跳跃”。你需要写一个程序判定给定序列是否存在“有趣的跳跃”。 输入 一行,第一个数是n(0 < n < 3000),为序列长度,接下..

yzclhj的博客 747

【解题总结】Waterloo Local 2012-2013(Codeforces Gym 100169)

感官难度:C=D<A<E<BC=D<A<E<BC=D<A<E<B。 A Bridges and Tunnels 题意:给定一张无向图,图上边分为室外和室内两类,每条边有权值。求以室外的边权和为第一关键字,室内和室外的边权和为第二关键字的最短路。 直接用 pair 存一下这两个关键字即可。 代码链接 E Tetrahedron Inequality 题意:给 6 条边,问能否组成一个体积非 0 的正四面体。多组数据。 枚举 6 条边的所有排列。首先保证能组

zqy1018的blog 359

1-6-07:有趣的跳跃

描述 一个长度为n(n>0)的序列中存在“有趣的跳跃”当前仅当相邻元素的差的绝对值经过排序后正好是从1到(n-1)。例如,1 4 2 3存在“有趣的跳跃”,因为差的绝对值分别为3,2,1。当然,任何只包含单个元素的序列一定存在“有趣的跳跃”。你需要写一个程序判定给定序列是否存在 “有趣的跳跃”。 输入一行,第一个数是n(0 < n < 3000),为序列长度,接下来有n个整数...

weixin_30552635的博客 1066

TouchGFX使用教程(三)

TouchGFX使用教程(三)界面事件控件修改方法图片等资源放置位置 界面事件 控件修改方法 图片等资源放置位置

yuanzhan11的专栏 6972

AI智能体项目管理框架agent-pm:从原理到实战的多智能体协作指南

在人工智能和自动化技术快速发展的今天,多智能体系统已成为实现复杂任务自动化的重要技术路径。其核心原理在于通过任务分解、角色分配和状态管理,让多个AI智能体像人类团队一样协同工作,从而突破单个模型的性能边界。这种架构的技术价值在于将软件工程中成熟的项目管理方法论引入AI领域,显著提升了自动化流程的可预测性、可观测性和可复用性。在实际应用场景中,多智能体协作框架能够高效处理从代码开发、文档撰写到数据分析等一系列任务,尤其适合需要多步骤、多专业协同的复杂项目。本文聚焦的agent-pm框架,正是这一领域的典型实践

weixin_42533910的博客 714

OpenJudge题解——1.6编程基础之一维数组

OpenJudge题解——1.6编程基础之一维数组

weixin_43100558的博客 9145

大白话系列:什么是微服务

用大白话和实际例子来通俗易懂地告诉你什么是微服务~

2401_87403086的博客 1242

lightoj 1147 - Tug of War

A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the

SSimpLe_Y的博客 1022

Ubuntu20.04系统Cuckoo sandbox所需环境搭建及安装(超详细图文)

请勿叫我大神,我只是互联网的搬运工~ 前言 本教程以Ubuntu20桌面版作为示范,服务器版同样适用,但可能稍微麻烦一点,服务器版本的话安装Xmanage套装使用可能会相对轻松一些,由于后面会使用到vbox的使用,对vbox命令行操作不熟悉的小伙伴用xmanage可能会相对轻松一点。 提示:以下是本篇文章正文内容,下面案例可供参考 安装ubuntu20桌面版 步骤1:转到https://ubuntu.com/download/desktop下载下载.iso。 步骤2:使用您选择的虚拟机监控程序(VMWa

ElsonHY的博客 1万+

rv1126 读取sensor输入,输出到屏幕显示【RK_MPI API接口】

rv1126 libdrm VO

学习记录 1万+

动态规划:二维01背包问题

详细讲解二维背包得来的5个步骤

evildoerxmf的博客 1705

当FPGA遇上Codesys:用Verilog实现ModbusTCP协议栈的3种方法(Xilinx/Vivado实测)

本文详细介绍了在Xilinx FPGA上使用Verilog实现Modbus TCP协议栈的三种方法,并与Codesys环境无缝集成。从基础架构设计到纯Verilog实现、基于AXI总线的混合方案,再到与Codesys的集成方法,为开发者提供了完整的工业自动化通信解决方案。通过实测数据对比,帮助开发者选择最优实现方案。

weixin_29172963的博客 522

HDU-2576 Tug of War

http://poj.org/problem?id=2576 二维数组01背包的变形。 Tug of War Time Limit:3000MS Memory Limit:65536K Total Submissions:8147 Accepted:2191 Description A tug of war is t...

weixin_30907935的博客 126

英伟达 Jetson AGX Thor 官方入门教程(一)系统U盘安装

NVIDIA Jetson AGX Thor开发套件快速安装指南 本指南详细介绍了NVIDIA Jetson AGX Thor开发套件的安装流程。该套件搭载T5000 SoM,为物理AI和机器人应用提供强大支持。安装过程采用全新的JetPack 7.0方案,通过USB闪存盘即可完成BSP(Jetson Linux)安装,无需依赖Ubuntu主机。指南包含硬件开箱说明、两种配置模式(带显示器/无显示器)、详细的ISO镜像下载和USB制作步骤(推荐使用Balena Etcher工具),以及首次启动后的初始设置流

机器人控制与仿真应用 2868

ATT MTU 交换机制:提升 BLE 数据传输效率的关键

在低功耗蓝牙(BLE)技术的面试中,ATT MTU 交换机制始终是高频考点。无论是芯片厂商的固件开发岗,还是物联网设备的协议栈调试岗,面试官都爱通过这个知识点考察候选人对 BLE 数据传输底层逻辑的理解。本文将系统梳理核心考点,结合历年真题详解应答思路,轻松应对这类问题。​

byte轻骑兵的技术小窝 2411

poj2576二维01背包

Description A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of pe

ghvn7777的专栏 518

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

代码随想录算法训练营第四十二天|01背包问题,你该了解这些! ● 01背包问题,你该了解这些! 滚动数组 ● 416. 分割等和子集

自用

weixin_48111139的博客 412

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

poj 2576

<br />Tug of WarTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 6615 Accepted: 1658<br />DescriptionA tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be

志当存高远 1418

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Tug of War(严格限制数量的二维费用背包)

Tug of War Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8500   Accepted: 2311 Description A tug of war is to be arranged at the local office picnic. For t

不会思考的机器 789

poj2576 Tug of War(二维费用背包)

题目链接:http://poj.org/problem?id=2576 题目大意:有n个人,给出他们的体重,将他们分为两队,尽量均分,而且相差人数不能超过一个人。 输出两队的体重,小的在前。 思路:二维费用背包   有两个限制因素,人数和体重 #include #include #include using namespace std; int dp[45010][120]; int

qq_36782366的博客 529

poj2576-Tug of War(二维背包)

#include #include #include using namespace std; bool f[45010][110]; int w[110]; struct poj2576 { /* [问题描述]:n个人,体重分别为w[1],w[2],...,w[n] * 将n个人分为两组,人数只差不得大于1 * 要求两组的体重和最接近 */ /* [解题思路]:f[n][i]

Nicke_的博客 709

POJ 2576 Tug of War 背包问题

题目链接:POJ 2576 Tug of War 题目 Description A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team o...

weixin_44273380的博客 370

POJ2577 Interpreter【模拟】

InterpreterTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2186 Accepted: 742DescriptionA certain computer has 10 registers and 1000 words of RAM. Each register or RAM location holds a 3-dig...

海岛Blog 700

今日份动态规划学习(二维01背包+01背包变形)

然后就可以想办法找出状态转移方程:有关是否要吃这个垃圾//不吃的生命//吃的生命然后就可以AC了。

2301_80475191的博客 1158

【动态规划】背包问题 {01背包问题;完全背包问题;二维费用背包问题}

【动态规划】背包问题 {01背包问题;完全背包问题;二维费用背包问题}

52Herzt的博客 5527
上一篇: poj 1742 Coins 【多重背包+二进制拆分优化】
下一篇: poj 2492 A Bug's Life【并查集】
Y390d
博客等级 码龄9年 11粉丝 165原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值