poj2823 线段树模板题 点修改(也可以用单调队列)
生活随笔
收集整理的這篇文章主要介紹了
poj2823 线段树模板题 点修改(也可以用单调队列)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這道題吧 沒計算時間 因為給了那么多 一算還可以
就直接寫了線段樹,劉汝佳那本模板
然后!poj的g++比C++慢大約500ms。。。。。。。g++tle,C++就過了
Sliding Window| Time Limit: 12000MS | ? | Memory Limit: 65536K |
| Total Submissions: 67576 | ? | Accepted: 19163 |
| Case Time Limit: 5000MS | ||
Description
An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:The array is [1?3?-1?-3?5?3?6?7], and k is 3.
| [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
The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.Output
There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.Sample Input
8 3 1 3 -1 -3 5 3 6 7Sample Output
-1 -3 -3 -3 3 3 3 3 5 5 6 7Source
POJ Monthly--2006.04.28, Ikki上代碼 通俗易懂哦~~~~~~看我的就行了
#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; }?
轉載于:https://www.cnblogs.com/xuyanqd/p/9073920.html
總結
以上是生活随笔為你收集整理的poj2823 线段树模板题 点修改(也可以用单调队列)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 事件总线模式
- 下一篇: 如何绘制漂亮的多序列比对图片