Lightoj 1235 - Coin Change (IV) 【二分】
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235
题意: 有N个硬币(N<=18)。问是否能在每一个硬币使用不超过两次的情况下支付正好K的面额。
思路 : dfs构造出用这些硬币用前一半能支付的全部费用和后一半能支付的全部费用。
之后排序,枚举前一半的每一个面值在第二个里面二分寻找就可以。(或者用set保存)。
代码:(set)
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
using namespace std;
int n;
int num1, num2;
long long w;
long long a[1000000];
set <long long>s1;
set <long long>s2;
void dfs1(long long sum, int x, int num)
{
if (x == num1)
{
s1.insert(sum);
return;
}
for (int i = 0; i <= 2; i++)
dfs1(sum + a[x] * i,x + 1, num + 1);
}
void dfs2(long long sum,int x, int num)
{
if (x == n)
{
s2.insert(sum);
return;
}
for (int i = 0; i <= 2; i++)
dfs2(sum + a[x] * i, x + 1, num + 1);
}
int main()
{
int t;
scanf("%d", &t);
int cases = 1;
while (t--)
{
s1.clear();
s2.clear();
memset(a, 0, sizeof(a));
scanf("%d %lld", &n, &w);
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
num1 = n >> 1;
num2 = n - num1;
dfs1(0, 0, num1);
dfs2(0, num1, n);
int ok = 0;
set <long long> ::iterator it;
for (it = s1.begin(); it != s1.end(); it++)
{
int tmp = *it;
//cout << tmp << endl;
if (s2.count(w-tmp) != 0)
{
ok = 1;
break;
}
}
if (ok) printf("Case %d: Yes\n", cases++);
else printf("Case %d: No\n", cases++);
}
return 0;
}
Lightoj 1235 - Coin Change (IV) 【二分】的更多相关文章
- LightOJ 1235 - Coin Change (IV) (折半枚举)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1235 题目描述: 给出n个硬币,每种硬币最多使用两次,问能否组成K面值? 解 ...
- 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 - 1232 - Coin Change (II)
先上题目: 1232 - Coin Change (II) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- LightOJ - 1231 - Coin Change (I)
先上题目: 1231 - Coin Change (I) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- C - Coin Change (III)(多重背包 二进制优化)
C - Coin Change (III) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- Spring Cloud (11) Hystrix-监控聚合监控
上一篇利用Hystrix Dashboard去监控断路器的Hystrix command,当我们有很多服务的时候,就需要聚合所有服务的Hystrix Dashboard数据了,这就需要Hystrix ...
- lua_string_pattern
两大特点: 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作字符串,而是返回一个新的字符串. 库函 ...
- 初学layer
canvas是支持图层layer渲染这种技术的,canvas默认就有一个layer,当我们平时调用canvas的各种drawXXX()方法时,其实是把所有的东西都绘制到canvas这个默认的layer ...
- MySQL 执行计划中Extra的浅薄理解
1.using where: Extra中出现“Using where”,通常来说,意味着全表扫描或者在查找使用索引的情况下,但是还有查询条件不在索引字段当中. 如果需要回表也是用这个. 2.usin ...
- echarts通过ajax请求展示多叉树
背景:在sqlserver使用过程中经常由于各种原因会出现阻塞,并发数较高,很难肉眼看出那个session阻塞了其他process,通过sql查询出根源也需要大量的重复操作才能够找到. 因此就有这方面 ...
- XML——读与写
XML写入 private static void writeXml() { using (XmlTextWriter xml = new XmlTextWriter(@"C:\Users\ ...
- [Windows Server 2008] ASP.net安装方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装ASP.n ...
- 【sqli-labs】 less48 GET -Error based -Blind -Numeric -Order By Clause(GET型基于盲注的整型Order By从句注入)
图片还是47...访问的的确是48 这个是基于bool的盲注 http://192.168.136.128/sqli-labs-master/Less-48/?sort=1 and sleep(0.1 ...
- python 模块学习——time模块
Python语言中与时间有关的模块主要是:time,datetime,calendar time模块中的大多数函数是调用了所在平台C library的同名函数, 所以要特别注意有些函数是平台相关的,可 ...
- cstringlist不完全用法
CStringList是CString链表,在MFC编程中STL之外的另一选择,用起来更加简洁. 插入数据:AddTail();AddHead() 删除数据:RemoveAll();RemoveAt( ...