MooFest(树状数组+离线处理)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Joined/article/details/78231387
/* 

题意:

给你n头牛的位置x,和他们的音调v。 让我们求的是什么呢,求的是两头牛之间的(位置之差)*(两头牛中的最大的音调)。

我们按照v的大小从小到大排个序,从1-n依次扫一遍,当扫到第i头牛的时候,i的音调就最大,所以 如果求出位置差的和的话,问题就解决了、


简单的总结一下:
主要分两个数组的思路很是巧妙啊。
在将一个数插进去的时候,牛的位置不一定在哪,所有看看左边的有几头牛,右边有几头牛,然后分别求出左边的差的和,右边差的和,然后两边差的和相加再乘以这头牛的音调(因为在前i头牛中,这头牛的音调最大)。
我们在这里分两个数组用来干什么的呢? 第一个数组用计数,左边有几头牛,第二个数组用来求左边的牛的位置的和,那么左边的差的和就是(现在这头牛的位置*左边牛的个数-左边牛的位置的和)
*/ 
#if 0
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXX=20000+100;
int c[3][MAXX];
int lowbit(int x)
{
 return x&(-x);
}
void add(int p,int x,int v)
{
 for(; x<MAXX; x+=lowbit(x))
 {
  c[p][x]+=v;
 }
}
int sum(int p,int x)
{
 int sum=0;
 for(; x>0; x-=lowbit(x))
 {
  sum+=c[p][x];
 }
 return sum;
}
struct node
{
 int x,v;
 bool operator <(const node& b)const
 {
  return v<b.v;
 }
}nodes[MAXX];

int main()
{
 ios::sync_with_stdio(false);
 int n;
    while(cin>>n&&n)
    {
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            cin>>nodes[i].v>>nodes[i].x;
        }
        sort(nodes+1,nodes+n+1);

        int total=0;
        long long  end=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);//左边有多少个数 
            int s2=sum(2,x);//左边数的位置的和 
            int temp1=s1*x-s2;//左边的坐标差 
            int temp2=total-s2-x*(i- s1);//右边的坐标差
       end+=((long long)temp1+temp2)*nodes[i].v;
        }
        cout<<end<<endl; 
    }
    return 0;

  
}
#endif


Problem 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 <br> <br>* 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. <br>


 


Output


* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows. <br>


 


Sample Input


4 3 1 2 5 2 6 4 3


 


Sample Output


57












没有更多推荐了,返回首页