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. pyqt5通过qt designer 设计方式连接多个UI图形界面

    当我们通过pyqt开发时,eric6为我们提供了一个方便的工具:图形化的绘制UI工具--qtdesigner.我们可以通过它开发多个UI,然后利用信号-槽工具,将功能代码附着在上面.也可以将多个界面连 ...

  2. C#正则表达式Regex类的使用

    C#中为正则表达式的使用提供了非常强大的功能,这就是Regex类.这个包包含于System.Text.RegularExpressions命名空间下面,而这个命名空间所在DLL基本上在所有的项目模板中 ...

  3. js获取播放器播放时间和停止播放

    html代码 <video id="myVideo" class="video-active" width="100%" height ...

  4. Delphi方法

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  5. 初学tiny4412

    1.解压友善之臂提供的uboot make tiny4412_config make 然后将sd卡插到电脑上,编辑虚拟机,选择对应的usb口(usb3.0兼容),如果没有usb3.0,可能是虚拟机版本 ...

  6. Python学习手册之Python介绍、基本语法(二)

    在上一篇文章中,我们介绍了Python的一些基本语法,现在我们继续介绍剩下的Python基本语法.查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/987193 ...

  7. 关于mysql连接时候出现"error 2003: can't connect to mysql server on 'localhost'(10061)问题的解决

    天,在使用navicat Premium 连接数据库时,出现了一个弹出窗口显示: "error 2003: can't connect to mysql server on 'localho ...

  8. C++拷贝构造函数 的理解

    #include <iostream> using namespace std; //拷贝构造函数的理解 class Point { public: Point(); Point(int ...

  9. class实现Stack

    基于class实现一个存储string类型的Stack 头文件: //stack.h #include<vector> #include<string> class Stack ...

  10. 隐式Dijkstra:在状态集合中用优先队列求前k小

    这种技巧是挺久以前接触的了,最近又突然遇到几道新题,于是总结了一下体会. 这种算法适用的前提是,标题所述的"状态集合"大到不可枚举(否则枚举就行了qaq) ...