poj 1990 MooFest(树状数组)

115 篇文章 0 订阅
11 篇文章 0 订阅

MooFest

Description

Every year, Farmer John’s N (1 <= N <= 20,000) cows attend “MooFest”,a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing.

Each cow i has an associated “hearing” threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)).

Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.

Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.
Input

  • Line 1: A single integer, N

  • Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location.
    Output

  • Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows.
    Sample Input

4
3 1
2 5
2 6
4 3
Sample Output

57

题意:一群牛参加完牛的节日后都有了不同程度的耳聋,第i头牛听见别人的讲话,别人的音量必须大于v[i],当两头牛i,j交流的时候,交流的最小声音为max{v[i],v[j]}*他们之间的距离。现在有n头牛,求他们之间两两交流最少要的音量和。

分析:树状数组。首先将这n头牛按照v值从小到大排序,这样的话,排在后面的牛和排在前面的牛讲话,两两之间所用的音量必定为后面的牛的v值,因此对于每一头牛只需要让它和它前面的所有牛讲话即可。

这样,知道了讲话的v值,还需要知道对于牛i,它前面总共有多少头牛,和在它前面所有牛的位置之和。所以我们需要两个数组,一个a[]记录个数,另一个b[]记录位置之和。

对于每一头牛i讲话的总能量为v[i]*(num*pos[i]-sum)(num为比牛i的v小的牛的个数,sum为比牛i的v小的所有牛的位置之和,具体详见代码)

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define maxn 20010
typedef long long LL;
struct node
{
    LL pos,val;
} q[maxn];
LL a[maxn],b[maxn];//a[]存储个数,b[]存储位置和
LL n;

LL lowbit(LL x)
{
    return x&-x;
}

LL update_sum(LL x,LL num)//更新位置之和
{
    while(x<maxn)
    {
        b[x]+=num;
        x+=lowbit(x);
    }
}

LL get_sum(LL x)//得到位置小于等于x的所有牛的位置之和
{
    LL sum=0;
    while(x)
    {
        sum+=b[x];
        x-=lowbit(x);
    }
    return sum;
}

LL update_num(LL x,LL num)//更新个数
{
    while(x<maxn)
    {
        a[x]+=num;
        x+=lowbit(x);
    }
}

LL get_num(LL x)//得到小于位置x的牛的个数
{
    LL sum=0;
    while(x)
    {
        sum+=a[x];
        x-=lowbit(x);
    }
    return sum;
}

bool cmp(node a,node b)
{
    if(a.val==b.val)
        return a.pos<b.pos;
    return a.val<b.val;
}

LL absolute(LL x)//求绝对值
{
    return x<0?-x:x;
}

int main()
{
    scanf("%lld",&n);
    for(LL i=1; i<=n; ++i)
        scanf("%lld%lld",&q[i].val,&q[i].pos);
    sort(q+1,q+1+n,cmp);
    LL ans=0;
    for(LL i=1; i<=n; ++i)
    {
        ans+=absolute(get_num(q[i].pos-1)*q[i].pos-get_sum(q[i].pos-1))*q[i].val;
        //求位置小于q[i](并且v小于q[i])的总能量值之和
        ans+=absolute((get_num(maxn-1)-get_num(q[i].pos))*q[i].pos-(get_sum(maxn-1)-get_sum(q[i].pos)))*q[i].val;
        //求位置大于q[i](并且v小于q[i])的总能量值之和
        update_num(q[i].pos,1);//更新
        update_sum(q[i].pos,q[i].pos);
    }
    printf("%lld\n",ans);
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值