POJ 1990 MooFest(树状数组)

MooFest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9006   Accepted: 4073

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

【题意】有n头牛,排列成一条直线,给出每头牛在直线上的坐标d。每头牛有一个v,如果牛i和牛j想要沟通的话,它们必须用max(v[i],v[j]),消耗的能量为:max(v[i],v[j]) * 它们之间的距离.

问要使所有的牛之间都能沟通(两两之间),总共需要消耗多少能量。

【思路】现将v从小到大排列,使得每次取到的是当前最大的v。

c[1]记录当前牛的数量c[2]记录当前所有牛的d之和。(二维树状数组)

题目大意:

给定n头牛的听力和坐标,每两头牛交谈需要(max(v(i),v(j) )*abs(dis[i]-dis[j])),n头牛总共要交谈(n*(n-1)/2)次

思路:

对牛的听力进行从小到大排序,那么对于第i头牛 交谈,需要计算

1:坐标比第i头牛小的牛的数量 a,坐标和b

2:坐标比第i头牛大的牛的数量 c,坐标和d

那么第i头牛交谈总共需要:

(d-c*a[i].y+a*a[i].y-b)*a[i].x

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N = 20000;
 7 struct node
 8 {
 9     int d, v;
10     bool operator <(const node &a)const
11         //从小到大排序,使得当前获得的v一定是出现过最大的。
12     {
13         return v<a.v;
14     }
15 }moo[N + 10];
16 int c[3][N + 10];
17 void update(int i, int d, int v)
18 {
19     while (d <= N)
20     {
21         c[i][d] += v;
22         d +=(d&-d);
23     }
24 }
25 int get_sum(int i, int d)
26 {
27     int res = 0;
28     while (d)
29     {
30         res += c[i][d];
31         d -= (d&-d);
32     }
33     return res;
34 }
35 int main()
36 {
37     int n;
38     while (~scanf("%d", &n))
39     {
40         memset(c, 0, sizeof(c));
41         for (int i = 1; i <= n; i++)
42             scanf("%d%d", &moo[i].v, &moo[i].d);
43         sort(moo + 1, moo + 1 + n);
44         int sum = 0;//记录所有坐标之和
45         long long int ans = 0;
46         for (int i = 1; i <= n; i++)
47         {
48             int d = moo[i].d;
49             sum += d;
50             update(1, d, 1);//c[1]记录牛数量
51             update(2, d, d);//c[2]记录牛坐标之和
52             int n1 = get_sum(1, d);//在i牛及他前面有多少头
53             int n2 = get_sum(2, d);//在i牛及他前面的牛坐标和为多少
54             int tmp1 = n1 * d - n2;//i左边的坐标差
55             int tmp2 = sum - n2 - d * (i - n1);//i右边的坐标差
56             ans += (long long int)(tmp1 + tmp2)*moo[i].v;
57             //不用longlong会溢出
58         }
59         printf("%lld
", ans);
60     }
61     return 0;
62 }
原文地址:https://www.cnblogs.com/caiyishuai/p/8488099.html