Logu P2251 Quality Detection and logu p2251 Detection
Background
None
Description
To check the quality of A total of N products on the production line, we first place A score A for each product to indicate its quality, then calculate the score of the product with the worst quality among the first M products. Q [m] = min {A1, A2 ,... am}, and Q [M + 1], Q [m + 2] from 2nd to m + 1... finally, calculate Q [N] from N-M + 1 to n. Perform further evaluation based on Q.
Please obtain the Q sequence as soon as possible.
Input/Output Format
Input Format:
Enter two rows in total.
The first line contains two numbers N and M, separated by spaces. The meaning is as described above.
The second line contains N numbers, indicating the quality of N products.
Output Format:
Output N-M + 1 rows in total.
1st to N-M + 1 rows have one number per row, and the number of I rows Q [I + M-1]. The meaning is as described above.
Input and Output sample input sample #1: Copy
10 416 5 6 9 5 13 14 20 8 12
Output example #1: Copy
5555588
Description
[Data range]
30% of data, N <= 1000
100% of data, N <= 100000
100% of data, M <= N, A <= 1 000 000
Monotonous queue bare question
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<deque> 7 #define LL long long 8 #define lb(x) ((x)&(-x)) 9 using namespace std;10 const int MAXN=1000001;11 inline int read()12 {13 char c=getchar();int x=0,f=1;14 while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}15 while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();return x*f;16 }17 struct node18 {19 int pos,val;20 node(){ pos=val=0; }21 node(int a,int b){ pos=a,val=b; }22 };23 deque<node>q;24 int a[MAXN];25 int main()26 {27 int n=read(),m=read();28 for(int i=1;i<=n;i++)29 a[i]=read();30 for(int i=1;i<=n;i++)31 {32 while(q.size()>0&&i-m>=q.front().pos) q.pop_front();33 while(q.size()>0&&a[i]<=q.back().val) q.pop_back();34 q.push_back(node(i,a[i]));35 if(i>=m) printf("%d\n",q.front().val); 36 }37 return 0;38 }