EZOJ #362历史

分析

就是保存前pi-1个数每个ai出现多少次

然后维护这些数当前剩余的最大值

每次和新加进来的比较即可

如果新的大直接取

否则新的最大值一定不大于原来的最大值

因此o(n)

代码

#include<bits/stdc++.h>
using namespace std;
int n,m,a[100100],p[100100],sum[100100],cnt;
inline int getmax(){while(!sum[cnt])cnt--;return cnt;}
int main(){
    int i,j,k,x,y;
    scanf("%d",&n);
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    scanf("%d",&m);
    for(i=1;i<=m;i++)scanf("%d",&p[i]);
    for(int _=1;_<=m;_++){
      cnt=n;
      for(i=1;i<p[_];i++)sum[a[i]]++;
      getmax();
      int pos=p[_],ans[2];
      ans[0]=ans[1]=0;
      for(i=1;i<=n;i++){
          if(pos>n)pos=n+1;
          if(getmax()>a[pos]){
            ans[i&1]+=getmax();
            sum[getmax()]--;
            sum[a[pos]]++;
        }else {
          ans[i&1]+=a[pos];
        }
        pos++;
      }
      printf("%d\n",ans[1]-ans[0]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yzxverygood/p/11520416.html