zoukankan      html  css  js  c++  java
  • HDU 5808 Price List Strike Back bitset优化的背包。。水过去了

    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[222];
    const int maxn = 20000 + 20;
    int dis[maxn];
    char ans[100000 + 20];
    void init() {
        for (int i = 1; i <= 100; ++i) vc[i].clear();
    }
    void work() {
        init();
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; ++i) {
            int price;
            scanf("%d", &price);
            vc[price].push_back(i);
        }
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &dis[i]);
        }
        for (int i = 1; i <= m; ++i) {
            int L, R, mostDis, sum;
            scanf("%d%d%d%d", &L, &R, &mostDis, &sum);
            bitset<120>dp;
            dp[0] = 1;
            bool flag = false;
            for (int j = 1; j <= 100 && !flag; ++j) {
                if (vc[j].size() == 0) 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] = '1';
            } else ans[i] = '0';
        }
        ans[m + 1] = '';
        printf("%s
    ", ans + 1);
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        int t;
        scanf("%d", &t);
        while (t--) work();
        return 0;
    }
    View Code
  • 相关阅读:
    利用数据库复制技术 实现MSSQL数据同步更新
    育子两篇你会教育自已的小孩吗
    hdu 1046 Gridland (找规律题)
    hdu 1022 Train Problem I (栈的操作,还水了半天)
    hdu 4022 Bombing (强大的map一对多的映射)
    POJ 1702 Eva's Balance (数论,平衡三进制)
    hdu 3951 Coin Game (博弈)
    hdu 1058 Humble Numbers (DP初步)
    hdu 2084 数塔 (DP初步)
    hdu 1056 HangOver (打表水题)
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6506951.html
Copyright © 2011-2022 走看看