首页 > 代码库 > MooFest_二维树状数组
MooFest_二维树状数组
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
43 12 52 64 3
Sample Output
57
【题意】有n头牛,排列成一条直线,给出每头牛在直线上的坐标d。每头牛有一个v,如果牛i和牛j想要沟通的话,它们必须用max(v[i],v[j]),消耗的能量为:max(v[i],v[j]) * 它们之间的距离.
问要使所有的牛之间都能沟通(两两之间),总共需要消耗多少能量。
【思路】现将v从小到大排列,使得每次取到的是当前最大的v。
c[1]记录当前牛的数量c[2]记录当前所有牛的d之和。(二维树状数组)
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int N=20000;struct node{ int d,v; bool operator <(const node &a)const //从小到大排序,使得当前获得的v一定是出现过最大的。 { return v<a.v; }}moo[N+10];int c[3][N+10];int lowbit(int x){ return x&(-x);}void update(int i,int d,int v){ while(d<=N) { c[i][d]+=v; d+=lowbit(d); }}int get_sum(int i,int d){ int res=0; while(d) { res+=c[i][d]; d-=lowbit(d); } return res;}int main(){ int n; while(~scanf("%d",&n)) { memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) scanf("%d%d",&moo[i].v,&moo[i].d); sort(moo+1,moo+1+n); int sum=0;//记录所有坐标之和 long long int ans=0; for(int i=1;i<=n;i++) { int d=moo[i].d; sum+=d; update(1,d,1);//c[1]记录牛数量 update(2,d,d);//c[2]记录牛坐标之和 int n1=get_sum(1,d);//在i牛及他前面有多少头 int n2=get_sum(2,d);//在i牛及他前面的牛坐标和为多少 int tmp1=n1*d-n2;//i左边的坐标差 int tmp2=sum-n2-d*(i-n1);//i右边的坐标差 ans+=(long long int)(tmp1+tmp2)*moo[i].v; //不用longlong会溢出 } printf("%lld\n",ans); } return 0;}
MooFest_二维树状数组
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。