hdu_2688_Cow Sorting(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838

题目大意:可以认为是一种插入排序,求交换成升序序列的价值最小

题目思路:用两个树状数组来存储,C1[i]来存储grumpy小于等于i的的个数,C2[i]来存储gumpy小于等于i的grumpy和,

并且有一个需要证明的定理:对某个位置i,如果前面比他大的有x个,那么a[i]至少要加x次;也就是说用两个树状数组来分别维护当前位置时前面有多少比他大;关于证明,假如任意一个数字左边有N个比它大的数字,那么在最后的排序结果中是升序的,那么左边这些数字必定要跑到这个的右边去,必定发生的交换次数就是N次,需要的时间最小就是左边的这些数的和加上对应的这个数字自身的消费。这是下限。

题目:

Cow Sorting

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


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).
 
代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int size=100005;
__int64 C1[size],C2[size];
int n;
int lowbit(int x)
{
    return x&(x^(x-1));
	//return x&-x;
}
__int64 sumC1(int end)
{
    __int64 ans=0;
    while(end>0)
    {
        ans+=C1[end];
        end-=lowbit(end);
    }
    return ans;
}
__int64 sumC2(int end)
{
    __int64 ans=0;
    while(end>0)
    {
        ans+=C2[end];
        end-=lowbit(end);
    }
    return ans;
}
void updata(int pos,int w,int num)
{
    while(pos<=size)
    {
        C1[pos]+=num;
        C2[pos]+=w;
        pos+=lowbit(pos);
    }
}
int main()
{
    int tmp;
    while(scanf("%d",&n)!=EOF)
    {
        memset(C1,0,sizeof(C1));
        memset(C2,0,sizeof(C2));
        __int64 ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&tmp);
            updata(tmp,tmp,1);
            __int64 fig=i-sumC1(tmp);
            if(fig!=0)
            {
                __int64 su=sumC2(n)-sumC2(tmp);
                ans+=fig*tmp+su;
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值