MooFest
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7714 | Accepted: 3472 |
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个奶牛,他们必须相互发出max(v[i],v[j])*abs(x[i]-x[j])的声音才能相互听到,问使每两个奶牛都互相听到声音,所需的总声音是多少;
分析:
直接暴力的话,20000*20000肯定要T;
由于我事先知道了是树状数组,所以就没往别的方面想,如果事先不知道的话,估计不会往这上面想;
对于每个奶牛,先将声音v从小到大排序,那么,对于每两个牛来说,取的一定是后者的v;
这样就可以用树状数组维护;
开两个数组 a[]存储其他牛个数,b[]其他牛存储位置和
对于每一头牛i讲话的总能量为(num*x[i]-sum)*v[i],num为其他牛的个数,sum为其他牛的坐标和;
由于其他牛的坐标相对于这头牛有>,<,两种情况,直接都加到sum里正负会冲突,所以干脆变成两种情况
1~i-1,i+1~maxn,小于的情况和大于的情况;
然后用ans记录总和即可
#include <iostream>
#include <iomanip>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<stdlib.h>
#include<queue>
#include<map>
#include<math.h>
#include<algorithm>
#include<vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define maxn 20005
using namespace std;
ll n;
struct node
{
ll v,x;
}e[maxn];
ll a[maxn],b[maxn];
bool cmp(node a,node b)
{
if(a.v==b.v)
return a.x<b.x;
return a.v<b.v;
}
ll lowbit(ll x)
{
return x&-x;
}
void add_num(ll x,ll num)
{
for(ll i=x;i<=maxn;i+=lowbit(i))
a[i]+=num;
}
void add_sum(ll x,ll num)
{
for(ll i=x;i<=maxn;i+=lowbit(i))
b[i]+=num;
}
ll read_num(ll x)
{
ll sum=0;
for(ll i=x;i>0;i-=lowbit(i))
{
sum+=a[i];
}
return sum;
}
ll read_sum(ll x)
{
ll sum=0;
for(ll i=x;i>0;i-=lowbit(i))
{
sum+=b[i];
}
return sum;
}
ll absolute(ll x)
{
return x<0?-x:x;
}
int main()
{
scanf("%lld",&n);
for(ll i=1;i<=n;i++)
scanf("%lld%lld",&e[i].v,&e[i].x);
sort(e+1,e+1+n,cmp);
ll ans=0;
for(ll i=1;i<=n;i++)
{
ans+=absolute((read_num(e[i].x-1)*e[i].x-read_sum(e[i].x-1))*e[i].v);
ans+=absolute(((read_num(maxn)-read_num(e[i].x))*e[i].x-(read_sum(maxn)-read_sum(e[i].x)))*e[i].v);
add_num(e[i].x,1);
add_sum(e[i].x,e[i].x);//从第二组开始计算,add放后面
}
printf("%lld\n",ans);
}