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;
}

最新文章

  1. ChatSecure
  2. [示例] Firemonkey 图片按钮(3态)
  3. LLVM 笔记(二)—— PHI node
  4. win下Redis安装使用
  5. 学习Excel 十大函数
  6. 大前端学习笔记整理【五】rem与px换算的计算方式
  7. 获取唯一UUID/UDID方案
  8. 如何使用emacs编写c语言程序,并编译运行
  9. 在Mysql中如何显示所有用户?
  10. JVM的数据类型
  11. 使用ServerSocket创建TCP服务器端
  12. jconsole远程查看jvm性能
  13. linux 历史命令用法(转)
  14. easy_install django==1.4.2_百度搜索
  15. JavaScript基础知识----六道有趣的Js基础题以及解答
  16. win10应用UserControl
  17. 超越halcon速度的二值图像的腐蚀和膨胀,实现目前最快的半径相关类算法(附核心源码)。
  18. 1119.(重、错)Pre- and Post-order Traversals
  19. MySql权威指南
  20. Flume学习之路 (一)Flume的基础介绍

热门文章

  1. html5异步单图片多图片上传两种实现方式 后台.net mvc接收
  2. Java AQS 的胡言乱语修正版
  3. C++的new&amp;delete
  4. SQL 游标介绍及使用
  5. EXPOSE ocker run -P 时,会自动随机映射 EXPOSE 的端口
  6. 思科交换机配置单播MAC地址过滤
  7. rtt学习之线程间同步与通信
  8. nyoj 11
  9. Win32 开发记录
  10. #5649,list&amp;parallel