POJ - 1990 MooFest

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头牛,求他们之间两两交流最少要的音量和。

思路:定义结构体,将声音从小到大排序,当计算某头牛时,只考虑声音的值比它小的那些牛。我们用两个树状数组分别表示牛的坐标之和及牛的数量之和

 

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=20000+100;
struct node {
    int v,x;
    bool operator <(const node &b)const
    {
        return v<b.v;
    }
}nodes[MAXN];
int c[3][MAXN]; //c[1]记录头数  c[2]记录距离 
int lowbit(int x)
{
    return x&(-x);
}
int sum(int i,int x)
{
    int res=0;
    while(x)
    {
        res +=c[i][x];
        x-=lowbit(x);
    }
    return res;
}
void add(int i,int x,int v)
{
    while(x<MAXN)
    {
        c[i][x]+=v;
        x+=lowbit(x);
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)==1&&n)
    {
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&nodes[i].v,&nodes[i].x);
        }
        sort(nodes+1,nodes+n+1);//按声音排序 
 
        int total=0;//记录所有坐标和
        long long ans=0;//结果
        for(int i=1;i<=n;i++)
        {
            int x = nodes[i].x;
            total+=x;
            add(1,x,1);
            add(2,x,x);
            int s1= sum(1,x);//在i牛前面的牛有多少头(包括i自己)
            int s2= sum(2,x);//在i牛前面的牛坐标和为多少(包括i自己)
            int temp1= s1*x-s2;//i左边的坐标差
            int temp2= total-s2-x*(i- s1);//i右边的坐标差
            ans += ((long long)temp1+temp2)*nodes[i].v;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

PS: 我今早帮好几个博主找了bug,下次看别人的题解一定要先提交一遍确保是正确的,我太难了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值