题目链接: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. 安装cloudermanager时出现org.spingframework.web.bind.***** host[] is not present at AnnotationMethodHandlerAdapter.java line 738 ****错误(图文详解)(博主推荐)

    不多说,直接上干货! 首先,这个问题,写给需要帮助的朋友们,本人在此,搜索资料近半天,才得以解决.看过国内和国外,资料甚少.特此,写此博客,为了弥补此错误解决的资料少的缘故! 问题详解  解决办法   ...

  2. mvp需要加上单利模式

    最大的中介者,需要设置成单利模式

  3. CSS3 -webkit-transform(元素变换)

    CSS3 -webkit-transform(元素变换)   -webkit-transform:none | 类型 类型:scale:缩放,1为原始大小.scale(x).正数放大,负数缩小.属性值 ...

  4. 强哥的分享--如何使用Spring Boot做一个邮件系统

    http://springboot.fun/ actuator是单机.集群环境下要使用Spring Boot Admin将各个单机的actuator集成越来 mvn clean package -Dm ...

  5. Unity手册-Unity概述

    Unity概述 Unity是一个强大引擎,他自带的多种工具可以满足你多种需求. 这种编辑器是直观的可定制的,让你的工作更大的自由. 原文 Unity is a powerful engine with ...

  6. frp使用总结

    笔者所知并成功实现内网穿透的方法: 花生壳 (需要花8块钱,使用花生壳给的二级域名,这里不做介绍) ngrok (免费,但是每次重启服务二级域名会变,付费的$5每月不会变) frp(开源免费,需要有自 ...

  7. iOS开发之GCD基础

    重新回顾.学习GCD.Block.先贴出一篇不错的讲解GCD基础使用的文章 原文地址:http://blog.csdn.net/aolan1108/article/details/17283415 做 ...

  8. 菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串

    一.概述 不同格式的JSON串传到后台来实现功能这个是我们经常要做的一件事,本篇博客就给大家介绍四种不同的JSON串传到后台后台如何用@RequestBody解析这些不同格式的JSON串的. 二.代码 ...

  9. App Inventor 网络资源及推荐书目

    Ai2服务器 官方服务器:http://ai2.appinventor.edu/ 官方备用服务器:(大陆可用):http://contest.appinventor.mit.edu/ 国内个人服务器: ...

  10. 最新一道面試題目,input: int[1,1,2,2,2,3,3,3],output [3,2,1],要求按照數字出現的次數從多到少排列元素。

    面試當時沒有及時答出來,感覺當時在面試官的提示下跑偏了.想用兩個數組來mapping key和value然後對等排序,但是因為面試官讓用Array.sort而沒想好有什麼好辦法,結果可想而知.但是題目 ...