- #include <bits/stdc++.h>
- #define sc(n) scanf("%d", &n)
- #define L 100003
- using namespace std;
- struct data{
- int mxx, mnn;
- }Tree[L*3];
- int Mem[L], N, D, ans, maxx;
- void Build(int s, int e, int node){
- if(s >= e){
- if(s == e)Tree[node].mxx = Tree[node].mnn = Mem[s];
- return;
- }
- int mid = (s+e)/2, leafL = node*2, leafR = leafL+1;
- Build(s, mid, leafL);
- Build(mid+1, e, leafR);
- Tree[node].mxx = max(Tree[leafL].mxx, Tree[leafR].mxx);
- Tree[node].mnn = min(Tree[leafL].mnn, Tree[leafR].mnn);
- }
- data Query(int s, int e, int node, int i, int j){
- if(s >= i && e <= j){
- return Tree[node];
- }
- if(s > j || e < i){
- data Pre;
- Pre.mxx = 0;
- Pre.mnn = INT_MAX;
- return Pre;
- }
- int mid = (s+e)/2, leafL = node*2, leafR = leafL+1;
- data x = Query(s, mid, leafL, i, j);
- data y = Query(mid+1, e, leafR, i, j);
- int mx = 0, mn = INT_MAX;
- mx = max(mx, max(x.mxx, y.mxx));
- mn = min(mn, min(x.mnn, y.mnn));
- data ret;
- ret.mxx = mx;
- ret.mnn = mn;
- return ret;
- }
- int searchAns(int s, int e){
- if(e > N)return 0;
- data Quu = Query(1, N, 1, s, e);
- int sum = Quu.mxx-Quu.mnn;
- maxx = max(maxx, sum);
- searchAns(s+1, e+1);
- return maxx;
- }
- int main(){
- int t, cs = 0;
- sc(t);
- while(t--){
- maxx = 0;
- sc(N);sc(D);
- for(int i = 1; i <= N; i++)sc(Mem[i]);
- Build(1, N, 1);
- int ans = searchAns(1, D);
- printf("Case %d: %d\n", ++cs, ans);
- }
- return 0;
- }
Saturday, June 6, 2015
LightOJ - 1093 - Ghajini
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment