题意:

给你N个排列不规则的数(1~N),任务是把它从小到大排好,每次仅仅能交换相邻两个数,交换一次的代价为两数之和。求最小代价

思路:对于当前数X。我们如果知道前面比它大的数有多少,如果为K,那么有部分代价是确定的,那就是K*X。然后还得加上比它大的那些数之和,这就是当数列到X为止,排好所须要的最小代价。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#define M 100005
#define LL __int64
using namespace std; struct node
{
LL sum;
int id;
}c[M];
int n;
int Lowbit(int x)
{
return x&(-x);
} void Update(int x,int k,int num)
{
while(x<=n)
{
c[x].id +=k;
c[x].sum +=num;
x+=Lowbit(x);
}
} LL getSum(int x)
{
LL sum=0;
while(x>0)
{
sum+=c[x].sum;
x-=Lowbit(x);
}
return sum;
} int getNum(int x)
{
int sum=0;
while(x>0)
{
sum+=c[x].id;
x-=Lowbit(x);
}
return sum;
}
int main()
{
while(~scanf("%d",&n))
{
int x;
memset(c,0,sizeof(c));
LL ans=0,k;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
Update(x,1,x);
LL k=(i-getNum(x));
if(k==0)
continue;
ans+=(k*x+getSum(n)-getSum(x));
}
printf("%I64d\n",ans);
}
return 0;
}
/*
4
3
2
4
1 17
*/

HDU 2838 (树状数组求逆序数)的更多相关文章

  1. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  3. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  4. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  5. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  6. Codeforces645B【树状数组求逆序数】

    题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

  7. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  9. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

  10. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

随机推荐

  1. Build website project by roslyn through devenv.com

    1.fetch the source code2.compile controls project3.copy files under bin folder of controls to bin fo ...

  2. Gym-101915A Printing Books 模拟

    题面 题意:给你N,X,   X表示这本书从X开始编号,每个X是几位数,计数器就加几, 然后问你如果从X,开始编号,计数器为N的时候,翻了几页,不能刚好为N输出-1. (例如,5 99,答案为2,因为 ...

  3. js设计模式-享元模式

    享元模式实际上是一种优化模式,目的在于提高系统的性能和代码的效率. 使用享元模式的条件:最重要的条件是网页中必须使用了大量资源密集型对象,如果只会用到了少许这类对象,那么这种优化并不划算.第二个条件是 ...

  4. 随时随地日志Debug

    对于一个应用程序而言,Log必不可少,但是有些时候仅仅想看下输出,如果加log的话就显得比较麻烦,这个时候就用到了Debug.WriteLine("测试下,你好,非常棒,牛叉!") ...

  5. HDU2034 人见人爱 A - B

    2019-05-17 09:37:46 加油,加油 !!! 结果要从小到大输出 NULL后面没有空格 m && n == 0才结束 注意:看题仔细,罚时很严重 #include < ...

  6. .net core虚拟目录配置

    .net core在iis上配置虚拟目录不起作用,只需要将app.UseStaticFiles中物理路径PhysicalFileProvider改为指定的路径即可,如:E:\\Html\\News a ...

  7. iOS数组越界

    数组越界就是假如你的下标总数现在为32个,然后你在下一秒又执行了一个方法要从50个数据里进行赋值啊筛选之类的,而你此时数组里的值为32个,50的数据还没有请求到,往往会出现数组越界的崩溃信息,大概是这 ...

  8. TYVJ 1427 线段树的基本操作

    题意: 单点修改,区间最值 思路: 线段树 原题请戳这里 //By SiriusRen #include <cstdio> #include <cstring> #includ ...

  9. LeetCode Weekly Contest 26

    写的有点晚了. 我每次都是先看一下这里http://bookshadow.com/leetcode/的思路,然后再开始写我自己的. 1. 521. Longest Uncommon Subsequen ...

  10. B - Ultra-Fast Mathematician

    Problem description Shapur was an extremely gifted student. He was great at everything including Com ...