Skip to content

Commit 66e4183

Browse files
Added English tutorial for 1093 lightoj-dev#512
1 parent 6fc38f5 commit 66e4183

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

1093/en.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
## [LightOj-1093](https://lightoj.com/problem/ghajini)
3+
## Problem Summary
4+
Amir is playing a game called "Find Max Difference". But he can only remember numbers for a short period of time, that is `d` milliseconds.
5+
The game involves:
6+
7+
- A sequence of integers appearing one at a time, for 1 millisecond each.
8+
- The goal is to calculate the maximum difference between any two integers that Amir can remember(i.e., within the last `d` milliseconds).
9+
- Input Constrains
10+
* T <= 5 (Number of test case)
11+
* 2 <= n <=10^5 (Total number of integers the screen shows)
12+
* 1 <= d <= n (Amir memory duration)
13+
* Sequence values: 0 <= a[i] <= 10^8
14+
15+
16+
17+
18+
19+
## Solution
20+
21+
- We will iterate the array. In each index we compare the minimum and maximum numbers from the last `d` integers. Amir remembers to maximize the difference and stores the max difference in a variable.
22+
- A naive approach of iterating through the last `d` numbers for every index would be too slow((O(n * d)).
23+
- We can efficiently find the minimum and maximum of the last `d` numbers using sparse table or segment tree. Then the complexity would be O(logn).
24+
25+
### C++ code using sparse table
26+
27+
```c++
28+
#include<bits/stdc++.h>
29+
using namespace std;
30+
const int maxN = 1e5+1;
31+
const int maxLog = 20;
32+
int stMax[maxN][maxLog];
33+
int stMin[maxN][maxLog];
34+
int lg[maxN];
35+
void init(int arr[],int n){
36+
for(int i=0;i<n;i++){
37+
stMax[i][0] = arr[i];
38+
stMin[i][0] = arr[i];
39+
}
40+
for(int j=1;j<maxLog;j++){
41+
for(int i=0;i+(1<<j)<=n;i++){
42+
stMax[i][j] = max(stMax[i][j-1],stMax[i+(1<<(j-1))][j-1]);
43+
stMin[i][j] = min(stMin[i][j-1],stMin[i+(1<<(j-1))][j-1]);
44+
}
45+
}
46+
}
47+
int queryMax(int l,int r){
48+
if(l<0)l=0;
49+
int j = lg[r-l+1];
50+
return max(stMax[l][j],stMax[r-(1<<j)+1][j]);
51+
}
52+
int queryMin(int l,int r){
53+
if(l<0)l=0;
54+
int j = lg[r-l+1];
55+
return min(stMin[l][j],stMin[r-(1<<j)+1][j]);
56+
}
57+
int main(){
58+
ios::sync_with_stdio(false);
59+
cin.tie(0);cout.tie(0);
60+
lg[1] = 0;
61+
for(int i=2;i<maxN;i++){
62+
lg[i] = lg[i/2]+1;
63+
}
64+
int tcs;
65+
cin>>tcs;
66+
for(int tc=1;tc<=tcs;tc++){
67+
int n,d;
68+
cin>>n>>d;
69+
int arr[n];
70+
for(int i=0;i<n;i++){
71+
cin>>arr[i];
72+
}
73+
init(arr,n);
74+
int ans = 0;
75+
for(int i=0;i<n;i++){
76+
ans = max(ans, queryMax(i-d+1,i)-queryMin(i-d+1,i));
77+
}
78+
cout<<"Case "<<tc<<": "<<ans<<endl;
79+
}
80+
}
81+
```
82+
83+
## Resources
84+
- [Sparse Table cp-algorithms](https://cp-algorithms.com/data_structures/sparse-table.html)
85+
- [geeksforgeeks](https://www.geeksforgeeks.org/sparse-table/)
86+
87+
## Author
88+
[Sudipta Singha](https://github.com/sudiptarathi2020)

0 commit comments

Comments
 (0)