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& ...
随机推荐
- 【ASP.NET Web API教程】6 格式化与模型绑定
原文:[ASP.NET Web API教程]6 格式化与模型绑定 6 Formats and Model Binding 6 格式化与模型绑定 本文引自:http://www.asp.net/web- ...
- ELK日志系统:Elasticsearch + Logstash + Kibana 搭建教程(转)
环境:OS X 10.10.5 + JDK 1.8 步骤: 一.下载ELK的三大组件 Elasticsearch下载地址: https://www.elastic.co/downloads/elast ...
- Swift - 给表格的单元格UITableViewCell添加图片,详细文本标签
表格UITableView中,每一单元格都是一个UITableViewCell.其支持简单的自定义,比如在单元格的内部,添加图片和详细文本标签. 注意UITableViewCell的style: (1 ...
- 超声波模块SRF05
//////////////////////////////////////////////////////////////////////////////// // // PIC16F877 ...
- 查看进程所用的内存(使用GetWindowThreadProcessId取得进程ID,OpenProcess打开进程和GetProcessMemoryInfo取得内存信息)
// function GetProcessMemorySize(_sProcessName: string; var _nMemSize: Cardinal): Boolean; var l_nWn ...
- CURD特性
本节课大纲: 一.ThinkPHP 3 的CURD介绍 (了解) 二.ThinkPHP 3 读取数据 (重点) 对数据的读取 Read $m=new Model('User'); ##返回一个实例 $ ...
- linux下的块设备驱动(二)
上一章主要讲了请求队列的一系列问题.下面主要说一下请求函数.首先来说一下硬盘类块设备的请求函数. 请求函数可以在没有完成请求队列的中的所有请求的情况下就返回,也可以在一个请求都不完成的情况下就返回. ...
- Java 反射机制[Method反射]
Java 反射机制[Method反射] 接着上一篇Java 反射机制[Field反射],通过调用Person类的setName方法将obj的name字段的Value设置为"callPerso ...
- UVa 10616 - Divisible Group Sums
称号:给你n数字.免去m一个,这使得他们可分割d.问:有多少种借贷. 分析:dp,D01背包. 背包整数分区. 首先.整点d.则全部数字均在整数区间[0,d)上: 然后,确定背包容量,最大为20*10 ...
- haskell,lisp,erlang你们更喜欢哪个?
haskell,lisp,erlang你们更喜欢哪个? haskell,lisp,erlang你们更喜欢哪个?