HDU 2838 (树状数组求逆序数)
题意:
给你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 (树状数组求逆序数)的更多相关文章
- hdu 5147 Sequence II (树状数组 求逆序数)
题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数)
链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...
- SGU180 Inversions(树状数组求逆序数)
题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)
题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...
- Codeforces645B【树状数组求逆序数】
题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...
- HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)
题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...
- 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预处理出 ...
随机推荐
- [HDU 4261] Estimation
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4261 [算法] 首先,有一个结论 : | a[1] - k | + | a[2] - k | + ...
- Node.js:Buffer
ylbtech-Node.js:Buffer 1.返回顶部 1. Node.js Buffer(缓冲区) JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文 ...
- PDOHelper (原创)
class PDOHelper{ public static $db =null;// new PDO('mysql:host=192.168.1.68;dbname=test','root','12 ...
- Linux Shell Scripting Cookbook 读书笔记 4
正则, grep 1. 正则表达式 正则表达式 描述 示例 ^ 行起始标记 ^hell匹配以hell开头的行 $ 行尾标记 test$匹配以test结尾的行 . 匹配任意一个字符 hell ...
- HDU1412 {A} + {B}
2019-05-17 10:15:01 每个元素之间有一个空格隔开. 每行最后一的元素后面没有空格,区别于HDU人见人爱A - B 注意使用STL的时候要清空 . a.clear(); #inclu ...
- Blender插件之操作器(Operator)实战
前言 在Blender中, 操作器(Operator)是它的核心. 用户通过各种操作器来创建和操作场景中的物体. 操作器对象继承自 class bpy.types.Operator(bpy_struc ...
- ManualResetEvent和AutoResetEvent的区别,分享来的
在讨论这个问题之前,我们先了解这样一种观点,线程之间的通信是通过发信号来进行沟通的.(这不是废话) 先来讨论ManualResetEvent,讨论过程中我会穿插一些AutoResetEvent的内容, ...
- python课程设计笔记(二)破冰基本语法
python两种编程方式:交互式与文件式 交互式:语法练习,输一条运行一条 文件式:通用,执行一组语句 注释 #单行注释 ...XXXXX...多行注释 逻辑 没有大括号,按缩进确定逻辑——缩进格数 ...
- 【OpenCV】关于 waitKey()的使用方法
C++: int waitKey(int delay=0) cvWaitKey()函数的功能是不断刷新图像,频率时间为delay,单位为ms. 返回值为当前键盘按键值. 所以显示图像时,如果需要在cv ...
- windwo下载完nvm无法执行node
安装node版本管理工具之NVM.安装方法:见链接. window安装完后,下载node后,无法执行node.见图(图片从网上找的). 最后问题原因是,1.nvm安装时,安装目录中存在空格. 解决办法 ...