Lost Cows POJ2182(线段树)

广东工业大学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]);//输出的时候就正着输
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要求对给定的n个数进行排序,并且输出每个数在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的数据结构。 根据引用中的描述,我们可以通过遍历数组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的数的个数。这个个数就是它在排序后的相对位置。 代码中的query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。 最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要求的排序功能,其中query函数用来求前缀和,add函数用来更新树状数组。在主函数中,我们从后往前遍历数组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans数组中。最后,我们按顺序输出ans数组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值