poj2182 Lost Cows[BIT二分]
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二分]的更多相关文章
- POJ2182 Lost Cows 题解
POJ2182 Lost Cows 题解 描述 有\(N\)(\(2 <= N <= 8,000\))头母牛,每头母牛有自己的独一无二编号(\(1..N\)). 现在\(N\)头母牛站成一 ...
- [POJ2182]Lost Cows(树状数组,二分)
题目链接:http://poj.org/problem?id=2182 题意:给定1~n个数和n个位置,已知ai表示第i个位置前有ai个数比当前位置的数小,求这个排列. 和刚才YY的题意蛮接近的,用树 ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- 疯牛-- Aggressive cows (二分)
疯牛 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小 ...
- POJ_2456 Aggressive cows 【二分求最大化最小值】
题目: Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are l ...
- 【POJ - 2456】Aggressive cows(二分)
Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x ...
- POJ 2456 Aggressive cows (二分)
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
- poj 2456 Aggressive cows 贪心+二分
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25944 Accepted: 11982 ...
- [poj2182] Lost Cows (线段树)
线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...
随机推荐
- 应用安全 - PHP - CMS - EmpireCMS - 漏洞 - 汇总
2006 Empire CMS <= 3.7 (checklevel.php) Remote File Include Vulnerability Empire CMS Checklevel.P ...
- 解析之Apache解析
- WebMvcConfigurationSupport与WebMvcConfigurer的关系
大家从网上及源码注释上查到的解释是,在spring中配置WebMvc时有两种方法,一种是继承WebMvcConfigurationSupport,重写里面相应的方法,还有一种是继承WebMvcConf ...
- java学习-3
输入语句Scanner的使用方法 1.导包 import java.util.Scanner 2.创建 从键盘输入:Scanner sc = new Scanner(System.in); 3.使用 ...
- K8s开始
部署前思考:
- [python] 执行 dos 命令
python的os模块 os模块调用CMD命令有两种方式:os.popen(),os.system(). 都是用当前进程来调用. os.system是无法获取返回值的.当运行结束后接着往下面执行程序. ...
- python函数 全局变量和局部变量
li1=[1,2,3,4,5] str1='abc' def func1(): li1=[7,8,9] str1='efg' print(str1) func1() print(li1)#输出的结果为 ...
- 卸载mysql后再安装提示The service already exists!问题解决方法
卸载mysql后再安装输入mysqld --install 回车后提示The service already exists! 原因:卸载的时候没有卸载干净 方法: 一.重新以管理员身份打开cmd 二. ...
- C++学习 之 类的声明及成员的访问(笔记)
1.类的声明 简单来说,属性以及对属性的操作的整合叫做类.要声明类可使用关键字class,并在它的后面定义类名,然后紧接着是属于该类的代码块{}.类的声明类似于函数声明,类的声明本身并不改变程序 的行 ...
- 使用Jsoup爬取网站图片
package com.test.pic.crawler; import java.io.File; import java.io.FileOutputStream; import java.io.I ...