Try Again

POJ 1990 MooFest

MooFest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7861   Accepted: 3552

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
题目大意:每头牛都有一个坐标pos,和听力v[i],两头交谈时,消耗的为(abs(x.pos-y.pos)*max(v[x].v[y]))N头牛共交谈(N*(N-1))/2次,问共
消耗多少。
解题思路:
//将N头牛排序后,从零开始便利后,只记录第i头牛前面有几头小于他
//注意此时没有加他本身,所以从零开始便利,aldis查找一个加一个,则count为便利到目前为止
//所有牛的坐标和,disx为左边存在的坐标比他小的牛的个数,aldis为坐标彼此牛小的坐标和
//所以此牛左边坐标比他大的牛数为i(不含本身)-disx,坐标比他大的牛的坐标和为count-aldis
//所以总坐标距离差为disx*node[i].pos-aldis+count-aldis-node[i].pos*(i-disx);
//总消费求和即
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) x&(-x)
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
const int mod=20006;
int vis[mod]/*记录个数*/,cnt[mod]/*求和*/,n;
struct Node
{
    int dis;
    int pos;
    friend bool operator<(const Node &a,const Node &b)
    {
        return a.dis<b.dis;
    }
}node[mod];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
}
void add(int *vis,int x,int val)
{
    for(int i=x;i<mod;i+=lowbit(i))
    {
        vis[i]+=val;
    }
}
ll query(int *vis,int x)
{
    ll ans=0;
    for(int i=x;i;i-=lowbit(i))
    {
        ans+=vis[i];
    }
    return ans;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        ll ans=0;
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&node[i].dis,&node[i].pos);
        }
        sort(node,node+n);
        ll count=0;
        for(int i=0;i<n;i++)
        {
            ll disx=query(vis,node[i].pos);//坐标小于第i头牛的个数
            ll rdisx=i-disx;//则左边坐标大于第i头牛的个数
            ll aldis=query(cnt,node[i].pos);//坐标小于第i头牛的坐标和
            ll raldis=count-aldis;//左边第i头牛的坐标和
            add(vis,node[i].pos,1);
            add(cnt,node[i].pos,node[i].pos);
            ans+=1ll*node[i].dis*(raldis-rdisx*node[i].pos+node[i].pos*disx-aldis);
            count+=node[i].pos;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

 

 
posted @ 2017-07-12 16:12  十年换你一句好久不见  阅读(287)  评论(0编辑  收藏  举报