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

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

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.
 

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
3231
 

Sample Output
7   
Hint
Input 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

  题意:你可以将相邻的数相互交换最终使得数组有序。(代价是每次花费相邻数和的时间)

  思路:运用冒泡排序的思想,对于当前数x而言,x会交换k次(设k为前面大于x的数的个数),设为前面大于x的数的和为sum,所以time = k*x + sum。

前面大于x的数的个数也就是逆序,用树状数组求解。

/*hdu2838树状数组nlogn求逆序数*/#include
#include
#include
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;}

转载地址:http://gmzgf.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
Leetcode C++ 剑指 Offer 09. 用两个栈实现队列
查看>>
Leetcode C++《每日一题》20200707 112. 路径总和
查看>>
云原生 第十一章 应用健康
查看>>
Leetcode C++ 《第202场周赛》
查看>>
云原生 第十二章 可观测性:监控与日志
查看>>
Leetcode C++ 《第203场周赛》
查看>>
云原生 第十三章 Kubernetes网络概念及策略控制
查看>>
《redis设计与实现》 第一部分:数据结构与对象 || 读书笔记
查看>>
《redis设计与实现》 第二部分(第9-11章):单机数据库的实现
查看>>
算法工程师 面经2019年5月
查看>>
搜索架构师 一面面经2019年6月
查看>>
稻草人手记
查看>>
第一次kaggle比赛 回顾篇
查看>>