LightOJ 1235 - Coin Change (IV) (折半枚举)
题目链接:
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) (折半枚举)的更多相关文章
- Lightoj 1235 - Coin Change (IV) 【二分】
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235 题意: 有N个硬币(N<=18).问是否能在每一个硬币使用不超过两 ...
- 1235 - Coin Change (IV)
1235 - Coin Change (IV) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 M ...
- Coin Change (IV) (dfs)
Coin Change (IV) Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu [Subm ...
- Lightoj 1231 - Coin Change (I) (裸裸的多重背包)
题目链接: Lightoj 1231 - Coin Change (I) 题目描述: 就是有n种硬币,每种硬币有两个属性(价值,数目).问用给定的硬币组成K面值,有多少种方案? 解题思路: 赤果果的 ...
- LightOj 1076 - Get the Containers (折半枚举好题)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1076 题目描述: 给出n个数,要求分成m段,问这m段中最大的总和,最小是多少 ...
- LightOJ - 1231 - Coin Change (I)
先上题目: 1231 - Coin Change (I) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- LightOJ - 1232 - Coin Change (II)
先上题目: 1232 - Coin Change (II) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- LightOj 1170 - Counting Perfect BST (折半枚举 + 卡特兰树)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1170 题目描述: 给出一些满足完美性质的一列数(x > 1 and y ...
- C - Coin Change (III)(多重背包 二进制优化)
C - Coin Change (III) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
随机推荐
- centos7+php7 swoole 安装
下载 swoole 首先下载swoole的源码包,这个操作很简单,没有太多说的. wget -c https://github.com/swoole/swoole-src/archive/v2.0.6 ...
- MVC Controller构造器注入
UnityDependencyResolver 的标准写法 public class UnityDependencyResolver : IDependencyResolver { priva ...
- LeetCode(27)题解:Remove Element
https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of th ...
- jquery.validate ajax验证
$("#form1").validate({ rules: { ctl00$ContentPlaceHolder1$txtShopName: { remote: { type: & ...
- iOS开发UIScrollView常见属性和方法
一.ScrollView常用方法和属性 @property(nonatomic)CGPoint contentOffset; 设置滚动的偏移量 @property(nonatomic)CGSize c ...
- 缓存框架Ehcache相关
单点缓存框架 只能针对单个jvm中,缓存容器存放jvm中,每个缓存互不影响 Ehcache gauva chache 内置缓存框架 jvm缓存框架 分布式缓存框架(共享缓存数据) Redis ...
- Silverlight中使用MVVM(2)
Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 Silverlight中使用MVVM(4)—演练 ...
- UVA-11082 Matrix Decompressing(有上下界的最大流)
题目链接: Matrix Decompressing 题意: 给一个矩阵的每行和每列的和,(给的是前i行或者列的和); 矩阵中每个元素的值在1到20之间,找出这样的一个矩阵: 思路: 把它转化成一个二 ...
- [SHOI 2012] 魔法树
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2836 [算法] 树链剖分 时间复杂度 : O(NlogN ^ 2) [代码] #in ...
- JAVA泛型与可变参数
泛型的引入: 早期的Object类型可以接收任意的对象类型,但是在实际的使用中,会有类型转换的问题.也就存在这隐患,所以Java提供了泛型来解决这个安全问题. 格式: 泛型类:public class ...