我是靠谱客的博主 沉默白云,这篇文章主要介绍Codeforces 374D Inna and Sequence 二分+树状数组,现在分享给大家,希望可以做个参考。

题目链接:点击打开链接

给定n个操作,m长的序列a
下面n个数

if(co>=0)则向字符串添加一个co (开始是空字符串)

else 删除字符串中有a的下标的字符

直接在序列上搞,简单模拟

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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 二分+树状数组的全部内容,更多相关Codeforces内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部