题目链接: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. smarty、thinkphp中的html加载其他的html文件的方式

    1.smarty 在模板文件中,使用定界符 {include file="header.html"}  不可以省略.html 2.thinkphp的html文件中 <incl ...

  2. VS2012的安装项目只能用InstallShield Limited Edition[附资源下载]

    以前版本的Visual Stuido中安装项目都可以使用微软自家的Visual Studio Installer,但是到了VS2012这一切都变了,只能用InstallShield Limited E ...

  3. Winform 中 DesignMode 返回值不正确的问题。

    本文转载:http://blog.csdn.net/sabty/article/details/5325260 以前也曾遇到这样的问题,不过影响不大也没有去详细了解.今天又重新遇到此问题,实在太不便. ...

  4. oracle恢复被覆盖的存储过程

    假设你不小心覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小.原理非常easy,存储过程的定义就是数据字典,改动数据字典跟改动普通表的数据没有差别,此时会把改动前的内容放到undo中,我们 ...

  5. Amoeba实现mysql主从读写分离

    Amoeba实现mysql主从读写分离 这段在网上看了下关于amoeba的文章,总体感觉好像要比mysql-proxy好的多,也参考了不少的资料,此文章可能与其他文章作者会有雷同的地方,请谅解,但是此 ...

  6. 使用附加导航(affix)实现内容切换

    <!DOCTYPE html> <html> <head> <title> new document </title> <meta c ...

  7. php 两个数组是否相同,并且输出全面的数据,相同的加一个字段标示

    方法一: $date是数组,数组中有字段id,name; $data1是数组,数组中有字段sort_id,name; 所以要通过$date[$i]['id']==$data1[$j]['sort_id ...

  8. Quartz Quick Start Guide

    Welcome to the QuickStart guide for Quartz. As you read this guide, expect to see details of: Downlo ...

  9. oracle-行转列

    <一>合并两个结果集,并且两个结果集的数据 根据条目自动归为一行结果集1 如下:SQL> select t1.fplx,t1.djje from yw_zjfpjl t1  ; FP ...

  10. power desinger 学习笔记<六>

    原帖地址:http://blog.csdn.net/spt110/article/details/8640849 PowerDesigner中Table视图同时显示Code和Name,像下图这样的效果 ...