线段树/树状数组 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 ...
随机推荐
- 使用GitLab CI + Capistrano部署CakePHP应用程序
使用GitLab CI + Capistrano部署CakePHP应用程序 摘要:本文描述了如使用GitLab CI + Capistrano部署CakePHP应用程序. 目录 1. 问题2. 解决方 ...
- C/C++语言中的位运算
在计算机程序中,数据的位是可以操作的最小数据单位,理论上可以用“位运算”来完成所有的运算和操作. 一般的位操作是用来控制硬件的,或者做数据变换使用,但是,灵活的位操作可以有效地提高程序运行的效率.C语 ...
- 细谈SetButtonInfo函数及其用途
SetButtonInfo用于设置某个按钮,它的接口定义如下: 下面是它的几个接口函数说明: void CToolBar::SetButtonInfo(int nIndex, UINT nID, UI ...
- 解决gradle多模块依赖在Idea中能运行,gradle build失败的问题。
最近需要初始化一个SpringBoot新项目遇到一个问题就是:项目中有多个子模块,使用gradle依赖管理成功. 项目结构如下: project --module1 --module2我的mo ...
- x$kccle视图深入剖析
今天是2014-05-27,实在无聊顺便研究一下x$kccle的内容吧.例如以下所有是自己分析和实验结果,真实可靠. 1.怎样获得v$log的底层表?我们能够通过autotrace完毕查看如 ...
- 将异常(getStackTrace)转化成String
方法一: private static String getStackMsg(Exception e) { StringBuffer sb = new StringBuffer(); StackTra ...
- HDU2476 String painter —— 区间DP
题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others) Me ...
- 查看识别hadoop是32位还是64位
问题导读: 1.从哪些地方可以识别hadoop是32位还是64位?2.hadoop本地库在什么位置? 来源:about云 本文链接:http://www.aboutyun.com/thread-127 ...
- Ubuntu 12.04 nethogs 流量监控查看
/*************************************************************** * Ubuntu 12.04 流量监控查看 * 说明: * 今天打算从 ...
- 纯CSS画WP8界面
我的手机是诺基亚920,13年4月份买的.工作之余,就想用css做一下WP8的界面效果,如上图所示.不做不知道,一做还挺难的.尤其是画那个QQ 的企鹅图标,太难画了.怎么画都不像. <!doct ...