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 ...
随机推荐
- 增加删除div
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- mysql中返回当前时间的函数或者常量
引用:http://blog.sina.com.cn/s/blog_6d39dc6f0100m7eo.html 1.1 获得当前日期+时间(date + time)函数:now() 除了 now() ...
- php中的抽象方法和抽象类
1.什么是抽象方法? 我们在类里面定义的没有方法提的方法就是抽象方法.所谓的没有方法体指的是,在声明的时候没有大括号以及其中的内容,而是直接在声明时在方法名后加上分号结束,另外在声明抽象方法时方 ...
- java攻城狮之路--复习xml&dom_pull编程
xml&dom_pull编程: 1.去掉欢迎弹窗界面:在window项的preferences选项中输入“configuration center” 找到这一项然后 把复选框勾去即可. ...
- PHP7安装Memcache+Memcached缓存加速WordPress教程
PHP7安装Memcache+Memcached缓存加速WordPress教程 2016年1月19日 6,691 Views 生活方式 PHP7最显著的变化就是性能的极大提升,已接近Facebook开 ...
- Please, commit your changes or stash them before you can merge
参照 : https://blog.csdn.net/iefreer/article/details/7679631 用git pull来更新代码的时候,遇到了下面的问题: error: Your l ...
- vim三种模式
一般模式 以vi打开一个文件就直接进入一般模式了.一般模式下可以移动光标查看内容,通过ESC回到一般模式. 一般模式下常用的操作: 撤销与重做 命令 说明 u 复原上一个操作 . 小数点 重复上一个操 ...
- BZOJ 2442: [Usaco2011 Open]修剪草坪 单调队列
Code: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- Django - 一对多数据示例
1.增加Host -id 可以在模版中增加代码: 备注: 1.counter (从1开始) 2.counter0(从0开始) 3.revcounter(倒序) 4.revcounter0(倒序从0开始 ...
- php第十四节课
投票 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...