ZOJ 3635 Cinema in Akiba(二分+树状数组)

题目链接:点击打开链接

题意:n个人轮流做到座位上, 第i个人做到第a[i]个空座上, 求最终每个人的座位情况。

思路:经典水题, 二分套树状数组。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = int(1e9);
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 50000 + 10;
int T,n,m,v,q,ans[maxn],bit[maxn];
int sum(int x) {
    int ans = 0;
    while(x > 0) {
        ans += bit[x];
        x -= x & -x;
    }
    return ans;
}
void add(int x, int d) {
    while(x <= n) {
        bit[x] += d;
        x += x & -x;
    }
}
int main() {
    while(~scanf("%d",&n)) {
        memset(bit, 0, sizeof(bit));
        for(int i=1;i<=n;i++) {
            scanf("%d",&v);
            int mid, l = 1, r = n;
            while(r > l) {
                mid = (l + r) >> 1;
                int res = mid - sum(mid);
                if(res >= v) r = mid;
                else l = mid + 1;
            }
            add(l, 1);
            ans[i] = l;
        }
        scanf("%d",&q);
        bool ok = true;
        while(q--) {
            scanf("%d",&v);
            if(ok) printf("%d",ans[v]);
            else printf(" %d",ans[v]);
            ok = false;
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(ZOJ,树状数组,二分)