poj 2823 Sliding Window(单调队列)

news/2024/7/10 5:27:39 标签: 优化, im, c
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views">

【题目大意】:给出n个数࿰c;求每k个数之间的最大最小值。


【解题思路】:今晚精神状态不太好࿰c;本来是想来切切水题就睡觉的。谁知道写个单调队列G++还一直tle...不知道怎么class="tags" href="/tags/YouHua.html" title=优化>优化了࿰c;不过幸运的是C++过了。

ce:pre">谁能告诉我G++要怎么改才能够过。

ce:pre">等明后天什么时候手痒写个线段树试试。


【代码】:

<code class="language-cpp">#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

int n,m;
int a[1000010],maxx[1000010],minn[1000010];

void solve_maxx(){
    int st=0,en=0;  
    for(int i=0; i<m; i++){  
       while(a[maxx[en]]<a[i] && en>=st) en--;  
       en++; 
       maxx[en]=i; 
    }  
    printf("%d ",a[maxx[st]]);
    for(int i=m; i<n; i++){  
        while(maxx[st]<=i-m && st<=en) st++;  
        while(a[maxx[en]]<a[i] && en>=st) en--;  
        en++;  
        maxx[en]=i;  
        if(i!=n-1) printf("%d ",a[maxx[st]]);  
    }  
    printf("%d\n",a[maxx[st]]);
}
void solve_minn(){
    int st=0,en=0;  
    for(int i=0; i<m; i++){  
        while(a[minn[en]]>a[i] && en>=st) en--;  
        en++;  
        minn[en]=i;  
    } 
    printf("%d ",a[minn[st]]); 
    for(int i=m; i<n; i++){  
        while(minn[st]<=i-m && st<=en) st++;  
        while(a[minn[en]]>a[i] && en>=st) en--;  
        en++;  
        minn[en]=i;  
        if(i!=n-1) printf("%d ",a[minn[st]]);  
    }  
    printf("%d\n",a[minn[st]]);  
}

int main() {
    while (~scanf("%d%d",&n,&m)){
        for (int i=0; i<n; i++) scanf("%d",&a[i]);
        solve_minn();
        solve_maxx();
    }
    return 0;
}
code>


cle>

http://www.niftyadmin.cn/n/971027.html

相关文章

阿里云上构建本地仓库Docker images

第一步&#xff0c;先在阿里云https://cr.console.aliyun.com/cn-hangzhou/namespaces上创建命名空间&#xff0c;再根据命名空间创建镜像仓库 第二步&#xff0c;本地上传images到阿里云容器镜像市场 1.本地创建images [rootcentos7-template ~]# docker commit test_centos x…

Kruscal算法

参考&#xff1a;http://blog.csdn.net/luomingjun12315/article/details/47700237 Kruskal算法 Kruskal算法是基于贪心的思想得到的。首先我们把所有的边按照权值先从小到大排列&#xff0c;接着按照顺序选取每条边&#xff0c;如果这条边的两个端点不属于同一集合&#xff0…

poj 3264 Balanced Lineup(RMQ)

【题目大意】&#xff1a;给出一些数&#xff0c;求出区间内最大值减去最小值的差值。 【解题思路】&#xff1a;经典的RMQ问题啊&#xff0c;用ST算法。&&还有线段树写法&#xff0c;改天写一下。 【ST算法】&#xff1a; 下面简单介绍一下ST算法&#xff1a; •Spars…

Flutter和原生应用性能对比

本文由玉刚说写作平台提供写作赞助&#xff0c;版权归玉刚说微信公众号所有 原作者&#xff1a;Xiasm 版权声明&#xff1a;未经玉刚说许可&#xff0c;不得以任何形式转载 前言 自从今年google IO大会推出flutter跨平台开发框架以来&#xff0c;flutter在各个技术论坛里被吵得…

hdoj 3086 Need for Speed(解方程)

【题目大意】&#xff1a;一个警察与Jiaoshou的距离为S&#xff0c;而且他们的运动速度满足方程va*tb&#xff0c;已知警察和Jiaoshou的a和b的值&#xff0c;问警察能否追上Jiaoshou&#xff0c;追上的话需要多少时间。 【解题思路】&#xff1a;数学题&#xff0c;解方程 【代…

Vue实现环形进度条方法组件

为什么80%的码农都做不了架构师&#xff1f;>>> <template> <div class"wrap"> <div class"circle" v-bind:class"{ clip-auto: isActive.circle }"> <div class"percent left" refleft v-bind:styl…

大数N的阶乘

模拟优化 先贴一个未优化过的按10进制写的&#xff1a; #include <iostream> #include <stdio.h> #include <sstream> #include <string.h> #include <algorithm> #define LL long long using namespace std; const int AX 1e6666; int a[AX];…

hdoj 1221 Rectangle and Circle(判点的位置)

【题目大意】&#xff1a;给出一个平行于x轴长方形&#xff0c;和圆&#xff0c;判断其是否相交 【解题思路】&#xff1a;简单题&#xff0c;1&#xff09;判长方形四个点是否都在圆内&#xff0c;有就不相交 2&#xff09;判圆心到长方形每条边的距离是否都大于圆的半径&…