Cow Sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2239    Accepted Submission(s): 711

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

题意:给出一组数从1到N打乱,要求把数组又一次有序(从小到大),仅仅能交换相邻的两个数字。代价为相邻两个数字和。求最小代价?

思路:对于每一个数字x,我们仅仅须要把它和前面比它大的数字交换。求出交换代价,反复运行就能得出答案。

这个代价就是,比它大的数字个数t*x+前面比它大的数字和。

<pre name="code" class="cpp">#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<functional>
#include<iostream>
using namespace std;
#define N 100005
#define ll __int64
int a[N],cnt[N],n; //记录数字个数
ll sum[N]; //记录数字和
int lowbit(int x)
{
return x&(-x);
}
void add(int x)
{
int d=x;
while(x<=n)
{
cnt[x]++;
sum[x]+=d;
x+=lowbit(x);
}
}
int sum1(int x) //求比x小的数字已经出现几个(包含x)
{
int s=0;
while(x)
{
s+=cnt[x];
x-=lowbit(x);
}
return s;
}
ll sum2(int x) //求当前出现的比x大的数字和
{
ll s=0;
while(x)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int i,k,t;
while(scanf("%d",&n)!=-1)
{
memset(cnt,0,sizeof(cnt));
memset(sum,0,sizeof(sum));
ll ans=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
add(a[i]);
t=sum1(a[i]);
k=i-t; //前面有几个比x大的数
if(k!=0)
{
ans+=(ll)a[i]*k; //注意数字会超出int
ans+=sum2(n)-sum2(a[i]); //求当前位置出现的比x大的数字和
}
}
printf("%I64d\n",ans);
}
return 0;
}


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

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

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

  2. hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

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

  3. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  4. LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组

    \(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...

  5. HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用

    这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...

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

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

  7. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  8. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  9. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  10. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

随机推荐

  1. SqlParameter参数化查询

    上篇博客写了关于重构代码用到的SQLHelper类,这个类包括四种函数,根据是否含参和是否有返回值各分两种.在这里写写传参过程用到的SqlParameter. 如果我们使用如下拼接sql字符串的方式进 ...

  2. CentOS6 yum源支持更多rpm包的升级(使用第三方软件库EPEL、RPMForge与RPMFusion)

    转载于http://blog.csdn.net/erazy0/article/details/6878153 在CentOS下运行yum install flash-plugin或yum instal ...

  3. android布局margin和padding差异!

    事实上从使用的时候就能够差别开来. android:padding android:layout_margin padding是在本控件级别的,而margin是在layout级别的. 最好拿有背景的控 ...

  4. 自己总结的ruby on rails 查询方法

    闲来无事,结合以前的代码,总结了ruby on rails的查询方法,方便自己以后查看,也方便后来人,如下,欢迎批评指正 1::simpleDB modules = find(:all, :condi ...

  5. Python 技巧

    1.根据路径导入模块 如果想引用指定路径下的某个模块,则需要使用sys.path.append("module_directory") 来把这个路径添加到sys下,这就涉及到Pyt ...

  6. JSP的学习(8)——JSP标签

    JSP标签也称为JSP Action(JSP动作)元素,用于在JSP页面中封装Java代码,这样使得在JSP页面中避免直接编写Java代码,让JSP真正成为MVC模式中的作为视图作用. 几个JSP常用 ...

  7. 【ASP.NET Web API教程】3 Web API客户端

    原文:[ASP.NET Web API教程]3 Web API客户端 Chapter 3: Web API Clients 第3章 Web API客户端 本文引自:http://www.asp.net ...

  8. J2EE SSH学习(二)安装Eclipse插件和第一个Eclipse项目

    (一)安装Eclipse插件 Eclipse有很多功能很强大的插件,我现在作为一个菜鸟只知道插件的功能通常都很牛叉实用或者很有趣,那么该怎么安装Eclipse插件呢? 我使用的是Eclipse 4.3 ...

  9. 快速排序的时间复杂度nlogn是如何推导的??

    本文以快速排序为例,推导了快排的时间复杂度nlogn是如何得来的,其它算法与其类似. 对数据Data = { x1, x2... xn }: T(n)是QuickSort(n)消耗的时间: P(n)是 ...

  10. quarze的工作原理

    quartz的工作原理 http://lavasoft.blog.51cto.com/62575/181907/ 几种定时任务的比較 http://blog.sina.com.cn/s/blog_69 ...