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 ...
随机推荐
- Parameter index out of range (1 > number of parameters, which is 0).
数据库错误:Parameter index out of range (1 > number of parameters, which is 0) ...
- linux命令(001) -- chkconfig
一.准备知识 在说明chkconfig命令的用途之前,有必要先了解一下Linux系统中/etc/rc[0-6].d目录的用途. 众所周知,在Linux系统定义了7种不同的启动级别,这7种启动级别的含义 ...
- Ubuntu 系统的常用快捷键
Ubuntu操作基本快捷键 ibus-setup :设置系统输入法 scp filename username@serverIp:/home/xxx/xxx/filename 回车输入该usern ...
- Rsync 传输不需要输入密码
1.背景 1) 一个作为服务器端:VM3(IP: 3.9.8.151) 2) 一个作为客户端:VM2(IP: 3.9.8.157) 3) 服务器端和客户端网络 ...
- Ajax——php基础知识(二)
header header('content-type:text/html; charset= utf-8');//设置编码格式为:utf-8 header('location:http://www. ...
- 4th 循环结构概述和for语句的格式及其使用
04.01_Java语言基础(循环结构概述和for语句的格式及其使用) A:循环结构的分类 for,while,do...while B:循环结构for语句的格式: for(初始化表达式;条件表达式; ...
- 浏览器加载 CommonJS 模块的原理与实现 (阮一峰大哥的 http://www.ruanyifeng.com/blog/2015/05/commonjs-in-browser.html)
就在这个周末,npm 超过了 cpan ,成为地球上最大的软件模块仓库. npm 的模块都是 JavaScript 语言写的,但浏览器用不了,因为不支持 CommonJS 格式.要想让浏览器用上这些模 ...
- [ZJOJ] 5794 2018.08.10【2018提高组】模拟A组&省选 旅行
Description 悠悠岁月,不知不觉,距那传说中的pppfish晋级泡泡帝已是过 去数十年.数十年 中,这颗泡泡树上,也是再度变得精彩,各种泡泡 天才辈出,惊艳世人,然而,似乎 不论后人如何的出 ...
- HDU - 2159 FATE(二维dp之01背包问题)
题目: 思路: 二维dp,完全背包,状态转移方程dp[i][z] = max(dp[i][z], dp[i-1][z-a[j]]+b[j]),dp[i][z]表示在杀i个怪,消耗z个容忍度的情况下 ...
- 关于预测io调用的思考
什么是预测io 预测io是linux2.6版本内核调用默认的调用程序,对应用程序进行跟踪,统计应用程序使用io情况,在读操作返回之前先停顿6ms时间(linux默认时间),如果这期间有读操作过来,可以 ...