当前位置: 代码迷 >> 综合 >> Educational Codeforces Round 80 (Rated for Div. 2) E - Messenger Simulator(前缀和,树状数组)
  详细解决方案

Educational Codeforces Round 80 (Rated for Div. 2) E - Messenger Simulator(前缀和,树状数组)

热度:63   发布时间:2024-01-26 00:51:00.0

? ? ?

题意:一个1到n的全排列,m次操作,表示将ai移动到数组的第一个位置,求过程中每个数的最小位置和最大位置。

1,最小值为 1 / 初始位置

2,最大值出现在某次移动这个数之前 or 全部移动完成之后

3,因为数字不重复所以维护一个前缀和即可(记得空出来移动的位置)

int n, m;
int c[MAXN*2];
void update(int x,int v){ while (x <= n+m) {c[x]+=v; x += (x & (-x)); }}
int que(int x){int sum = 0; while (x) { sum += c[x]; x -= (x & (-x)); }return sum;}
signed main()
{cin >> n >> m;vector<int> ans1(n+1), ans2(n+1), a(m), pos(n+1);rpp(i,n){ans1[i] = ans2[i] = i;  pos[i]= i+m;update(i+m,1);}int l=m;rep(i, m){cin >> a[i];ans1[a[i]] = 1;ans2[a[i]]=max(ans2[a[i]],que(pos[a[i]]));update(pos[a[i]],-1);pos[a[i]]=l--;update(pos[a[i]],1);}rpp(i,n) ans2[i] = max(ans2[i],que(pos[i]));rpp(i,n) cout<<ans1[i]<<" "<<ans2[i]<<endl;return 0;
}
  相关解决方案