POJ1990 MooFest
Posted SilverNebula
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1990 MooFest相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 30000KB | 64bit IO Format: %lld & %llu |
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
Source
将牛按音量由小到大排序,算每头牛时,计算它和所有音量小于它的牛所贡献的答案。
用树状数组存牛的位置和坐标,由小到大依次添加。距离的计算方式见代码。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define LL long long 8 using namespace std; 9 const int mxn=30010; 10 struct node{ 11 int v; 12 int x; 13 }c[mxn]; 14 int cmpv(const node a,const node b){ 15 return a.v<b.v; 16 } 17 int n; 18 LL ans=0; 19 // 20 LL t[2][mxn+1]; 21 inline int lowbit(int x){return x&-x;} 22 void add(int k,int p,int v){//k==0 个数 k==1 距离 23 while(p<=mxn){t[k][p]+=v;p+=lowbit(p);} 24 } 25 LL smm(int k,int x){ 26 LL res=0; 27 while(x){res+=t[k][x];x-=lowbit(x);} 28 return res; 29 } 30 // 31 int main(){ 32 scanf("%d",&n); 33 int i,j; 34 for(i=1;i<=n;i++) scanf("%lld%lld",&c[i].v,&c[i].x); 35 sort(c+1,c+n+1,cmpv); 36 for(i=1;i<=n;i++){ 37 int num=smm(0,c[i].x); 38 LL dis=smm(1,c[i].x); 39 ans+=c[i].v*(num*c[i].x-dis);//算x小于当前值的牛 40 ans+=(smm(1,mxn)-dis-(i-1-num)*c[i].x)*c[i].v;//算x大于当前值的牛 41 add(0,c[i].x,1); 42 add(1,c[i].x,c[i].x); 43 } 44 printf("%lld\n",ans); 45 return 0; 46 }
以上是关于POJ1990 MooFest的主要内容,如果未能解决你的问题,请参考以下文章