poj2182 Lost Cows-线段树

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, NLines 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

题目大意:
有编号1-n的n个数字,2<=n<=8000,乱序排列,排序是未知的。对于每个位置的数字,知道排在它前面比它小的数字有多少个。求这个乱序数列的顺序。

解题关键:
在剩下的编号中,第pre[n]+1个数字就是ans[n]

代码如下:

#include <iostream>
using namespace std;
const int N = 10010;
int ans[N];
int pre[N];struct node {int l, r;int len;
} tree[4 * N];void build(int l, int r, int u) {tree[u].l = l;tree[u].r = r;if (l == r) {tree[u].len = l - r + 1;
//		cout << u << " - " << tree[u].len << endl;return;}int mid = (l + r) >> 1;build(l, mid, 2 * u);build(mid + 1, r, 2 * u + 1);tree[u].len = tree[2 * u].len + tree[2 * u + 1].len;
//	cout << u << " - " << tree[u].len << endl;
}int query(int u, int num) {tree[u].len--;if (tree[u].l == tree[u].r)return tree[u].l;if (tree[2 * u].len < num) {return query(2 * u + 1, num - tree[2 * u].len);}if (tree[2 * u].len >= num) {return query(2 * u, num);}
}int main() {int n, i;cin >> n;pre[1] = 0;for (int i = 2; i <= n; i++)cin >> pre[i];build(1, n, 1);
//	for (int i = 1; i <= n; i++)
//		cout << tree[i].len << " ";
//	cout << endl;for (int i = n; i >= 1; i--) {ans[i] = query(1, pre[i] + 1);}for (int i = 1; i <= n; i++)cout << ans[i] << endl;
//	cout << endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/311114.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

简洁直观解释精确率、召回率、F1 值、ROC、AUC

混淆矩阵 当我们在做二分类预测时&#xff0c;把预测情况与实际情况的所有结果两两混合&#xff0c;结果就会出现以下4种情况&#xff0c;就组成了混淆矩阵。 P&#xff08;Positive&#xff09;&#xff1a;代表正样本N&#xff08;Negative&#xff09;&#xff1a;代表负样…

基于Tensorflow搭建卷积神经网络CNN(水果识别)保姆及级教程

项目介绍 TensorFlow2.X 搭建卷积神经网络&#xff08;CNN&#xff09;&#xff0c;实现水果识别。搭建的卷积神经网络是类似VGG的结构(卷积层与池化层反复堆叠&#xff0c;然后经过全连接层&#xff0c;最后用softmax映射为每个类别的概率&#xff0c;概率最大的即为识别结果…

C++实现线段树RMQ-单点修改,区间查询

代码如下&#xff1a; #include <iostream> #include <algorithm> using namespace std; const int N 10010; int input[N]; const int INF 1 << 30;struct node {int l, r;int mw; } tree[4 * N];void init_tree(int n) //初始化&#xff0c;因为要求最小…

如何编写高性能的C#代码(四)字符串的另类骚操作

原文来自互联网&#xff0c;由长沙DotNET技术社区编译。如译文侵犯您的署名权或版权&#xff0c;请联系小编&#xff0c;小编将在24小时内删除。作者介绍&#xff1a;史蒂夫戈登&#xff08;Steve Gordon&#xff09;是Microsoft MVP&#xff0c;Pluralsight的作者&#xff0c;…

statusbar 尺寸 显示图标_移动端页面设计规范尺寸大起底 - 椰树飘香

移动端尺寸繁多&#xff0c;包括IOS和安卓&#xff0c;尺寸多达十余种&#xff0c;所以移动页面尺寸的适配一直是前端和设计的头疼。今天来总结一下当前市场上的一些移动端尺寸&#xff0c;方便设计师和前端去考虑适配。但是最好还是针对自己的产品做调查&#xff0c;根据数据去…

新建项目上传gitee(码云)教程

登录码云 新建一个仓库后&#xff0c;复制HTTPS地址&#xff1a; 本地项目操作 打开需要上传gitee的项目文件夹&#xff0c;并打开Git Bash窗口 本地仓库初始化&#xff1a; git init添加到暂存区 git add .提交到本地仓库 git commit -m "first commit"关联到…

C++实现线段树求区间和-区间查询

代码如下&#xff1a; #include <iostream> using namespace std; const int N 10010; int input[N];struct node {int l, r;int sum; } tree[4 * N];void build(int l, int r, int u)//建树{tree[u].l l;tree[u].r r;if (l r) {tree[u].sum input[l];return ;}int…

OpenSilver: 通过WebAssembly 复活Silverlight

本月早些时候&#xff0c;Userware发布了第一个版本的OpenSilver&#xff0c;微软Silverlight 的开源重新实现。OpenSilver 通过WebAssembly 实现无需任何其他插件在 浏览器上运行。OpenSilver 的当前版本可作为"技术预览"版本提供&#xff0c;它涵盖了大约 60% 的原…

db2有主键时默认hash分区_MySQL分区表最佳实践

前言&#xff1a;分区是一种表的设计模式&#xff0c;通俗地讲表分区是将一大表&#xff0c;根据条件分割成若干个小表。但是对于应用程序来讲&#xff0c;分区的表和没有分区的表是一样的。换句话来讲&#xff0c;分区对于应用是透明的&#xff0c;只是数据库对于数据的重新整…

AcWing 788 逆序对的数量-归并排序

给定一个长度为 n 的整数数列&#xff0c;请你计算数列中的逆序对的数量。 逆序对的定义如下&#xff1a;对于数列的第 i 个和第 j 个元素&#xff0c;如果满足 i<j 且 a[i]>a[j] &#xff0c;则其为一个逆序对&#xff1b;否则不是。 输入格式 第一行包含整数 n &a…

程序员过关斩将-- 喷一喷坑爹的面向UI编程

点击上方“蓝字”关注我们菜菜哥&#xff0c;求你个事呗&#xff1f;说来听听&#xff0c;假装你男朋友可不干不是哦&#xff0c;是正经事。前几天一个项目UI改了&#xff0c;好多人跟着加班修改&#xff0c;怎么样尽量避免这种情况呢&#xff1f;UI修改顶多和客户端开发人员关…

python二维散点分布图_深入理解皮尔逊相关系数amp;python代码

1.常见理解误区&#xff08;1&#xff09;计算出变量A和变量B的皮尔逊相关系数为0&#xff0c;不代表A和B之间没有相关性&#xff0c;只能说明A和B之间不存在线性相关关系。例&#xff1a;温度和冰淇淋销量之间的散点图像如下&#xff0c;可以发现大致成二次函数图像&#xff0…

hdu4911 Inversion-归并排序

解题思路&#xff1a; 如果原序列的逆序对数大于交换次数&#xff0c;那么最少的逆序对数量就是原序列逆序对-交换次数。 如果原序列的逆序对数小于等于交换次数&#xff0c;那么最少的逆序对数量为0&#xff0c;因为交换次数超过逆序对数&#xff0c;可以把这些逆序对全部消除…

【.net core】电商平台升级之微服务架构应用实战

一、前言这篇文章本来是继续分享IdentityServer4 的相关文章&#xff0c;由于之前有博友问我关于微服务相关的问题&#xff0c;我就先跳过IdentityServer4的分享&#xff0c;进行微服务相关的技术学习和分享。微服务在我的分享目录里面是放到四月份开始系列文章分享的&#xff…

c语言将一个已知头结点的单链表逆序_C语言实现常用数据结构:静态链表数组实现(第5篇)...

「今天是学习C语言第 148 天」纸上学来终觉浅&#xff0c;绝知此事要躬行。—— 陆游「冬夜读书示子聿」# 静态链表使用数组实现&#xff0c;利用数组下标代替指针&#xff0c;从而实现数据结点之间的先后关系。实现要点&#xff1a;1.数组下标为0的位置为头结点&#xff0c;指…

CentOS7 防火墙(firewalld)开启常见端口命令

CentOS7 防火墙开启常见端口命令 1、安装Firewall命令&#xff1a; yum install firewalld firewalld-config2、Firewall开启常见端口命令 firewall-cmd --zonepublic --add-port80/tcp --permanent firewall-cmd --zonepublic --add-port443/tcp --permanent firewall-cmd …

集成平台集群任务动态分派

源宝导读&#xff1a;MIP集成平台是为了解决企业大量异构系统之间快速、稳定集成的需要&#xff0c;助力企业数字化转型&#xff0c;明源云自主研发的平台系统。本文将对"事件任务分派"场景的架构设计以及实践成果进行分享。背景MIP集成平台是为了解决企业大量异构系…

dotcpp1115 DNA-打印图案

题目描述 小强从小就喜欢生命科学&#xff0c;他总是好奇花草鸟兽从哪里来的。终于&#xff0c; 小强上中学了&#xff0c;接触到了神圣的名词–DNA.它有一个双螺旋的结构。这让一根筋的小强抓破头皮&#xff0c;“要是能画出来就好了” 小强喊道。现在就请你帮助他吧 输入 输…

akb48_AKB48里历史——六年的终结

注&#xff1a;这是2012年发行的一本在BUBUKA连载的基础上补充了一些内容的粉丝公式教科书&#xff0c;从里面找了部分内容翻译了一下&#xff0c;节选的内容主要说的是2011年的事情&#xff0c;以当时作者的视角&#xff0c;是AKB48第一次新老粉丝换代的时期。前田敦子和大岛优…

[头脑风暴] 解读Docker Bridge网络模型

背景这几天在研究Kubernetes&#xff0c; 遇到一个有意思的nodejs镜像&#xff1a;luksa/kubia# 不带端口映射启动容器 docker run -it -d luksa/kubia # 连接到默认的Bridge网桥&#xff0c;容器IP是 172.17.0.2之后&#xff0c;在宿主机使用容器IP和8080 端口可访问该容器…