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

USACO 2004 U S Open

解题思路:首先按牛的声音大小进行排序,这样就保证了与当前牛交流的牛都是以当前牛的声音为标准的,然后考虑当前牛的左右两侧的牛到当前牛的距离之和,令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;}

				
时间: 2024-11-09 04:01:40

POJ 1990 MooFest的相关文章

POJ 1990 MooFest(zkw线段树)

[题目链接] http://poj.org/problem?id=1990 [题目大意] 给出每头奶牛的位置和至少要多少分贝的音量才能听到谈话 现在求奶牛两两交流成功需要的分贝*距离的总和. [题解] 我们将奶牛对于需要的分贝排序,那么在计算的时候, 每头奶牛只要计算和序列前面所有奶牛的答案即可 那么只要维护前面所有奶牛和其距离之差的绝对值之和即可 将其拆分为坐标小于该奶牛的坐标之和,和大于该奶牛的部分 发现只要用线段树维护区间和以及区间数的个数即可. [代码] #include <cstdio

POJ 1990 MooFest(树状数组)

                                                                    MooFest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7077   Accepted: 3181 Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest"

POJ 1990——MooFest(2个树状数组)

MooFest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5235   Accepted: 2260 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

POJ 1990 MooFest 树状数组

这题是我看了大白书树状数组后刷的第一道题,确实难度不小,所以只好上网找题解了,网上的做法确实精彩.这题的题意主要是有N头牛,每两头牛之间交流的费用为它们的距离乘上两者音量的最大值(即max(v(i),v(j))),然后统计所有牛两两交流的总费用.一开始能想到的做法便是O(n2)的暴力枚举了,当时的我也只能想到这样的复杂度,很纳闷怎么能和树状数组搭上边呢?然后看了别人的题解后才惊叹其思路之妙. 在博客 http://www.cnblogs.com/Fatedayt/archive/2011/10/

POJ 1990 MooFest【 树状数组 】

题意:给出n头牛,每头牛有一个听力v,坐标x,两头牛之间的能量为max(v1,v2)*dist(v1,v2),求总的能量值 先将每头牛按照v排序,排完顺序之后,会发现有坐标比当前的x小的,会有坐标比当前的x大的 假设坐标比x小的有num个 那么 距离之和 = x*num - 前面坐标的和 + 后面坐标的和 - (n-num-1)* x 又因为 后面坐标的和 = 整个区间坐标的和 - 前面坐标的和 所以用两个数组,一个数组用来算个数,另外一个数组用来算和 学习的这一篇题解--http://www.

MooFest POJ - 1990 (树状数组)

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 cours

MooFest POJ - 1990

测评传送门 题意: n 头牛互相对着叫,每头牛有一个 耳背度ai,位置bi; 使得两头牛可以互相听见需要的音量 abs(bi - bj)*max(ai , aj) 求所有牛可以互相听见的总音量 input 4 3 1 2 5 2 6 4 3 output 57 思路: n方的暴力做法很显然,也很好写,但必挂! 于是就需要用数据结构来优化 code #include<stdio.h> #include<algorithm> #define lowbit(x) x&(-x) #

POJ1990--POJ 1990 MooFest(树状数组)

Time Limit: 1000MSMemory Limit: 30000K Total Submissions: 8141Accepted: 3674 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 e

I-MooFest(POJ 1990)

MooFest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5697   Accepted: 2481 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