CF374D
Sol
注意到加数次数总共就很少,那么删数操作也同样很少,所以暴力删除即可。
考虑如何找到原数所在的下标,直接二分,找到第一个满足 \(i\) 减去 \(1\sim i\) 已经被删的数的数量等于 \(k\) 的下标就是第 \(k\) 个数实际的位置,可以用二分树状数组。
时间复杂度 \(O(n\log^2 n)\)。
Code
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define pf push_front
#define desktop "C:\\Users\\incra\\Desktop\\"
#define IOS ios :: sync_with_stdio (false),cin.tie (0),cout.tie (0)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> PII;
const int dx[] = {1,0,-1,0},dy[] = {0,-1,0,1};
template <typename T1,typename T2> bool tomax (T1 &x,T2 y) {
if (y > x) return x = y,true;
return false;
}
template <typename T1,typename T2> bool tomin (T1 &x,T2 y) {
if (y < x) return x = y,true;
return false;
}
LL power (LL a,LL b,LL p) {
LL ans = 1;
while (b) {
if (b & 1) ans = ans * a % p;
a = a * a % p;
b >>= 1;
}
return ans;
}
int fastio = (IOS,0);
// #define endl '\n'
#define puts(s) cout << (s) << endl
const int N = 2000010;
int n,m;
int a[N],b[N];
int tmp;
struct BIT {
int c[N];
int lowbit (int x) {
return x & -x;
}
void modify (int x,int d) {
for (int i = x;i < N;i += lowbit (i)) c[i] += d;
}
int query (int x) {
int ans = 0;
for (int i = x;i;i -= lowbit (i)) ans += c[i];
return ans;
}
}c;
int get (int x) {
int l = 1,r = tmp;
while (l < r) {
int mid = l + r >> 1;
if (mid - c.query (mid) >= x) r = mid;
else l = mid + 1;
}
if (l - c.query (l) != x) return -1;
return l;
}
void mian () {
cin >> m >> n;
for (int i = 1;i <= n;i++) cin >> b[i];
while (m--) {
int op;
cin >> op;
if (op == -1) {
if (!tmp) continue;
for (int i = 1;i <= n;i++) {
int t = get (b[i] - i + 1);
// cout << "pos: " << t << endl;
if (t == -1) break;
c.modify (t,1);
}
}
else a[++tmp] = op;
// for (int i = 1;i <= tmp;i++) {
// if (!(c.query (i) - c.query (i - 1))) cout << i << ' ';
// }
// cout << "----------" << endl;
}
vector <int> ans;
for (int i = 1;i <= tmp;i++) {
if (!(c.query (i) - c.query (i - 1))) ans.pb (a[i]);
}
if (!ans.size ()) puts ("Poor stack!");
else for (int x : ans) cout << x;
}
int main () {
int T = 1;
// cin >> T;
while (T--) mian ();
return 0;
}

浙公网安备 33010602011771号