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 ...
随机推荐
- js改变表单的内容样式
一.改变单个样式 var obj = document.getElementById("id"); obj.style.cssText = " display: ...
- [剑指Offer]11-旋转数组的最小数字(二分查找)
题目链接 https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=13&tqId=11159&t ...
- f5 SNAT
request过程: 1.真实源地址(3.3.3.3)将数据包发给f5虚拟的vs地址(1.1.1.5:80): 2.f5将真实源地址(3.3.3.3)转换成SNAT地址(1.1.1.100),并将vs ...
- c#: 判断Firefox是否安装
1.源起: KV项目需要给浏览器安装下载插件,就需要判断是否安装对应浏览器,发现判断卸载目录方法,32位程序在.net 2.0运行环境下,常规方法不能访问64位注册表位置,导致不能判断. 2.卸载键值 ...
- c#特性attribute:
特性是被编译到metadata中, 是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...
- C# 一个数组未赋值引发的错误
在电脑前又是一天,后来脑子也糊里糊涂,可能是基础还不牢固,设置断点,找了找问题才发现数组定义出了问题, 我是这样定义数组的,string[] auths ; string auths=new stri ...
- 9.Mysql字符集
9.字符集9.1 字符集概述 字符集就是一套文字符号及其编码.比较规则的集合. ASCII(American Standard Code for Information Interchange)字符集 ...
- (转)在JAVA实现DataTable对象(三)——DataTable对象实现
大家都是行家,我就直接上代码了,我这个代码应该还是能看懂的,嘻嘻…. 1: import java.util.ArrayList; 2: import java.util.List; 3: 6: ...
- IIS7中的站点、应用程序和虚拟目录详细介绍
IIS7中的站点.应用程序和虚拟目录详细介绍 这里说的不是如何解决路径重写或者如何配置的问题,而是阐述一下站点(site),应用程序(application)和虚拟目录 (virtual direct ...
- hdu 4717(三分) The Moving Points
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 n个点,给出初始坐标和移动的速度和移动的方向,求在哪一时刻任意两点之间的距离的最大值的最小. 对于最 ...