FZUOJ Problem 2178 礼品配送
Problem 2178 礼物分配
题目链接: Click Here~
Problem Description
Input
(N<=30) 第二行有N个整数,表示Eric所衡量的每一个礼物的价值vi。(1<=vi<=10000000) 第三行也有N个整数,表示R.W所衡量的每一个礼物的价值wi。(1<=wi<=10000000)
Output
这种话总情况是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 礼品配送的更多相关文章
- fzuoj Problem 2129 子序列个数
http://acm.fzu.edu.cn/problem.php?pid=2129 Problem 2129 子序列个数 Accept: 162 Submit: 491Time Limit: ...
- fzuoj Problem 2179 chriswho
http://acm.fzu.edu.cn/problem.php?pid=2179 Problem 2179 chriswho Accept: 57 Submit: 136 Time Limi ...
- fzuoj Problem 2182 水题
http://acm.fzu.edu.cn/problem.php?pid=2182 Problem 2182 水题 Accept: 188 Submit: 277Time Limit: 100 ...
- fzuoj Problem 2177 ytaaa
http://acm.fzu.edu.cn/problem.php?pid=2177 Problem 2177 ytaaa Accept: 113 Submit: 265Time Limit: ...
- FZUOJ Problem 2200 cleaning DP
Problem 2200 cleaning Problem Description N个人围成一圈在讨论大扫除的事情,需要选出K个人.但是每个人与他距离为2的人存在矛盾,所以这K个人中任意两个人的距 ...
- 【BZOJ】【2178】圆的面积并
自适应辛普森积分 Orz Hzwer 辛普森真是个强大的东西……很多东西都能积= = 这题的正解看上去很鬼畜,至少我这种不会计算几何的渣渣是写不出来……(对圆的交点求图包,ans=凸包的面积+一堆弓形 ...
- bzoj 2178
这题调精度真痛苦啊(向管理员要了数据才调出来). 用的是hwd在WC2015上讲的方法,考虑将原图分割,根据每个圆的左右边界和圆与圆交点的横坐标来分割,这样原图就被分成很多竖着的长条,并且每一条中间都 ...
- ArcGIS空间分析工具
1. 3D分析 1.1. 3D Features toolset 工具 工具 描述 3D Features toolset (3D 要素工具集) Add Z Information 添加 Z 信息 添 ...
- UVA 11237 - Halloween treats(鸽笼原理)
11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...
随机推荐
- flexbox语法(摘抄)
flex 容器上的属性: flex-direction: row | row-reverse | column | column-reverse (项目的排列方向) flex-wrap : nowra ...
- Bootstrap验证控件的使用
前端HTML代码 <form id="myForm" method="post" class="form-horizontal" ac ...
- 两个Hacker,专门Patch TObject
http://hallvards.blogspot.fr/2006/03/hack-8-explicit-vmt-calls.html http://www.deltics.co.nz/blog/po ...
- Redis 学习笔记五 经常使用php函数
PHPRedis的安装在这里: http://blog.csdn.net/xundh/article/details/46288277 键值操作 $redis = new Redis(); $redi ...
- javascript中使用Map
mis.comm.js.Map = function() { this.elements = new Array(); //获取MAP元素个数 this.size = function() { ret ...
- HNCU1099:堆积木
http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1099 题目描述 小明的侄子要过生日了,小 ...
- 深刻:截获windows的消息并分析实例(DefWindowProc),以WM_NCHITTEST举例(Windows下每一个鼠标消息都是由 WM_NCHITTEST 消息产生的,这个消息的参数包含了鼠标位置的信息)
1,回调函数工作机制 回调函数由操作系统自动调用,回调函数的返回值当然也是返回给操作系统了. 2,截获操作系统发出的消息,截获到后,将另外一个消息返回给操作系统,已达到欺骗操作系统的目的. 下面还是以 ...
- [Android学习笔记]使用getIdentifier()获取资源Id
使用getIdentifier()获取资源Id Android中可以使用getIdentifier()获取资源ID ex: 根据图片名称获取图片Id private int getImageResId ...
- hdu1217Arbitrage--解题报告
题意:套利,一个US币换取0.5 British pound,而1 British pound 换取10.0 French francs,同一时候 1 French franc buys 0.21 U ...
- BI中事实表和维度表的定义
一个典型的样例是,把逻辑业务比作一个立方体,产品维.时间维.地点维分别作为不同的坐标轴,而坐标轴的交点就是一个详细的事实.也就是说事实表是多个维度表的一个交点.而维度表是分析事实的一个窗体. 首先介绍 ...