holiday.pas/c/cpp

Description

经过几个月辛勤的工作,FJ 决定让奶牛放假。假期可以在1…N 天内任意选择一段(需要连

续),每一天都有一个享受指数W。但是奶牛的要求非常苛刻,假期不能短于P 天,否则奶

牛不能得到足够的休息;假期也不能超过Q 天,否则奶牛会玩的腻烦。FJ 想知道奶牛们能

获得的最大享受指数。

Input(holiday.in)

第一行:N,P,Q.

第二行:N 个数字,中间用一个空格隔开。

Output(holiday.out)

一个整数,奶牛们能获得的最大享受指数。

Sample Input

5 2 4

-9 -4 -3 8 -6

Sample Output

5

Limitation

time:1s

memory:65536kb

50% 1≤N≤10000

100% 1≤N≤100000

1<=p<=q<=n

Hint

选择第3-4 天,享受指数为-3+8=5。

****把每一个区间的享受指数都存到一个二维数组里面,然后根据要求的最短天数和最长天数的区间进行循环,如果比上一个存的数值大的话就更换,最后输出最大值

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cctype>
using namespace std;
typedef long long ll;
#define enter printf("\n")
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch))
{
ans = ans * + ch - ''; ch = getchar();
}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) {putchar('-'); x = -x;}
if(x == ) {putchar(''); return;}
int q[], N = ;
q[] = ;
while(x) {q[++N] = x % ; x /= ;}
while(N) {putchar('' + q[N]); --N;}
} //忽略掉优化 int n, l, r;
ll a[maxn], sum[maxn];
ll ans = (ll)-INF * INF; ll dp[maxn][];
int size[maxn];
void RMQ(int n)
{
for(int i = ; i <= n; ++i)
dp[i][] = sum[i];
for(int j = ; ( << j) <= n; ++j)
for(int i = ; i + ( << j) - <= n; ++i)
dp[i][j] = max(dp[i][j - ], dp[i + ( << (j - ))][j - ]);
int k = ;
for(int i = ; i <= n; ++i)
{
if (( << k) <= i) k++;
size[i] = k - ;//求长度j
}
}
ll query(int L, int R)
{
int k = size[R - L + ];
return max(dp[L][k], dp[R - ( << k) + ][k]);
}
int main()
{
freopen("holiday.in", "r", stdin);
freopen("holiday.out", "w", stdout);
n = read(); l = read(); r = read();
for(int i = ; i <= n; ++i)
{
a[i] = read();
sum[i] = sum[i - ] + a[i];
} //求前缀和
RMQ(n);
for(int i = ; i <= n - l; ++i)
{
int L = i + l, R = i + r;
if(R > n) R = n;
ll _ans = query(L, R) - sum[i];
ans = max(ans, _ans);
}
write(ans); enter;
return ;
}

****首先感谢绿镜小哥哥(詹宜瑞童鞋)对女生的讲解。。。。

这道题可以先思考如果就是从第一天放假那么就是求【1,l】【1,r】之间的最大值

过渡到这道题可以枚举某一天为第一天。。相应的应该把真正的第一天到这个假的第一天之间的数值减去。

这道题用RMQ,特地还去翻了下他的博客,有个图看起来还不错

holiday的更多相关文章

  1. 【UOJ #29】【IOI 2014】holiday

    http://uoj.ac/problem/29 cdq四次处理出一直向左, 一直向右, 向左后回到起点, 向右后回到起点的dp数组,最后统计答案. 举例:\(fi\)表示一直向右走i天能参观的最多景 ...

  2. 水题 ZOJ 3876 May Day Holiday

    题目传送门 /* 水题:已知1928年1月1日是星期日,若是闰年加1,总天数对7取余判断就好了: */ #include <cstdio> #include <iostream> ...

  3. zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5500 The 12th Zhejiang Provincial ...

  4. 第十二届浙江省大学生程序设计大赛-May Day Holiday 分类: 比赛 2015-06-26 14:33 10人阅读 评论(0) 收藏

    May Day Holiday Time Limit: 2 Seconds Memory Limit: 65536 KB As a university advocating self-learnin ...

  5. HDU 4118 Holiday's Accommodation

    Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Jav ...

  6. hdoj 1827 Summer Holiday【强连通分量&&缩点】

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU 4118 树形DP Holiday's Accommodation

    题目链接:  HDU 4118 Holiday's Accommodation 分析: 可以知道每条边要走的次数刚好的是这条边两端的点数的最小值的两倍. 代码: #include<iostrea ...

  8. Summer Holiday(强联通入度最小点)

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. IOI2014 day2 task4 Holiday

    题目 题目链接 大意:从左到右有\(n\)个城市,一开始在城市\(start\),每一天有两种选择: 前往相邻的城市. 访问当前城市(每个城市只能访问一次),访问城市\(i\)可以获得\(attrac ...

  10. May Day Holiday

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practic ...

随机推荐

  1. 创建 JavaScript 类和对象 prototype

    创建 JavaScript 对象 通过 JavaScript,您能够定义并创建自己的对象. 创建新对象有两种不同的方法: 定义并创建对象的实例(直接创建方式) person=new Object(); ...

  2. Create and format Word documents using R software and Reporters package

    http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-pa ...

  3. JavaEE编程实验 实验1 Java常用工具类编程(未完成)

    1.使用String类分割split将字符串“Solutions to selected exercises can be found in the electronic document The T ...

  4. Q-Q图

    来自:https://mp.weixin.qq.com/s/_UTKNcOgKQcCogk2C2tsQQ 正负样本数据集符合独立同分布是构建机器学习模型的前提,从概率的角度分析,样本数据独立同分布是正 ...

  5. C++.【转】C++数值类型与string的相互转换

    1.C++数值类型与string的相互转换 - JohnGu - 博客园.html(https://www.cnblogs.com/johngu/p/7878029.html) 2. 1.数值类型转换 ...

  6. Rancher与OpenLDAP对接

    简要说明: Rancher官网文档中,关于访问控制,有与OpenLDAP对接的介绍,但只是简要一笔带过,Rancher与OpenLDAP对接页面中的几个参数如何填写,并没有详细的说明. 本文通过Ope ...

  7. 力扣(LeetCode) 509. 斐波那契数

    斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = 1 F(N) = F(N ...

  8. Golang获得执行文件的当前路径

    运行环境:golang1.9.2+win7x64golang1.9.2+centos6.5×64 /*获取当前文件执行的路径*/ func GetCurPath() string { file, _ ...

  9. nginx+php上传大文件配置

    //nginx- //上传文件大小限制,需在nginx.conf中配置 'client_max_body_size' => '500M', //php- //允许上传文件大小的最大值,需在php ...

  10. 转载:删除github上文件夹的两种方式

    http://www.jianshu.com/p/286be61bb9b8 删除github上文件夹的两种方式(解决已经加入ignore的文件夹无法从远程仓库删除的问题) 如果此文件夹已被加入git追 ...