CodeForces 1K-1400R-1324D
1324D-Pair of Topics
Description
Link
Analysis
题意概述
给定两个长度为 n n n 的序列 { a n } , { b n } \{a_n\},\{b_n\} {an},{bn}( 2 ≤ n ≤ 2 × 1 0 5 , 1 ≤ a i ≤ 1 0 9 , 1 ≤ b i ≤ 1 0 9 2\le n\le 2\times10^5,1\le a_i\le10^9,1\le b_i\le10^9 2≤n≤2×105,1≤ai≤109,1≤bi≤109),求满足 a i + a j > b i + b j a_i+a_j>b_i+b_j ai+aj>bi+bj 的数对 ( i , j ) (i,j) (i,j) 的个数(其中 i < j i<j i<j)
分析
尽量将下标相同的元素联系在一起,考虑移项,得约束条件: ( a i − b i ) + ( a j − b j ) > 0 (a_i-b_i)+(a_j-b_j)>0 (ai−bi)+(aj−bj)>0
令 c i = a i − b i c_i=a_i-b_i ci=ai−bi,类比在有序序列中寻找和为定值的数的方法( l e e t c o d e leetcode leetcode 的 A + B A+B A+B 问题),将 c i c_i ci 升序排序,依次从下一个位置向后二分大于当前值的负值的数的下标,并从 n n n 中减去,得到对于单个 i i i 的答案,累加即可
(对于 { c n } \{c_n\} {cn} 在排序后二分得到的数对,是否满足 i < j i<j i<j:只需要使得下标不出现重复、数对不重复计算即可,同一数对的下标 i , j i,j i,j 必然存在确定的大小关系)
此外,数对个数可能超过 i n t int int 的表示范围
Code
#include <iostream>
#include <cstring>
#include <algorithm>
#define LL int64_tusing namespace std;constexpr const int N = 2e5 + 10;int a[N], b[N], c[N], n;
LL ans;int main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n;for(int i = 1; i <= n; i++)cin >> a[i];for(int i = 1; i <= n; i++){cin >> b[i];c[i] = a[i] - b[i];}sort(c + 1, c + 1 + n);for(int i = 1; i <= n - 1; i++){int pos = upper_bound(c + i + 1, c + n + 1, -c[i]) - (c + 1);ans += n - pos;}cout << ans;return 0;
}
Tag
基础的二分查找
CodeForces 1K-1400R-1324D相关推荐
- [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 ...
- CodeForces - 1324D Pair of Topics (分治+排序)
CodeForces - 1324D Pair of Topics 题目大意: 这题大意ai+aj>bi+bj全在这个式子上,就问你满足的组合有几种, 题目分析: 整理一下,得到(ai-bi)+ ...
- Codeforces 1324D Pair of Topics
题目链接:https://codeforces.com/contest/1324/problem/D 题目描述 有两个长度为 n 的数组 A, B.问有多少对 (i,j) 满足 i < j 且 ...
- [codeforces 1324D] Pair of Topics 分而治之+排列组合
Codeforces Round #627 (Div. 3) 比赛人数6434 [codeforces 1324C] Frog Jumps 一直向右+边界处理 总目录详见https://bl ...
- Codeforces 1324D(红黑树+求指定区间中大于指定值的个数)
代码: #include <bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #define IOS ios::sy ...
- CodeForces - 1324D Pair of Topics(二分或双指针)
题意:略 题记: 做法一:二分 #include<bits/stdc++.h>using namespace std; typedef long long ll; const int N= ...
- CodeForces - 1324D Pair of Topics(思维+二分)
题目链接:https://vjudge.net/contest/362265#problem/D The next lecture in a high school requires two topi ...
- 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...
- Codeforces Round #694 (Div. 1 + Div2)(A ~ H,8题全,超高质量题解)【每日亿题】2021/2/1、2/2
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 [每日亿题]Codeforces Round #694 (Div. 1 + Div2)(A ~ ...
- CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换,清晰易懂)
CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换) 首先一共有四个操作,第一个和第四个都是线段树的基本操作,直接用线段树实现. 第 ...
最新文章
- 哪个更快:Java 堆还是本地内存
- noj 邮票分你一半z的生日
- 树莓派wiringPi库详解
- java 随机数 【指定范围】
- Mac修改系统用户名好方法(文件无丢失)
- iPhone12详细拆解报告出炉!(附主要器件清单)
- pdo 参数化查询 mysql函数_PDO笔记之参数化查询
- BUUOJ reverse 不一样的flag
- oracle单表存储记录,oracle从各个表获得数据保存到另一个表
- windows ffmpeg 推送摄像头数据到rtmp服务
- 厉害了!厦门大学“嘉庚一号”火箭成功发射 问鼎九天
- PyTorch命令行中执行python程序使用指定的GPU
- 2021泰安市地区高考成绩排名查询,2021年山东高考成绩排名及一分一段表
- office 公式编辑器 插入花体格式字母
- java高校贫困生助学贷款系统ssm框架
- 医疗卫生行业中的领域模型
- Ubuntulinux离线安装ClamTk杀毒软件步骤和使用方法
- Unity 场景光照出现问题
- 常用的OpenCV函数速查
- kubernetes资源控制器【一】- ReplicaSet控制器