Lost Cows(BIT poj2182)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10609 | Accepted: 6797 |
Description
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
* 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
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)的更多相关文章
- 【POJ2182】Lost Cows
[POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...
- POJ2182 Lost Cows 题解
POJ2182 Lost Cows 题解 描述 有\(N\)(\(2 <= N <= 8,000\))头母牛,每头母牛有自己的独一无二编号(\(1..N\)). 现在\(N\)头母牛站成一 ...
- [poj2182] Lost Cows (线段树)
线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...
- Lost Cows(线段树 POJ2182)
Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10354 Accepted: 6631 Descriptio ...
- POJ2182 Lost Cows
题意 Language:Default Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13448 Accep ...
- [POJ2182]Lost Cows(树状数组,二分)
题目链接:http://poj.org/problem?id=2182 题意:给定1~n个数和n个位置,已知ai表示第i个位置前有ai个数比当前位置的数小,求这个排列. 和刚才YY的题意蛮接近的,用树 ...
- hdu 2711&&poj2182 Lost Cows (线段树)
从后往前查第一个为0的奶牛肯定应该排在第一个.每次从后往前找到第一个为0的数,这个数应该插在第j位.查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束. 至于这种查找修改的操作,再没 ...
- 【POJ2182】Lost Cows 树状数组+二分
题中给出了第 i 头牛前面有多少比它矮,如果正着分析比较难找到规律.因此,采用倒着分析的方法(最后一头牛的rank可以直接得出),对于第 i 头牛来说,它的rank值为没有被占用的rank集合中的第A ...
- Poj2182 Lost Cows(玄学算法)
题面 Poj 题解 不难发现最后一位就是\(pre[n]+1\),然后消除这个位置对其他位置的贡献,从左到右扫一遍,必定有至少一个位置可以得出,循环这个过程,\(O(n^2)\)出解. #includ ...
随机推荐
- jquery-1.10.2 获取checkbox的checked属性总是undefined
项目中用的jquery-1.10.2 需要检测一个checkbox的选中状态,想当然的用 .attr("checked") ,结果发现,无论是否选中,这个值都是 undefined ...
- Qt实现基于G.729A(G729A)的语音聊天
一.G.729协议简介G.729协议是由ITU-T的第15研究小组提出的,并在1996年3月通过的8Kbps的语音编码协议.G.729系列主要有以下几种:G.729—最基本的G.729标准协议,原始版 ...
- PHPDocumentor安装与使用
phpDocuemtor官网:http://www.phpdoc.org/ 通过pear安装,进入dos的php目录,输入pear install -a PhpDocumentor.如果想使用web接 ...
- bzoj1655 [Usaco2006 Jan] Dollar Dayz 奶牛商店
Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...
- Integer to English Words 解答
Question Convert a non-negative integer to its english words representation. Given input is guarante ...
- Calculate Number Of Islands And Lakes 解答
Question 1 1 1 1 1 01 0 1 0 0 11 0 1 0 0 11 1 0 1 1 1 1 is earth, 0 is water. i) count the number of ...
- JS中简单的this学习
我在学习JS初期,在使用this的时候经常出现问题,当然就是在现在,也有一些场景不能很好的明白this到底指代的是什么?看下面一个例子: var x = 10; var foo = { x ...
- PyQt4中无边框窗口的移动(拖动)
import sys from PyQt4.QtGui import * from PyQt4.Qt import * from PyQt4.QtCore import * class AboutUs ...
- HDU4907小技巧
原题http://acm.hdu.edu.cn/showproblem.php?pid=4907 Task schedule Time Limit: 2000/1000 MS (Java/Others ...
- Hacker(13)----搜集目标计算机的重要信息
扫描与嗅探攻防 黑客若要发起入侵攻击,他需要做好充分的准备的工作,首先通过嗅探和扫描等操作来搜索信息,从而确定目标计算机,以便准确发出攻击.嗅探和扫描操作可以利用专业的软件工具实现,如X-Scan.S ...