POJ 1990 MooFest

Posted zsychanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1990 MooFest相关的知识,希望对你有一定的参考价值。

MooFest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5339   Accepted: 2300

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

Source

解题思路:首先按牛的声音大小进行排序,这样就保证了与当前牛交流的牛都是以当前牛的声音为标准的,然后考虑当前牛的左右两側的牛到当前牛的距离之和,令s1表示牛左側的牛的数目,s2表示牛的左側的牛到当前牛的距离之和,total表示当前的全部牛的总距离长度
左側距离L=cow[i].x*s1-s2;右側距离R=(total-s2)-(i-s1)*cow[i].x;
s1,s2用树状数组维护
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int cnt[20005],len[20005];
struct COW
{
	int v;
	int x;
}cow[20005];
int cmp(COW a,COW b)
{
	return a.v<b.v;
}
int lowbit(int x)
{return x&(-x);}
void add(int *s,int pos,int n,int val)
{
	int i;
	for(i=pos;i<=n;i+=lowbit(i))
		s[i]+=val;
}
int sum(int *s,int pos,int n)
{
	int i,sum=0;
	for(i=pos;i>0;i-=lowbit(i))
		sum+=s[i];
	return sum;
}
int main()
{
	int i,n,m;
	long long ans,total,s1,s2;
	while(scanf("%d",&n)!=-1)
	{
		ans=total=m=0;
		memset(cnt,0,sizeof(cnt));
		memset(len,0,sizeof(len));
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&cow[i].v,&cow[i].x);
			if(m<cow[i].x)
				m=cow[i].x;
		}
		sort(cow+1,cow+1+n,cmp);
		for(i=1;i<=n;i++)
		{
			add(cnt,cow[i].x,m,1);
			add(len,cow[i].x,m,cow[i].x);
			total+=cow[i].x;
			s1=sum(cnt,cow[i].x-1,m);
			s2=sum(len,cow[i].x-1,m);
                        long long L=<strong style="font-family: ‘Times New Roman‘, Times, serif; background-color: rgb(255, 255, 255);"></strong><pre name="code" class="cpp" style="display: inline !important;">s1*cow[i].x-s2;

long long R=
total-s2-(i-s1)*cow[i].x;

ans+=cow[i].v*(L+R);}cout<<ans<<endl;}return 0;}



以上是关于POJ 1990 MooFest的主要内容,如果未能解决你的问题,请参考以下文章

POJ1990 MooFest

POJ 1990 MooFest

POJ 1990 MooFest

MooFest POJ - 1990 树状数组

MooFest POJ - 1990 (树状数组)

POJ 1990 MooFest