Cow Sorting

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

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).

 
 
题意: n个数的排列,每次可以互换相邻的元素,最终变成一个递增的序列,每次互换的代价为互换的两个数的和,求最小代价。
 
题解:从无序到递增过程,就是冒泡排序的过程。对于每一个元素a[i],需要交换的次数就是在a[1]~a[i-1]中,所有比a[i]大的元素的个数cnt,a[i]到最终状态花费的代价就是cnt*a[i]+所有被交换的元素的和sum
树状数组处理出  比a[i]小的数的个数b[i] 和 所有比a[i]小的数的和c[i]就可以直接处理
 
#include<iostream>
#include<string.h>
#define ll long long
using namespace std;
ll a[100005],b[100005],c[100005];
//a[i]保存原始数据,b[i]保存比a[i]小的数的个数,c[i]保存所有比a[i]小的数的和
ll lowbit(ll x)
{
return x&(-x);
} ll getnum(ll x)//求比x小的数的个数
{
ll cnt=0;
while(x>0)
{
cnt=cnt+b[x];
x=x-lowbit(x);
}
return cnt;
} ll getsum(ll x)//求比x小的数的和
{
ll ans=0;
while(x>0)
{
ans=ans+c[x];
x=x-lowbit(x);
}
return ans;
} void add(ll x,ll y)//更新,对第x个位置的数进行更新,y是更新值
{
while(x<=100000)
{
b[x]=b[x]+1;
c[x]=c[x]+y;
x=x+lowbit(x);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
ll ans=0,sum=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
add(a[i],a[i]);//将第x个位置的值,修改为x
sum=sum+a[i];//sum求所有数的和
ans=ans+a[i]*(i-getnum(a[i]));//i-getnum(a[i])是比a[i]大的数的个数
ans=ans+sum-getsum(a[i]);//sum-getsum(a[i])是所有比a[i]大的数的和
}
printf("%lld\n",ans);
}
return 0;
}

hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数的更多相关文章

  1. hdu 2838 Cow Sorting (树状数组)

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

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

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

  3. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. HDU Cow Sorting (树状数组)

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

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

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

  7. hdu_2838_Cow Sorting(树状数组求逆序对)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 题意:给你一串数,让你排序,只能交换相邻的数,每次交换花费交换的两个树的和,问最小交换的价值 题 ...

  8. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  9. hdu2838 cow sorting用树状数组求逆序对

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2838/ 题目解法:题目给出一个1-n的排列,操作只有一种:交换相邻的元素,代价是两个元素之和,问将该序列变成升序 ...

随机推荐

  1. mui 监听 手机 物理返回键

    mui.back = function(){ return  //禁用物理返回键  也可以写其他逻辑 }

  2. mybatis Plus 多表联合查询

    //实体类package com.sk.skkill.entity; import com.baomidou.mybatisplus.annotation.TableField;import com. ...

  3. python学习笔记:分支 与 循环

    if 语句 if 条件: ...... # 条件为真的时候,执行缩进的代码 if 条件: ...... # 条件为真的时候执行 else: ...... # 条件为假的时候执行 if 条件1: ... ...

  4. Debian9安装Metasploit

    参考博文:https://www.jianshu.com/p/ea0629b9e367 0x0 添加Kali源 deb http://http.kali.org/kali kali-rolling m ...

  5. 吴裕雄--天生自然Python Matplotlib库学习笔记:matplotlib绘图(2)

    import numpy as np import matplotlib.pyplot as plt fig = plt.figure() fig.subplots_adjust(bottom=0.0 ...

  6. 这里是常见的HTTP状态码

    遇到过的HTTP状态码 200(Ok):请求成功,服务器成功返回网页. 403(Forbidden):服务器已经理解请求,但是拒绝执行它. 404(Not Found):请求失败,请求所希望得到的资源 ...

  7. GRE Over IPSec配置

    路由器GRE over IPSec站点到站点VPN         问题分析:对于前面的经典的IPSec VPN的配置来说,兼容性较好,适合于多厂商操作的时候,但是这种经典的配置方式不适合在复杂的网路 ...

  8. Codeforces Round #621 (Div. 1 + Div. 2) C. Cow and Message

    Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is ...

  9. leetcode 0205

    目录 700 二叉搜索树中的搜索 175 组合两个表 仍旧不理解 left join 590. N叉树的后序遍历 递归: 迭代: 589 N叉树的前序遍历 递归, 注意 递归 过程中附带的 actio ...

  10. 02-12Android学习进度报告十二

    今天学习了ListView的焦点问题,基本了解了ListView的使用内容. 首先可以为抢占了控件的组件设置:android:focusable="false" 只需为抢占了Lis ...