题目大意

给定N个位置,每个位置i都有一个value[i]值,从中选择若干个位置,使得连续的M个位置中的被选中的位置数目不超过Q,求出所有选择方案中value和最大的方案,输出其最大value和。

分析

1、可以采用枚举的方式,枚举出所有的选择可能情况,每次枚举成功后,更新全局变量最大值即可。时间复杂度O(2^N).当然会超时...

//index为dfs搜索到的位置,num_in_last_M 为前M个位置中有多少个已经被选中,
//total_value为搜索到当前位置时,总的value
void dfs(int index, int num_in_last_M, int total_value){
if (index == N){ //递归出口
max_value = max_value > total_value ? max_value : total_value;
return;
}
//当前index处的位置不选中
dfs(index + , num_in_last_M, total_value);
if (index < M){
if (num_in_last_M < Q){
used[index] = true; //used[i] 用于表示位置i是否被选中
dfs(index + , num_in_last_M + , total_value + wastes[index]);
}
}
else if(num_in_last_M - used[index - M] < Q){
used[index] = true;
dfs(index + , num_in_last_M - used[index - M] + , total_value + wastes[index]);
}
//回溯!!!
used[index] = false;
}

2、采用动态规划的方式 
    dp[i][j] 表示前i个位置中,位置(i-M+1, i-M+2, ... i)构成的选中情况的二进制表示(0表示未选中,1表示选中)为j时,可以得到的最大值。 
    问题满足最优化子结构:从dp[i-1][k] 过渡到dp[i][j]时,如果dp[i][j]取得了最大值,那么dp[i-1][k]也一定为状态(i-1, k)的最大值; 问题无后效性:从dp[i-1][k]推演到dp[i][j]时,不关心 状态(i-1,k)是如何得到的,只关心最后的结果。 
    在求完dp数组之后,如果想要获得最后的结果,需要遍历dp[N]K来获得所有情况的最大值。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
#include<functional> using namespace std; int dp[1005][1030];
int waste[1005];
int main(){
int N, Q, M;
scanf("%d %d %d", &N, &M, &Q);
for (int i = 0; i < N; i++){
scanf("%d", &waste[i]);
}
for (int i = 0; i < N; i++){
for (int j = 0; j < (1 << min(i + 1, M)); j++){
int k = 0;
int jj = j;
while (jj){
k += (jj & 1);
jj >>= 1;
}
if (k > Q)
dp[i][j] = 0;
else{
jj = j >> 1;
dp[i][j] = max(dp[i][j], i > 0 ? dp[i - 1][jj] : 0);
jj |= (1 << min(i, M-1));
dp[i][j] = max(dp[i][j], i > 0 ? dp[i - 1][jj] : 0);
if (j & 1)
dp[i][j] += waste[i];
}
}
}
int result = 0;
for (int x = 0; x < (1 << min(M, N)); x++){
result = max(result, dp[N - 1][x]);
}
printf("%d\n", result);
return 0;
}

hiho_1044 状态压缩的更多相关文章

  1. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  2. HDU 3605:Escape(最大流+状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...

  3. [HDU 4336] Card Collector (状态压缩概率dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

  4. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  5. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  6. NOIP2005过河[DP 状态压缩]

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  7. vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  8. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  9. poj3254 状态压缩dp

    题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法.     分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于 ...

随机推荐

  1. Intellij IDEA svn的使用记录

    这里的忽略一直灰色的,可以进入 这里的版本控制里进行忽略选择 或者 这里进行添加 这里有三个选择 按照顺序 1.忽略指定的文件 2.忽略文件夹下所有文件 3.忽略符合匹配规则的文件 到Commit C ...

  2. jquery简单插件到复杂插件(2)--简单手风琴

    手风琴就是展示与隐藏 <div id="dataContent"> <div class="dataLeft fl"> <div ...

  3. PHP 之 FastCGI 与 mod_php 详解

    背景 PHP最常用的方式是以模块的方式(mod_php)运行在Apache中,也是Apache运行PHP的默认方式:但在Nginx中,Nginx又使用的是PHP-FPM,但是PHP-FPM到底是个什么 ...

  4. if for case 及多参数同时传递

    #!/bin/bash in 'start') echo "start server..." ;; 'stop') echo "stop server..." ...

  5. percona-toolkit工具包的使用教程之开发类工具

    percona-toolkit工具包的使用教程之开发类工具 1.  pt-duplicate-key-checker l  功能介绍: 功能为从mysql表中找出重复的索引和外键,这个工具会将重复的索 ...

  6. 初试 uTenux

    申请的的开发套件到目前还没到手,看到网友们都开始动手干了,我也是按捺不住了,所以就先在悠龙公司的主页下载了uTenux_V1.5.00r160.zip,打算看看,先了解一下. 下面是文件目录表: └─ ...

  7. C++,利用链式栈实现括号匹配,界面友好,操作方便,运行流畅

    #include<iostream> #include<string> using namespace std; struct Node { char ch; Node* ne ...

  8. 如何优雅的处理Nodejs中的异步回调

    前言 Nodejs最大的亮点就在于事件驱动, 非阻塞I/O 模型,这使得Nodejs具有很强的并发处理能力,非常适合编写网络应用.在Nodejs中大部分的I/O操作几乎都是异步的,也就是我们处理I/O ...

  9. V-rep学习笔记:机器人逆运动学数值解法(The Pseudo Inverse Method)

    There are two ways of using the Jacobian matrix to solve kinematics. One is to use the transpose of ...

  10. Know How And When To Use System.Message_Level To Control Messages In Oracle Forms

    SYSTEM.MESSAGE_LEVEL is used to control the messages that end users see when they use the Oracle For ...