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 ...
随机推荐
- Android原理View、ViewGroup
Android的UI界面都是由View和ViewGroup及其派生类组合而成的.其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的容器,其本身也是从View派生出来的.Andro ...
- adb调试实用命令
获取设备IMEI: adb shell dumpsys iphonesubinfo 文件在设备和PC端的操作:adb push [PC端源文件路径] [设备的目的文件路径] 例如:adb push C ...
- BZOJ 3550 Vacation
http://www.lydsy.com/JudgeOnline/problem.php?id=3550 题意:有3N个数,你需要选出一些数,首先保证任意长度为N的区间中选出的数的个数<=K个, ...
- LeetCode_Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...
- C51工具是怎么进行覆盖分析的
C51工具针对8051微控制器的有限存储器资源进行了优化设计. 为了最有效地利用存储器,根据一个很容易解释的方法,自动变量和函数参数在存储器中均进行覆盖处理. 首先,连接器根据源程序生成调用树.例如: ...
- eclipse打开文件位置Open Explorer 插件
,OpenExplorer插件可以满足这个功能,可以到https://github.com/samsonw/OpenExplorer/downloads下载最新版本,将jar包放到eclipse的pl ...
- php安装pear、pecl
安装pear.pecl特别简单,只需要两步. wget http://pear.php.net/go-pear.phar php go-pear.phar [root@localhost bin]# ...
- Thinkpad E430+CentOS 6.4+ linux-3.10.12内核网卡驱动(无线+有线)配置
配置并编译安装内核模块和内核后,解压附件 firmware.tar.bz2,拷贝其中的rtlwifi文件夹到/lib/firmware下,然后 执行装载内核模块命令: sudo modprobe rt ...
- SQL查询最近三个月的数据(查询最近几天,几年等等)
定义和用法 :: 天,这样就可以找到付款日期. 我们使用如下 ,OrderDate) AS OrderPayDate FROM Orders 结果: OrderId OrderPayD ...
- 使用WCF实现SOA面向服务编程—— 架构设计
原文地址:http://www.cnblogs.com/leslies2/archive/2011/03/29/1997889.html SOA本身就是一种面向企业级服务的系统架构,简单来说,SOA就 ...