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 ...
随机推荐
- 自学Python十二 战斗吧Scrapy!
初窥Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 还是先推荐几个学习的教程:Scrapy 0.2 ...
- MVC系列学习(七)-模板页
1.新建一个MVC项目,选择基本 2.查看文件 看到VS为我们生成了一些东西 布局页面,Layout 指定了模板页 3.开始实例 首先控制器中的代码如下: 视图中代码如下: 1.在/Views/_Vi ...
- B树、B+树、红黑树、AVL树比较
B树是为了提高磁盘或外部存储设备查找效率而产生的一种多路平衡查找树. B+树为B树的变形结构,用于大多数数据库或文件系统的存储而设计. B树相对于红黑树的区别 在大规模数据存储的时候,红黑树往往出现由 ...
- [hihocoder][Offer收割]编程练习赛58
最大的K-偏差排列 每次取可选范围里的最大的数字,如果最左侧的数字还没有使用就直接使用最左侧的数字 #pragma comment(linker, "/STACK:102400000,102 ...
- 【PostgreSQL-9.6.3】触发器概述(普通触发器)
一个触发器声明了当执行一种特定类型的操作时数据库应该自动执行一个特殊的函数.触发器可以被附加到表.视图和外部表.触发器经常用于做完整性约束,或者某种业务规则的约束. 1. 触发器的创建语法如下: CR ...
- JS高级——Function
Function构造函数 可以用来新建函数对象 1.一个参数都不传的情况创建的就是一个空的函数 2.只传一个参数的情况这个参数就是函数体 3.传多个参数的情况,最后一个参数为函数体,前面的参数都是该函 ...
- C# SqlParameter 使用
//System.Data.SqlClient.SqlParameter[] sqlParameters = new System.Data.SqlClient.SqlParameter[]{ }; ...
- git怎么克隆远程仓库到本地仓库
参考: https://blog.csdn.net/zhangzeshan/article/details/81564990 不知道为什么输入git的克隆地址就会提示密码错误 ,使用http地址就直接 ...
- CAD当前选择实体发生变化调用事件(com接口)
主要用到函数说明: _DMxDrawXEvents::SelectModified 当前选择实体发生变化,会调用该事件,详细说明如下: 参数 说明 IDispatch* pAryId 当前被选择的实体 ...
- .htaccess使用
RewriteEngine on #请求替换 #test-zhangsan-20 替换为 test.php?name=zhangsan&age=20 RewriteRule test-([a- ...