1093 - Ghajini
Time Limit:1 second(s)
|
Memory Limit:32 MB
|
Amir is having a short term memory problem. He can't remember anything for more thandmilliseconds.
Amir is playing a game named 'Find Max Difference'. The game is actually designed for children. There is a screen which shows an integer for 1 millisecond. In the very next millisecond the screen shows another integer. The target of the game is to find the
maximum difference of any two numbers shown in the screen.
But soon Amir found that the game is more difficult for him, because his short term memory problem. So, he uses a paper to write the maximum difference he has found so far. So, Amir wants your help. You have to write a program to help Amir.
Input
Input starts with an integerT (≤ 5), denoting the number of test cases.
Each case starts with two integersn (2 ≤ n ≤ 105),d (1 ≤ d ≤ n),nmeans the total number of integers the screen will show. The next line containsnspace separated integers
in range[0, 108].
Output
For each case, print the case number and the maximum difference found by Amir.
Sample Input
|
Output for Sample Input
|
3
6 2
6 0 8 8 8 4
8 3
19 8 4 13 12 1 0 13
2 2
1 1
|
Case 1: 8
Case 2: 15
Case 3: 0
|
题意:给定一个序列,然后要求出所有长度为d的连续区间内最大的(最大值和最小值之差),直接建立线段树,然后枚举每个长度为d的区间保存最大值即可。
线段树功能:区间询问最大最小值,不用更新。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int MAXN = 111111;
int Max[MAXN<<2],Min[MAXN<<2],N,D,T;
void pushUP(int rt){
Max[rt] = max(Max[rt<<1],Max[rt<<1|1]);
Min[rt] = min(Min[rt<<1],Min[rt<<1|1]);
}
void build(int l,int r,int rt){
if(l==r){
scanf("%d",&Max[rt]);
Min[rt] = Max[rt];
return;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushUP(rt);
}
int QueryMax(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return Max[rt];
}
int m = (l+r)>>1;
int ret = -1;
if(m>=L) ret = max(ret,QueryMax(L,R,lson));
if(m<R) ret = max(ret,QueryMax(L,R,rson));
return ret;
}
int QueryMin(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return Min[rt];
}
int m = (l+r)>>1;
int ret = 0x3fffffff;
if(m>=L) ret = min(ret,QueryMin(L,R,lson));
if(m<R) ret = min(ret,QueryMin(L,R,rson));
return ret;
}
int main(){
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
scanf("%d%d",&N,&D);
build(0,N-1,1);
int ans = -1;
for(int i=0;i<=N-D;i++){
ans = max(ans,abs(QueryMax(i,i+D-1,0,N-1,1)-QueryMin(i,i+D-1,0,N-1,1)));
}
printf("Case %d: %d\n",cas,ans);
}
return 0;
}
分享到:
相关推荐
【标题】"LightOJ-Solved-Code"指的是一个关于在Light Online Judge平台上解决编程问题的代码集合。这个集合很可能包含了作者在LightOJ上解答各类算法问题的C++源代码。LightOJ是一个在线的编程竞赛平台,它提供了一...
标题中的“lightoj-tutorial-editorial-by-sayeed”指的是LightOJ(Light Online Judge)平台上的一篇教程或社论,由用户Sayeed撰写,旨在帮助用户了解如何在LightOJ上创建类似于Codeforces编辑模式的教程。...
【标题】"LightOJ-solutions" 是一个与编程竞赛相关的资源,主要包含了参与 LightOJ(Light Online Judge)平台的解题方案。这个压缩包很可能是某位参赛者或编程爱好者整理的代码集合,旨在分享他们在解决 LightOJ ...
leetcode中国 数据结构和算法编码 议程 :balloon: 不是为了比赛,而是为了训练和兴趣。 Python3 你可以在这里找到我的 LeetCode 解决方案:(等待打开) ...LightOJ 1012 --- dfs transform 13. HDU 1495 --- compl
"LightOJ"是一个在线判题系统,专为竞赛编程和算法训练而设计。它允许用户提交代码并立即获得运行结果、时间复杂度和空间复杂度等反馈。这个平台广泛支持多种编程语言,包括C++,因此在标签中提到了"C++"。现在我们...
解析编程问题,并将其发送给CHelper插件以实现IntelliJ IDEA。 竞争性伴侣解析程序...-HackerRank-HDU在线法官-Kattis-LightOJ-NYTD在线法官-PEG法官-POJ-QDUOJ-Timus-URI在线法官 支持语言:English (United States)
LightOJ是一个在线编程竞赛平台,它为程序员和计算机科学爱好者提供了一系列的算法问题来解决。这个存储库是一个专门针对LightOJ问题的教程集合,旨在帮助用户更好地理解和解决这些问题。下面,我们将深入探讨其中...
【压缩包子文件的文件名称列表】"LightOJ-master"可能是一个Git仓库的名称,暗示了这个问题解决方案可能是开源的,并且包含了一个项目的主分支。通常,这样的仓库会包含源代码、文档、测试用例和其他资源,帮助用户...
leetcode 2 和 c 动态规划 动态规划相关问题的解答。 这些问题来自各种在线评委,包括 、 、 等。 解决方案是用 C++ 编码的。 —— —— —— —— —— —— —— —— —— —— —— —— ...——
已解决的编程问题 Online Judges 这个存储库包含我解决的各种在线法官的编程问题的解决方案,即 UVa、Topcoder、Codeforces、Hackerrank、LightOj、Spoj、Project Euler 等。
- **熟悉基础算法和数据结构**:了解并掌握排序、搜索、图论、动态规划等基础算法,同时理解链表、树、队列、堆等数据结构的实现和应用。 - **练习编程技巧**:通过解决实际问题来提高编程技巧,注意代码的效率、...
这是乔杜里医学博士。 伊斯玛姆·拉赫曼(Ishmam Rahman) :closed_mailbox_with_raised_flag: 联络我: ... LightOJ: ://lightoj.com/user/ishmam64 脸书: : 目标:使自己在新技术的海洋中立足,在这里我可
描述中提到的“uva”,“lightoj”,“spoj”,“timus”都是知名的在线编程竞赛平台,它们为程序员提供了各种算法和逻辑思维的练习题目。 在这个项目的压缩包 "solving-oj-problems-master" 中,我们可以推测它...
race_words = [“后缀数组”,“前缀特里”,“动态编程”,“竞赛”,“ codeforces”,“编程”,“竞争性编程”,“算法”,“数据结构”,“ codeforces”,“ light oj”,“ lightoj”,“ spoj”,“堆栈”,...
这个仓库是关于什么的 创建该存储库是为了组织与数据结构和算法有关的问题的解决方案。 并且,如果可能的话,为学习与数据结构和算法有关的各种概念提供一种更简单的方法。 以下评委使用的问题 ...
开心农场java源码AA Noman Ansary 你好呀! 我的名字是AA Noman Ansary 。 但我更喜欢被称为Showrav...问题解决:LightOJ。 代码部队。 蒂姆斯。 紫外线。 成就 以下是我的一些显著成就: BRAC 大学副校长证书。 (2019)
测试程序TestProgram 是针对竞争性编程程序(即 Codeforces、lightOJ、OmegaUp 等)的专用测试工具。 当我们解决问题时,我们必须非常小心,并致力于解决所有可能的情况。 我们的解决方案在登顶前正确解决的测试用例...