Codeforces 374D Inna and Sequence 二分法+树状数组

主题链接:点击打开链接

特定n一个操作,m长序列a
下列n的数量

if(co>=0)向字符串加入一个co (開始是空字符串)

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 1000005
bool 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;
}


posted @ 2015-12-08 16:48  mengfanrong  阅读(234)  评论(0编辑  收藏  举报