jdoj 2171: Grape

题意

题目大意

一个农场的葡萄架上挂着n串葡萄,若取一个葡萄就会获得与其相应的美味值。对于连续的k串葡萄,最多取b串,最少取a串,

问能够获得的最大美味值为多少

数据范围

n<=10000,0<=a<=b<=k<=10

分析

考虑到在选到第i串葡萄时,影响到决策的只有i前面k串葡萄

f[i] [sta] 表示,考虑完第i串葡萄,后k串葡萄的状态为sta时的最大美味值

小技巧

  1. 看看n的范围,再想想我们的状态表示,可得,每次转移只需要前一个状态的信息,所以我们可以用i&1表示当前位置,那么(i+1)&1就是下一个位置

  2. 为了方便,不妨将这k串葡萄中的距离i最远的一串葡萄使用最后一个二进制位表示, 这样,我们在状态转移时>>1即可

只有在a <= cnt[sta] <= b时才考虑转移,转移时状态或上 1<<k,不转移时在合法的时候取最大值即可。

向前推进max

/*

5 3 1 2

-1 1 2 -2 3

now_ans = 6

ans = 9

*/

#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
const int N = 11;
const int MAX = (1<<N)-1;
#define lowbit (i&-i) inline int read()
{
int x=0,f=1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')f=-1;ch = getchar();}
while(ch >='0' && ch <='9'){x=(x<<1)+(x<<3)+ch-'0';ch = getchar();}
return x*f;
} int n,k,a,b,sum, max_k;
int f[2][1<<N], val[10000+9], cnt[1<<N]; int getone(int i) {
int res = 0;
while(i) ++res, i -= lowbit;
return res;
}
int getval(int i) {
int res = 0, pos = 1;
while(i) {
if(i&1) res += val[pos];
i = i>>1, ++pos;
}
return res;
} void pre() {
n = read(), k = read(), a = read(), b = read();
for(int i = 1; i <= n; i++) val[i] = read(), sum += val[i];
max_k = (1<<k)-1;
for(int i = 0; i <= max_k; i++) cnt[i] = getone(i);
for(int i = 0; i <= max_k; i++) if(cnt[i]>=a && cnt[i]<=b) f[k&1][i] = getval(i);
} void solve() {
for(int i = k; i < n; i++) {//小技巧
int o = (i&1), op = (o+1)&1;//o表示当前第一维,op表示下一个第一维
memset(f[op], -0x3f, sizeof(f[op]));
for(int j = 0; j <= max_k; j++) if(cnt[j]>=a && cnt[j]<=b) {
int tmp1 = (j>>1), tmp2 = ((j|(1<<k))>>1);
//tmp1表示不选,tmp2表示选
if(cnt[tmp1]>=a && cnt[tmp1]<=b) f[op][tmp1] = max(f[op][tmp1], f[o][j]);//不减?
if(cnt[tmp2]>=a && cnt[tmp2]<=b) f[op][tmp2] = max(f[op][tmp2], f[o][j]+val[i+1]);
}
}
int ans = -2147483646;
for(int j = 0; j <= max_k; j++) if(cnt[j]>=a && cnt[j]<=b) ans = max(ans, f[n&1][j]);
printf("%d\n",ans*2-sum);
}
int main() {
pre();
solve();
return 0;
}

jdoj 2171: Grape的更多相关文章

  1. grape动态PHP结构(三)——API接口

    一.app视图与控制器

  2. grape动态PHP结构(二)——管理后台

    一.概述

  3. grape动态PHP结构(一)——目录结构与配置文件

    一.结构介绍 结构的名字grape,中文名叫葡萄,因为最近一个同事经常带葡萄到公司给我们吃,受到启发想到了这个名字. 1)本结构需要在PHP5.5中运行,如果要在5.4中运行,有些地方就要做些修改 2 ...

  4. fzu 2171 防守阵地 II

    Problem 2171 防守阵地 II Accept: 31    Submit: 112Time Limit: 3000 mSec    Memory Limit : 32768 KB  Prob ...

  5. Maven, Ivy, Grape, Gradle, Buildr, SBT, Leiningen, ant

    Maven, Ivy, Grape, Gradle, Buildr, SBT, Leiningen, ant

  6. CJOJ 2171 火车站开饭店(树型动态规划)

    CJOJ 2171 火车站开饭店(树型动态规划) Description 政府邀请了你在火车站开饭店,但不允许同时在两个相连的火车站开.任意两个火车站有且只有一条路径,每个火车站最多有 50 个和它相 ...

  7. 【Rails App】 应用服务器从Passenger切换为Puma, Grape出现线程安全问题

    Grape中的代码如下: def market @market ||= Market.find(params[:id]) end @market基于类层次的实例变量,属于非线程安全,如果一直使用多线程 ...

  8. Chromium Embedded Framework (CEF)_3.2171.2069_v20170606_x86.tar.xz

    CEF 为观看各个直播平台而特此修改的浏览器 可以单独提取 Flash 视频, 并可以修改视频的大小等功能 [增加了960x90% 和 1280x90%] 这次修改是主要针对 YY web 直播平台 ...

  9. Chromium Embedded Framework (CEF)_3.2171.1979_v20170602_x86.tar.xz

    CEF 为观看各个直播平台而特此修改的浏览器 可以单独提取 Flash 视频, 并可以修改视频的大小等功能 这次修改是主要针对 YY web 直播平台 对录屏的朋友有很大帮组 CEF_3.2171.1 ...

随机推荐

  1. [Linux] 安装grafana并且添加influxdb监控

    安装grafana,官网提供了ubuntu的安装包,直接进行安装 wget https://dl.grafana.com/oss/release/grafana_6.5.1_amd64.deb dpk ...

  2. rasa结合kashgari训练数据时,out of memory错误

    这两天新搬办公室,网络不好用,将就了.博客园也上不了,github也上不了了,工作效率降低不少.今天遇到同事使用rasa用机器人项目的问题,一个4核的Tesla K80 GPU,性能照说不差,但一运行 ...

  3. 03-Node.js学习笔记-系统模块path路径操作

    3.1为什么要进行路径拼接 不同操作系统的路径分隔符不统一 /public/uploads/avatar window 上是 \ / 都可以 Linux 上是 / 3.2路径拼接语法 path.joi ...

  4. Codeforces Round #588 (Div. 2)

    传送门 A. Dawid and Bags of Candies 乱搞. Code #include <bits/stdc++.h> #define MP make_pair #defin ...

  5. python爬虫初认识

    一.爬虫是什么? 如果我们把互联网比作一张大的蜘蛛网,数据便是存放于蜘蛛网的各个节点,而爬虫就是一只小蜘蛛, 沿着网络抓取自己的猎物(数据)爬虫指的是:向网站发起请求,获取资源后分析并提取有用数据的程 ...

  6. LeetCode解题笔记 - 1. Two Sum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  7. HTML网页自动跳转(重定向)

    HTML网页自动跳转(重定向) meta <head> <meta http-equiv="refresh" content="5;url=https: ...

  8. Codeforces Round #598 (Div. 3) F. Equalizing Two Strings 构造

    F. Equalizing Two Strings You are given two strings s and t both of length n and both consisting of ...

  9. kafka 重放 重播 从某个时间点或者offset开始消费

    转自: https://www.jianshu.com/p/932663e9a226 consumer.subscribe(topicA); consumer.poll(100);//正常订阅topi ...

  10. Docker容器 MySQL中文乱码解决方案

    docker exec进入容器 sudo docker exec -it 588340b778f6 bash 执行以下命令,将 character-set-server=utf8 写入mysql配置文 ...