Lost Cows POJ2182(线段树)

这篇博客介绍了如何运用线段树数据结构解决一个关于数组排序的问题——Lost Cows。具体情境是,有一群编号从1到N的牛,它们在排队时并未按编号顺序排列,而我们已知每头牛前面有多少编号更小的牛。通过建立线段树,并进行逆序查询,可以确定每头牛的原始编号。博客中提供了详细的算法解析、代码实现和样例解释,帮助读者理解线段树在解决此类问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

广东工业大学ACM寒假集训专题五E
vjudge
poj

Lost Cows

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17979 Accepted: 11115
Description

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.
Input

  • 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.
    Output

  • 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.

Sample Input
5
1
2
1
0
Sample Output

2
4
5
3
1
翻译其实简单,建议自己看看,不想看也可以看我简单翻译。

有n头牛,每头牛有一个序号(范围是1-n)。牛打乱顺序排成一队,现在知道每头牛前面比自己序号的牛的数量。需要求出现在每头牛的序号。

分析: 设数组 a [ i ] a[i] a[i]从后往前考虑,最后一头牛 a [ i ] = 0 a[i] = 0 a[i]=0,那么它的编号为第 a [ i ] + 1 a[i] + 1 a[i]+1编号:为 1 1 1,倒数第二头牛的编号为除去最后一头牛的编号后的第 a [ i − 1 ] + 1 a[i-1] + 1 a[i1]+1编号:为 3 3 3,其他的类推,所以可以维护之前已经选掉的编号,求第k大的数字, t r e e [ n o d e ] tree[node] tree[node] 表示该区间已经被选掉的点的个数。
比如样例:
开一个 t a g tag tag数组,全部设1。(全部牛没标记)

最后一个是 0 0 0,加1等于1,此时 t a g [ ] tag[] tag[]节点全是1,找第一个1( t a g [ ] = 1 , 1 , 1 , 1 , 1 tag[]={1,1,1,1,1} tag[]=1,1,1,1,1),下标为1,变成0。(把牛标记了)

倒数第二个是1,加1等于2,找 t a g tag tag剩下的第2个1( t a g [ ] = 0 , 1 , 1 , 1 , 1 tag[]={0,1,1,1,1} tag[]=0,1,1,1,1),第二个1下标为3,然后这个1变成0,。

倒数第三个是2,加一等于3,找到第三个1,( t a g [ ] = 0 , 1 , 0 , 1 , 1 tag[]={0,1,0,1,1} tag[]=0,1,0,1,1),下标为5。以此类推。
最后倒序输出
因为我们要方便数1的个数(没标记的牛的个数),线段树就可以,因为本身带有二分,可以比较快找到正确位置。

图解建树过程,每个节点储存子节点没有标记的牛的数量
请添加图片描述
图解查找过程(标记牛),(前两个)

请添加图片描述
续:
请添加图片描述

#include<iostream>
using namespace std;
int a[8005],ans[8005];
struct xx{
    int v,l,r;//可以不用结构体,用的原因是在查找的时候就不用再函数参数加l和r了
}tree[400005];//线段树数组要是原数组4倍
void build_tree(int node,int l,int r)
{
    tree[node].v=r-l+1;//初始时[l, r]区间中的数的个数为 r - l + 1
    tree[node].l=l;
    tree[node].r=r;
    if(l==r){
        return;
    }
    int left_node=node<<1;//2*node
    int right_node=node<<1|1;//2*node+1
    int mid=(l+r)>>1;//(l+r)/2
    build_tree(left_node,l,mid);
    build_tree(right_node,mid+1,r);
}
int query_tree(int node,int k){//因为用结构体,所以不用传入l和r
    tree[node].v--;
    //for(int i=1;i<=10;i++)cout<<tree[i].v<<' ';
    //cout<<endl;
    //如果搜到了叶子结点(左节点等于右节点),那么必定是这个值了
    if(tree[node].l==tree[node].r)return tree[node].l;
    int left_node=node<<1;//2*node
    int right_node=node<<1|1;//2*node+1
    //cout<<tree[left_node].v<<'|'<<k<<endl;
    //左子区间的值的个数不足x个,那么就在右子区间找第 x - k大的数(k为左区间数的个数)
    if(tree[left_node].v<k)return query_tree(right_node,k-tree[left_node].v);
	//左区间的数的个数 >= x,那么就在左区间搜索即可
    else return query_tree(left_node,k);
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=2;i<=n;i++){
        scanf("%d",&a[i]);//因为只有n-1个数据,第一头牛没有数据
    }
    a[1]=0;//好像没用,反正第一头牛没数据,0就对了
    build_tree(1,1,n);
    //for(int i=1;i<=10;i++)cout<<tree[i].v<<' ';
    for(int i=n;i>=1;i--)ans[i]=query_tree(1,a[i]+1);//存的时候就倒着存
    for(int i=1;i<=n;i++)printf("%d\n",ans[i]);//输出的时候就正着输
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值