Description:
There are points on a coordinate axis OX. The point is located at the integer point and has a speed . It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the point at the moment (t can be non-integer) is calculated as .
Consider two points and . Let be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value will be 0.
Your task is to calculate the value (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer — the number of points.
The second line of the input contains integers , where xi is the initial coordinate of the i-th point. It is guaranteed that all xi are distinct.
The third line of the input contains integers , where is the speed of the point.
Output
Print one integer — the value (the sum of minimum distances over all pairs of points).
Examples
input
3
1 3 2
-100 2 3
output
3
input
5
2 1 4 3 5
2 2 2 3 4
output
19
input
2
2 1
-3 0
output
0
解题思路:
给出 个点和他们的初始位置 和移动速度 ,问所有点对在任意时刻全部最小值的和是多少,不是同一时刻而是每个点对的最小值的和,可以是很多时刻。
时间可以是任意实数,对于任意两点,如果 ,且 ,那么 永远不可能相遇。否则肯定可以相遇,所以只需要计算 ,且 的情况即可。这种情况贡献就是距离,树状数组维护一个和就好了。
AC代码:
const int N = 5e3 + 5;
int n, d;
int ans[N];
int ch[N];
int fa[N];
int c, now, mx;
int cnt, pos;
int get_dep()
{
int ans = now;
c++;
if (c == mx)
now++, mx *= 2, c = 1;
return ans;
}
int main()
{
int t;
sd(t);
while (t--)
{
mem(ch, 0);
sdd(n, d);
cnt = 0;
rep(i, 1, n - 1)
{
ans[i] = i;
cnt += i;
}
pos = n - 1;
now = 1, c = 1, mx = 2;
if (cnt < d)
{
puts("NO");
continue;
}
while (cnt > d)
{
int dep = get_dep();
if (dep >= pos)
break;
int tmp = cnt - d;
if (tmp >= (pos - dep))
{
cnt -= pos - dep;
ans[pos] = dep;
}
else
{
ans[pos] -= tmp;
cnt = d;
}
pos--;
}
if (cnt > d)
puts("NO");
else
{
puts("YES");
ans[0] = 0;
rep(i, 1, n - 1)
{
rep(j, 0, n - 1) if (ans[j] == ans[i] - 1 && ch[j] < 2)
{
ch[j]++;
fa[i] = j;
break;
}
}
rep(i, 1, n - 1)
{
printf("%d%c", fa[i] + 1, i == n - 1 ? '\n' : ' ');
}
}
}
return 0;
}