Codeforces Round #627 (Div. 3) D. Pair of Topics(二分/直接遍历)

The next lecture in a high school requires two topics to be discussed. The ii -th topic is interesting by aiai units for the teacher and by bibi units for the students.

The pair of topics ii and jj (i<ji<j ) is called good if ai+aj>bi+bjai+aj>bi+bj (i.e. it is more interesting for the teacher).

Your task is to find the number of good pairs of topics.

Input

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

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109 ), where aiai is the interestingness of the ii -th topic for the teacher.

The third line of the input contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1091≤bi≤109 ), where bibi is the interestingness of the ii -th topic for the students.

Output

Print one integer — the number of good pairs of topic.

Examples

Input

5
4 8 2 6 2
4 5 4 1 3

Output

7

Input

4
1 3 2 4
1 3 2 4

Output

0这种题套路就是移一下项转化为ai-bi,然后先对ai-bi=ci进行排序。若要ci+cj>0,这两个起码要有一个为0,所以从大到小扫一遍,当ci大于0时,用upper_bound找到第一个大于-ci的数,这样一定能保证这个区间里的数的和大于0,把长度累加到答案里就行。其实也可以双指针同时往中间移动,这么写啰嗦一点不过常数比较小....能过万岁
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
struct good
{
    int a,b;
}g[200005];
int res[200005];
int n;

int main()
{
    cin>>n;
    int i;
    for(i=1;i<=n;i++)scanf("%d",&g[i].a);
    for(i=1;i<=n;i++)scanf("%d",&g[i].b);
    for(i=1;i<=n;i++)res[i]=g[i].a-g[i].b;
    sort(res+1,res+n+1);
    long long ans=0;
    for(i=n;i>=1;i--)
    {
        if(res[i<0])break;
        int pos=upper_bound(res+1,res+i+1,-res[i])-res;
        if(pos>=1&&pos<i)
        {
            ans+=i-pos;
        }
    }
    cout<<ans;
 } 


原文地址:https://www.cnblogs.com/lipoicyclic/p/12590480.html

时间: 2024-08-05 03:42:52

Codeforces Round #627 (Div. 3) D. Pair of Topics(二分/直接遍历)的相关文章

Codeforces Round #627 (Div. 3) 补题

CF1324A Yet Another Tetris Problem 长度为n的数组a中有一组数,可以任意使其中一项+2,问能否使a中所有项的值相同. 感觉div.3的题目更多地在考简化问题的能力--比如原题目以俄罗斯方块作背景,让我想到的是能不能消除所有方块,导致代码很难写.但如果像上述一样简化题意,方向就很明确了:只要判断是否所有数均为偶数/均为奇数即可. CF1324A-代码 #include<cstdio> #include<iostream> #include<cs

Codeforces Round #426 (Div. 2) C. The Meaningless Game (二分 or pow函数)

Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting. The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it fast

Codeforces Round #256 (Div. 2) D. Multiplication Table(二分查找)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/contest/448/problem/D -----------------------------------------------------------------------------------------------------------------------------------------------

Codeforces Round #426 (Div. 1) A.The Meaningless Game (二分+数学)

题目链接: http://codeforces.com/problemset/problem/833/A 题意: 给你 \(a\) 和 \(b\),两个人初始化为 \(1\).两个人其中一方乘以 \(k^2\),另一方就乘以 \(k\).问你能不能达到 \((a,b)\) 这个最终状态. 题解: 设 \(X\), \(P\) 表示两个乘积的集合. 那么,显然: \(S^{2}*P=a\) ------ 1 \(S*P^{2}=b\) ------ 2 所以:\(a*b = S^{3}*P^3\)

Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output We consider a positive integer perfect, if and only if the sum of its digits is exactly 1010. Given a positive integ

Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】

任意门:http://codeforces.com/contest/1077/problem/D D. Cutting Out time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given an array ss consisting of nn integers. You have to find any ar

Codeforces Round #380 (Div. 2) C. Road to Cinema 二分

题意: 1.给你n辆车,k个加油站,s路程,t秒内到达 2.所有车都有两种mode:1km / min 和 1km / 2min,耗油量是 2/km 和 1/km 3.每辆车有价格和油罐容量,路过加油站汽油可瞬间满血.求最便宜的车. 思路: 1.写一个bool函数judge,用来判断车花费最少时间mint,如果mint<=t返回真.用加油站把路程分组,每组求出mint,累加起来. 2.每组路程dis有三种情况: A.油罐容量feul<dis,说明用慢速都到大不了,return false: B

Codeforces Round #220 (Div. 2) D 树状数组 &amp;&amp; 二分

/*题目*/ 题意:给了n,m,然后一个包含m个数的数组nnum,数组默认从小到大排序,然后是 n个操作,输入一个数x,若x为0,把0加到这个字符串的末尾,若x为1,把1加到这个字符串的末尾,若x为-1,那么把字符串里的 下标 与 nnum数组里的元素相等的  给删除,字符串一开始是空的,问你最后字符串里有什么,若为空 就输出 POOR STACK 这题目看这操作一般都很容易联想到线段树,树状数组,一开始我建了个树状数组,但是超时了,毕竟操作很多,而且 在删除操作里,若nnum数组很大,等同于1

Codeforces Round #609 (Div. 2)E--K Integers(贪心+二分+树状数组+逆序对)

K Integers 参考博客:https://blog.csdn.net/Q755100802/article/details/103664555 [题意] 给定一个1到n的排列,可以交换相邻的两个元素. 现在定义一个函数f(x),表示在原排列中,通过交换操作,形成一个1,2,3....x的排列的子串,需要的最小操作步骤. 子串意味着这个排列必须是相邻的.现在你需要求出f(1),f(2),f(3)......f(n). [分析] 在1~x这几个元素相邻的情况下,因为最后排列不存在逆序对,根据贪