Codeforces 374D Inna and Sequence 二分+樹狀數組

來源:互聯網
上載者:User

標籤:class   blog   code   http   tar   com   

題目連結:點擊開啟連結

給定n個操作,m長的序列a
下面n個數

if(co>=0)則向字串添加一個co (開始是Null 字元串)

else 刪除字串中有a的下標的字元

直接在序列上搞,簡單類比

#include<stdio.h>#include<iostream>#include<string.h>#include<set>#include<vector>#include<map>#include<math.h>#include<string>#include<stdlib.h>#include<algorithm>using namespace std;#define N 1000005bool use[N], b[N];int top;int n, m;int a[N];int c[N], maxn;inline int lowbit(int x){return x&(-x);}void change(int pos, int val){while(pos<=maxn){c[pos]+=val;pos+=lowbit(pos);}}int sum(int pos){int ans = 0;while(pos)ans+=c[pos], pos-=lowbit(pos);return ans;}set<int>myset;set<int>::iterator p;void Erase(int pos, int r){int l = 1;while(l<=r) {int mid = (l+r)>>1;int tmp = sum(mid);if(tmp==pos) {p = myset.upper_bound(mid);p--;mid = *p;change(mid, -1);use[mid] = 1;myset.erase(p);return ;}if(tmp>pos) r = mid-1;else l = mid+1;}}void init(){myset.clear(); memset(c, 0, sizeof c); maxn = n+10; top = 0;}int main(){int i,j,co;while(~scanf("%d %d",&n,&m)){init();for(i=0;i<m;i++)scanf("%d",&a[i]);int len = 0;for(i = 1; i <= n; i++) {scanf("%d",&co);if(co>=0)use[i] = 0, b[i] = co, change(i,1), len++, myset.insert(i);else {use[i] = 1;int j = lower_bound(a, a+m, len) - a;if(j==0 && len<a[0])continue;if(len!=a[j])j--;while(j>=0) {Erase(a[j], i);j--;len--;}}}if(!len)puts("Poor stack!");else {for(i = 1; i <= n; i++) if(!use[i])printf("%d",b[i]);puts("");}}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.