hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4766 Accepted Submission(s): 1727
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.
Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.
2
3
1
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).
#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;
}
hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数的更多相关文章
- hdu 2838 Cow Sorting (树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用
这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5147 Sequence II (树状数组 求逆序数)
题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组
\(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...
- hdu_2838_Cow Sorting(树状数组求逆序对)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 题意:给你一串数,让你排序,只能交换相邻的数,每次交换花费交换的两个树的和,问最小交换的价值 题 ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- hdu2838 cow sorting用树状数组求逆序对
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2838/ 题目解法:题目给出一个1-n的排列,操作只有一种:交换相邻的元素,代价是两个元素之和,问将该序列变成升序 ...
随机推荐
- Unix套接字接口
简介 套接字是操作系统中用于网络通信的重要结构,它是建立在网络体系结构的传输层,用于主机之间数据的发送和接收,像web中使用的http协议便是建立在socket之上的.这一节主要讨论网络套接字. 套接 ...
- devexpress layoutview
1.设定数据源 2.设置view 3.设置 templat cardview 4 显示
- CSS - 引入方法
1. 外部样式表 <head> <link rel="stylesheet" type="text/css" href="style ...
- 如何使html中的元素不被选中
有时候,为了提高用户的体验度,需要使网页中的部分内容防误操作,不被选中,比如今天的商城项目中的一个细节部分: + —号其实是a标签做的,当连续点击多次,就会使符号被选中,这样感觉起来不太好,于是查找解 ...
- IPv4地址被用光,IPv6将接手
截止2019年11月26号,全球所有43亿个IPv4地址已全部分配完毕,这一情况也宣告着IPv6时代的正式来临.IPv6和5G一样是关系到国家安全和战略发展的重大事情. IPv6简单来说,就是一个互联 ...
- springmvc项目的搭建
springmvc替代servlet的工作 Servlet - Springmvc jsp ->Servlet (Springmvc)->Jsp springmvc配置文件 ...
- C - Water The Garden
It is winter now, and Max decided it's about time he watered the garden. The garden can be represent ...
- jquery 分页 Ajax异步
//使用Ajax异步查询数据 <div class="table-responsive"> <table class="table table-bord ...
- The Captain 题解
20200216题目题解 这是一篇题解祭题解记,但一共就一道题目.(ROS菜大了) 题目如下: The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x ...
- 基于通用二进制方式安装MySQL-5.7.24(比源码安装MySQL快许多)及破密码
确保系统中有依赖的libaio软件 yum -y install libaio 使用wget命令下载mysql-5.7.24软件包 wget http://mirrors.sohu.com/mysql ...