标签:opera printf code min pop value maximum enter arc
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 59348 | Accepted: 17040 | |
Case Time Limit: 5000MS |
Description
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
Output
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
//单调队列
#include<algorithm>
#include<cstdio>
using namespace std;
int a[1000011];//数组数据
int Q[1000011];//队列
int I[1000011];//I[i]表示队列中的Q[i]在数组中的下标
int OutMin[1000011];//最小值
int OutMax[1000011];//最大值
int n,k;
void GetMin()
{
int i;
int head=1;
int tail=0;
for(i=1;i<k;++i)
{
while(head <=tail && Q[tail]>a[i])
tail--;
tail++;
Q[tail]=a[i];
I[tail]=i;
}
for(i=k;i<=n;++i)
{
while(head <=tail && Q[tail]>a[i])
tail--;
tail++;
Q[tail]=a[i];
I[tail]=i;
while(I[head]<=i-k)
head++;
OutMin[i-k] = Q[head];
}
}
void GetMax()
{
int i;
int head=1;
int tail=0;
for(i=1;i<k;++i)
{
while(head <=tail && Q[tail]<a[i])
tail--;
tail++;
Q[tail]=a[i];
I[tail]=i;
}
for(i=k;i<=n;++i)
{
while(head <=tail && Q[tail]<a[i])
tail--;
tail++;
Q[tail]=a[i];
I[tail]=i;
while(I[head]<=i-k)
head++;
OutMax[i-k]=Q[head];
}
}
int main()
{
int i;
scanf("%d%d",&n,&k);
if(k>n)
k=n;
for(i=1;i<=n;++i)
{
scanf("%d",&a[i]);
}
GetMin();
GetMax();
for(i=0;i<(n-k);++i)
{
printf("%d ", OutMin[i]);
}
printf("%d\n", OutMin[n-k]);
for(i=0;i<=(n-k);++i)
{
printf("%d ", OutMax[i]);
}
printf("%d\n", OutMax[i]);
return 0;
}
//优先队列
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int a[1000011];//数组数据
int OutMin[1000011];//最小值
int OutMax[1000011];//最大值
int cnt1=0;
int cnt2=0;
int n,k;
struct cmp1
{
bool operator()(const int a1,const int a2)
{
return a[a1]>a[a2];
}
};
struct cmp2
{
bool operator()(const int a1,const int a2)
{
return a[a1]<a[a2];
}
};
priority_queue <int ,vector<int>,cmp1> Q1;
priority_queue <int ,vector<int>,cmp2> Q2;
int main()
{
int i;
scanf("%d%d",&n,&k);
if(k>n)
k=n;
for(i=1;i<=n;++i)
{
scanf("%d",&a[i]);
}
for(i=1;i<=k;++i)
{
Q1.push(i);
Q2.push(i);
}
OutMin[cnt1++]=a[Q1.top()];
OutMax[cnt2++]=a[Q2.top()];
for(i=k+1;i<=n;++i)
{
Q1.push(i);
Q2.push(i);
while(i-Q1.top()>=k)
Q1.pop();
OutMin[cnt1++]=a[Q1.top()];
while(i-Q2.top()>=k)
Q2.pop();
OutMax[cnt2++]=a[Q2.top()];
}
for(i=0;i<=(n-k);++i)
{
printf("%d%c", OutMin[i], (i < n - k) ? ‘ ‘ : ‘\n‘);
}
for(i=0;i<=(n-k);++i)
{
printf("%d%c", OutMax[i], (i < n - k) ? ‘ ‘ : ‘\n‘);
}
return 0;
}
标签:opera printf code min pop value maximum enter arc
原文地址:http://www.cnblogs.com/552059511wz/p/6674783.html