blog题解鸽了许久了。。本来说好的切一题写一个题解的说


一个$1 \sim n$数列,已知每个数前面比他小的数个数,试确定该序列。


相当的一道水题。可以发现数列最后一个数是首先可以确定下来的。然后把这个数扔掉,看数列倒数第二个数前面有多少更小的其实就是目前剩下的数中的排名。相当于有一颗$n$个点的$1 \sim n$的平衡树,每次查一个排名,找出这个数,并删掉。但是并不想打平衡树。因为这题的操作只涉及简单的删除和查rank操作,可以考虑用BIT代替平衡树。具体是因为BIT本身就是具有树形二分结构的,画图应该可以感觉出来。$C[i]$表示值为i的数有几个(本题中最多$1$个,有些题还要离散化这个$i$),前缀和维护,查rank的时候不断查$C[x]$并二分下去,和平衡树就类似了。有点难讲,不过真的很好理解。

没了。

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define _dbg(x,y) cerr<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
using namespace std;
template<typename T>inline char MIN(T&A,T B){return A>B?A=B,:;}
template<typename T>inline char MAX(T&A,T B){return A<B?A=B,:;}
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline T read(T&x){
x=;char c;int f=;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=+;
int C[N],a[N];
int n;
#define lowbit(x) x&(-x)
inline void Update_add(int x,int k){for(;x<=n;x+=lowbit(x))C[x]+=k;}
inline int Query_sum(int x){int ret=;for(;x;x-=lowbit(x))ret+=C[x];return ret;}
inline int Query_kth(int x){
int i=<<__lg(n);
while((lowbit(i))^){
if(x<=C[i])i-=((lowbit(i))>>);
else x-=C[i],i+=((lowbit(i))>>);
while(i>n)i-=((lowbit(i))>>);
}
return C[i]^x?i+:i;
} int main(){//freopen(".in","r",stdin);freopen(".out","w",stdout);
read(n);a[]=;
for(register int i=;i<=n;++i)read(a[i]),++a[i];
for(register int i=;i<=n;++i)C[i]=lowbit(i);
for(register int i=n;i;--i){
int x=Query_kth(a[i]);
Update_add(x,-),a[i]=x;
}
for(register int i=;i<=n;++i)printf("%d\n",a[i]);
return ;
}

poj2182 Lost Cows[BIT二分]的更多相关文章

  1. POJ2182 Lost Cows 题解

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

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

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

  3. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  4. 疯牛-- Aggressive cows (二分)

    疯牛 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...

  5. POJ_2456 Aggressive cows 【二分求最大化最小值】

    题目: Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are l ...

  6. 【POJ - 2456】Aggressive cows(二分)

    Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x ...

  7. POJ 2456 Aggressive cows (二分)

    题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...

  8. poj 2456 Aggressive cows 贪心+二分

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25944   Accepted: 11982 ...

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

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

随机推荐

  1. MathType 6.0中MT Extra(TrueType)问题

    问题 MathType 6.0中MT Extra(TrueType)字体问题在打开MathType6.0时,有时会提示MathType需要安装一个较新版本的MT Extra(TrueType)字体,这 ...

  2. PJzhang:ping命令的基本用法

    猫宁!!! 参考链接:https://www.cnblogs.com/diantong/p/9626751.html http://aiezu.com/article/linux_ping_comma ...

  3. 统计学习方法 | 第1章 统计学习方法概论 | Scipy中的Leastsq()

    Scipy是一个用于数学.科学.工程领域的常用软件包,可以处理插值.积分.优化.图像处理.常微分方程数值解的求解.信号处理等问题.它用于有效计算Numpy矩阵,使Numpy和Scipy协同工作,高效解 ...

  4. java 常用jar包方法

    1.Mapper3   常用接口 https://blog.csdn.net/fangwenzheng88/article/details/78713091 2.分页 pageHelper

  5. 分享个昨天学的,sqlserver查表的所有列的列名,类型,长度的sql

     select a.name as 列名, a.length as 长度,b.name as 类型 from syscolumns a left join systypes b on a.xtype  ...

  6. [转帖]Unix版权史

    Unix版权史   http://www.ruanyifeng.com/blog/2010/03/unix_copyright_history.html 阮一峰的blog 里面写到了 system V ...

  7. Spark中分布式使用HanLP(1.7.0)分词示例

    HanLP分词,如README中所说,如果没有特殊需求,可以通过maven配置,如果要添加自定义词典,需要下载“依赖jar包和用户字典". 分享某大神的示例经验: 是直接"java ...

  8. # 江西ccpc省赛-waves-(DP做法)

    江西ccpc省赛-waves-(DP做法) 题链:http://acm.hdu.edu.cn/showproblem.php?pid=6570 题意:给你长度为N,1≤N≤100000的一个数组,其中 ...

  9. python协程gevent案例:爬取斗鱼美女图片

    分析 分析网站寻找需要的网址 用谷歌浏览器摁F12打开开发者工具,然后打开斗鱼颜值分类的页面,如图: 在里面的请求中,最后发现它是以ajax加载的数据,数据格式为json,如图: 圈住的部分是我们需要 ...

  10. Tomcat Connector(BIO, NIO, APR)三种运行模式(转)

    Tomcat支持三种接收请求的处理方式:BIO.NIO.APR . BIO 阻塞式I/O操作即使用的是传统 I/O操作,Tomcat7以下版本默认情况下是以BIO模式运行的,由于每个请求都要创建一个线 ...