http://acm.hdu.edu.cn/showproblem.php?pid=6406
Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n apples on the tree ripen, and Taotao will go to pick these apples.
Input The first line of input is a single line of integer T (1≤T≤10) , the number of test cases.
Output For each query, display the answer in a single line.
Sample Input 1 5 3 1 2 3 4 4 1 5 5 5 2 3
Sample Output 1 5 3 Hint For the first query, the heights of the apples were 5, 2, 3, 4, 4, so Taotao would only pick the first apple. For the second query, the heights of the apples were 1, 2, 3, 4, 5, so Taotao would pick all these five apples. For the third query, the heights of the apples were 1, 3, 3, 4, 4, so Taotao would pick the first, the second and the fourth apples. |
题意:
给一个序列,每次贪心选取比前一个数大的数。每次询问修改一个数,求修改后的序列的能选出 多少个数。询问不叠加。
分析:
我用的队列维护上升序列,然后看更新的点是否对队列中的数产生影响,或者对没有加入队列的数产生影响
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[100005],b[100005];
int num[100005];///0到i中有一个出现在队列
int R[100005];
int vis[100005];///上一个在队列的位置
int f[100005];
stack<int>q;
int main()
{
int t,i,j,n,m,k,x,y,l,r,mid,maxn,cnt;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
cnt=1;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(i==1)
f[cnt]=i,b[cnt++]=a[i],vis[1]=1;
else
if(a[i]>b[cnt-1])
{
f[cnt]=i;
b[cnt++]=a[i],vis[i]=i;
}
else
{
vis[i]=vis[i-1];
}
num[i]=cnt-1;
}
while(!q.empty())q.pop();
for(i=n;i>0;i--)
{
while(!q.empty()&&q.top()<=a[i])
q.pop();
q.push(a[i]);
R[i]=q.size();///从这一点严格上升到n的长度
}
while(k--)
{
scanf("%d%d",&x,&y);
if(a[x]>y) ///变小
{
if(vis[x]!=x)///不在队列中,不产生影响
printf("%d\n",cnt-1);
else
{ ///在队列中,变小之后可能会将非队列中的数加入队列
maxn=num[x]-1;
if(num[x]-1>0&&b[num[x]-1]>=y)///变小的数是否小于前一个队列中的数,如果小于离开队列
{
for(l=x+1;l<=n;l++){
if(a[l]>b[num[x]-1])
{
maxn+=R[l];
break;
}
}
printf("%d\n",maxn);
continue;
}
else
{
maxn++;
for(l=x+1;l<=n;l++){
if(a[l]>y)
{
maxn+=R[l];
break;
}
}
printf("%d\n",maxn);
}
}
}
else
if(a[x]==y)
printf("%d\n",cnt-1);
else ///变大
{
if(vis[x]==x){///变大之前在队列,变化后一定在队列,但是会将后面的部分队列元素踢出
l=upper_bound(b+1,b+cnt,y)-b;
printf("%d\n",cnt-l+num[x]);
}
else
if(num[x]>0&&b[num[x]]>=y)///变大之前不在队列,变化之后也不会在队列,不产生影响
printf("%d\n",cnt-1);
else
{ ///变大之后加入队列,并且踢出队列中的某些数
l=upper_bound(b+1,b+cnt,y)-b;
printf("%d\n",cnt-l+num[x]+1);
}
}
}
}
}