Lost Cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17113 Accepted: 10664

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

Source

USACO 2003 U S Open Orange

问题链接:POJ2182 HDU2711 Lost Cows
问题简述:(略)
问题分析:区间查询问题,用树状数组或线段树解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序(树状数组)如下:

/* POJ2182 HDU2711 Lost Cows */#include <iostream>
#include <cstdio>using namespace std;const int N = 8000 + 1;
int n, tree[N], pre[N], ans[N];inline int lowbit(int x) {return x & -x;}
void add(int x, int d) {while(x <= n) tree[x] += d, x += lowbit(x);}int sum(int x)
{int sum = 0;while(x > 0) sum += tree[x], x -= lowbit(x);return sum;
}int find(int x)
{int l = 1, r = n;while (l < r) {int mid = (l + r) >> 1;if (sum(mid) < x)l = mid + 1;elser = mid;}return l;
}int main()
{while(~scanf("%d", &n)) {pre[1] = 0;for (int i = 2; i <= n; i++) scanf("%d", &pre[i]);for (int i = 1; i <= n; i++) tree[i] = lowbit(i);for (int i = n; i > 0; i--) {int p = find(pre[i] + 1);add(p, -1);ans[i] = p;}for (int i = 1; i <= n; i++)printf("%d\n", ans[i]);}return 0;
}

AC的C++语言程序(线段树)如下:

/* POJ2182 HDU2711 Lost Cows */#include <iostream>
#include <cstdio>using namespace std;const int N = 8000 + 1;
struct {int l, r, len;
} tree[4 * N];
int pre[N], ans[N];void buildTree(int left, int right, int u)
{tree[u].l = left;tree[u].r = right;tree[u].len = right - left + 1;              //更新结点u的值if(left == right) return;buildTree(left, (left + right) >> 1, u << 1);    //递归左子树。buildTree(((left + right) >> 1) + 1, right, (u << 1) + 1);    //递归右子树。
}int query(int u, int num)
{tree[u].len--;         // 对访问到的区间维护len。即把这个结点上牛的数量减去一。if (tree[u].l == tree[u].r) return tree[u].l;// 情况1:左子区间内牛的个数不够,则查询右子区间的左起第num - tree[u<<1].len个元素。if (tree[u << 1].len < num)return query((u << 1) + 1, num - tree[u << 1].len);// 情况2:左子区间内牛的个数足够,则依旧查询左子区间的左起第num个元素。elsereturn query(u << 1, num);
}int main()
{int n;while(~scanf("%d", &n)) {pre[1] = 0;for (int i = 2; i <= n; i++) scanf("%d", &pre[i]);buildTree(1, n, 1);for (int i = n; i >= 1; i--)ans[i] = query(1, pre[i] + 1);for (int i = 1; i <= n; i++)printf("%d\n", ans[i]);}return 0;
}

POJ2182 HDU2711 Lost Cows【树状数组+线段树】相关推荐

  1. D-query SPOJ - DQUERY(求区间不同数的个数)(树状数组||线段树+离散)(主席树+在线)

    English Vietnamese Given a sequence of n numbers a1, a2, -, an and a number of d-queries. A d-query ...

  2. HDU 1556 前缀和 树状数组 线段树

    解法一: a[i]表示以 i作为起点,对 i-n的气球全部上色的次数  对(start,end)区间上色 ++a[start] --a[end+1]抵消掉 end+1-n的部分 问题转换为求 a的前缀 ...

  3. jzoj3854-分组【树状数组,线段树】

    正题 题目链接:https://jzoj.net/senior/#contest/show/2990/2 题目大意 一个小队满足要求 队长的地位最高 所有队员和队长的年龄差不超过kkk 给出nnn个人 ...

  4. 模板三连击:树状数组+线段树+主席树

    没事儿干,复习模板...... 1.树状数组 本来不想写这个的,但是反正就几分钟就打完了,所以就写了,水AC数. 洛谷 P3374 [模板]树状数组 1 1 #include<cstdio> ...

  5. 51nod 1680区间求和 (dp+树状数组/线段树)

    不妨考虑已知一个区间[l,r]的k=1.k=2....k=r-l+1这些数的答案ans(只是这一个区间,不包含子区间) 那么如果加入一个新的数字a[i](i = r+1) 则新区间[l, i]的答案为 ...

  6. CCF201709-5 除法(100分)【树状数组+线段树】

    试题编号: 201709-5 试题名称: 除法 时间限制: 10.0s 内存限制: 256.0MB 问题描述: 问题描述 小葱喜欢除法,所以他给了你N个数a1, a2, ⋯, aN,并且希望你执行M次 ...

  7. P2357 守墓人(树状数组/线段树)

    题目描述 在一个荒凉的墓地上 有一个令人尊敬的守墓人, 他看守的墓地从来 没有被盗过, 所以人们很放心的把自己的先人的墓 安顿在他那 守墓人能看好这片墓地是必然而不是偶然..... 因为....守墓人 ...

  8. 差分+树状数组 线段树【P2357】 守墓人

    题目描述-->p2357 守墓人 敲了一遍线段树,水过. 树状数组分析 主要思路: 差分 简单介绍一下差分(详细概念太麻烦,看下面. 给定一个数组 7 8 6 5 1 8 18 20 35 // ...

  9. [CSP-S模拟测试]:影魔(树状数组+线段树合并)

    题目背景 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万. 千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄. 每一个灵魂,都有着 ...

最新文章

  1. Qt Designer提升控件
  2. php中this,self,parent三个关键字之间的区别(转载)
  3. 如何把本地项目上传到Github上面(详细版)
  4. 有开电商的集合了,了解Water Pamola通过恶意订单对电商发起攻击
  5. PE学习(六)第六章 栈与重定位表 实例栈溢出、模拟加载器加载DLL、遍历重定位表
  6. 基于 Flink 的超大规模在线实时反欺诈系统的建设与实践
  7. 疯子的算法总结11--次小生成树+严格次小生成树
  8. android笔记之在WebView中显示ProgressBar的两种方法
  9. OpenCasCade在一个窗体中的两个picture控件中 分别显示
  10. Zynq硬件开发之Xilinx官方技术手册解读(一)
  11. python爬虫之scrapy入门
  12. 概率论中 Var是什么意思?概率论方差概念介绍
  13. foxmail邮箱修改服务器,foxmail基本设置方法.foxmail使用技巧
  14. 自制H桥有刷电机驱动板
  15. 轮播图左右按钮会被选中的问题
  16. Cassandra CQL使用详解
  17. 用PS制作燃烧的火焰人物
  18. 用户运营 - 获客成本与提高转化率
  19. 从零开始安卓端相机功能开发(二)让我们来开发一个相机
  20. 奥丁神叛虚拟机 台服登录不上游戏 账号无法登录游戏解决办法

热门文章

  1. 吐槽 git 的一些愚蠢的接口设计: add/delete/remove/rm 选项随心所欲, 缺乏一致性
  2. 关于遥感图像的控制点片匹配算法的一点想法(一)
  3. ArcGIS Maritime Server 开发教程(四)Maritime Service 开发实践
  4. windows下cocos2d-x android打包
  5. CAsyncSocket使用总结
  6. 在Linux SSH全称,linux – 按名称获取打开的ssh连接列表
  7. python成员变量,成员函数的总结
  8. linux部署多个jar 会宕机_我常用的自动化部署技巧,贼好用,推荐给大家!
  9. IDEA 查看源码快捷键
  10. java有关问题,Java常见有关问题和解决方法