MooFest
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7949 | Accepted: 3588 |
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
题意:n头奶牛每头耳背程度v,坐标x。两牛交流需要音量为distance * max(v(i), v(j)),求所有牛两两交流所需总和
解题思路:因为每次计算都是取耳背程度大的,所以先按耳背程度从小到大排序,然后第i头需要加上的答案就是:(在它前面的牛的个数*第i头牛的x-前面的牛的坐标和+后面的牛的坐标和-在它后面的牛的个数*第i头牛的x)*第i头牛的v,所以用两个树状数组来维护即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
struct node
{
int x, v;
}a[20005];
int n,sum1[20005],sum2[20005];
bool cmp(node a, node b)
{
return a.v < b.v;
}
int lowbit(int k)
{
return k&-k;
}
void add(int k, int val,int x[])
{
while (k <= 20000)
{
x[k] += val;
k += lowbit(k);
}
}
LL getsum(int k, int x[])
{
LL sum = 0;
while (k)
{
sum += 1LL * x[k];
k -= lowbit(k);
}
return sum;
}
int main()
{
while (~scanf("%d", &n))
{
memset(sum1, 0, sizeof sum1);
memset(sum2, 0, sizeof sum2);
for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].v, &a[i].x);
sort(a + 1, a + 1 + n, cmp);
LL ans = 0;
for (int i = 1; i <= n; i++)
{
LL sum = getsum(a[i].x, sum1);
LL cnt = getsum(a[i].x, sum2);
ans = ans +1LL* (1LL * cnt*a[i].x - sum)*a[i].v;
sum = getsum(20000, sum1)-sum;
cnt = getsum(20000, sum2)-cnt;
ans = ans + 1LL * (sum - 1LL * cnt*a[i].x)*a[i].v;
add(a[i].x, a[i].x, sum1);
add(a[i].x, 1, sum2);
}
printf("%lld\n", ans);
}
return 0;
}