标签:uri second task ret val esc term ali sub
这道题吧 没计算时间 因为给了那么多 一算还可以
就直接写了线段树,刘汝佳那本模板
然后!poj的g++比C++慢大约500ms。。。。。。。g++tle,C++就过了
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 67576 | Accepted: 19163 | |
Case Time Limit: 5000MS |
Description
Window position | Minimum value | Maximum value |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
Output
Sample Input
8 3 1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3 3 3 5 5 6 7
Source
上代码 通俗易懂哦~~~~~~看我的就行了
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int Max[5000100]; int Min[5000100]; int pinf = 2000000000; int ninf = -2000000000; int n,k; void build(int l,int r,int cur){ if(l == r){ scanf("%d",&Max[cur]); Min[cur] = Max[cur]; return; } int mid = (l + r)/2; build(l,mid,cur*2); build(mid + 1,r,cur*2 + 1); Min[cur] = min(Min[cur*2],Min[cur*2 + 1]); Max[cur] = max(Max[cur*2],Max[cur*2 + 1]); return; } int ql,qr; int queryMax(int l,int r,int cur){ if(ql <= l&&r <= qr){ return Max[cur]; } int mid = (l + r)/2; int ans = ninf; if(ql <= mid){ ans = max(ans,queryMax(l,mid,cur*2)); } if(qr > mid){ ans = max(ans,queryMax(mid + 1,r,cur*2 +1)); } return ans; } int queryMin(int l,int r,int cur){ if(ql <= l && r <= qr){ return Min[cur]; } int mid = (l + r)/2; int ans = pinf; if(ql <= mid){ ans = min(ans,queryMin(l,mid,cur*2)); } if(qr > mid){ ans = min(ans,queryMin(mid + 1,r,cur*2 + 1)); } return ans; } int main(){ while(scanf("%d%d",&n,&k) != EOF){ build(1,n,1); ql = 1;qr = k; printf("%d",queryMin(1,n,1)); ql++;qr++; while(qr <= n){ printf(" %d",queryMin(1,n,1)); ql++;qr++; } printf("\n"); ql = 1;qr = k; printf("%d",queryMax(1,n,1)); ql++;qr++; while(qr <= n){ printf(" %d",queryMax(1,n,1)); ql++;qr++; } printf("\n"); } return 0; }
标签:uri second task ret val esc term ali sub
原文地址:https://www.cnblogs.com/xuyanqd/p/9073920.html