首页 > 其他 > 详细

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

时间:2014-06-26 07:53:18      阅读:428      评论:0      收藏:0      [点我收藏+]

题目链接:点击打开链接

给定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;
}


Codeforces 374D Inna and Sequence 二分+树状数组,布布扣,bubuko.com

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

原文:http://blog.csdn.net/qq574857122/article/details/34423657

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!