Problem 2178 礼物分配

题目链接: Click Here~

Problem Description

在双胞胎兄弟Eric与R.W的生日会上,他们共收到了N个礼物,生日过后他们决定分配这N个礼物(numv+numw=N)。对于每一个礼物他们俩有着各自心中的价值vi和wi,他们要求各自分到的礼物数目|numv-numw|<=1,而且各自所衡量的礼物价值的差值|sumv-sumw|尽可能小,如今他们想知道最小的差值是多少。

 Input

第一行为一个整数表示数据组数T。 接下来T组数组,每组数据第一行为一个整数N。

(N<=30) 第二行有N个整数,表示Eric所衡量的每一个礼物的价值vi。(1<=vi<=10000000) 第三行也有N个整数,表示R.W所衡量的每一个礼物的价值wi。(1<=wi<=10000000)

 Output

对于每组数据。输出最小的差值。


算法分析:
能够easy想到的方法是直接枚举暴力。如今对第一个人来分析,每一个物品都有选或者不选的可能。

这种话总情况是2^30次方。肯定超时。想着怎么优化。

非常easy的想到了二分搜索。就是折半查找。

思想就好是先预处理出前一半部分的结果。

然后,在用前面的结果推断后面的情况。这样就能够优化到了2^15次方了。


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std; typedef __int64 LL;
const int INF = 1 << 30;
const int MAXN = 40;
vector<int> num[MAXN];
int vi[MAXN],wi[MAXN]; int main() {
int n,T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i = 0;i < n;++i) {
scanf("%d",&vi[i]);
}
for(int i = 0;i < n;++i) {
scanf("%d",&wi[i]);
} for(int i = 0;i <= n;++i)
num[i].clear(); int n2 = n/2;
int cnt,sum1 ,sum2,sum;
for(int S = 0;S < 1 << n2; ++S) {
cnt = 0,sum1 = 0,sum2 = 0;
for(int i = 0;i < n2;++i) {
if(S >> i & 1) {
sum1 += vi[i];
cnt++;
} else {
sum2 += wi[i];
}
}
num[cnt].push_back(sum1 - sum2);
} for(int i = 0;i < n2;++i) {
sort(num[i].begin(),num[i].end());
num[i].erase(unique(num[i].begin(),num[i].end()),num[i].end());
} int ans = INF;
for(int S = 0;S < 1 << (n-n2);++S) {
sum,cnt = 0,sum1 = 0,sum2 = 0;
for(int i = 0;i < (n-n2);++i) {
if(S >> i & 1) {
sum1 += vi[i+n2];
cnt++;
} else {
sum2 += wi[i+n2];
}
}
int t = n - n2 - cnt;
sum = sum1 - sum2;
vector<int>::iterator iter;
iter = lower_bound(num[t].begin(),num[t].end(),-sum); if(iter != num[t].end() && abs(*iter + sum) < ans)
ans = abs(*iter + sum); if(iter != num[t].begin()) {
--iter;
if(abs(*iter + sum) < ans) ans = abs(*iter + sum);
}
} printf("%d\n",ans);
}
return 0;
} /* 3
1 2 3
4 2 1 5
1 2 3 5 4
1 1 1 1 5 6
1 2 3 4 5 5
1 1 1 1 1 8 */





版权声明:本文博客原创文章,博客,未经同意,不得转载。

FZUOJ Problem 2178 礼品配送的更多相关文章

  1. fzuoj Problem 2129 子序列个数

    http://acm.fzu.edu.cn/problem.php?pid=2129 Problem 2129 子序列个数 Accept: 162    Submit: 491Time Limit: ...

  2. fzuoj Problem 2179 chriswho

    http://acm.fzu.edu.cn/problem.php?pid=2179 Problem 2179 chriswho Accept: 57    Submit: 136 Time Limi ...

  3. fzuoj Problem 2182 水题

    http://acm.fzu.edu.cn/problem.php?pid=2182 Problem 2182 水题 Accept: 188    Submit: 277Time Limit: 100 ...

  4. fzuoj Problem 2177 ytaaa

    http://acm.fzu.edu.cn/problem.php?pid=2177 Problem 2177 ytaaa Accept: 113    Submit: 265Time Limit: ...

  5. FZUOJ Problem 2200 cleaning DP

    Problem 2200 cleaning  Problem Description N个人围成一圈在讨论大扫除的事情,需要选出K个人.但是每个人与他距离为2的人存在矛盾,所以这K个人中任意两个人的距 ...

  6. 【BZOJ】【2178】圆的面积并

    自适应辛普森积分 Orz Hzwer 辛普森真是个强大的东西……很多东西都能积= = 这题的正解看上去很鬼畜,至少我这种不会计算几何的渣渣是写不出来……(对圆的交点求图包,ans=凸包的面积+一堆弓形 ...

  7. bzoj 2178

    这题调精度真痛苦啊(向管理员要了数据才调出来). 用的是hwd在WC2015上讲的方法,考虑将原图分割,根据每个圆的左右边界和圆与圆交点的横坐标来分割,这样原图就被分成很多竖着的长条,并且每一条中间都 ...

  8. ArcGIS空间分析工具

    1. 3D分析 1.1. 3D Features toolset 工具 工具 描述 3D Features toolset (3D 要素工具集) Add Z Information 添加 Z 信息 添 ...

  9. UVA 11237 - Halloween treats(鸽笼原理)

    11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...

随机推荐

  1. [Android学习笔记]try-catch

    private boolean test() { boolean result = true; String str = null; try { Log.d("test",&quo ...

  2. 【Python】Python 基础知识

    数字和表达式 >>> 2+3 5 >>> 1.0/2.0 0.5 >>> 1.0//2.0 # // 0.0 >>> 1%2 # ...

  3. javascripte (三) 改变html图像

    <script> function changeImage(){ element=document.getElementById("myimage") if (elem ...

  4. 【菜鸟看框架】——EF怎样自己主动生成实体

    引言 在上一篇博客中给大家介绍了一些关于EF框架的基本知识.让大家对实体架构算是有了一个入门的认识,当然知识 这一篇博客是不能非常清楚的理解实体架构的内涵的.我们须要在实践中自己去不断的研究和探索当中 ...

  5. 访问项目时,不能自动加载index.php文件

    1.修改配置文件D:\lamp\apache\conf\httpd.conf加上DirectoryIndex index.hmtl index.php <IfModule !mpm_netwar ...

  6. IE与FF脚本兼容性问题

    (1) window.event: 表示当前的事件对象,IE有这个对象,FF没有,FF通过给事件处理函数传递事件对象 (2) 获取事件源 IE用srcElement获取事件源,而FF用target获取 ...

  7. hdu4341(分组背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4341 题意:一个人在原点(0,0)抓金子,每块金子有一个获得需要的时间t和价值v.而且有的金子可能在一 ...

  8. 利用objc的runtime来定位次线程中unrecognized selector sent to instance的问题

    昨天遇到一个仅仅有一行错误信息的问题: -[NSNull objectForKey:]: unrecognized selector sent to instance 0x537e068 因为这个问题 ...

  9. xcode Workspaces

    A workspace is an Xcode document that groups projects and other documents so you can work on them to ...

  10. 使用Ambari快速部署Hadoop大数据环境

    使用Ambari快速部署Hadoop大数据环境   发布于2013-5-24   前言 做大数据相关的后端开发工作一年多来,随着Hadoop社区的不断发展,也在不断尝试新的东西,本文着重来讲解下Amb ...