Cow Sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2185    Accepted Submission(s): 683

Problem Description
Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage Sherlock's milking equipment, Sherlock would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes Sherlock a total of X + Y units of time to exchange two cows whose grumpiness levels are X and Y.

Please help Sherlock calculate the minimal time required to reorder the cows.

 
Input
Line 1: A single integer: N
Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.
 
Output
Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.
 
Sample Input
3
2
3
1
 
Sample Output
7
 
Hint

Input Details

Three cows are standing in line with respective grumpiness levels 2, 3, and 1.
Output Details

2 3 1 : Initial order.
2 1 3 : After interchanging cows with grumpiness 3 and 1 (time=1+3=4).
1 2 3 : After interchanging cows with grumpiness 1 and 2 (time=2+1=3).

 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  3450 2227 3030 2642 2836 
 

题意:

求逆序数两两的总和: 如 3 2 1 :sum=(3+2)+(3+1)+(2+1)=12;   1 2 3: sum=0;

树状数组:

其实这题并不难,抓住一个点和熟悉树状数组大概就可以做出来了,那个点就是如何求得和。

这里才用的方法是参考别人的 ,自己想了一段时间没想出来。

对于新插入的一个元素,运用树状数组,可以求得比它小的元素的个数,比它小的元素的和,在它之前的元素的总和。

而对于每一个新元素,其sum[m]=m*(比它大的元素个数)+(前i个元素的和)-(比它小的元素的和)。

然后累加得解。

实现:

 //46MS    2584K    955 B    C++
#include<stdio.h>
#include<string.h>
#define ll __int64
#define N 100005
ll cnt[N],ssum[N],tsum[N];
inline ll lowbit(ll k)
{
return k&(-k);
}
void update(ll c[],ll k,ll detal)
{
for(;k<N;k+=lowbit(k))
c[k]+=detal;
}
ll getsum(ll c[],ll k)
{
ll s=;
for(;k>;k-=lowbit(k))
s+=c[k];
return s;
}
int main(void)
{
ll n,m;
while(scanf("%I64d",&n)!=EOF)
{
memset(cnt,,sizeof(cnt));
memset(ssum,,sizeof(ssum));
memset(tsum,,sizeof(tsum));
ll s=,temp=;
for(ll i=;i<=n;i++){
scanf("%I64d",&m);
update(cnt,m,);
update(ssum,m,m);
update(tsum,i,m);
temp=getsum(cnt,m-);
s+=m*(i-temp-);
s+=getsum(tsum,i-);
s-=getsum(ssum,m-);
//printf("**%I64d\n",s);
}
printf("%I64d\n",s);
}
return ;
}

hdu 2838 Cow Sorting (树状数组)的更多相关文章

  1. hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  3. LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组

    \(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...

  4. HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用

    这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...

  5. hdu 2838 Cow Sorting (树状数组+逆序对)

    题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...

  6. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  7. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  8. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  9. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

随机推荐

  1. Co. - Microsoft - Windows - Tomcat、JDK、MySQL通过 Inno 集成为exe部署包

    需求 客户设备为Windows系统,需要部署公司产品,因此将Tomcat.JDK.MySQL.Java.war 打包整合成exe文件,Windows下一键部署安装. 最佳实践 1.下载免安装的mysq ...

  2. 服务命令只支持基本的LSB操作(启动、停止、重新启动、尝试重启、重新加载、强制重新加载、状态)。对于其他操作,请尝试使用systemctl。

    The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, forc ...

  3. 云监控自定义HTTP状态码说明

    您在使用站点监控时,返回的6XX状态码均为云监控自定义HTTP状态码,具体含义如下表所示: 状态码      含义     备注  610  HTTP连接超时      监测点探测您的网站时出现连接超 ...

  4. 【mvrp多协议vlan注册协议给予三种注册方式的验证】

    MVRP 多vlan注册协议给予三种注册模式的配置 一:根据项目需求搭建好拓扑图如下 二:配置: 首先对项目做理论分析,sw1,sw2,sw3所组成的直连网络中,为使不同的PC之间进行通信,按vlan ...

  5. Git----将本地代码推送到远程仓库

    1.初始化本地 git init 2.添加文件 -A等于 -. 和-a的集合 git add -A 3.提交 git commit -m 'add' 4.关联到远程库 git remote add o ...

  6. [转]Makefile中使用$$的使用

    在makefile中,会经常使用shell命令,也经常见到$var 和 $$var的情况,有什么区别呢,区别大了.不要认为在makefile的规则的命令行中使用$var就是将makefile的变量和s ...

  7. 嵌入式框架Zorb Framework搭建七:任务的实现

    我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...

  8. 【Leetcode】804. Unique Morse Code Words

    Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...

  9. P1016 旅行家的预算

    P1016 旅行家的预算 题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2 ...

  10. mysql 5.7.19 zip版本 windows安装步骤

    请注意此文档用于msyql5.7系列及以后版本(包括最新 mysql 8.0.11)zip版本windows下的安装1.下载mysql省略2.解压mysql到D:\Program Files\mysq ...