题目链接:

  http://www.lightoj.com/volume_showproblem.php?problem=1235

题目描述:

  给出n个硬币,每种硬币最多使用两次,问能否组成K面值?

解题思路:

  因为K草鸡大,尽管n很小,但是2^n很大,无法用背包做到O(nK)的复杂度。如果暴力枚举复杂度立马飙升到O(2^(n+1))。···········

  所以引进一种新的算法,折半查找:把所要枚举的值,先把前部分的值所有情况枚举出来,再把后部分的值所有情况枚举出来并排序,结合二分进行查找。 这样可以把复杂度降到O(2^(n/2)*log2(n))。

  最近不做题,感觉自己萌萌哒。大家玩敲七,到我这里,我总是一脸懵逼的样子。希望一天一个dp,到老也能萌萌哒  (。・`ω´・)

代码:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std; #define maxn 20000
int cnt1, cnt2, k, n, num;
int a1[maxn], a2[maxn], b[]; void dfs (int sum, int x)
{
if (x == num)
{
num == n/ ? a1[cnt1++] = sum : a2[cnt2++] = sum;
return ;
}
for (int i=; i<; i++)
dfs (sum+b[x]*i, x+);
} bool sreach (int x)
{
int low = , high = cnt2-; while (low <= high)
{
int mid = (low + high) / ;
if (a1[x] + a2[mid] == k)
return true;
else if (a1[x] + a2[mid] > k)
high = mid - ;
else
low = mid + ;
} return false;
} int main ()
{
int T, l = ;
scanf ("%d", &T); while (T --)
{
scanf ("%d %d", &n, &k);
for (int i=; i<n; i++)
scanf ("%d", &b[i]); cnt1 = cnt2 = ;
num = n / ;
dfs (, ); num = n;
dfs (, n/);
sort (a2, a2 + cnt2); int i;
for (i=; i<cnt1; i++)
{
if (sreach (i))
break;
}
if (i == cnt1) printf("Case %d: No\n", l++);
else printf ("Case %d: Yes\n", l++);
}
return ;
}

LightOJ 1235 - Coin Change (IV) (折半枚举)的更多相关文章

  1. Lightoj 1235 - Coin Change (IV) 【二分】

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235 题意: 有N个硬币(N<=18).问是否能在每一个硬币使用不超过两 ...

  2. 1235 - Coin Change (IV)

    1235 - Coin Change (IV)    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 M ...

  3. Coin Change (IV) (dfs)

    Coin Change (IV) Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu [Subm ...

  4. Lightoj 1231 - Coin Change (I) (裸裸的多重背包)

    题目链接: Lightoj  1231 - Coin Change (I) 题目描述: 就是有n种硬币,每种硬币有两个属性(价值,数目).问用给定的硬币组成K面值,有多少种方案? 解题思路: 赤果果的 ...

  5. LightOj 1076 - Get the Containers (折半枚举好题)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1076 题目描述: 给出n个数,要求分成m段,问这m段中最大的总和,最小是多少 ...

  6. LightOJ - 1231 - Coin Change (I)

    先上题目: 1231 - Coin Change (I)   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit:  ...

  7. LightOJ - 1232 - Coin Change (II)

    先上题目: 1232 - Coin Change (II)   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...

  8. LightOj 1170 - Counting Perfect BST (折半枚举 + 卡特兰树)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1170 题目描述: 给出一些满足完美性质的一列数(x > 1 and y ...

  9. C - Coin Change (III)(多重背包 二进制优化)

    C - Coin Change (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

随机推荐

  1. (29)java web的hibernate使用-crud的dao

    1, 做个简单的util public class HibernateUtils { private static SessionFactory sf; static { //加载主要的配置文件 sf ...

  2. mysql字符串的常用函数(截取和拼接)

    #截取字符串(先正序取2个,再倒序取1个)SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('aaa-gg-cc-dd','-',2),'-',-1) #获取子表某个字段的 ...

  3. Linux内核日志开关

    Linux内核日志开关 1.让pr_debug能输出 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -59,7 +59,7 ...

  4. Hibernate写hql语句与不写hql语句的区别?

    写hql语句与不写hql语句的区别? 写hql语句:书写HQL语句,所有的查询与投影的设计均使用HQL语句完成. 不写hql语句:没有任何查询语句,所有的查询与投影的设计使用面向对象格式完成. 二者选 ...

  5. EOS智能合约为何选择Web Assembly(wasm)

    比特币的程序非常简单,由解锁脚本和锁定脚本构成.以太坊有智能合约,有图灵完备的虚拟机EVM,但是指令也相对简单,且自成一套.这两种程序本质上都是脚本程序,即由程序翻译指令并执行,而不是由本地机器CPU ...

  6. Java 猫扑(mop)打卡小应用

    唉 mop又没打卡,前面十几天全没啦,像我们这些IT码农虽然天天上网,但是总是忘记打卡,这不一失足生成千古恨,失败了撒.好不容易每次打卡都能得几百份的,唉.​1. [代码][Java]代码   pac ...

  7. silverlight DataGrid 内嵌ComboBox 实现加载和保存

    <Grid x:Name="LayoutRoot" Background="White" Height="322" Width=&qu ...

  8. c++11实现DLL帮助类

    用过DLL的人都会发现,在C++中调用dll中的函数有点繁琐,调用过程如下:在加载dll后还要定义一个对应的函数指针类型,接着调用GetProcAddress获取函数地址,再转成函数指针,最后调用函数 ...

  9. 「LuoguP4047」 [JSOI2010]部落划分

    Description 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落之间则经常发生争斗.只是,这一切都成 ...

  10. cmake官方文档

    https://cmake.org/cmake/help/v3.0/genindex.html