Time Limit: 2 second

Memory Limit: 256 MB

【问题描述】

给你一个长度为N 的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:

Window position Min value Max 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

你的任务是找出窗口在各位置时的Max value和Min value。

【输入格式】

第一行N,K,第二行为长度为N的数组

【输出格式】

第一行每个位置的Min value,第二行每个位置的Max value

【输入样例1】

8 3

1 3 -1 -3 5 3 6 7

【输出样例1】

-1 -3 -3 -3 3 3

3 3 5 5 6 7

数据规模

20%:N≤500;50%:N≤100000;100%:N≤1000000;

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t019

【题解】



线段树(开到3s才过)



【完整代码】

#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int MAXN = 1e6+100; int ma[MAXN<<2],mi[MAXN<<2];
int n,k,a[MAXN]; void build(int l,int r,int rt)
{
if (l==r)
{
ma[rt] = mi[rt] = a[l];
return;
}
int m = (l+r)>>1;
build(lson);
build(rson);
ma[rt] = max(ma[rt<<1],ma[rt<<1|1]);
mi[rt] = min(mi[rt<<1],mi[rt<<1|1]);
} int findma(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
return ma[rt];
int m = (l+r)>>1;
int temp1 = -21e8,temp2 = -21e8;
if (L <= m)
temp1 = findma(L,R,lson);
if (m < R)
temp2 = findma(L,R,rson);
return max(temp1,temp2);
} int findmi(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
return mi[rt];
int m = (l+r)>>1;
int temp1 = 21e8,temp2 = 21e8;
if (L <= m)
temp1 = findmi(L,R,lson);
if (m < R)
temp2 = findmi(L,R,rson);
return min(temp1,temp2);
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%d%d",&n,&k);
for (int i = 1;i <= n;i++)
scanf("%d",&a[i]);
build(1,n,1);
for (int r = k;r <= n;r++)
{
int l = r-k+1;
printf("%d",findmi(l,r,1,n,1));
if (r==n)
puts("");
else
putchar(' ');
}
for (int r = k;r <= n;r++)
{
int l = r-k+1;
printf("%d",findma(l,r,1,n,1));
if (r==n)
puts("");
else
putchar(' ');
}
return 0;
}

【t019】window(线段树做法)的更多相关文章

  1. POJ 2823 Sliding Window 线段树区间求和问题

    题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...

  2. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  3. POJ 2482 Stars in Your Window 线段树扫描线

    Stars in Your Window   Description Fleeting time does not blur my memory of you. Can it really be 4 ...

  4. POJ 2823 Sliding Window 线段树

    http://poj.org/problem?id=2823 出太阳啦~^ ^被子拿去晒了~晚上还要数学建模,刚才躺在床上休息一下就睡着了,哼,还好我强大,没有感冒. 话说今年校运会怎么没下雨!!!说 ...

  5. POJ 2482 Stars in Your Window 线段树

    如果按一般的思路来想,去求窗户能框住的星星,就很难想出来. 如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了. 不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其 ...

  6. PKU 2823 Sliding Window(线段树||RMQ||单调队列)

    题目大意:原题链接(定长区间求最值) 给定长为n的数组,求出每k个数之间的最小/大值. 解法一:线段树 segtree节点存储区间的最小/大值 Query_min(int p,int l,int r, ...

  7. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

  8. HDU2665 kth number 线段树做法

    题意:求区间第k小 思路: 线段树 每个节点上保存 当前区间已经排序好的序列 (归并一下就好了嘛 复杂度 O(l)的) 这样建树的时空复杂度都是 O(nlogn)的 对于 每次询问 二分一个答案 在树 ...

  9. 【BZOJ 1012】 [JSOI2008]最大数maxnumber(线段树做法)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 [题意] [题解] 预开一个20W长度的线段树; 这里a[1..20W]={0} ...

随机推荐

  1. HDU1969 Pie(二分搜索)

    题目大意是要办生日Party,有n个馅饼,有f个朋友.接下来是n个馅饼的半径.然后是分馅饼了, 注意咯自己也要,大家都要一样大,形状没什么要求,但都要是一整块的那种,也就是说不能从两个饼中 各割一小块 ...

  2. [D3] Animate Transitions in D3 v4

    D3 makes it easy to add meaningful animations to your data visualizations. Whether it’s fading in ne ...

  3. Altium Designer如何设置pcb尺寸

  4. TCP快速重传与快速恢复原理分析(四种不同的算法)

    在TCP/IP中,快速重传和恢复(fast retransmit and recovery,FRR)是一种拥塞控制算法,它能快速恢复丢失的数据包.没有FRR,如果数据包丢失了,TCP将会使用定时器来要 ...

  5. jQuery笔记---选择器(二)

    1.选择器练习: 1)查找UL中的元素的内容 格式:$(“ul li:XX”).text() XX:代表方法 比如:获取到第一元素,然后获取当中的值 $(“ul li:first”).text() 获 ...

  6. Nginx 设置,设置已经解析的域名,在nginx中没有定义相应server时的默认访问

    https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80785027

  7. python打印即时输出的方法

    >>> import sys>>> sys.stdout.flush() 但是实验了,上面的报错,应该是不对的. 实验了,下面的报错,应该是不对的. 使用 prin ...

  8. [Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js

    You often use the same data in different ways across pages. This lesson walks you through setting up ...

  9. unity 3d开发的大型网络游戏

    unity 3d开发的大型网络游戏 一.总结 1.unity的官网上面应该有游戏列表 2.unity3D是很好的3d游戏引擎,也支持2d,也能做很多画面精良的3A级游戏 3.范围:电脑游戏,手机游戏, ...

  10. php 发送QQ邮箱邮件

    这是我的源码比较简陋 https://www.lanzous.com/i2l7h8f 感谢 https://www.cnblogs.com/woider/p/6980456.html 下载phpmai ...