- #include <bits/stdc++.h>
- using namespace std;
- #define mx 100001
- int arr[mx];
- int tree[mx * 3];
- int tree1[mx*3];
- void init(int node, int b, int e)
- {
- if (b == e) {
- tree[node] = arr[b];
- return;
- }
- int Left = node * 2;
- int Right = node * 2 + 1;
- int mid = (b + e) / 2;
- init(Left, b, mid);
- init(Right, mid + 1, e);
- tree[node] = min(tree[Left], tree[Right]);
- }
- int query(int node, int b, int e, int i, int j)
- {
- if (i > e || j < b)
- return INT_MAX; //বাইরে চলে গিয়েছে
- if (b >= i && e <= j)
- return tree[node]; //রিলেভেন্ট সেগমেন্ট
- int Left = node * 2; //আরো ভাঙতে হবে
- int Right = node * 2 + 1;
- int mid = (b + e) / 2;
- int p1 = query(Left, b, mid, i, j);
- int p2 = query(Right, mid + 1, e, i, j);
- return min(p1, p2); //বাম এবং ডান পাশের যোগফল
- }
- void init1(int node, int b, int e)
- {
- if (b == e) {
- tree1[node] = arr[b];
- return;
- }
- int Left = node * 2;
- int Right = node * 2 + 1;
- int mid = (b + e) / 2;
- init1(Left, b, mid);
- init1(Right, mid + 1, e);
- tree1[node] = max(tree1[Left], tree1[Right]);
- }
- int query1(int node, int b, int e, int i, int j)
- {
- if (i > e || j < b)
- return INT_MIN; //বাইরে চলে গিয়েছে
- if (b >= i && e <= j)
- return tree1[node]; //রিলেভেন্ট সেগমেন্ট
- int Left = node * 2; //আরো ভাঙতে হবে
- int Right = node * 2 + 1;
- int mid = (b + e) / 2;
- int p1 = query1(Left, b, mid, i, j);
- int p2 = query1(Right, mid + 1, e, i, j);
- return max(p1,p2); //বাম এবং ডান পাশের যোগফল
- }
- int main()
- {
- int ts;
- scanf("%d", &ts);
- for(int p = 1; p <=ts; p++){
- int n, d;
- scanf("%d%d", &n, &d);
- for(int i = 1; i <= n; i++) scanf("%d", &arr[i]);
- init(1, 1, n);
- init1(1, 1, n);
- int l, r;
- int ans = 0;
- for(int j = 1; j <=n-d+1; j++){
- int s = query(1, 1, n, j, j+d-1);
- int r = query1(1, 1, n, j, j+d-1);
- //cout<<j<<" = j j+d-1 = "<<j+d-1<<" s = "<<s<<" r = "<<r<<endl;
- if(s != INT_MAX && r != INT_MIN) ans = max(ans, r-s);
- }
- printf("Case %d: %d\n", p, ans);
- }
- return 0;
- }
Wednesday 26 July 2017
lightoj 1093 Ghajini
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
Problem link: Problem Analysis: It is actually a basic Bisection problem , as we can see here we can not actually find a formula fo...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
No comments:
Post a Comment