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. Scala学习笔记--List、ListBuffer

    ListBuffer(列表缓冲) ListBuffer类位于  scala.collection.mutable.ListBuffer val buf = new ListBuffer[Int] va ...

  2. AsEnumrable和AsQueryable的区别

    http://www.cnblogs.com/jianglan/archive/2011/08/11/2135023.html 在写LINQ语句的时候,往往会看到.AsEnumerable() 和 . ...

  3. tableViewCell 的删除按钮

    - (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIn ...

  4. java读取property文件

    property文件中: url = jdbc:mysql://localhost:3306/resume   user= root   pwd = 123 java代码读取:       packa ...

  5. 省去在线安装 直接下载Chrome官方离线安装包

    首页>软件之家>便捷上网 省去在线安装 直接下载Chrome官方离线安装包 2013-10-12 23:22:02来源:IT之家 原创作者:阿象责编:阿象人气:54487 评论:19 谷歌 ...

  6. 匹配“is outside location”

    <pre name="code" class="html">is outside location 怎么匹配? . 匹配除换行外的所有单个字符,通常 ...

  7. FFmpeg缩放swscale详解 <转>

    利用ffmpeg进行图像数据格式的转换以及图片的缩放应用中,主要用到了swscale.h文件中的三个函数,分别是: struct SwsContext *sws_getContext(int srcW ...

  8. 使用Open Flash Chart(OFC)制作图表(Struts2处理)

    Java开源项目中制作图表比较出色的就是JFreeChart了,相信大家都听说过,它不仅可以做出非常漂亮的柱状图,饼状图,折线图基本图形之外,还能制作甘特图,仪表盘等图表.在Web应用中可以为项目增色 ...

  9. web前端之 HTML介绍

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...

  10. python之路-模块 splinter

    Splinter介绍 Splinter is an open source tool for testing web applications using Python. It lets you au ...