poj 2823 Sliding Window 题解

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 53037 Accepted: 15207
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 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.
Window positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

————————————————————————我是分割线————————————————————————————————

单调队列模板题。

维护两个单调队列,一个单调增,一个单调减。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cstdlib>
 8 #include<iomanip>
 9 #include<cassert>
10 #include<climits>
11 #define maxn 1000001
12 #define F(i,j,k) for(int i=j;i<=k;i++)
13 #define M(a,b) memset(a,b,sizeof(a))
14 #define FF(i,j,k) for(int i=j;i>=k;i--)
15 #define inf 0x7fffffff
16 using namespace std;
17 int read(){
18     int x=0,f=1;char ch=getchar();
19     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
20     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
21     return x*f;
22 }
23 int a[maxn],q1[maxn],q2[maxn];
24 int main()
25 {
26     std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
27     #ifdef LOCAL
28     freopen("data.in","r",stdin);
29     freopen("data.out","w",stdout);
30     #endif
31     int n,m,k;
32     cin>>n>>k;
33     F(i,1,n)
34     {
35         cin>>a[i];
36     }
37     int head=1,tail=0;
38     F(i,1,n)
39     {
40         while(head<=tail&&a[i]<=a[q1[tail]]) tail--;
41         q1[++tail]=i;
42         while(head<=tail&&q1[tail]-q1[head]>=k) head++;
43         if(i>=k) cout<<a[q1[head]]<<" ";
44     }
45     cout<<endl;
46     head=1,tail=0;
47     F(i,1,n)
48     {
49         while(head<=tail&&a[i]>=a[q2[tail]]) tail--;
50         q2[++tail]=i;
51         while(head<=tail&&q2[tail]-q2[head]>=k) head++;
52         if(i>=k) cout<<a[q2[head]]<<" ";
53     }
54     return 0;
55 }
poj 2823

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容介绍 项目结构: Controller层:使用Spring MVC来处理用户请求,负责将请求分发到相应的业务逻辑层,并将数据传递给视图层进行展示。Controller层通常包含控制器类,这些类通过注解如@Controller、@RequestMapping等标记,负责处理HTTP请求并返回响应。 Service层:Spring的核心部分,用于处理业务逻辑。Service层通过接口和实现类的方式,将业务逻辑与具体的实现细节分离。常见的注解有@Service和@Transactional,后者用于管理事务。 DAO层:使用MyBatis来实现数据持久化,DAO层与数据库直接交互,执行CRUD操作。MyBatis通过XML映射文件或注解的方式,将SQL语句与Java对象绑定,实现高效的数据访问。 Spring整合: Spring核心配置:包括Spring的IOC容器配置,管理Service和DAO层的Bean。配置文件通常包括applicationContext.xml或采用Java配置类。 事务管理:通过Spring的声明式事务管理,简化了事务的处理,确保数据一致性和完整性。 Spring MVC整合: 视图解析器:配置Spring MVC的视图解析器,将逻辑视图名解析为具体的JSP或其他类型的视图。 拦截器:通过配置Spring MVC的拦截器,处理请求的预处理和后处理,常用于权限验证、日志记录等功能。 MyBatis整合: 数据源配置:配置数据库连接池(如Druid或C3P0),确保应用可以高效地访问数据库。 SQL映射文件:使用MyBatis的XML文件或注解配置,将SQL语句与Java对象映射,支持复杂的查询、插入、更新和删除操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值