第一次用单调队列...我好菜啊55555...
这道题和昨天做的一道题很像,,昨天的不会,今天有点开窍了,就记录下来。。
这个就感性的理解一下吧,解释写在代码里了。附上AC代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
typedef pair<int,int>pp;
#define mkp make_pair
#define pb push_back
const int MAX=10005;
int cost[MAX];
int n,h,st,en;
int num[MAX];
deque<pp>q;//单调队列
int main()
{
freopen("G.IN","r",stdin);
while(scanf("%d%d%d%d",&n,&h,&st,&en)==4)
{
memset(cost,0,sizeof(cost));
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
scanf("%d",&cost[i]);
q.clear();
for(int i=1;i<=n;i++)
{
//队尾元素不满足:价格高于当天买
while(!q.empty()&&q.back().first>=cost[i])//注意是">="!
q.pop_back();//队尾出队
q.pb(mkp(cost[i],i));
//队头元素不满足:存活超过了h天
while(!q.empty()&&q.front().second<=(i-h))
q.pop_front();//队头出队
num[q.front().second]++;
}
for(int i=st;i<en;i++)
printf("%d ",num[i]);
printf("%d\n",num[en]);
}
return 0;
}