[Codeforces Round #679 (Div. 2)] D. Shurikens (思维,树状数组)
题面:
题意:
现在有价格分别为([1,n])的武器,以及2种操作。
- +意味着店主拿一个武器放在展台;
- -x意味客户买了价格为(mathit x)的武器。
初始时展台为空,且客户们总是买当前展台里最便宜的武器。
问你是否有一个放武器的顺序满足给定的操作。
思路:
我们只需要维护出当前放入展台的武器的价格的最大值和最小值即可,
维护方法:
1、连续的购物武器时,后买的价格一定比前面的高。
2、用树状数组单点修改区间查询和当前展台中武器的个数可以得出当前购买武器时的价格上限。
3、如果当前展台中武器个数大于1,那么这次购买武器的价格为(num)时,展台中剩余武器的价格一定大于(num),可以得出价格下限。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ' ', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
int ans[maxn];
int isok = 1;
stack<int> st;
int tree[maxn];
int lowbit(int x)
{
return (-x)& x;
}
void add(int pos, int val)
{
while (pos < maxn) {
tree[pos] += val;
pos += lowbit(pos);
}
}
int ask(int pos)
{
int res = 0;
while (pos > 0) {
res += tree[pos];
pos -= lowbit(pos);
}
return res;
}
int pre[maxn];
int main()
{
#if DEBUG_Switch
freopen("C:\code\input.txt", "r", stdin);
#endif
//freopen("C:\code\output.txt","w",stdout);
cin >> n ;
int id = 0;
int mi = 0;
int cnt = 0;
repd(i, 1, n + n) {
char op;
cin >> op;
if (op == '+') {
st.push(++id);
mi = 0;
} else {
int x;
cin >> x;
cnt = ask(n) - ask(x - 1);
if (isok == 0 || x < mi || 0 == (int)st.size() || x > n - sz(st) + 1 - cnt || x < pre[st.top()] ) {
isok = 0;
continue;
}
mi = x;
ans[st.top()] = x;
st.pop();
if (st.size() > 0) {
pre[st.top()] = max(pre[st.top()], x + 1);
}
add(x, 1);
}
}
if (isok) {
printf("YES
");
repd(i, 1, n) {
printf("%d%c", ans[i], i == n ? '
' : ' ' );
}
} else {
printf("NO
");
}
return 0;
}