HDU-2838 Cow Sorting(树状数组)
来源:互联网 发布:遗迹软件 编辑:程序博客网 时间:2024/04/29 07:33
Cow Sorting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3219 Accepted Submission(s): 1080
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.
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.
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
3231
Sample Output
7HintInput DetailsThree cows are standing in line with respective grumpiness levels 2, 3, and 1.Output Details2 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
2009 Multi-University Training Contest 3 - Host by WHU
题意:你可以将相邻的数相互交换最终使得数组有序。(代价是每次花费相邻数和的时间)
思路:运用冒泡排序的思想,对于当前数x而言,x会交换k次(设k为前面大于x的数的个数),设为前面大于x的数的和为sum,所以time = k*x + sum。
前面大于x的数的个数也就是逆序,用树状数组求解。
/*hdu2838树状数组nlogn求逆序数*/#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long ll;const int N = 100005;struct node{ int num; ll sum;}C[N];int n;int lowbit(int t){ return t&(-t);}void add(int t,int k,int s){ while(t <= n) { C[t].num += k; C[t].sum += s; t += lowbit(t); }}//求得前面小于当前的数的个数int getnum(int t){ int ans = 0; while(t > 0) { ans += C[t].num; t -= lowbit(t); } return ans;}//前面所有小于当前数的和ll getsum(int t){ ll ans = 0; while(t > 0) { ans += C[t].sum; t -= lowbit(t); } return ans;}int main(){ while(~scanf("%d",&n)) { memset(C,0,sizeof(C)); int x; ll ans = 0; for(int i = 1;i <= n;i++) { scanf("%d",&x); add(x,1,x); ll k = i - getnum(x);//逆序数 if(k != 0) { ans += k*x + getsum(n) - getsum(x); } } printf("%lld\n",ans); } return 0;}
0 0
- hdu 2838 Cow Sorting(树状数组)
- hdu 2838 Cow Sorting(树状数组)
- hdu-2838(Cow Sorting)----》树状数组
- HDU - 2838 Cow Sorting (树状数组)
- hdu 2838 Cow Sorting(树状数组)
- HDU-2838 Cow Sorting(树状数组)
- hdu 2838 Cow Sorting(树状数组)
- HDU 2838 Cow Sorting(树状数组)
- HDU 2838 cow sorting 树状数组
- hdu 2838 Cow Sorting (树状数组)
- HDU 2838 Cow Sorting(树状数组)
- hdu 2838 cow sorting(树状数组)
- hdu 2838 Cow Sorting 树状数组
- HDU 2838 Cow Sorting(树状数组)
- HDU 2838 Cow Sorting 树状数组
- HDU 2838 Cow Sorting [树状数组]【数据结构】
- Hdu 2838 Cow Sorting 树状数组
- HDU 2838 Cow Sorting(树状数组-水题)
- Virtualbox怎么设置访问外网以及主机访问虚拟机
- intent 传递list或者对象
- 深入了解Angularjs指令中的ngModel
- java中的锁
- 进程共享技术-管道
- HDU-2838 Cow Sorting(树状数组)
- Android Studio 掌握这些调试技巧,Debug能力不能再高啦
- Java多线程中join方法的理解
- 近期资源整理<内含视频,第三方包冲突,pod库冲突,编译冲突>
- 深入理解java异常处理机制
- MT4/MT5使用dll调用matlab
- Java编程思想-第二章-一切都是对象-2.3-2.5笔记
- mac使用vscode(简单暴力,只说重点)
- 2016.9.2 号从新开始,之前的博客莫名其妙被封,新开一块!