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 ...
随机推荐
- BasicExcel说明文档
BasicExcel说明文档 BasicExcel原始链接:http://www.codeproject.com/Articles/13852/BasicExcel-A-Class-to-Read-a ...
- 本地windows主机无法访问虚拟机里主机解决办法
一:设置虚拟机里IP,使其与本地计算机IP在同一网段 本地计算机网络IP设置如下: 虚拟机里ip为192.168.1.9 设置IP步骤请参考:Linux里如何设置IP(RED HAT) 二:将虚拟机网 ...
- Linux iostat监测IO状态(转)
Linux iostat监测IO状态 2010-03-1 | 13:13分类:Linux,技术细节 | 标签:Linux | 53,945 views Linux系统出现了性能问题,一般我 ...
- bzoj 1912 巡逻(树直径)
Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...
- Windows文件居然有解锁一说,并且还会引起SignTool Error,真是昏倒!
I'm running Windows 7 and when I try to run a batch file, it says, "The publisher could not be ...
- Box.net获5000万美元投资(SAAS博士)
9月29日,据国外媒体报道,“云”存储服务商Box.net已经从Salesforce等投资者获得新一轮融资5000万美元. Box.net过去的投资者也参与了新一轮的融资.最终,在CRM巨人Sales ...
- Resharper 7小技巧系列:导航、书签、和最近编辑
原文 http://www.cnblogs.com/feichexia/archive/2012/09/21/resharper_tricks_part1.html 先上一张花了一个多小时画的Resh ...
- sort详解
一. 简介 sort命令是帮我们依据不同的数据类型进行排序. 二. 语法 sort [-bcfMnrtk][源文件][-o 输出文件] 补充说明:sort可针对文本文件的内容,以行为单位来排序(默认为 ...
- 7款纯CSS3实现的炫酷动画应用|慕课网只学有用的!
关于我们 | 时尚廊 ♦ 时尚廊,中国大陆地区首家以"Lounge"为概念的艺文空间 ♦ 7款纯CSS3实现的炫酷动画应用|慕课网只学有用的! 7款纯CSS3实现的炫酷动画应用
- [React] Styling a React button component with Radium
React's inline styles allow components to stand on their own by not requiring any external CSS. Howe ...