Lost Cows
Posted czy-power
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lost Cows相关的知识,希望对你有一定的参考价值。
问题 F: Lost Cows
时间限制: 1 Sec 内存限制: 128 MB提交: 10 解决: 9
[提交] [状态] [讨论版] [命题人:admin]
题目描述
N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood ‘watering hole‘ and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.
Regrettably, FJ does not have a way to sort them. Furthermore, he‘s not very good at observing problems. Instead of writing down each cow‘s brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.
Given this data, tell FJ the exact ordering of the cows.
Regrettably, FJ does not have a way to sort them. Furthermore, he‘s not very good at observing problems. Instead of writing down each cow‘s brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.
Given this data, tell FJ the exact ordering of the cows.
输入
* Line 1: A single integer, N
* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.
* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.
输出
* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.
样例输入
5
1
2
1
0
样例输出
2
4
5
3
1
思路:建立线段树,权值为包含的元素个数,逆序询问并修改权值即可。
#include<iostream> #include<cstring> #include<cstring> #include<cstdio> #include<cmath> #include<vector> #include<queue> #include<map> #include<algorithm> #define REP(i, a, b) for(int i = (a); i <= (b); ++ i) #define REP(j, a, b) for(int j = (a); j <= (b); ++ j) #define PER(i, a, b) for(int i = (a); i >= (b); -- i) using namespace std; const int maxn = 8010; template <class T> inline void rd(T &ret){ char c; ret = 0; while ((c = getchar()) < ‘0‘ || c > ‘9‘); while (c >= ‘0‘ && c <= ‘9‘){ ret = ret * 10 + (c - ‘0‘), c = getchar(); } } struct node{ int l,r,lh; }g[maxn*4]; int n,p[maxn],tmp[maxn]; void build(int l,int r,int rt){ g[rt].l=l,g[rt].r=r,g[rt].lh=r-l+1; if(l==r)return; int mid=(l+r)>>1; build(l,mid,rt*2); build(mid+1,r,rt*2+1); return; } int query(int rt,int now){ g[rt].lh--; if(g[rt].l==g[rt].r)return g[rt].l; if(now<=g[rt*2].lh)return query(rt*2,now); else return query(rt*2+1,now-g[rt*2].lh); } int main() { rd(n); REP(i,2,n)rd(p[i]); build(1,n,1); p[1]=0; PER(i,n,1){ tmp[i]=query(1,p[i]+1); } REP(i,1,n)cout<<tmp[i]<<endl; }
!--StartMarkForVirtualJudge-->以上是关于Lost Cows的主要内容,如果未能解决你的问题,请参考以下文章