http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1268&judgeId=193772

给出n = 20个数,问其是否有和为k的情况

分两段,用O(2^(n / 2))复杂度维护出每一段的所有情况。

然后枚举第一段,二分判断第二段是否也有k - first

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
map<int, bool>dp1;
set<int>dp2;
void work() {
int n, k;
scanf("%d%d", &n, &k);
dp1[] = true;
dp2.insert();
for (int i = ; i <= n / ; ++i) {
int x;
scanf("%d", &x);
map<int, bool> :: iterator it = dp1.end();
it--;
while (true) {
dp1[it->first + x] = true;
if (it == dp1.begin()) break;
it--;
}
}
for (int i = n / + ; i <= n; ++i) {
int x;
scanf("%d", &x);
set<int> :: iterator it = dp2.end();
it--;
while (true) {
dp2.insert(*it + x);
if (it == dp2.begin()) break;
it--;
}
}
for (map<int, bool> :: iterator it = dp1.begin(); it != dp1.end(); ++it) {
if (dp2.count(k - it->first)) {
cout << "Yes" << endl;
return;
}
}
cout << "No" << endl;
return;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

1268 和为K的组合 Meet in mid二分思路的更多相关文章

  1. 51Nod 1268 和为K的组合

    51Nod  1268  和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...

  2. 51Nod:1268 和为K的组合

    1268 和为K的组合  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以 ...

  3. 51nod 1268 和为K的组合 dfs

    题目: 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以,输出:& ...

  4. 51nod1268 和为K的组合(DFS)

    1268 和为K的组合  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以 ...

  5. 经典K线组合图解 > 正文

    日K线波段中上下影线的箱体操作法(完整) http://video.sina.com.cn/v/b/130809461-2486130757.html!!经典K线组合图解 > 正文 http:/ ...

  6. 77. Combinations (java 求C(n,k)的组合,排除重复元素)

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...

  7. 很多k线形态或k线组合是需要验证的

    1.十字星:表明多空力量平衡.是否翻转,需要验证. 2.要反应市场心理的k线才是有效的K线,不然就是伪k或伪k线形态.

  8. Leetcode刷题笔记(Python 找出所有相加之和为n的k个组合,组合中只允许含有1-9的正整数,并且每种组合中不存在重复的数字。)

    eg:输入:k=3,n=9 输出: [[1,2,6],[1,3,5],[2,3,4]] 输入:k=2,n=5 输出:[[1,4][2,3]] #!/usr/bin/env python # -*- c ...

  9. 51nod 1105 第K大的数 【双重二分/二分套二分/两数组任意乘积后第K大数】

    1105 第K大的数  基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...

随机推荐

  1. SQL 主机

    SQL 主机 SQL 主机 如果您想要您的网站存储数据在数据库并从数据库显示数据,您的 Web 服务器必须能使用 SQL 语言访问数据库系统. 如果您的 Web 服务器托管在互联网服务提供商(ISP, ...

  2. [Zlib]_[0基础]_[使用zlib库压缩文件]

    场景: 1. WIndows上没找到系统提供的win32 api来生成zip压缩文件, 有知道的大牛麻烦留个言. 2. zlib比較经常使用,编译也方便,使用它来做压缩吧. MacOSX平台默认支持z ...

  3. python05-09

    一.lambda表达式 def f1(): return 123 f2 = lambda : 123 def f3 = (a1,a2): return a1+a2 f4 = lambda a1,a2 ...

  4. 在EasyUI的DataGrid中嵌入Combobox

    在做项目时,须要在EasyUI的DataGrid中嵌入Combobox,花了好几天功夫,在大家的帮助下,最终看到了它的庐山真面: 核心代码例如以下: <html> <head> ...

  5. 2016/3/17 Mysq select 数学函数 字符串函数 时间函数 系统信息函数 加密函数

    一,数学函数主要用于处理数字,包括整型.浮点数等. ABS(X) 返回x的绝对值 SELECT ABS(-1)--返回1 CEll(X),CEILING(x)  返回大于或等于x的最小整数 SELEC ...

  6. C从控制台(stdin)输入带空格的字符串到字符数组中

    用scanf("%s",array); 的话遇到空格就停止接收后面的字符了,那怎么才能接收带空格的字符串呢? 1.用 gets() ,它可以接收带空格的字符串, 直到回车才结束输入 ...

  7. Docker vs. Kubernetes vs. Apache Mesos: Why What You Think You Know is Probably Wrong

    Docker vs. Kubernetes vs. Apache Mesos: Why What You Think You Know is Probably Wrong - Mesosphere h ...

  8. IntelliJ Idea 工具

     IntelliJ Idea 优化: 1.如何取消文件自动保存 File->Settings -> Appearance&Behavior -> System Setting ...

  9. YTU 2428: C语言习题 计算该日在本年中是第几天

    2428: C语言习题 计算该日在本年中是第几天 时间限制: 1 Sec  内存限制: 128 MB 提交: 1505  解决: 857 题目描述 定义一个结构体变量(包括年.月.日).编写一个函数d ...

  10. frameset使用总结

    frameset使用总结 2009-06-15 00:59 213人阅读 评论(0) 收藏 举报 框架bordermicrosoftinternethtml文档 FRAMESET 元素是 FRAME  ...