Cow Sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2163    Accepted Submission(s): 671

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
3 2 3 1
 
Sample Output
7

Hint

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).

 
Source
 
 
求逆序和求和....属于树状数组的组合题目...
代码:
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 100000
#define lowbit(x) ((x)&(-x))
__int64 aa[maxn+]; //求逆序数
__int64 bb[maxn+]; //求和
int n;
void ope(int x,__int64 *dat,int val)
{
while(x<=n)
{
dat[x]+=val;
x+=lowbit(x);
}
}
__int64 getsum(int x,__int64 *dat)
{
__int64 ans=;
while(x>)
{
ans+=dat[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int i,a;
__int64 res;
while(scanf("%d",&n)!=EOF)
{
memset(aa,,sizeof(aa));
memset(bb,,sizeof(bb));
res=;
for(i=;i<n;i++)
{
scanf("%d",&a);
res+=(getsum(maxn,aa)-getsum(a,aa))*a+(getsum(maxn,bb)-getsum(a,bb));
ope(a,bb,a);
ope(a,aa,); //求逆序数
}
printf("%I64d\n",res);
}
return ;
}

HDUOJ-----2838Cow Sorting(组合树状数组)的更多相关文章

  1. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)

    Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. HDU2838Cow Sorting(树状数组)

    题目意思是说给你一列数,每次可以将相邻的两个数交换,这一步的代价是这两个数的和,求将所有数排好序的最少代价. 题解: 我们可以这么思考,由于每次都是交换相邻的两个数,所以将一个数放到它自己的位置去后, ...

  3. hdu 2838 Cow Sorting (树状数组+逆序对)

    题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...

  4. hdu2838Cow Sorting(树状数组+逆序数)

    题目链接:点击打开链接 题意描写叙述:给定一个长度为100000的数组,每一个元素范围在1~100000,且互不同样,交换当中的随意两个数须要花费的代价为两个数之和. 问怎样交换使数组有序.花费的代价 ...

  5. hdu2838 cow sorting用树状数组求逆序对

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2838/ 题目解法:题目给出一个1-n的排列,操作只有一种:交换相邻的元素,代价是两个元素之和,问将该序列变成升序 ...

  6. hdu 2838 Cow Sorting(树状数组)

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. 树状数组--K.Bro Sorting

    题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/D Description Matt’s frie ...

  8. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

  9. 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 ...

随机推荐

  1. 第十章 Redis持久化--RDB+AOF

    注:本文主要参考自<Redis设计与实现> 1.Redis两种持久化方式 RDB 执行机制:快照,直接将databases中的key-value的二进制形式存储在了rdb文件中 优点:性能 ...

  2. [转]linux sort 命令详解

    原文网址:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html 1 sort的工作原理 sort将文件的每一行作为一个单位,相互 ...

  3. 访问者模式讨论篇:java的动态绑定与双分派

    java的动态绑定 所谓的动态绑定就是指程执行期间(而不是在编译期间)判断所引用对象的实际类型,根据其实际的类型调用其相应的方法.java继承体系中的覆盖就是动态绑定的,看一下如下的代码: class ...

  4. [置顶] 一道经典的sql面试题不同的写法

    用一条SQL语句   查询出每门课都大于80分的学生姓名,表( #test)如下:    Name Course Mark 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 ...

  5. Android中ActionBar及Overflow的显示

    最近在按照Android的API文档学习Android中actionbar的使用,Action bar 最基本的形式,就是为 activity 显示标题,并且在标题左边显示一个 app icon.在这 ...

  6. CentOS开启和关闭防火墙

    CentOS Linux开启和关闭防火墙命令有两种,一种是临时的,重启即复原:另外一种是永久性的,重启不会复原.   1) 临时生效,重启后复原 开启: service iptables start ...

  7. jquery 返回顶端组件

    自己写了一个基于jquery的返回页面顶端的组件. (function($) { var g; $.backtop = function(options) { extend($.backtop.con ...

  8. 游戏服务器框架:Leaf/go

    Leaf 是一个使用 Go 语言开发的开源游戏服务器框架,注重运行效率并追求极致的开发效率.Leaf 适用于几乎所有的游戏类型.其主要的特性: 良好的使用体验.Leaf 总是尽可能的提供简洁和易用的接 ...

  9. Oracle设置为自启动

    Oracle设置为自启动 学习了:http://blog.itpub.net/31015730/viewspace-2148412/ oracle自带dbstart命令,主要进行系统启动设置: 在/e ...

  10. Asp.Net 之 <%%>相关内联代码块用法

    1.<%@ ... %> 用来添加命名空间引用,如:<%@ import namespace="system.data"> 2.<% ... %> ...