Lightoj 1235 - Coin Change (IV) 【二分】
2015-07-25 11:14
363 查看
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235
题意: 有N个硬币(N<=18),问能否在每个硬币使用不超过两次的情况下支付正好K的面额。
思路 : dfs构造出用这些硬币用前一半能支付的所有费用和后一半能支付的所有费用。之后排序,枚举前一半的每个面值在第二个里面二分寻找即可。(或者用set保存)。
代码:(set)
题意: 有N个硬币(N<=18),问能否在每个硬币使用不超过两次的情况下支付正好K的面额。
思路 : dfs构造出用这些硬币用前一半能支付的所有费用和后一半能支付的所有费用。之后排序,枚举前一半的每个面值在第二个里面二分寻找即可。(或者用set保存)。
代码:(set)
[code]#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; }
相关文章推荐
- Hibernate中根据日期(天数)来查询
- 部分规则无法上载到Exchange Server并已被停用。这可能是由于系统不支持其中的某...
- 批处理常用命令及用法大全
- MTK平台闪光灯驱动分析
- 大疆Phantom无人机ios遥控软件Auotpilot
- 云服务器之间实时文件同步和文件备份的最简单高效的免费方案
- PostgreSQL PL/Python - Python Procedural Language 安装
- 从今天开始
- net user的使用
- java.io学习总结
- 使用rsync同步Linux数据到Windows
- 黑马程序员---C语言的标识符命名原则与保留字
- 多目录工程的makefile示例
- 通过异或交换变量的数值
- AD中查询180天未登录的计算机账户
- OpenCV图像细化的一个例子
- WinXP/Win7/Win8本地用户配置文件迁移至域用户
- AOP学习并实践
- django 1.8 官方文档翻译: 2-6-3 提供初始数据
- Leetcode 12 Integer to Roman