题意:

给你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. 【POJ 2096】 Collecting Bugs

    [题目链接] http://poj.org/problem?id=2096 [算法] 概率DP [代码] #include <algorithm> #include <bitset& ...

  2. Arduino-OLED-四度显示

    double Fahrenheit(double celsius) { ; } //摄氏温度度转化为华氏温度 double Kelvin(double celsius) { return celsiu ...

  3. B - Mike and Cellphone(map)

    Problem description While swimming at the beach, Mike has accidentally dropped his cellphone into th ...

  4. CI中的url相关函数以及路由设置和伪静态技术

    当使用CI框架进行开发时,我们的一些数据传递的URL不应该写死,可以使用如下方法:比如说我们需要表单提交一个数据: 1.在controller控制器中我们需要先创建一个加载helper和视图的方法: ...

  5. [Offer收割]编程练习赛38

    漏写的数字 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #inclu ...

  6. 仿QQ浏览器mac版官网主页 html+css3特效

    这是一款超酷的CSS3动态背景动画特效,CSS3仿QQ浏览器官网彗星动画背景特效. 在线演示本地下载

  7. Java基础10一面向对象

    抽象 概念:当一个类中没有足够的信息描述一个现实生活中具体存在的事物,那么这个类就是抽象类. 抽象类一般是对概念领域中的描述. 语法: [访问修饰符] abstract class 类名{ } 如: ...

  8. JavaScript实现复选框的全选、不选、反选

    方法一: <html> <head> <meta charset="utf-8"> <title>无标题文档</title&g ...

  9. RN配置

    Write by lz: 详细参考官方网址: http://reactnative.cn/docs/0.43/getting-started.html#content 若是无法安装 Chocolate ...

  10. C#多线程(Thread)开发基础

    除非另有说明,否则所有的例子都假定以下命名空间被引用: using System; using System.Threading; 1      基本概念 在描述多线程之前,首先需要明确一些基本概念. ...