题目链接:https://codeforces.com/contest/1324/problem/D

题目描述
有两个长度为 n 的数组 A, B。问有多少对 (i,j) 满足 i < j 且 A[i]+A[j] > B[i]+B[j]。
数据范围
2 <= n <= 2*10^5,对于数组的每个元素 x 有 x ∈[1, 10^9]。
样例

n = 5
A = [4,8,2,6,2]
B = [4,5,4,1,3]
答案为 7, 分别为(1,2) (1,4) (2,3) (2,4) (2,5) (3,4) (4,5)

题目类型:排序,双指针,区间查询
解题思路
由公式 A [ i ] + A [ j ] > B [ i ] + B [ j ] A[i]+A[j] \gt B[i]+B[j] A[i]+A[j]>B[i]+B[j] 得 A [ i ] − B [ i ] > B [ j ] − A [ j ] A[i]-B[i] \gt B[j]-A[j] A[i]−B[i]>B[j]−A[j] 。
构造两个数组:

  • D a b [ i ] = A [ i ] − B [ i ] D_{ab}[i] = A[i] - B[i] Dab​[i]=A[i]−B[i]
  • D b a [ i ] = B [ i ] − A [ i ] D_{ba}[i] = B[i] - A[i] Dba​[i]=B[i]−A[i]

则该题答案等价于 满足条件 D a b [ i ] > D a b [ j ] D_{ab}[i] \gt D_{ab}[j] Dab​[i]>Dab​[j] 且 i < j i \lt j i<j 的 ( i , j ) (i,j) (i,j)的数量
为了便于统计,可将 D a b , D b a D_{ab},D_{ba} Dab​,Dba​ 排序,排序时需记录位置信息。
设数组 P o s a , P o s b Pos_a,Pos_b Posa​,Posb​分别记录 D a b , D b a D_{ab},D_{ba} Dab​,Dba​ 的元素在排序前的位置。
设 a n w anw anw 为最终答案,初始为 0 。
使用双指针 p , q p,q p,q 遍历排序后的 D a b , D b a D_{ab},D_{ba} Dab​,Dba​, p p p 每增加一, q q q 应增加至满足 D a b [ p ] > D a b [ q ] D_{ab}[p]\gt D_{ab}[q] Dab​[p]>Dab​[q] 的最大值。更新完 p , q p,q p,q 后统计满足 P o s b [ j ] > P o s a [ p ] , j ∈ [ 1 , q ] Pos_b[j] > Pos_a[p], j ∈ [1, q] Posb​[j]>Posa​[p],j∈[1,q] 的 j j j 的数量并累加到 a n w anw anw 中。此处可借助线段树,树状数组等区间查询算法完成。

#include <bits/stdc++.h>using namespace std;const int MAXN = 200001;int A[MAXN], B[MAXN], st[MAXN*4];struct Diff {int pos;int diff;bool operator < (const Diff &r) const {return this->diff < r.diff;}
} diffAB[MAXN], diffBA[MAXN];void update(int *st, int root, int L, int R, int goal) {st[root]++;if(L == R) {return;}int mid = (L+R)>>1;if(goal <= mid) {update(st, root<<1, L, mid, goal);} else {update(st, root<<1|1, mid+1, R, goal);}
}int query(int *st, int root, int L, int R, int range) {if (range <= 0) {return 0;}if (R == range) {return st[root];}int mid = (L+R)>>1;if (range <= mid) {return query(st, root<<1, L, mid, range);}return st[root<<1] + query(st, root<<1|1, mid+1, R, range);
}int main() {cin.sync_with_stdio(false);int n;cin >> n;for(int i = 1; i <= n; i++) {cin >> A[i];}for(int i = 1; i <= n; i++) {cin >> B[i];}for(int i = 1; i <= n; i++) {diffAB[i].pos = i;diffAB[i].diff = A[i] - B[i];diffBA[i].pos = i;diffBA[i].diff = B[i] - A[i];}sort(diffAB+1, diffAB+n+1);sort(diffBA+1, diffBA+n+1);int64_t anw = 0;for(int i = 1, j = 1; i <= n; i++) {while(j <= n && diffBA[j].diff < diffAB[i].diff) {update(st, 1, 1, n, diffBA[j].pos);j++;}anw += query(st, 1, 1, n, diffAB[i].pos-1);}cout << anw << endl;return 0;
}


扫描图片关注 HelloNebula 获取更多有趣题目~

Codeforces 1324D Pair of Topics相关推荐

  1. CodeForces - 1324D Pair of Topics (分治+排序)

    CodeForces - 1324D Pair of Topics 题目大意: 这题大意ai+aj>bi+bj全在这个式子上,就问你满足的组合有几种, 题目分析: 整理一下,得到(ai-bi)+ ...

  2. [codeforces 1324D] Pair of Topics 分而治之+排列组合

    Codeforces Round #627 (Div. 3)   比赛人数6434 [codeforces 1324C]  Frog Jumps   一直向右+边界处理 总目录详见https://bl ...

  3. CodeForces - 1324D Pair of Topics(思维+二分)

    题目链接:https://vjudge.net/contest/362265#problem/D The next lecture in a high school requires two topi ...

  4. CodeForces - 1324D Pair of Topics(二分或双指针)

    题意:略 题记: 做法一:二分 #include<bits/stdc++.h>using namespace std; typedef long long ll; const int N= ...

  5. [Codeforces Round #627]1324D - Pair of Topics[二分]

    1324D - Pair of Topics[二分] time limit per test memory limit per test input output 2 seconds 256 mega ...

  6. cf 1324D. Pair of Topics

    D. Pair of Topics 题意:给定ab序列,问i<j且ai+aj>bi+bj的对数. 转化:ai-bi<-(aj-bj) 一开始拿到题目想着sort,但是发现i<j ...

  7. CodeForces - 1324 D. Pair of Topics 思维+多解法

    CodeForces - 1324 D. Pair of Topics 原题地址: http://codeforces.com/contest/1324/problem/D 基本题意: 给你一个数组, ...

  8. Codeforces1324D Pair of Topics (思维 + 二分)

    题目链接: Pair of Topics 大致题意 给定两个数组a和b, 要求找出所有满足i < j 并且 ai + aj > bi + bj 的所有数对. 解题思路 我们不难想到先对等式 ...

  9. Codeforces 1324 D. Pair of Topics(二分)

    题意: 给出两组长度为 n n n 的数组 a i , b i a_i,b_i ai​,bi​,问满足 ( i < j ) a i + a j > b i + b j (i < j) ...

最新文章

  1. Apache服务器主配置文件 httpd.conf 中文版
  2. 阿里资深技术专家:在各阶段中,3年经验的程序员应具备哪些技术能力(进阶必看)...
  3. centos安装python3_在CentOS8 上安装Python3
  4. POJ1942-Paths On a Grid-组合数学
  5. 多域环境下people picker查找不到用户问题的解决(转载jianyi)
  6. 【计算机网络】协议,接口,服务
  7. 武汉传媒学院有计算机专业吗,武汉传媒学院比较好的专业有哪些呢?
  8. 分布式监控系统开发【day38】:报警阈值程序逻辑解析(三)
  9. 远程连接 Mysql 失败的解决方法
  10. WebConfig配置文件详解
  11. poj 1251 Jungle Roads
  12. Android CircleMenu:旋转转盘选择Menu
  13. 元宇宙引擎脑语言2500令v0.5.6
  14. 北京三大春天赏花圣地
  15. 学习笔记——经纬度坐标系及定位相关API
  16. CSS实现文字动画炫酷效果
  17. 阿里云机器学习PAI-快速上手指南
  18. RVM切换ruby版本
  19. 如何利用clusterProfiler进行基因集的KEGG富集分析?
  20. 【腾讯】2017暑期实习生

热门文章

  1. XSSF 导入导出excel.xlsx 解决获取空白单元格自动跳过问题,校验excel表头是否符合需求
  2. 最全CSS基础知识图片形式整理
  3. 2022LOL微博杯模糊问题,1080p高清看微博杯the shy比赛直播
  4. 欢迎 V 的到来:简书新浪微博联合认证公告
  5. 数字图像处理 总复习(第七章)*秋昊
  6. 轻松输入并注音生僻字
  7. 清理Win11磁盘的方法
  8. redis主从结构 (一主一从,一主多从,主从从)
  9. 美颜API是什么意思?美颜API和美颜SDK有什么区别?
  10. c语言笔记(第一周)