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. Centos java 安装

    第一步:查看Linux自带的JDK是否已安装 (卸载centOS已安装的1.4) 安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java versi ...

  2. android JNI 资料大全

    AndroidJNI 通过C++调用JAVA 1. JNIEnv对象    对于本地函数    JNIEXPORT void JNICALL Java_video1_TestNative_sayHel ...

  3. jQuery选择器特殊字符与属性空格问题

    一.选择器中含有特殊符号的注意事项 1.选择器中含有“.”.“#”.“(”或“]”等特殊字符 根据W3C的规定,属性值中是不能含有这些特殊字符的,但在实际项目中偶尔会遇到表达式中含有“#”和“.”等特 ...

  4. I2S简单学习

    以下只是个人看法,有不妥之处,请批评指出. 参考资料:http://blog.csdn.net/ce123_zhouwei/article/details/6919954: 一.I2S接口简述 I²S ...

  5. lmhostid获取hostid为空问题

    lmhostid获取hostid为空问题 问题描写叙述 今天迁移曾经的一个装有flexlm的虚拟机,结果发如今迁移后启动时报错 ... Wrong hostid on SERVER line for ...

  6. 最简单的基于FFmpeg的移动端样例:IOS 视频转码器

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

  7. 打造字符界面的多媒体Linux系统

    0 环境与目标 硬件:ThinkPad R52笔记本一台. 目标:字符界面的Linux系统,同时可以看图片,听音乐,看视频,无线上网,重要的是可以听着音乐编程了! 注意:本文打造的系统主要是供一个工程 ...

  8. YTU 2436: C++ 习题 输出日期时间--友元类

    2436: C++ 习题 输出日期时间--友元类 时间限制: 1 Sec  内存限制: 128 MB 提交: 1243  解决: 690 题目描述 设计一个日期类和时间类,编写display函数用于显 ...

  9. YTU 2553: 谁是赢家

    2553: 谁是赢家 时间限制: 1 Sec  内存限制: 128 MB 提交: 94  解决: 25 题目描述 某一天,hcbbt等一群弱菜去tamara巨巨家里一起玩了一个卡牌游戏.巨 巨家里有2 ...

  10. Fresco-Facebook的图片加载框架的使用

    目前常用的开源图片加载框架有:1.Universal-Image-Loader,该项目存在于Github上面https://github.com/nostra13/Android-Universal- ...