线段树/树状数组 POJ 2182 Lost Cows
题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号
分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号:为1,倒数第二头牛的编号为除去最后一头牛的编号后的第a[i-1] + 1编号:为3,其他的类推,所以可以维护之前已经选掉的编号,求第k大的数字,sum[rt] 表示该区间已经被选掉的点的个数。另外树状数组也可以做,只不过用二分优化查找第k大的位置。
收获:逆向思维,求动态第K大
代码(线段树):
/************************************************
* Author :Running_Time
* Created Time :2015/9/9 星期三 17:01:08
* File Name :P.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 8e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct ST {
int sum[N<<2];
void build(int l, int r, int rt) {
sum[rt] = 0;
if (l == r) return ;
int mid = (l + r) >> 1;
build (lson);
build (rson);
}
int query(int p, int l, int r, int rt) {
sum[rt]++;
if (l == r) return l;
int mid = (l + r) >> 1;
int tmp = mid - l + 1 - sum[rt<<1];
if (tmp >= p) return query (p, lson);
else {
return query (p - tmp, rson);
}
}
}st;
int a[N], ans[N]; int main(void) {
int n;
while (scanf ("%d", &n) == 1) {
a[1] = 0;
for (int i=2; i<=n; ++i) {
scanf ("%d", &a[i]);
}
st.build (1, n, 1);
for (int i=n; i>=1; --i) {
ans[i] = st.query (a[i] + 1, 1, n, 1);
}
for (int i=1; i<=n; ++i) {
printf ("%d\n", ans[i]);
}
} return 0;
}
代码(树状数组):
/************************************************
* Author :Running_Time
* Created Time :2015/9/9 星期三 18:19:28
* File Name :P_BIT.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 8e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n;
struct BIT {
int c[N];
void init(void) {
memset (c, 0, sizeof (c));
}
void updata(int i, int x) {
while (i <= n) {
c[i] += x; i += i & (-i);
}
}
int query(int i) {
int ret = 0;
while (i) {
ret += c[i]; i -= i & (-i);
}
return ret;
}
int bsearch(int l, int r, int k) {
while (l <= r) {
int mid = (l + r) >> 1;
if (mid - query (mid) >= k) r = mid - 1;
else l = mid + 1;
}
return l;
}
}bit;
int a[N], ans[N]; int main(void) {
while (scanf ("%d", &n) == 1) {
a[1] = 0;
for (int i=2; i<=n; ++i) scanf ("%d", &a[i]);
bit.init ();
for (int i=n; i>=1; --i) {
ans[i] = bit.bsearch (1, n, a[i] + 1);
bit.updata (ans[i], 1);
}
for (int i=1; i<=n; ++i) {
printf ("%d\n", ans[i]);
}
} return 0;
}
线段树/树状数组 POJ 2182 Lost Cows的更多相关文章
- POJ 2182 Lost Cows 【树状数组+二分】
题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2182 Lost Cows (树状数组 && 二分查找)
题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...
- 树状数组 || POJ 2352 Stars
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...
- 树状数组 || POJ 3321 Apple Tree
一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...
- poj 2182 Lost Cows(段树精英赛的冠军)
主题链接:http://poj.org/problem? id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2182 Lost Cows (求序列第k大)
题解 二分+树状数组 显然最和一个数的值就是rank 那么其它数有什么规律? 从后往前匹配rank,我们可以发现第i个数的rank为还没有匹配的rank第(a[i]+1)大的数 这可以用 树状数组+二 ...
- POJ 2182 Lost Cows (线段树)
题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...
- LCA+树状数组 POJ 2763 Housewife Wind
题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
随机推荐
- 用 querySelectorAll 来查询 DOM 节点
用 querySelectorAll 来查询 DOM 节点 Document.querySelectorAll - Web API 接口 | MDN https://developer.mozilla ...
- yum报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:
原因:学python的时候,把centos7自带的python2.7改成了python3.6.2.而yum使用的是python2,所以会出现yum报错. 解决方法: 在文件/usr/bin/yum./ ...
- android adapter公共写法
在开发过程中,会写很多的adapter类,其中很多公共的部分,不需要每次都去书写,可以为开发者省下很多时间 提取一个ListViewAdapter public abstract class List ...
- 【腾讯bugly干货分享】精神哥手把手教你怎样智斗ANR
上帝说要有ANR,于是Bugly就有了ANR上报.那么ANR究竟是什么? 近期非常多童鞋问起精神哥ANR的问题,那么这次就来聊一下,鸡爪怎么泡才好吃.噢不,是怎样高速定位ANR. ANR是什么 简单说 ...
- iOS开发中对于一些常用的相对路径(持续更新)
1.iOS开发的证书的描述文件放置地点 ~/Library/MobileDevice/Provisioning Profiles 2.$(SRCROOT)代表的是这个项目文件夹所在的位置 $(PR ...
- Parallels Desktop 设置win网络连接
目的: 1 虚拟机中的win系统技能访问外网 2 可以和Mac系统互联 首先来实现1,很简单: 打开控制中心对应系统的设置 选择[硬件]->[网络] 源:设置共享网络 到此就达到1目的了: 现在 ...
- POJ1077 Eight —— A*算法
主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 关于A*算法:g(n)表示从起点到任意节点n的路径花费,h(n)表示从节点n到目标节点路径花费 ...
- MySQL的IFNULL简单使用说明
MySQL IFNULL函数简介 MySQL IFNULL函数是MySQL控制流函数之一,它接受两个参数,如果不是NULL,则返回第一个参数. 否则,IFNULL函数返回第二个参数. 两个参数可以是文 ...
- Python学习笔记_Mysql数据库、Excel
一.操作mysql数据库 import pymysql # 1.连上数据库:账号,密码,ip,端口号,数据库 # 2.建立游标(去数据库拿东西的工人) # 3.执行sql # 4.获取结果 # 5.关 ...
- Java中的switch语句
switch可以替代if..else..,另外据说switch采用二分搜索,效率会更高一点. switch(type) { case 1 : type_name="INCOMING" ...