poj2182 Lost Cows(经典单点更新)
来源:互联网 发布:java形参 编辑:程序博客网 时间:2024/05/22 15:19
http://poj.org/problem?id=2182
题意:每个牛有1~n中的一个序号,给你从第二个牛开始每个牛以前比其序号小的牛的个数,求最后每个牛的序号。
思路:这题虽然和poj2828相像,但是第一眼不容易看出来,看不出来就没法使线段树。
poj2828是已知每个数的序号求位置,特点是越靠后选的位置越准确;
poj2182是已知每个数的位置求序号,特点是越靠后选的序号越准确。
线段树处理的就是准确的先更新,不怎么准确的钻空子,前后两道题用线段树处理的分别是位置和序号。
另外上一道题是已知序号,这题已知的是位置减一(前面有多少个),所以在update传递参数额时候要对bef加一。
#include <stdio.h>#include <algorithm>#include <string.h>#include <math.h>using namespace std;typedef long long ll;const int N = 8005;struct line{ int l, r; int len;}tree[N*4];int bef[N], ans[N];void build(int i, int l, int r){ tree[i].l = l; tree[i].r = r; tree[i].len = r-l+1; if(tree[i].l == tree[i].r)//到了子节点 { return;//别忘了返回。。 } int mid = (tree[i].l+tree[i].r)>>1; build(i*2, l, mid); build(i*2+1, mid+1, r);}void update(int i, int pos, int num)//单点更新,更新的是输出的队列数组{ tree[i].len--; if(tree[i].l==tree[i].r)//到了的叶子节点不一定就是期望的节点 { ans[num] = tree[i].l; return; } if(pos <= tree[i*2].len) update(i*2, pos, num);//长度从1开始,pos从1开始,直接比较 else update(i*2+1, pos-tree[i*2].len, num);}int main(){ // freopen("in.txt", "r", stdin); int n; while(~scanf("%d", &n)) { build(1, 1, n); bef[1] = 0; for(int i = 2; i <= n; i++) scanf("%d", &bef[i]); for(int i = n; i > 0; i--) update(1, bef[i]+1, i); for(int i = 1; i <= n; i++) printf("%d\n", ans[i]); } return 0;}
0 0
- poj2182 Lost Cows(经典单点更新)
- POJ2182 Lost Cows 线段树单点更新
- POJ2182:Lost Cows(单点)
- hdu 2711 Lost Cows(单点更新)
- poj2182 lost cows题解(线段树)
- POJ2182 HDU2711 Lost Cows
- poj2182 Lost Cows
- poj2182--Lost Cows
- poj2182--Lost Cows
- POJ2182 Lost Cows
- poj2182 Lost Cows
- poj2182-Lost Cows
- poj2182 Lost Cows
- poj2182 lost cows
- Lost Cows poj2182 线段树
- poj2182 Lost Cows (线段树)
- Lost Cows(线段树 POJ2182)
- poj2182 Lost Cows(线段树)
- Max retries exceeded with url Error
- LeetCode 404. Sum of Left Leaves 题解
- Alexnet输入大小调整的经验
- 数据结构——线段树
- 152. Maximum Product Subarray Medium
- poj2182 Lost Cows(经典单点更新)
- C++第2次实验:简单计算问题体验
- solr入门--solrJ搜索引擎入门
- web前端学习之路(1)Javascript DOM编程艺术总结
- Android添加横线和竖线分割界面
- C语言函数嵌套定义问题
- 创建守护进程为什么fork两次
- android stdio部局中遇到的几个问题
- c++ const 用法