hdu-2838 Cow Sorting---逆序对的花费
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2838
题目大意:
就是求将之前的排列变成一个递增的排列,每交换两个数的代价为两个数的和,求变成递增的排列所需的最小代价为多少。
其实就是求出逆序对的花费(每对逆序对的花费是这个逆序对的和)
解题思路:
用两个树状数组维护即可,一个维护逆序对的数目,一个维护逆序对的值的和
注意:全部用long long
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#include<sstream>
#define lowbit(i) (i&(-i))
using namespace std;
const int maxn = 1e6 + ;
typedef long long ll;
ll tree_sum[maxn], tree_num[maxn], n;
void add(ll x, ll d, ll tree[])
{
while(x <= n)tree[x] += d, x += lowbit(x);
}
ll sum(ll x, ll tree[])
{
ll ans = ;
while(x)
ans += tree[x], x -= lowbit(x);
return ans;
}
int main()
{
while(scanf("%lld", &n) != EOF)
{
ll x, ans = ;
memset(tree_num, , sizeof(tree_num));
memset(tree_sum, , sizeof(tree_sum));
for(int i = ; i <= n; i++)
{
scanf("%lld", &x);
add(x, , tree_num);
add(x, x, tree_sum);
ans += (sum(n, tree_num) - sum(x, tree_num)) * x + sum(n, tree_sum) - sum(x, tree_sum);
}
cout<<ans<<endl;
}
return ;
}
hdu-2838 Cow Sorting---逆序对的花费的更多相关文章
- hdu 2838 Cow Sorting (树状数组+逆序对)
题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...
- hdu 2838 Cow Sorting (树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2838 Cow Sorting(树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 2838 Cow Sorting
题意: 给出一个数组,要求把他们排成升序,两个数字交换的代价是x + y,求代价的总和. 思路: 简单的逆序对变形,树状数组维护小于的cnt和sum即可. 代码: #include <stdio ...
- BZOJ_2058_[Usaco2010 Nov]Cow Photographs_逆序对
BZOJ_2058_[Usaco2010 Nov]Cow Photographs_逆序对 题意: 奶牛的图片 Farmer John希望给他的N(1<=N<=100,000)只奶牛拍照片, ...
- BNUOJ-26474 Bread Sorting 逆序对
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26474 题意:给一个数列,可以对三个数操作:把最后一个数放到第一个,前两个数后移一位.问最 ...
- bzoj2058: [Usaco2010 Nov]Cow Photographs(逆序对)
题目大意:给出n个数的序列,每次可以交换相邻的两个数,问把序列变成编号i在编号i+1左边,编号1在编号n右边(一个环)最少需要多少步.如:35421最少交换两次变为34512. 一开始看到这题,只会O ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
随机推荐
- Go语言基础之3--时间和日期序列
一.时间和日期类型 1. time包 2. time.Time类型,用来表示时间 3. 获取当前时间, now := time.Now() 实例1-1 打印输出当前时间 package main i ...
- PHP、thinkPHP5.0开发网站文件管理功能(三)重命名文件
public function renames(){ if(request()->isAjax()){ $file = iconv('UTF-8','GB2312',urldecode(inpu ...
- Python Pandas -- Series
pandas.Series class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath ...
- Redis:存储对象的两种方式(序列化和json字符串)
方式一:序列化操作 public class SerializeUtil { /* * 序列化 * */ public static byte[] serizlize(Object ...
- Mybatis学习笔记14 - 动态sql之foreach标签
一.查询给定集合中员工id对应的所有员工信息 示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import ...
- android Activity启动过程(一)从startActivty开始说起
从启动startActivity开始说起 MainActivity.startActivity() Activity.startActivity() Activity.startActivityFor ...
- Perf -- Linux下的系统性能调优工具,第 2 部分 特殊用法以及内核调优示例
https://www.ibm.com/developerworks/cn/linux/l-cn-perf2/ 本文内容简介 之前介绍了 perf 最常见的一些用法,关注于 Linux 系统上应用程序 ...
- HDU 4323——Magic Number——————【dp求编辑距离】2012——MUT——3
Magic Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- .NET Core 部署到CentOS–3.supervisord守护进
1. 安装 sudo yum install python-setuptoolssudo easy_install supervisor 2. 配置 安装成功后,输入supervisord 会显示配置 ...
- MVC学习手册之数据注解与验证
MVC学习手册之数据注解与验证 新建一个MVC5的WEB应用程序,VS2013会自动生成一段代码,以下是Account控制器下Register.cshtml 页面的代码: @model WebAppl ...