Lost Cows
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10609   Accepted: 6797

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
题意:有一个序列a:1,2,…,N(2 <= N <= 8,000). 现该序列为乱序,已知第i个数前面的有a[i]个小于它的数。求出该序列的排列方式。  
暴力求解:O(n*n)
 #include <iostream>
#include <cstdio>
#include <set>
using namespace std;
int num[],a[];
int main()
{
int n;
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int k=;
set<int> s;
set<int>::iterator p;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<n;i++)
s.insert(i+);
for(i=n;i>=;i--)
{
p=s.begin();
while(a[i]--) p++;
num[i]=*p;
s.erase(*p);
}
num[i]=*s.begin();
for(i=;i<=n;i++)
printf("%d\n",num[i]);
}
}

 树状数组 O(nlogn)

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int a[],bit[];
int num[];
int lowbit(int i)
{
return i&-i;
}
void add(int i,int s)
{
while(i<=n)
{
bit[i]+=s;
i=i+lowbit(i);
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i=i-lowbit(i);
}
return s;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int s=;
memset(bit,,sizeof(bit));
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
add(i,);
for(i=n;i>=;i--)
{
int l=,r=n,mid;
int tem=a[i]+,p;
while(l<=r)
{
int mid=(r+l)/;
if(sum(mid)>=tem)
{
p=mid;
r=mid-;
}
else
{
l=mid+;
}
}
s+=p;
num[i]=p;
add(p,-);
}
num[]=(n+)*n/-s;
for(i=;i<=n;i++)
printf("%d\n",num[i]);
}
}
												

Lost Cows(BIT poj2182)的更多相关文章

  1. 【POJ2182】Lost Cows

    [POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...

  2. POJ2182 Lost Cows 题解

    POJ2182 Lost Cows 题解 描述 有\(N\)(\(2 <= N <= 8,000\))头母牛,每头母牛有自己的独一无二编号(\(1..N\)). 现在\(N\)头母牛站成一 ...

  3. [poj2182] Lost Cows (线段树)

    线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...

  4. Lost Cows(线段树 POJ2182)

    Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10354 Accepted: 6631 Descriptio ...

  5. POJ2182 Lost Cows

    题意 Language:Default Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13448 Accep ...

  6. [POJ2182]Lost Cows(树状数组,二分)

    题目链接:http://poj.org/problem?id=2182 题意:给定1~n个数和n个位置,已知ai表示第i个位置前有ai个数比当前位置的数小,求这个排列. 和刚才YY的题意蛮接近的,用树 ...

  7. hdu 2711&&poj2182 Lost Cows (线段树)

    从后往前查第一个为0的奶牛肯定应该排在第一个.每次从后往前找到第一个为0的数,这个数应该插在第j位.查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束. 至于这种查找修改的操作,再没 ...

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

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

  9. Poj2182 Lost Cows(玄学算法)

    题面 Poj 题解 不难发现最后一位就是\(pre[n]+1\),然后消除这个位置对其他位置的贡献,从左到右扫一遍,必定有至少一个位置可以得出,循环这个过程,\(O(n^2)\)出解. #includ ...

随机推荐

  1. 正式学习react(二)

    今天把上一篇还没学习完的 webpack部分学习完: 之前有说过关于css的webpack使用.我们讲了 ExtractTextPlugin 来单独管理css讲了module.loaders下关于 c ...

  2. 提交 应用ID 证书

    https://developer.apple.com/account/ios/profile/profileCreate.action?formID=960914622

  3. Shell脚本调试技术

    http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/ 一. 前言 shell编程在unix/linux世界中使用得非常广泛,熟练掌握 ...

  4. PowerShell常用的属性

    get-location | get-member  -membertype  property -------获取对象的属性---------- 获取对象特定的成员, 湖区.Net Framwork ...

  5. windows下删除Linux

    在Windows下删除Linux系统的方法(附修复MBR的工具下载) 我的电脑安装了双系统,Windows和Linux,不过由于Linux在最近一段时间内不会使用,所以我打算删除Linux.   删除 ...

  6. java查找重复类/jar包/普通文件

    开发web应用时,有时更新了类却没有生效,其实是因为jboss/tomcat中其他发布包下有同名类(包括全路径都相同). 于是萌发了做个程序来检查指定目录是否存在重复类(通过asm从类文件中取类的全路 ...

  7. IO-APIC

    MP-BIOS bug :8254 timer not connected to IO-APIC解决办法 云计算中在基于一个template image instance vmServer时出现上述的 ...

  8. <转载>C++命名空间

    原文http://blog.csdn.net/liufei_learning/article/details/5391334 一. 为什么需要命名空间(问题提出) 命名空间是ANSIC++引入的可以由 ...

  9. iOS程序员对算法的要求

    算法和数据结构(鉴于二者的关联,以下统称算法),对于程序员的重要性一直是个具有争议性的话题.有一些程序员内心对算法有着天然的排斥,面试当中一旦考察算法知识,会被不少程序员吐槽,但有部分公司又一直在坚持 ...

  10. php字符串标点等字符截取不乱吗 封装方法

    方法一: /**   +----------------------------------------------------------  * 功能:字符串截取指定长度  * leo.li hen ...