HDU2838Cow Sorting(树状数组)
题目意思是说给你一列数,每次可以将相邻的两个数交换,这一步的代价是这两个数的和,求将所有数排好序的最少代价。
题解:
我们可以这么思考,由于每次都是交换相邻的两个数,所以将一个数放到它自己的位置去后,其他的数的相对位置没变,那么排序其他的数所需要消耗的代价将与这个数没关系,所以排序过程将相当于是冒泡排序。
这样的话我们用树状数组可以记录第i个数之前有多少个比它小的的数x
再记录这些比它小的数的和是多少sum
然后这个数移动到他自己的位置的代价就是
x*a[i]+sum。
最后的复杂度就是nlogn
以后排序和求和的题应该多想想树状数组~恩恩

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct NODE
{
int num;//记录个数
__int64 sum;//记录比它小的数的和
}c[MAXN];
int N; void init()
{
mem0(c);
} int lowbit(int x)
{
return x & (-x);
} void edit(int k, int num)
{
while(k <= )
{
c[k].num += ;
c[k].sum += num;
k += lowbit(k);
}
} int getnum(int k)//得到比当前数小的数的个数
{
int num = ;
while(k > )
{
num += c[k].num;
k -= lowbit(k);
}
return num;
} __int64 getsum(int k)//得到比当前数小的数的和
{
__int64 sum = ;
while(k>)
{
sum += c[k].sum;
k-=lowbit(k);
}
return sum;
} int main()
{
while(~scanf("%d", &N))
{
init();
int num;
__int64 ans = ;
for(int i=;i<=N;i++)
{
scanf("%d", &num);
edit(num, num);
int cnt = i - getnum(num);//有cnt的数比num大
if(cnt != )
{
ans += (__int64)cnt*num + getsum()-getsum(num);
}
}
printf("%I64d\n", ans);
}
return ;
}
HDU2838Cow Sorting(树状数组)的更多相关文章
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组
E. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- hdu 2838 Cow Sorting (树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组
\(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用
这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- hdu_2838_Cow Sorting(树状数组求逆序对)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 题意:给你一串数,让你排序,只能交换相邻的数,每次交换花费交换的两个树的和,问最小交换的价值 题 ...
- BIT 树状数组 详解 及 例题
(一)树状数组的概念 如果给定一个数组,要你求里面所有数的和,一般都会想到累加.但是当那个数组很大的时候,累加就显得太耗时了,时间复杂度为O(n),并且采用累加的方法还有一个局限,那就是,当修改掉数组 ...
随机推荐
- UVa 1225 Digit Counting
题意:给出n,将前n个整数顺次写在一起,统计各个数字出现的次数. 用的最笨的办法--直接统计-- 后来发现网上的题解有先打表来做的 #include<iostream> #include& ...
- hadoop DataNode实现分析
在前面说hadoop整体实现的时候, 说过DataNode的需要完成的首要任务是K-V存储. 第二个功能是 完成和 ...
- /etc/selinux/config
/etc/selinux/configSELINUX=disabled改成了SELINUX=enforcing机器无法启动 linux无法启动怎么解决:[1]selinux配置错误 SELinux 入 ...
- 20160201.CCPP体系详解(0011天)
内容概要:C语言基本数据类型及运算题库(含答案) 第二章 基本数据类型及运算 一.选择题 1. 若以下选项中的变量已正确定义,则正确的赋值语句是[C]. A) x1=26.8%3; B) 1+2=x2 ...
- django - 修改 自增长id,起始值
常常你会遇到这样的情况,需要自增长的起始值是 0,再次从 0开始. 两个选择: 1. drop table_name; django重新建表. 2. ALTER TABLE table_name AU ...
- ORACLE 分区
在建设数据仓库过程中,经常会有大量数据,短时间内表中数据量有限,查询性能还可以,但随着时间的延长,表中数据量迅速增加,查询速度就会变慢,性能下降,这时就要考虑对表进行分区. 一.oracle的分区 当 ...
- Java 中的二维数组
所谓二维数组,可以简单的理解为是一种“特殊”的一维数组,它的每个数组空间中保存的是一个一维数组. 那么如何使用二维数组呢,步骤如下: 1. 声明数组并分配空间 或者 如: 2. 赋值 二维数组的赋值, ...
- 高新服务平台在SUSE系统上重新部署笔录
安装步骤 ./shutdown.sh 停止tomcat (1) 删除 zjgx 目录下 除bbs 和Demos所有目录和文件 (2)上传zjgx0519.zip 到zjgx目录下 (3)unz ...
- c/c++ 编译器内存对齐问题
C语言结构体对齐问题详解 转载自:http://blog.csdn.net/tiany524/article/details/6295551 测试环境32位机 WinXP: 编译器VC6(MS cl. ...
- UIButton 在UIScrollView里面 点击效果不明显的问题
self.scrollView.delaysContentTouches = NO; -(BOOL)touchesShouldCancelInContentView { return YES; }