poj2823
Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 70057 | Accepted: 19895 | |
Case Time Limit: 5000MS |
Description
An array of size n ≤ 106 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 position | Minimum value | Maximum value |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
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
<span style="color:#000000">8 3
1 3 -1 -3 5 3 6 7
</span>
Sample Output
<span style="color:#000000">-1 -3 -3 -3 3 3
3 3 5 5 6 7</span>
题意:窗口滑动于一个数组中,求从左到右每个位置窗口中的最大值,最小值。
思路:单调队列,对于求最大值,队列中每次插入一个新值需要删除队尾小于它的值,以及队首超过窗口范围的值;最小值类似。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 1e6+10;
struct node
{
int val;
int p;
}maxque[maxn],minque[maxn];
int ansmax[maxn],ansmin[maxn];
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
int a;
int l1=0,r1=0,l2=0,r2=0;
for(int i = 0;i<n;i++)
{
scanf("%d",&a);
while(l1<r1&&maxque[r1-1].val<=a)
r1--;
maxque[r1].val = a;
maxque[r1].p = i;
r1++;
while(l1<r1-1&&maxque[l1].p<=i - k)l1++;
while(l2<r2&&minque[r2 - 1].val>=a)
r2--;
minque[r2].val = a;
minque[r2].p = i;
r2++;
while(l2<r2-1&&minque[l2].p<=i-k)l2++;
if(i>=k-1)
{
ansmax[i] = maxque[l1].val;
ansmin[i] = minque[l2].val;
}
}
if(k>n)
{
printf("%d\n%d\n",minque[l2],maxque[l1]);
continue;
}
for(int i = k-1;i<n;i++)
{
printf("%d%c",ansmin[i],i==n-1?'\n':' ');
}
for(int i = k-1;i<n;i++)
{
printf("%d%c",ansmax[i],i==n-1?'\n':' ');
}
}
}
hdu6319(2018多校3-A)
Problem A. Ascending Rating
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 4167 Accepted Submission(s): 1375
Problem Description
Before the start of contest, there are n ICPC contestants waiting in a long queue. They are labeled by 1 to n from left to right. It can be easily found that the i-th contestant's QodeForces rating is ai.
Little Q, the coach of Quailty Normal University, is bored to just watch them waiting in the queue. He starts to compare the rating of the contestants. He will pick a continous interval with length m, say [l,l+m−1], and then inspect each contestant from left to right. Initially, he will write down two numbers maxrating=−1 and count=0. Everytime he meets a contestant k with strictly higher rating than maxrating, he will change maxrating to ak and count to count+1.
Little T is also a coach waiting for the contest. He knows Little Q is not good at counting, so he is wondering what are the correct final value of maxrating and count. Please write a program to figure out the answer.
Input
The first line of the input contains an integer T(1≤T≤2000), denoting the number of test cases.
In each test case, there are 7 integers n,m,k,p,q,r,MOD(1≤m,k≤n≤107,5≤p,q,r,MOD≤109) in the first line, denoting the number of contestants, the length of interval, and the parameters k,p,q,r,MOD.
In the next line, there are k integers a1,a2,...,ak(0≤ai≤109), denoting the rating of the first k contestants.
To reduce the large input, we will use the following generator. The numbers p,q,r and MOD are given initially. The values ai(k<i≤n) are then produced as follows :
ai=(p×ai−1+q×i+r)modMOD
It is guaranteed that ∑n≤7×107 and ∑k≤2×106.
Output
Since the output file may be very large, let's denote maxratingi and counti as the result of interval [i,i+m−1].
For each test case, you need to print a single line containing two integers A and B, where :
AB==∑i=1n−m+1(maxratingi⊕i)∑i=1n−m+1(counti⊕i)
Note that ``⊕'' denotes binary XOR operation.
Sample Input
1 10 6 10 5 5 5 5 3 2 2 1 5 7 6 8 2 9
Sample Output
46 11
题意:窗口滑动,需要求得包含每个窗口最大值的式子A和每个窗口第一个元开始的上升序列长度的式子B。
思路:A易求得,对于B,我们考虑从数组尾端到首段来移动窗口维护一个单调递减的单调队列,这样每次队列的长度就是上升序列长度。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e7+10;
ll a[maxn];
ll ans1[maxn],ans2[maxn];
struct node
{
ll val;
int p;
}maxque[maxn];
int main()
{
int t;
int n,m,k;
ll p,q,r,mod;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%lld%lld%lld%lld",&n,&m,&k,&p,&q,&r,&mod);
for(int i=1;i<=k;i++)
{
scanf("%lld",&a[i]);
}
for(int i=k+1;i<=n;i++)
{
a[i] = ((p*a[i-1])%mod+(q*i)%mod+r)%mod;
}
int l=0,r=0;
for(int i=n;i>=1;i--)
{
while(l<r&&maxque[r-1].val<=a[i])
r--;
maxque[r].val = a[i];
maxque[r].p = i;
r++;
while(l<r-1&&maxque[l].p>=i + m)l++;
if(i<=n-m+1)
{
ans1[i] = maxque[l].val^i;
ans2[i] = (r - l)^i;
}
}
ll resA = 0,resB = 0;
for(int i=1;i<=n-m+1;i++)
{
resA += ans1[i];
resB += ans2[i];
}
printf("%lld %lld\n",resA,resB);
}
}