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预处理出 ...
随机推荐
- Node.js:教程
ylbtech-Node.js:教程 1.返回顶部 1. Node.js 教程 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaS ...
- xss 记录cookie
<p> <img src="http://act.ci123.com/global/ueditor_new/php/upload/98591403834900.jpg&qu ...
- [JavaEE] Hibernate连接池配置测试
转载自51CTO http://developer.51cto.com/art/200906/129914.htm Hibernate支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以 ...
- os.clock()导致的bug
os.clock () 功能:返回一个程序使用CPU时间的一个近似值 最近做了一个功能,这个功能需要统计时间间隔,例如每隔0.5秒做一次调用. 我用了os.clock()去统计时间,结果在pc机上都没 ...
- BZOJ3573: [Hnoi2014]米特运输(树上乱搞)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1669 Solved: 1031[Submit][Status][Discuss] Descript ...
- javascirpt之 this、apply、call、bind
this.apply.call.bind 这又是一个面试经典问题~/(ㄒoㄒ)/~~也是 ES5中众多坑中的一个,在 ES6 中可能会极大避免 this 产生的错误,但是为了一些老代码的维护,最好还是 ...
- C-Store论文阅读笔记
C-Store论文由今年的图灵奖获得者Mike Stonebraker提出来,整体架构在数据库领域相当不错.数据库采用读写分开存的架构,只写块的数据定期会和只读块儿的数据进行合并,产生新的只读块儿.而 ...
- SQL数据库链接代码的解释
SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=(local);Initial ...
- testdirector
TestDirector是全球最大的软件测试工具提供商Mercury Interactive公司生产的企业级测试管理工具,也是业界第一个基于Web的测试管理系统
- 杭电2061WA
#include<stdio.h> struct mem { char s[50]; int c; int f; }; int main() { struct mem x[60]; int ...