题目大意:
就是告诉N个人依次插队的顺序要求输出最终的序列
大致思路:
对于整个插入倒着执行, 那么前面的插入不会影响后面插入的人
记初始数组为[0, 1, 1, 1, ...., 1]一共n + 1个数下标从0到n
那么每次就相当于找出当前前缀和大于p的插入(p, value)然后更新找到的位置的值为0即可
查询操作用线段树维护, 单点修改也是
代码如下:
话说C++1600ms+, G++3600ms+差别好大
Result : Accepted Memory : 4544 KB Time : 1672 ms
/* * Author: Gatevin * Created Time: 2015/8/15 23:09:30 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define maxn 200020 struct Segment_Tree { #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 int val[maxn << 2]; void pushUp(int rt) { val[rt] = val[rt << 1] + val[rt << 1 | 1]; return; } void build(int l, int r, int rt) { if(l == r) { if(l == 0) val[rt] = 0; else val[rt] = 1; return; } int mid = (l + r) >> 1; build(lson); build(rson); pushUp(rt); } void update(int l, int r, int rt, int pos, int value) { if(l == r) { val[rt] = value; return; } int mid = (l + r) >> 1; if(mid >= pos) update(lson, pos, value); else update(rson, pos, value); pushUp(rt); } int query(int l, int r, int rt, int value) { if(l == r) { return l; } int mid = (l + r) >> 1; if(val[rt << 1] > value) return query(lson, value); else return query(rson, value - val[rt << 1]); } }; Segment_Tree ST; pair<int, int> ins[maxn]; int seq[maxn]; int n; int main() { while(~scanf("%d", &n)) { ST.build(0, n, 1); for(int i = 1; i <= n; i++) scanf("%d %d", &ins[i].first, &ins[i].second); for(int i = n; i >= 1; i--) { int pos = ST.query(0, n, 1, ins[i].first); seq[pos] = ins[i].second; ST.update(0, n, 1, pos, 0); } for(int i = 1; i <= n; i++) printf("%d%c", seq[i], i == n ? '\n' : ' '); } return 0; }