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. Lintcode9-Fizz Buzz-Easy

    Fizz Buzz Given number n. Print number from 1 to n. But: when number is divided by 3, print "fi ...

  2. maven . mac

    编辑文件 .bash_profile 1.vim .bash_profile 输入: M2_HOME=/Users/lizhimin/Documents/maven/apache-maven-3.3. ...

  3. CART决策树

     CART(Classification and Regression tree)分类回归树由L.Breiman,J.Friedman,R.Olshen和C.Stone于1984年提出.ID3中根据属 ...

  4. 转youhu科技的文章 勿怪 感激 感激

    资源加载 资源加载是加载模块中最为耗时的部分,其CPU开销在Unity引擎中主要体现在Loading.UpdatePreloading和Loading.ReadObject两项中,相信经常查看Prof ...

  5. Java里的String类为什么是final的

    今天在看<图解设计模式>,里面出了一个问题“String类用final修饰,导致它无法被继承(扩展),这样做违反了开闭原则,这么做有什么正当理由?” 答案是效率和安全性  首先是效率,由于 ...

  6. 函数indexOf()和lastIndexOf()

    返回前面起第一个字符的位置indexOf(“字符”); 它是从前面开始数(从左边开始数),而且只找第一个,然后返回该字符的位置,索引号都是从0开始的.返回的是个数值. var txt = “abcde ...

  7. Cross-site request forgery 跨站请求伪造

    Cross-site request forgery 跨站请求伪造 简称为CSRF或者XSRF,通过伪装来自受信任用户的请求来利用受信任的网站 攻击者盗用了你的身份,以你的名义发送恶意请求,对服务器来 ...

  8. [Solution] The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

    HttpServlet需要tomcat等. 右键project点开properties>project facets> 在右侧栏的Runtime tab中勾选tomcat或者新建tomca ...

  9. Spring 拦截器实现+后台原理(MethodInterceptor)

    MethodInterceptor MethodInterceptor是AOP项目中的拦截器(注:不是动态代理拦截器),区别与HandlerInterceptor拦截目标时请求,它拦截的目标是方法. ...

  10. springmvc如何进行热部署开发

    1.场景还原 在工程量大的情况下,tomcat运行部署一次会花费相当多的时间,这样太 浪费人力以及时间了:今天笔者将 讲解一下如何配置springmvc工程的热部署 2.实现方案 其实很简单! ①在t ...