博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDUOJ-----2838Cow Sorting(组合树状数组)
阅读量:4350 次
发布时间:2019-06-07

本文共 2201 字,大约阅读时间需要 7 分钟。

Cow Sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2163    Accepted Submission(s): 671


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
 
 
求逆序和求和....属于树状数组的组合题目...
代码:
1 #include
2 #include
3 #include
4 #define maxn 100000 5 #define lowbit(x) ((x)&(-x)) 6 __int64 aa[maxn+2]; //求逆序数 7 __int64 bb[maxn+2]; //求和 8 int n; 9 void ope(int x,__int64 *dat,int val)10 {11 while(x<=n)12 {13 dat[x]+=val;14 x+=lowbit(x);15 }16 }17 __int64 getsum(int x,__int64 *dat)18 {19 __int64 ans=0;20 while(x>0)21 {22 ans+=dat[x];23 x-=lowbit(x);24 }25 return ans;26 }27 int main()28 {29 int i,a;30 __int64 res;31 while(scanf("%d",&n)!=EOF)32 {33 memset(aa,0,sizeof(aa));34 memset(bb,0,sizeof(bb));35 res=0;36 for(i=0;i
View Code

 

转载于:https://www.cnblogs.com/gongxijun/p/3672181.html

你可能感兴趣的文章
Log4net如何增加自定义字段(三种方式)
查看>>
简单实用的jQuery分页插件
查看>>
一行代码实现C#的四舍五入
查看>>
[转载]unity优化1
查看>>
最新LAMP教程,玩转linux
查看>>
切片的个人理解
查看>>
大数据 hadoop 环境搭建
查看>>
jmeter正则表达式解析
查看>>
17.centos7基础学习与积累-003-命令练习01
查看>>
Air Jordan 8 Retro Performance Review
查看>>
暑假生活第八周总结
查看>>
JQuery中的siblings()是什么意思
查看>>
(转)用graph-easy描绘kubenetes描绘k8s组件逻辑图
查看>>
Uiautomator 2.0 - 实战
查看>>
thinkphp去掉index.php
查看>>
C#经典之Application.DoEvents()的使用
查看>>
Spark的几个问题
查看>>
网页栅格系统研究(1):960的秘密
查看>>
2014-04-04
查看>>
Visual Studio Code初识与自动化构建工具安装
查看>>