题目链接:

  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. mac下Tomcat的安装

    转自:http://blog.csdn.net/liuyuyefz/article/details/8072485 对于Apache Tomcat 估计很多童鞋都会,那么今天就简单说下在mac上进行t ...

  2. Android 的assets文件资源与raw文件资源读取

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zpf8861/article/details/34504183   res/raw和assets的同 ...

  3. mybatis中GenericTokenParser的用法

    代码示例 import org.apache.ibatis.parsing.GenericTokenParser;import org.apache.ibatis.parsing.TokenHandl ...

  4. fminunc

    options = optimset('GradObj', 'on', 'MaxIter', 400); % Run fminunc to obtain the optimal theta% This ...

  5. ewasm项目初探

    为了改进EVM1.0,以太坊的新一代虚拟机项目ewasm (github.com/ewasm)将支持WebAssembly(wasm),wasm在性能,扩展性,开发工具,社区都更有优势.除以太坊外,一 ...

  6. java24点算法

    输入任意的四个数,求出所有能得到二十四点的算式,不过我是菜鸟,可能性能方面不好,希望各位多多指教​1. [代码][Java]代码     import java.util.ArrayList;impo ...

  7. javascript 无刷新上传图片之原理

    刚开始我认为可以像ajax 那样获取到数据然后通过ajax 发送请求,后来发现浏览器为了客户端的安全默认并没有给javascript 这个权限.这个方法当然是行不同了.我看了好像开源的上传图片原理,当 ...

  8. codeforces 441B. Valera and Fruits 解题报告

    题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...

  9. iOS添加弹出菜单

    最近接触的项目需要实现一个弹出窗,类似于点击微信navigation bar右上角的bar button所展现的弹出窗,最终效果如下: Demo代码存放在https://github.com/LuoD ...

  10. 使用masonry手写约束

    在iOS开发过程中,手写contraints是非常痛苦的一件事情,往往那么一丢丢功能要写大量的代码,非常容易发生错误,并且非常不方便调试.所以只有在不得以的情况下才采用手工方式写contraints, ...