Sliding Window 单调队列求区间内的最值

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

***************************************************************************************************************************

单调队列,求区间内的最值(注意是多个定长区间)

***************************************************************************************************************************

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdio>
 6 #include<algorithm>
 7 using namespace std;
 8 int a[1000001];
 9 int que[100000001];
10 int n,k;
11 void Min()//单调递增求最小值
12 {
13     int head=1,tail=0;
14     int it;
15     for(it=0;it<k-1;it++)
16     {
17         while(head<=tail&&a[que[tail]]>=a[it])tail--;
18         tail++;
19         que[tail]=it;
20     }
21     for(it=k-1;it<n;it++)
22     {
23         while(head<=tail&&a[que[tail]]>=a[it])tail--;
24         tail++;
25         que[tail]=it;
26         while(que[head]<it-k+1)head++;
27         printf("%d",a[que[head]]);
28         printf("%c",((it==n-1)?'\n':' '));
29 
30     }
31 }
32 void Max()//单调递减,求最大值
33 {
34     int head=1,tail=0;
35     int it;
36     for(it=0;it<k-1;it++)
37     {
38         while(head<=tail&&a[que[tail]]<=a[it])tail--;
39         tail++;
40         que[tail]=it;
41     }
42     for(it=k-1;it<n;it++)
43     {
44         while(head<=tail&&a[que[tail]]<=a[it])tail--;
45         tail++;
46         que[tail]=it;
47         while(que[head]<it-k+1)head++;
48         printf("%d",a[que[head]]);
49         printf("%c",((it==n-1)?'\n':' '));
50 
51     }
52 }
53 int main()
54 {
55     int i,j;
56     while(scanf("%d%d",&n,&k)!=EOF)
57     {
58         for(i=0;i<n;i++)
59         scanf("%d",&a[i]);
60         Min();
61         Max();
62     }
63     return 0;
64 
65 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3450728.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The Sliding Window Protocol is a flow control protocol used in computer networks to ensure reliable and efficient data transfer between two nodes. It is implemented using a sliding window, which is a buffer of fixed size that stores the data packets to be transmitted and received. The sliding window protocol is a stop-and-wait protocol, which means that the sender sends a packet and waits for an acknowledgement from the receiver before sending the next packet. The receiver sends an acknowledgement packet to the sender indicating that it has received the packet successfully. The sliding window protocol has two parameters: the window size and the sequence number. The window size represents the number of packets that can be sent without waiting for an acknowledgement. The sequence number is a unique identifier assigned to each packet to ensure that the packets are delivered in the correct order. Here is a sample program in Python that implements the Sliding Window Protocol: ```python import socket import time # Define the window size and sequence number WINDOW_SIZE = 4 SEQ_NUM_SIZE = 4 # Define the packet format PACKET_FORMAT = "!I1024s" # Define the server address and port SERVER_ADDRESS = "localhost" SERVER_PORT = 12345 # Define the data to be sent DATA = "Hello, world!".encode("utf-8") # Create the socket and connect to the server client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((SERVER_ADDRESS, SERVER_PORT)) # Initialize the sequence number and window seq_num = 0 window_start = 0 window_end = WINDOW_SIZE # Send the packets while window_start < len(DATA): # Send the packets in the current window for i in range(window_start, window_end): # Create the packet packet_data = DATA[i:i+1024] packet_seq_num = seq_num.to_bytes(SEQ_NUM_SIZE, byteorder="big") packet = struct.pack(PACKET_FORMAT, packet_seq_num, packet_data) # Send the packet client_socket.send(packet) # Increment the sequence number seq_num += 1 # Wait for the acknowledgements ack_received = False while not ack_received: # Set the timeout client_socket.settimeout(1) # Wait for the acknowledgement try: ack = client_socket.recv(1024) # Check if the acknowledgement is valid if ack: ack_seq_num = int.from_bytes(ack, byteorder="big") if ack_seq_num == window_start: ack_received = True # Update the window window_start += 1 window_end += 1 except socket.timeout: # If the timeout occurs, resend the packets in the current window for i in range(window_start, window_end): packet_data = DATA[i:i+1024] packet_seq_num = (seq_num - WINDOW_SIZE + i).to_bytes(SEQ_NUM_SIZE, byteorder="big") packet = struct.pack(PACKET_FORMAT, packet_seq_num, packet_data) client_socket.send(packet) # Wait for a short period of time before sending the next window time.sleep(0.1) # Close the socket client_socket.close() ``` In this program, the client sends the data in packets of size 1024 bytes and waits for an acknowledgement from the server before sending the next packet. The program uses a sliding window of size 4, which means that the client can send up to 4 packets at a time without waiting for an acknowledgement. The program also implements a timeout mechanism to handle lost packets. If the client does not receive an acknowledgement within 1 second, it resends the packets in the current window. Overall, the Sliding Window Protocol provides reliable and efficient data transfer in computer networks by using a sliding window to control the flow of data between two nodes.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值