题目链接:

  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. jquery事件手冊

    方法 描写叙述 bind() 向匹配元素附加一个或很多其它事件处理器 blur() 触发.或将函数绑定到指定元素的 blur 事件 change() 触发.或将函数绑定到指定元素的 change 事件 ...

  2. linux PC手把手搭建minigui3.0开发环境

    1.下载网址http://www.minigui.com/en/download/ 2.下载资料: 3.安装过程: (1)安装 libminigui-gpl-3.0.12.tar.gz tar zxv ...

  3. iOS simulator+Appium

    Why are you trying to run iOS automation on a real device? That's a bad idea. iOS Automation on a re ...

  4. Creating a .bash_profile on your mac

    A typical install of OS X won't create a .bash_profile for you. When you want to run functions from ...

  5. hadoop-client

    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common --> <dependency> ...

  6. Hive两种访问方式:HiveServer2 和 Hive Client

        老版HiveClient:  要求比较多,需要Hive和Hadoop的jar包,各配置环境.       HiveServer2:   使得与YARN和HDFS的连接从Client中独立出来, ...

  7. RedisCluster集群原理

    主从复制,数据值每个服务器都存了. 针对redis集群的解决方案, 连接这个集群,不用在乎Master了 6台redis 1.why use Redis? 减轻数据库访问压力 2.持久化 RDB(间隔 ...

  8. POJ - 2417 Discrete Logging(Baby-Step Giant-Step)

    d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...

  9. LA-4356&&hdu-2469 (极角排序+扫描线)

    题目链接: Fire-Control System Time Limit: 12000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (J ...

  10. H5页面解决左右滑动问题

    在head里面加入. <meta name="viewport" content="width=device-width, initial-scale=1.0, u ...