题目来源:http://poj.org/problem?id=2823
问题描述
Time Limit: 12000MS |
Memory Limit: 65536K |
|
Total Submissions: 70616 |
Accepted: 20045 |
|
Case Time Limit: 5000MS |
Description
An array of size n ≤ 10^6 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 knumbers 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.
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
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 7
Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7
Source
POJ Monthly--2006.04.28, Ikki
------------------------------------------------------------
题意
给定n个数和一个宽度为k的固定长度窗口,窗口从n个数组左端滑到右端,依次输出窗口中的最小值和最大值。(虽然题目没说,但此题仍是多组测试数据)
------------------------------------------------------------
思路
用线段树重新做了这道题。线段树理论参见博文:一步一步理解线段树。
本题利用了线段树建树的过程和查询过程。补充上述博文中未提到的,建树过程的复杂度为O(n). 采用线段树后总复杂度为O((n-k)logn). 在POJ上用C++提交AC,用G++提交TLE,原因未知。
------------------------------------------------------------
代码
#include
#include
const int POS_INF = 10000000;
const int NEG_INF = -10000000;
const int NMAX = 1000005;
int n, k;
int a[NMAX] = {};
// 线段树节点结构体:区间左端点,区间右端点,区间最大值,区间最小值
struct node {
int tmax, tmin;
};
node tree[NMAX*4] = {}; // 用数组tree模拟完全二叉树
// 线段树函数:build & query
// build: 通过数组a递归构造线段树tree
void build(int root, int lp, int rp)
{
if (lp == rp)
{
tree[root].tmax = a[lp];
tree[root].tmin = a[lp];
return;
}
int mid = (lp+rp)/2;
build(2*root+1, lp, mid);
build(2*root+2, mid+1, rp);
tree[root].tmax = std::max(tree[2*root+1].tmax, tree[2*root+2].tmax);
tree[root].tmin = std::min(tree[2*root+1].tmin, tree[2*root+2].tmin);
}
// query: 给定a数组上某个区间(ql,qr),通过线段树递归(tl,tr)查询区间最大值和最小值
int query(int root, int ql, int qr, int tl, int tr, bool is_max)// is_max 0/1: 查询最小/大值
{
if (ql > tr || qr < tl)
{
if (is_max)
{
return NEG_INF;
}
else
{
return POS_INF;
}
}
else if (ql <= tl && qr >= tr)
{
if (is_max)
{
return tree[root].tmax;
}
else
{
return tree[root].tmin;
}
}
else
{
int mid = (tl+tr)/2;
if (is_max)
{
return std::max(query(2*root+1,ql,qr,tl,mid,1),
query(2*root+2,ql,qr,mid+1,tr,1));
}
else
{
return std::min(query(2*root+1,ql,qr,tl,mid,0),
query(2*root+2,ql,qr,mid+1,tr,0));
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("poj2823.txt", "r", stdin);
#endif
int i;
while (scanf("%d%d", &n, &k)!=EOF)
{
for (i=0; i