题目链接:http://poj.org/problem?id=2182

Lost Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12736   Accepted: 8168

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

题意概括:

有 N 头牛跑散了位置,现在我们可以依次知道他们前面有几头原来编号比他们原来编号小的牛,求他们原来的编号是多少。

解题思路:

①可以直接逆推暴力,因为草稿模拟一下可以知道,我们可以求最后一头牛他的编号是多少(即 F[ N ] + 1,F[ N ] 为他前面有多少头序号比他小的牛,并且他后面没有牛了,所以可以从最小的1开始逆推)。推出了最后一个,接下来可以推出倒数第二个,这里需要用到一个 visi [ x ] 来标记 编号X 是否已经被用;已经被用的编号可以丢掉不考虑了。推倒数第二个的推法跟推倒数第一个的一样,只不过有些比它小的编号已经被用掉了,只需要考虑那些还没用的编号,从这些编号里的最小编号开始往前逆推。所以双重循环可以搞定全部的牛牛了。

②树状数组+二分

树状数组的 SUM( X ) 用于记录 编号X 后面满足小于等于 X 的已经用掉了的编号的个数;F[ i ] 就是题目给出的 第 i 个牛 前面比 第i 个牛的编号小的编号的个数; 我们需要二分的就是 X,判断 X是不是当前第 i 头牛的编号。

如果 (X-1)-SUM( X - 1) == F[ i ] (即 编号X 前面剩下的小于 X 的编号的数量恰好等于 第 i 头牛编号的条件, 则 X 就是 第 i 头牛的编号啦)

如果(X-1)-SUM( X - 1)  > F[ i ]   (说明编号偏大咯)

如果(X-1)-SUM( X - 1) < F[ i ]      (说明编号偏小咯)

AC code:

 ///树状数组+二分
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#define ll long long int
#define INf 0x3f3f3f3f
using namespace std; const int MAXN = 8e3+;
int f[MAXN];
int b[MAXN];
int num[MAXN];
int N;
int lowbit(int x)
{
return x&(-x);
}
void add(int x, int value)
{
for(int i = x; i <= N; i+=lowbit(i))
b[i]+=value;
}
int sum(int x) ///后面比x小的数的个数
{
int res = ;
for(int i = x; i > ; i-=lowbit(i))
res+=b[i];
return res;
}
int main()
{
scanf("%d", &N);
num[] = ;
for(int i = ; i <= N; i++) ///前面比编号为i的数小的数的个数
scanf("%d", &f[i]); num[N] = f[N]+;
add(num[N], );
for(int i = N-; i > ; i--)
{
int l = , r = N;
while(r > l)
{
int mid = (l+r)>>;
if(mid--sum(mid) >= f[i])
{
r = mid;
}
else
{
l = mid+;
}
}
num[i] = l;
add(num[i], );
}
for(int i = ; i <= N; i++)
printf("%d\n", num[i]);
return ; }

POJ 2182 Lost Cows 【树状数组+二分】的更多相关文章

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

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

  2. POJ 2481:Cows 树状数组

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14906   Accepted: 4941 Description ...

  3. 【POJ2182】Lost Cows 树状数组+二分

    题中给出了第 i 头牛前面有多少比它矮,如果正着分析比较难找到规律.因此,采用倒着分析的方法(最后一头牛的rank可以直接得出),对于第 i 头牛来说,它的rank值为没有被占用的rank集合中的第A ...

  4. POJ 2892 Tunnel Warfare(树状数组+二分)

    题目链接 二分求上界和下界,树状数组.注意特殊情况. #include <cstring> #include <cstdio> #include <string> ...

  5. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  6. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

  7. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  8. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  9. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

随机推荐

  1. 小程序 给最外层view设置百分之百高度不起作用

    <view class="content"> <view class="today"> <view class="inf ...

  2. fastclick.js源码解读分析

    阅读优秀的js插件和库源码,可以加深我们对web开发的理解和提高js能力,本人能力有限,只能粗略读懂一些小型插件,这里带来对fastclick源码的解读,望各位大神不吝指教~! fastclick诞生 ...

  3. 2、java内存间交互操作

    关于主内存与工作内存之间具体的交互协议,即一个变量如何从主内存拷贝到工作内存,如何从工作内存同步回主内存之类的实现细节,java内存模型中定义了8种操作来完成,虚拟机实现时必须保证这8种操作都是原子的 ...

  4. C++11并发编程:async,future,packaged_task,promise

    一:async std::async:用于创建异步任务,可以代替创建线程,函数原型:async(std::launch::async | std::launch::deferred, f, args. ...

  5. Ubuntu以及CentOS7修改ssh端口号详细步骤

    1.Ubuntu修改ssh端口号步骤: 1.修改sshd.config文件.执行vim etc/ssh/sshd_config.增加上我们需要增加的ssh的端口号.图例增加了5309的端口号. ESC ...

  6. 关于Json字符串"反序列化Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path..."

    描述的很清楚就是说给它的不是一个对象,而是一个数组,所以他在建议你用JArray去解析,但是你明明就是给它的一个对象,并不是一个数组 这是我下意识的去把我的json字符串中的"[ ]&quo ...

  7. elasticSearch2.4与grafana,stagemonitor集成做监控需要执行的mapping

    PUT /_template/stagemonitor-metrics-{ "template": "stagemonitor-metrics-*", &quo ...

  8. 表单提交前的confirm验证提示

    今天要做一个修改提交前的提示,点击修改按钮进行提示,然后根据confirm的结果来决定是否提交;发现平时很常见的一个功能,自己不会.所以只能去晚上找资料了; 举例如下: <form action ...

  9. Oracle 过程中变量赋值

    create or replace function get_sal1(id employees.employee_id%type) return number is sal employees.sa ...

  10. markdown语法简单总结

    最常用的十个MarkDown语法总结: 标题:只要在这段文字前加 # 号即可 # 一级标题 最大 ## 二级标题 ### 三级标题 无序列表:在文字前加上 - 或 * 有序列表:在文字前加1. 2.  ...