题目传送门

题意: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的更多相关文章

  1. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  2. POJ 2182 Lost Cows (树状数组 && 二分查找)

    题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...

  3. 树状数组 || POJ 2352 Stars

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  4. 树状数组 || POJ 3321 Apple Tree

    一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...

  5. poj 2182 Lost Cows(段树精英赛的冠军)

    主题链接:http://poj.org/problem? id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. POJ 2182 Lost Cows (求序列第k大)

    题解 二分+树状数组 显然最和一个数的值就是rank 那么其它数有什么规律? 从后往前匹配rank,我们可以发现第i个数的rank为还没有匹配的rank第(a[i]+1)大的数 这可以用 树状数组+二 ...

  7. POJ 2182 Lost Cows (线段树)

    题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...

  8. LCA+树状数组 POJ 2763 Housewife Wind

    题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...

  9. 树状数组 POJ 2481 Cows

    题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...

随机推荐

  1. HttpClient 认证

    第四章 HTTP认证 HttpClient提供对由HTTP标准规范定义的认证模式的完全支持.HttpClient的认证框架可以扩展支持非标准的认证模式,比如NTLM和SPNEGO. 4.1 用户凭证 ...

  2. putty software caused connection abort

    错误现象:在非常短的时间内就失去连接.并报"Software caused connection abort" 解决的方法:首先得排除是网络不是不通畅.假设在局域网中要确定IP没有 ...

  3. 文件宝iOS/iPhone/iPad客户端简介

    App Store地址:https://itunes.apple.com/cn/app/id1023365565?mt=8 文件宝-装机必备的文件管家,专业的rar-zip 解压工具,局域网看片神器, ...

  4. 2016/06/10 日历插件 Datepicker

    显示效果: <!doctype html> <html lang="en"> <head> <meta charset="utf ...

  5. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

  6. PrintWrite

    向文本输出流打印对象的格式化表示形式.此类实现在 PrintStream 中的所有 print 方法.它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入. 与 Print ...

  7. Easy smart REST with kbmMW

    使用新版kbmMW开发的 smart service,也可以轻松的发布为通过REST来调用的功能.   一个 kbmMW smart service象下面这样实现,就可以使用REST来访问:   ty ...

  8. linux下lk和kernel层通信方式[2]

    U-Boot与Linux内核的交互 说明:本文所使用的U-Boot的版本是1.1.6,平台是S3C2440. 目录 一.简介 1.1标记列表二.设置标记存放的地址 2.1相关的结构体定义 2.2标记存 ...

  9. Servlet的HelloWorld

    设置好TOMCAT环境变量(如何设置?)后在命令行输入startup可以启动Tomcat,输入shutdown可以关闭Tomcat. /WEB-INF/web.xml是称为部署描述器的配置文件,Jav ...

  10. SPOJ:Ada and Orange Tree (LCA+Bitset)

    Ada the Ladybug lives near an orange tree. Instead of reading books, she investigates the oranges. T ...