题目链接:HDOJ - 1171

题目大意

有 n 种物品,每种物品有一个大小和数量。要求将所有的物品分成两部分,使两部分的总大小尽量接近。

题目分析

令 Sum 为所有物品的大小总和。那么就是用给定的物品做完全背包,背包容量为 (Sum / 2) ,得到的结果是较小的一部分的大小。

完全背包问题可以使用单调队列优化,O(nm) 。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; const int MaxN = 1000 + 5, MaxM = 250000 + 5; int n, Sum, Sum1, A, B, Head1, Tail1, Head2, Tail2;
int V[MaxN], Num[MaxN], Q1[MaxM], Q2[MaxM], f[MaxM]; int main()
{
while (true) {
scanf("%d", &n);
if (n < 0) break;
Sum = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &V[i], &Num[i]);
Sum += V[i] * Num[i];
}
Sum1 = Sum >> 1;
for (int i = 0; i <= Sum1; ++i) f[i] = 0;
int Ni, Vi, t;
for (int i = 1; i <= n; ++i) {
Ni = Num[i]; Vi = V[i];
for (int j = 0; j < Vi; ++j) {
Head1 = Tail1 = 0;
Head2 = Tail2 = 0;
for (int k = j, Cnt = 0; k <= Sum1; k += Vi, ++Cnt) {
if (Tail1 - Head1 == Ni + 1) {
if (Q2[Head2 + 1] == Q1[Head1 + 1]) ++Head2;
++Head1;
}
t = f[k] - Cnt * Vi;
Q1[++Tail1] = t;
while (Head2 < Tail2 && Q2[Tail2] < t) --Tail2;
Q2[++Tail2] = t;
f[k] = Q2[Head2 + 1] + Cnt * Vi;
}
}
}
B = f[Sum1];
A = Sum - B;
printf("%d %d\n", A, B);
}
return 0;
}

  

[HDOJ 1171] Big Event in HDU 【完全背包】的更多相关文章

  1. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  2. HUD 1171 Big Event in HDU(01背包)

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  3. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

  4. HDU 1171 Big Event in HDU (多重背包变形)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. HDU - 1171 Big Event in HDU 多重背包

    B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...

  6. hdu 1171 Big Event in HDU (01背包, 母函数)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. HDOJ 1171 Big Event in HDU

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. 1171 Big Event in HDU 01背包

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:把商品分成两半,如不能均分,尽可能的让两个数相接近.输出结果:两个数字a,b且a>=b. ...

  9. HDU 1171 Big Event in HDU(01背包)

    题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...

随机推荐

  1. MySQL 基础(DDL)

    SQL分类         SQL语句主要可以划分为一下3个类别      DDL:数据定义语言,定义数据段.数据库.数据表等      DML :数据操纵语句,用于添加.删除.更新和查询数据库记录 ...

  2. 无法将类型“System.Collections.Generic.IEnumerable<EmailSystem.Model.TemplateInfo>”隐式转换为“System.Collections.Generic.List<EmailSystem.Model.TemplateInf

    List<Model.Template> templateList = templateBLL.RecommendTemplateByOrder(modelEbay); List<M ...

  3. PMD使用手册

    文中测试例子均采用JHotDraw7源码,官网关于 JHotDraw的检测结果:http://pmd.sourceforge.net/reports/jhotdraw_JHotDraw.html 下载 ...

  4. Android 仿PhotoShop调色板应用(一)概述

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在前面的系列我已经将Android中颜色渲染的原理及使用做了一个整体上概述. 现在开始根据一个比较复杂的实现进行具体的分析,这就是PhotoSho ...

  5. HTML表单介绍

    表单语法结构如下: <form action="url" method="get|post" name="value" enctype ...

  6. html+css3实现网页时钟

    在网页上实现时钟功能,效果如右图所示: 运用到的关键技术有:css3中的旋转.旋转基点设置 旋转:transform:rotate(*deg) 旋转基点:transform-origin: x轴偏移 ...

  7. Driving the Activity Lifecycle

    Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActiv ...

  8. 零基础学习云计算及大数据DBA集群架构师【预科2015年12月14日周一】

    1.第一天比较轻松,上午填表格,录指纹,拍照片,做自我介绍. 2.下午老师简单介绍了一下PC\交换机\路由器\塔式服务器\机架式服务器(1U\2U)\刀片服务器\磁带机 3.班主任陈老师朱老师,预科秦 ...

  9. hadoop集群环境搭建之zookeeper集群的安装部署

    关于hadoop集群搭建有一些准备工作要做,具体请参照hadoop集群环境搭建准备工作 (我成功的按照这个步骤部署成功了,经实际验证,该方法可行) 一.安装zookeeper 1 将zookeeper ...

  10. (转)兼容主流浏览器的CSS透明代码

    透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; - ...