首页 > 其他 > 详细

Lost Cows

时间:2019-02-11 22:44:37      阅读:195      评论:0      收藏:0      [点我收藏+]

问题 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. 

 

输入

* 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 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;
}

 

 

Lost Cows

原文:https://www.cnblogs.com/czy-power/p/10363442.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!