poj--2299(树状数组+离散化)
一、离散化:
https://www.cnblogs.com/2018zxy/p/10104393.html
二、逆序数
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = ;
struct Node{
LL data;
int val;
}cur[maxn];
LL a[maxn];
bool cmp(Node A,Node B)
{
return A.data<B.data;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x<maxn-)
{
a[x]++;
x+=lowbit(x);
}
}
int sum(int x)
{
int ans=;
while(x>)
{
ans+=a[x];
x-=lowbit(x);
}
return ans;
}
int main(void)
{
int n,i;
while(~scanf("%d",&n)&&n)
{
memset(a,,sizeof(a));
for(i=;i<=n;i++)
{
scanf("%lld",&cur[i].data);cur[i].val=i;
}
sort(cur+,cur++n,cmp);
LL ans=;
for(i=;i<=n;i++)
{
update(cur[i].val);
ans+=i-sum(cur[i].val);
}
printf("%lld\n",ans);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = ;
struct Node{
LL data;
int val;
}cur[maxn];
LL a[maxn];
bool cmp(Node A,Node B)
{
return A.data<B.data;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x<maxn-)
{
a[x]++;
x+=lowbit(x);
}
}
int sum(int x)
{
int ans=;
while(x>)
{
ans+=a[x];
x-=lowbit(x);
}
return ans;
}
int main(void)
{
int n,i;
while(~scanf("%d",&n)&&n)
{
memset(a,,sizeof(a));
for(i=;i<=n;i++)
{
scanf("%lld",&cur[i].data);cur[i].val=i;
}
sort(cur+,cur++n,cmp);
LL ans=;
for(i=n;i>=;i--)
{
ans+=sum(cur[i].val);
update(cur[i].val);
}
printf("%lld\n",ans);
}
return ;
}
poj--2299(树状数组+离散化)的更多相关文章
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- Ultra-QuickSort (POJ 2299)树状数组+离散化
题目链接 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm ...
- poj 2299 树状数组求逆序数+离散化
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...
- poj 2299 树状数组求逆序对数+离散化
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 54883 Accepted: 20184 ...
- POJ 2299树状数组求逆序对
求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...
- Ultra-QuickSort POJ - 2299 树状数组求逆序对
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...
随机推荐
- TZOJ 3244 Happy YuYu's Birthday(数学几何)
描述 9月10日教师节,也是YuYu的生日,妈妈给YuYu准备了一个很大的圆形蛋糕,YuYu看中了蛋糕中间那诱人的樱桃(都挤到一块啦),小家伙很高兴,心里开始盘算着如何将樱桃全部分给自己.YuYu是个 ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- iOS指令集
公司在进行项目重构时,其中一个地方的改动就是调整了iOS的指令集.更改指令集主要可以对手机应用的安装机型做出控制,同时在研发过程中也可以控制相关的模拟器和真机.它们原则上是向下兼容的,比如iphone ...
- 微信公众号开发(5)---使用开源组件开发公众号OAuth2.0网页授权授权登录
搞清微信公众号授权登录的步骤步骤,我们的开发就完成了一大步 献上github 地址: https://github.com/Wechat-Group/weixin-java-tools/wiki/MP ...
- python requests的content和text方法的区别(转)
原文地址: http://blog.csdn.net/xie_0723/article/details/51361006 问题: 一直在想requests的content和text属性的区别,从pri ...
- tomcat实现多端口、多域名访问(只针对一个tomcat)
说明:这个部分介绍如何在tomcat中进行配置,使同一个应用可以通过不同的端口号进行访问. 在某些需要进行安全控制的场景中会应用到.例如:不同地址段只能通过某个端口访问. 2 找到tomcat的主目录 ...
- thinkphp装修平台源码
每日签到微擎微赞自助授权中心站长工具(new)☜=每日新帖=微信开发手册VIP优惠活动 开启辅助访问切换到宽版 用户名 自动登录 找回密码 密码 登录 立即注册 只需一步,快速开始 首页 微鱼商业 ...
- 内置函数 hashlib configparser logging 模块 C/S B/S架构
1.内置函数 # 内置的方法有很多 # 不一定全都在object中 # class Classes: # def __init__(self,name): # self.name = name # s ...
- PC 上的 LVM 灾难修复
LVM 介绍 LVM 简介 LVM 是逻辑盘卷管理(Logical Volume Manager)的简称,最早是 IBM 为 AIX 研发的存储管理机制.LVM 通过在硬盘和分区之间建立一个逻辑层,可 ...
- 在 Ubuntu 上使用微信客户端
原文地址: http://www.myzaker.com/article/5979115d1bc8e08c30000071/ 在这个快速信息交互时代,无论是工作还是生活,都需要频繁的网络社交,而在中国 ...