http://acm.hdu.edu.cn/showproblem.php?pid=5808

用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = true表示100这个数字出现过。

对于每一个新的数字,val,有转移方程,

dp = dp | (dp << val)

比如现在0000101,然后枚举了2进来,0000101 | 0010100

那个区间是暴力扫过去的,是水过去的,关键学了下bitset优化的背包。

#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>
vector<int>vc[];
const int maxn = + ;
int dis[maxn];
char ans[ + ];
void init() {
for (int i = ; i <= ; ++i) vc[i].clear();
}
void work() {
init();
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i) {
int price;
scanf("%d", &price);
vc[price].push_back(i);
}
for (int i = ; i <= n; ++i) {
scanf("%d", &dis[i]);
}
for (int i = ; i <= m; ++i) {
int L, R, mostDis, sum;
scanf("%d%d%d%d", &L, &R, &mostDis, &sum);
bitset<>dp;
dp[] = ;
bool flag = false;
for (int j = ; j <= && !flag; ++j) {
if (vc[j].size() == ) continue;
int lo = lower_bound(vc[j].begin(), vc[j].end(), L) - vc[j].begin();
int up = upper_bound(vc[j].begin(), vc[j].end(), R) - vc[j].begin();
up--;
for (int k = lo; k <= up; ++k) {
if (dis[vc[j][k]] > mostDis) continue;
dp = dp | (dp << j);
if (dp[sum]) {
flag = true;
break;
}
}
}
if (!flag) {
ans[i] = '';
} else ans[i] = '';
}
ans[m + ] = '\0';
printf("%s\n", ans + );
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

HDU 5808 Price List Strike Back bitset优化的背包。。水过去了的更多相关文章

  1. hdu 5506 GT and set dfs+bitset优化

    GT and set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Probl ...

  2. poj 1742 Coins(二进制拆分+bitset优化多重背包)

    \(Coins\) \(solution:\) 这道题很短,开门见山,很明显的告诉了读者这是一道多重背包.但是这道题的数据范围很不友好,它不允许我们直接将这一题当做01背包去做.于是我们得想一想优化. ...

  3. HDU 5890 Eighty seven(DP+bitset优化)

    题目链接 Eighty seven 背包(用bitset预处理)然后对于每个询问O(1)回答即可. 预处理的时候背包. #include <bits/stdc++.h> using nam ...

  4. HDU - 5887:Herbs Gathering (map优化超大背包)

    Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering t ...

  5. 回滚线段树+bitset优化01背包——cf981E

    /*首先考虑如何计算一个点的可能凑出的值,这就是一个01可行性背包问题那么再拓展到一段区间[1..n]的点上,每个query都可以看做是一段区间上的点[l,r]加上一个体积为x的物品,转换到01背包上 ...

  6. HDU4460-Friend Chains-BFS+bitset优化

    bfs的时候用bitset优化一下. 水题 #include <cstdio> #include <cstring> #include <algorithm> #i ...

  7. 【HDU 5808】 Price List Strike Back (整体二分+动态规划)

    Price List Strike Back There are nn shops numbered with successive integers from 11 to nn in Bytelan ...

  8. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...

  9. hdu 5036 Explosion bitset优化floyd

    http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么 ...

随机推荐

  1. js modify local file

    https://stackoverflow.com/questions/4561157/is-it-possible-to-modify-a-html-file-from-which-the-scri ...

  2. Linux epoll 源码注释

    https://www.cnblogs.com/stonehat/p/8613505.html 这篇文章值得好好读,先留个记录,回头看. IO多路复用之epoll总结 - Anker's Blog - ...

  3. ReactMotion Demo8 分析

    链接 首先通过spring函数Motion的style参数, 传入Motion Component, 计算style的过程: const style = lastPressed === i & ...

  4. 重入锁ReentrantLock用法以及如何实现重进入

    在java多线程中,可以使用synchronized实现线程之间的同步互斥,但在jdk1.5中增加了ReentrantLock类也能达到同样的效果,而且在使用上更加灵活,扩展功能上更加强大. 创建My ...

  5. oracle监听器启动,实例启动

    1,su - oracle登录oracle用户 2,sqlplus / as sysdba登录oracle 3,show parameter service_names; 若正常输出servicena ...

  6. Java数据库操作类演示

    只在mysql上测试过,不知道算不算好使​1. [代码][Java]代码     package org.load.demo; import java.io.IOException;import ja ...

  7. Docker安装 人生第一次

    Ubuntu 系列安装 Docker 通过系统自带包安装 Ubuntu 14.04 版本系统中已经自带了 Docker 包,可以直接安装. $ sudo apt-get update $ sudo a ...

  8. 一题多解 —— linux 日志文件(log)reload 重新载入

    1. tail -F 等同于–follow=name –retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪 也即可以间接实现从日志文件末尾,不断载 ...

  9. CodeForces546D:Soldier and Number Game(筛区间素数因子个数和)

    Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and giv ...

  10. [Java] public, private, protected

      同包不同类的成员 不同包中子类的成员 不同包非子类的成员 public √ √ √ protected √ √ × 默认 √ × × private × × ×