一、离散化:

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(树状数组+离散化)的更多相关文章

  1. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  2. Ultra-QuickSort (POJ 2299)树状数组+离散化

    题目链接 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm ...

  3. poj 2299 树状数组求逆序数+离散化

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

  4. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  5. POJ 2299树状数组求逆序对

    求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...

  6. Ultra-QuickSort POJ - 2299 树状数组求逆序对

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  7. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  8. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  10. POJ 2299 Ultra-QuickSort(树状数组+离散化)

    http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...

随机推荐

  1. tcp/ip通信第5期之客户机端程序

    /*此程序是tcp/ip通信的客户机端程序, 测试运行在redhat6系统上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> ...

  2. android.support.v4与Android.support.v7

    Android提供了android.support.v4和android.support.v7两个库,以便低版本API可以使用高版本API的功能. Fragment(碎片)类,是在Android 3. ...

  3. 解决com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭

    com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭. at com.microsoft.sqlserver.jdbc.SQLServerEx ...

  4. C# 创建WebService的简单示例

    工具Visual Studio 2013 1.创建一个空的Web应用程序. 2.鼠标右击项目,添加->新建项 选择Web服务(ASMX),点击添加.一个简单的webservice就创建完成了.

  5. js倒计时跳转页面实现

  6. springboot 项目添加jaeger调用链监控

    1.添加maven依赖<dependency> <groupId>io.opentracing.contrib</groupId> <artifactId&g ...

  7. PAT 甲级 1015 Reversible Primes(20)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  8. Pandas设置值

    1.创建数据 >>> dates = pd.date_range(', periods=6) >>> df = pd.DataFrame(np.arange(24) ...

  9. execute() 和 sumbit() 的区别

    execute()内部实现 1.首次通过workCountof()获知当前线程池中的线程数, 如果小于corePoolSize, 就通过addWorker()创建线程并执行该任务: 否则,将该任务放入 ...

  10. Get、Post 提交的乱码问题

    1.问题 在spring mvc开发的时候出现乱码问题: 2.解决方案 (1)Get提交:tomcat容器接收的造成的乱码问题,修改server.xml文件: (2)Post提交:在web.xml中配 ...