博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
https://codeforces.com/contest/1311F. Moving Points
阅读量:3952 次
发布时间:2019-05-24

本文共 2414 字,大约阅读时间需要 8 分钟。

F. Moving Points

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn points on a coordinate axis OXOX. The ii-th point is located at the integer point xixi and has a speed vivi. It is guaranteed that no two points occupy the same coordinate. All nn points move with the constant speed, the coordinate of the ii-th point at the moment tt (tt can be non-integer) is calculated as xi+t⋅vixi+t⋅vi.

Consider two points ii and jj. Let d(i,j)d(i,j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points ii and jj coincide at some moment, the value d(i,j)d(i,j) will be 00.

Your task is to calculate the value ∑1≤i<j≤n∑1≤i<j≤n d(i,j)d(i,j) (the sum of minimum distances over all pairs of points).

Input

The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of points.

The second line of the input contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤1081≤xi≤108), where xixi is the initial coordinate of the ii-th point. It is guaranteed that all xixi are distinct.

The third line of the input contains nn integers v1,v2,…,vnv1,v2,…,vn (−108≤vi≤108−108≤vi≤108), where vivi is the speed of the ii-th point.

Output

Print one integer — the value ∑1≤i<j≤n∑1≤i<j≤n d(i,j)d(i,j) (the sum of minimum distances over all pairs of points).

Examples

input

Copy

31 3 2-100 2 3

output

Copy

3

input

Copy

52 1 4 3 52 2 2 3 4

output

Copy

19

input

Copy

22 1-3 0

output

Copy

0

【题意】

给你n个点,每个点有一个速度v,对于任意时刻,每个点的位置为x+vt。问对于所有点i,j,∑1≤i<j≤n d(i,j)的最小值是多少。

题解:简单推算可以发现,对于每个位置,考虑位置在它左面且速度比他小的或者在它右面速度比他大的,只有这两种情况答案不为0,其他早晚会相遇,为0,这两种情况是我们需要计算的值,这两种其实是同一种情况,以每个位置为右端点,这样只需找出第i个位置左面所有比他速度小的数。将速度离散化(和速度无关)后,将位置排序,对于每个位置,ans+=这个位置的x*(比他速度小的点的数量)-(比他速度小的点的x的和)。求和就要想到树状数组,用两个树状数组num,和sum分别维护这两个变量。

#include
#define ll long longusing namespace std;const int maxx=2e5+100;struct node{ int x,v; bool operator<(const node &a)const //先把x排序,直接排完 { return x
>n; for(int i=1;i<=n;i++) cin>>p[i].x; for(int i=1;i<=n;i++) cin>>p[i].v,b[i]=p[i].v; sort(p+1,p+1+n); sort(b+1,b+1+n); int len=unique(b+1,b+1+n)-b-1;//速度去重 init(); ll ans=0; for(int i=1;i<=n;i++) { int pos=lower_bound(b+1,b+1+len,p[i].v)-b;//有多少个速度比它小的 ans+=query(num,pos)*(ll)p[i].x-query(sum,pos); add(num,pos,1);//前面比他速度小的点的数量 add(sum,pos,p[i].x);//前面比他速度小的点的x之和 } cout<
<

 

转载地址:http://utyzi.baihongyu.com/

你可能感兴趣的文章
1017 A除以B (20 分)
查看>>
1019 数字黑洞 (20 分)
查看>>
1032 挖掘机技术哪家强 (20 分)
查看>>
今夕何夕 HDU - 6112 ( 模拟 )
查看>>
Dividing HDU - 1059 ( 多重背包 - 二进制简化 )
查看>>
Robberies HDU - 2955 ( 0-1背包 )
查看>>
FATE HDU - 2459 ( 二维完全背包 )
查看>>
B. Working out CodeForces - 429B (动态规划)
查看>>
10635 - Prince and Princess UVA-10635 (最长公共子序列的O(nlogn)的解法:LCS转换为LIS)
查看>>
Sizeof和Strlen
查看>>
lower_bound和upper_bound
查看>>
Subsequence POJ - 3061 ( 尺取法 )
查看>>
常见HTTP状态码大全
查看>>
Python很简单?学会魔术方法才算入门!
查看>>
大数据揭秘:低学历成功逆袭概率多少?结果令人震惊!
查看>>
这16个数据可视化案例,惊艳了全球数据行业
查看>>
大数据死亡率报告揭秘:SUV与轿车到底谁更危险?
查看>>
2017年网络流行语TOP20 , 没用过算我输!
查看>>
GitHub最著名的20个Python机器学习项目!
查看>>
看完这13张图,不得不佩服还是外国人会玩人工智能
查看>>