博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cow Sorting hdu 2838
阅读量:5732 次
发布时间:2019-06-18

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

Cow Sorting

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

Total Submission(s): 2224    Accepted Submission(s): 701

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
 
 
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 #define ll long long10 typedef struct abcd11 {12 ll sum,n;13 } abcd;14 abcd a[110000];15 ll lowbit(ll x)16 {17 return x&(-x);18 }19 ll update(ll x)20 {21 int y=x;22 while(x<110000)23 {24 a[x].n++;25 a[x].sum+=y;26 x+=lowbit(x);27 }28 }29 ll fun(ll x)30 {31 ll ans=0;32 while(x>0)33 {34 ans+=a[x].n;35 x-=lowbit(x);36 }37 return ans;38 }39 ll fun1(ll x)40 {41 ll ans=0;42 while(x>0)43 {44 ans+=a[x].sum;45 x-=lowbit(x);46 }47 return ans;48 }49 int main()50 {51 ll n,i,x,ans,z,sum;52 while(~scanf("%I64d",&n))53 {54 memset(a,0,sizeof(a));55 sum=ans=0;56 for(i=0; i
View Code

 

转载于:https://www.cnblogs.com/ERKE/p/3837772.html

你可能感兴趣的文章
POJ 2236 Wireless Network (并查集)
查看>>
python分类
查看>>
linux 中常见的压缩和解压缩的命令
查看>>
GitBlit (1)-- 在linux 安装 GitBlit 并运行
查看>>
Windows与Linux之间的文件自动同步
查看>>
topcoder srm 714 div1
查看>>
20160215
查看>>
mxnet导入图像数据
查看>>
LeetCode – Refresh – Merge Sorted Array
查看>>
程序是如何执行的(一)a=a+1
查看>>
go : 结构
查看>>
【Python第五篇】Python面向对象(初级篇)
查看>>
innobackupex参数之 --throttle 限速这个值设置多少合理 原创
查看>>
18 已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果
查看>>
BZOJ - 3578: GTY的人类基因组计划2
查看>>
理解WebKit和Chromium(电子书)
查看>>
爱——无题
查看>>
分布式服务框架原来与实践 读书笔记一
查看>>
Aho-Corasick automation-KMP
查看>>
【http】post和get请求的区别
查看>>