CODEVS2144 砝码称重2 (哈希表)
由于m很大,所以不能使用DP。
注意到n≤30,直接暴力2^n会TLE。
所以,将砝码平均分成两份,对一份进行一次暴力,用哈希表存下可能的结果。
对下一份再进行一次暴力,在哈希表中搜索剩余的砝码重量是否存在,若存在则更新答案。
输出最小答案即可。
Program CODEVS2144;
const maxn=;
maxh=;
var a:array[..maxn] of longint;
n,i,ans:longint;
m:int64;
h:array[..maxh,..] of longint;
procedure insert(x,y:longint);
var hash:longint;
begin
hash:=x mod maxh;
while (h[hash,]<>-) and (h[hash,]<>x) do
hash:=(hash+) mod maxh;
if h[hash,]=- then
begin
h[hash,]:=x;
h[hash,]:=y;
end
else
if y<h[hash,] then h[hash,]:=y;
end;
function find(x:longint):longint;
var hash:longint;
begin
hash:=x mod maxh;
while (h[hash,]<>-) and (h[hash,]<>x) do
hash:=(hash+) mod maxh;
if h[hash,]=- then exit() else
exit(h[hash,]);
end;
procedure dfs(x:longint;sum:int64;num:longint);
begin
if sum>m then exit;
if x=n div + then
begin
insert(sum,num);
exit;
end;
dfs(x+,sum,num);
dfs(x+,sum+a[x],num+);
end;
procedure work(x:longint;sum:int64;num:longint);
var j:longint;
begin
if sum>m then exit;
if x=n+ then
begin
if sum=m then
begin
if num<ans then ans:=num;
exit;
end;
j:=find(m-sum);
if j<> then
begin
if j+num<ans then
ans:=j+num;
end;
exit;
end;
work(x+,sum,num);
work(x+,sum+a[x],num+);
end;
begin
for i:= to maxh do h[i,]:=-;
readln(n,m);
for i:= to n do readln(a[i]);
ans:=maxn;
dfs(,,);
work(n div +,,);
writeln(ans);
end.
CODEVS2144 砝码称重2 (哈希表)的更多相关文章
- NOI题库--砝码称重V2(多重背包2^n拆分)
以前只会写多重背包的原版,渣的不行,为了做此题不得不学习了一下,发现其实也不难,只要理解了方法就好多了(PS:其实和倍增挺像的) 8756:砝码称重V2 总时间限制: 1000ms 内存限制: 655 ...
- Codevs No.2144 砝码称重2
2016-05-31 22:01:16 题目链接: 砝码称重2 (Codevs No.2144) 题目大意: 给定N个砝码,求称出M的重量所需砝码最小个数 解法: 贪心 使砝码数量最小,当然是每个砝码 ...
- 安徽省2016“京胜杯”程序设计大赛_A_砝码称重
砝码称重 Time Limit: 1000 MS Memory Limit: 65536 KB Total Submissions: 61 Accepted: 37 Description 小明非常喜 ...
- P2347 砝码称重-DP方案数-bitset
P2347 砝码称重 DP做法 : 转化为 01背包. 进行方案数 更新.最后统计种类. #include<bits/stdc++.h> using namespace std; #def ...
- 51nod 1449 砝码称重 (进制思想)
1449 砝码称重 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 现在有好多种砝码,他们的重量是 w0,w1,w ...
- 51nod 1837 砝码称重【数学,规律】
题目链接:51nod 1837 砝码称重 小 Q 有 n 个砝码,它们的质量分别为 1 克. 2 克.……. n 克. 他给 i 克的砝码标上了编号 i (i = 1, 2, ..., n),但是编号 ...
- P2347 砝码称重
P2347 砝码称重 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1 ...
- Codevs 2144 砝码称重 2
2144 砝码称重 2 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 有n个砝码,现在要称一个质量为m ...
- 洛谷P1441 砝码称重
P1441 砝码称重 题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 输入输出格式 输入格式: 输入文件weight.in ...
随机推荐
- astgo-完整功能介绍
核心功能: Astgo最核心和强大的功能是呼叫中心模块.接入方式:中继卡.模拟卡接入,中继网关.O口网关接入.网络IP接入等.单机200个坐席,通话实时录音.灵活队列分组.开放式IVR设计,修改业务逻 ...
- 编译android4.4 报错error: call to '__property_get_too_small_error' declared with attribute 的处理 (转载)
转自:http://blog.csdn.net/syhost/article/details/14448899 完整的报错为: system/core/include/cutils/propertie ...
- Linux 用户管理(1) (/etc/passwd)
Linux所有的用户都在/etc/passwd文件里面. 1.为什么需要用户 1)计算机及网络资源的合理分配 2)可以控制用户访问系统的权限.3)身份认证 4) 进程 以某个用户的身份来运行 2.用 ...
- [ NOI 2005 ] 聪聪与可可
\(\\\) \(Description\) 一张\(N\)个点,\(M\)条边的有向图中,猫在\(A\)点,鼠在\(B\)点,每一秒两者按照以下规则移动: 猫先走去往老鼠所在地的最短路,可以走一步或 ...
- window.showModalDialog的问题
通过window.showModalDialog的方式弹出B页面,总报“拒绝访问”的错误,将站点添加到受信任站点可以解决这个问题
- Android集成二维码扫描功能
文章转载自 https://github.com/yipianfengye/android-zxingLibrary 在具体介绍该扫描库之前我们先看一下其具体的使用方式,看看是不是几行代码就可以集成 ...
- js获取某年某月一共多少天
const getDaysInMonth = (year, month) => { let date = new Date(year, month, 1); return new Date(da ...
- HDU_1028_Ignatius and the Princess III_(母函数,dp)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- PHP 之ip查询接口
/** * @param $ip 待查询的ip * @return mixed */ function getIpAddressInfo($ip) { $ipurl = 'http://api.ip1 ...
- nginx设置绑定解析实现二级域名多域名
apache(httpd)配置多个二级域名看这个链接:https://www.cnblogs.com/Crazy-Liu/p/10879928.html 网站的目录结构为/home/www├── bb ...