首页 > 代码库 > poj 1990 MooFest
poj 1990 MooFest
MooFest
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7513 | Accepted: 3373 |
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.
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.
* 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
题意:一共有N头牛拍成一列,每头牛耳聋程度为Vi,所在位置为Li,并且每两头牛之间都要进行交互,若牛i与牛j进行交互,交互所需要的音量为max(Vi,Vj)*|Li-Lj|,则那么像这样的交互总共需要N(N-1)/2次交互,问总共会发出多少音量。
思路:首先可以将所有牛按照耳聋程度从低到高排列,之后按此顺序一头牛一头牛取出来的方式来考虑,可以知道每次新取出的那头牛总是已经取出的牛中耳聋程度最大的,那么以这头牛为中心,计算其他已取出的牛与这头牛进行交互所发出的所有
音量,那么问题的关键在于如何计算这些音量,可以使用数据结构线段树组BIT,建立两个线段树组——bit_num和bit_pos,分别用来存已经取出的牛的数量以及每头牛对应的位置。这样,让我们继续考虑刚才的问题,假设新取出一头牛,设这头牛
的位置在x,耳聋程度v,那么使用bit_num的求和功能,计算出在位置x之前的牛的数量总和num,再用bit_pos求出位置在x之前的所有牛的位置xi的总合pos,那么在当前牛的左边牛与该牛发生交互所产生的音量总和可以计算为v*(num*x-pos),
那么在当前牛右边所有的牛与该牛发生交互所产生的音量同理也可以计算出。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> using namespace std; const int N_MAX = 20000+3; typedef long long ll; int N, Q; pair<int, int>cows[N_MAX]; //BIT ll bit_num[N_MAX], bit_pos[N_MAX]; ll sum(ll *b, int i) {//树状数组b的前i个数求和 ll s = 0; while (i > 0) { s += b[i]; i -= i&(-i); } return s; } ll sum(ll*b,int x,int y) {//计算区间[x,y)的和 return sum(b, y-1) - sum(b,x-1); } void add(ll *b,int i,int v) { while (i <= N_MAX) { b[i] += v; i += i&-i; } } int main() { scanf("%d",&N); for (int i = 0; i < N;i++) { scanf("%d%d",&cows[i].first,&cows[i].second); } sort(cows,cows+N);//按耳聋的程度由低到高排序 ll res = 0; for (int i = 0; i < N;i++) {//一头牛一头牛的放进树状数组 int v = cows[i].first, pos = cows[i].second; int right_num, left_num; right_num = sum(bit_num, 1, pos); left_num = sum(bit_num, pos, N_MAX); res += v*(right_num*pos - sum(bit_pos, 1, pos) + sum(bit_pos, pos + 1, N_MAX) - left_num*pos); add(bit_num, pos, 1); add(bit_pos, pos, pos); } printf("%lld\n",res); return 0; }
poj 1990 MooFest
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。