洛谷P2032 扫描

十 分 不 良 的 过 来 复 制 题 面 q w q \color{red}十分不良的过来复制题面qwq qwq


题目描述

有一个 1 ∗ n 的矩阵,有 n 个正整数。
现在给你一个可以盖住连续的 k 的数的木板。
一开始木板盖住了矩阵的第 1 ∼ k 个数,每次将木板向右移动一个单位,直到右端与第 n 个数重合。
每次移动前输出被覆盖住的最大的数是多少。

输入输出格式

输入格式:

第一行两个数,n,k,表示共有 n 个数,木板可以盖住 k 个数。
第二行 n 个数,表示矩阵中的元素。

输出格式:

共 n − k + 1 行,每行一个正整数。
第 i 行表示第 i ∼ i + k − 1 个数中最大值是多少。

输入输出样例

输入样例#1:
5 3
1 5 3 4 2
输出样例#1:
5
5
4

说明

对于 20% 的数据保证:1 ≤ n ≤ 1e3,1 ≤ k ≤ n
对于 50% 的数据保证:1 ≤ n ≤ 1e4,1 ≤ k ≤ n
对于 100% 的数据保证:1 ≤ n ≤ 2 ∗ 1e6,1 ≤ k ≤ n
矩阵中元素大小不超过 1e4。


复 制 完 了 q w q \color{brown}复制完了qwq qwq


− − − − − − − − − − − − − − − − − − 下 面 放 上 代 码 − − − − − − − − − − − − − − − − − − − \color{green}------------------下面放上代码-------------------

c o d e \color{red}c\color{green}o\color{blue}d\color{orange}e code


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <deque>
#define maxn 2000005
using namespace std ;
int n , m , k , a[maxn] ;
deque<int>q ;
int read() ;
int main () {
	n = read() , k = read() ;
	for(int i = 1 ; i <= n ; i ++) {
		a[i] = read () ;
	}
	for(int i = 1 ; i <= n ; i ++) {
		while(!q.empty() && a[q.back()] <= a[i]) q.pop_back() ;
		while(!q.empty() && i-q.front()+1 > k) q.pop_front() ;
		q.push_back(i) ;
		if(i >= k) cout << a[q.front()] <<endl ; 
	}
	return 0 ;
}
int read() {
	int x = 0 , f = 1 ;char s = getchar () ;
	while(s > '9' || s < '0') {if(s == '-') f = -1 ; s = getchar () ;}
	while(s <='9' && s >='0') {x = x * 10 + (s-'0'); s = getchar () ;}
	return x * f ; 
}

非 s t l 版 本 \color{red}非stl版本 stl

c o d e \color{red}c\color{green}o\color{blue}d\color{orange}e code

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std ;
int n , m , k ;
typedef pair<int,int>pa ;
int q[2000010] ;
int a[2000010] ;
int head = 0 , tail = 1 ;
int main(){
    scanf("%d%d",&n,&k) ;
    scanf("%d",&a[1]) ;
    q[++tail] = 1 ;
    for(int i = 2; i <= n; i++)
    {
        scanf("%d", &a[i]);
        while(head <= tail && a[i] > a[q[tail]]) tail--; 
        q[++tail] = i;
        if(i - q[head] == k) head++;      
        if(i >= k) printf("%d\n", a[q[head]]); 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值