Codeforces1324D Pair of Topics (思维 + 二分)
题目链接: Pair of Topics
大致题意
给定两个数组a和b, 要求找出所有满足i < j 并且 ai + aj > bi + bj 的所有数对.
解题思路
我们不难想到先对等式变形, 不妨设ci = ai - bi
, 则原式变为ci > -cj
<==> ci + cj > 0
.
这是什么? 权值线段树!!!. 好吧, 我又来了个1A权值线段树. 竟和 1538C 如此的相似.
我们发现, 我们可以去枚举每一个ci, 通过二分的方式找到符合的cj数量.
为了避免重复计算答案, 我们不妨假设ci > cj. 这样假设每次从index位置起, j ∈ [index, n] 均成立, 我们累加结果[i, index) 即可.
AC代码
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 2E5 + 10;
int a[N];
int main()
{int n; cin >> n;rep(i, n) scanf("%d", &a[i]);rep(i, n) {int x; scanf("%d", &x);a[i] -= x;}sort(a + 1, a + 1 + n);ll res = 0;rep(i, n) {int index = upper_bound(a + 1, a + 1 + n, -a[i]) - a;res += max(i - index, 0);}cout << res << endl;return 0;
}
END
Codeforces1324D Pair of Topics (思维 + 二分)相关推荐
- CodeForces - 1324 D. Pair of Topics 思维+多解法
CodeForces - 1324 D. Pair of Topics 原题地址: http://codeforces.com/contest/1324/problem/D 基本题意: 给你一个数组, ...
- 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) ...
- 2022.11.11 CodeForces1324D Pair of Topics
原题链接:传送门 题意:数组a和b中,有几个ai+aj>bi+bj 思路:我们对这个式子整理0>(bi-ai)+(bj-aj),所以我们可以选择双指针或二分去搜答案,那么这里我们可以思考一 ...
- CodeForces - 1324D Pair of Topics(二分或双指针)
题意:略 题记: 做法一:二分 #include<bits/stdc++.h>using namespace std; typedef long long ll; const int N= ...
- codeforces 1324 D. Pair of Topics(思维)
题目描述: 参考博客 题意: 给两个大小为 n 的数组A, B, 判断数组中有多少对 满足 Ai + Aj > Bi + Bj. 解题思路: 由于 n 的大小限制,两个for循环必然超时. 调整 ...
- CodeForces - 1324D Pair of Topics(思维+二分)
题目链接:https://vjudge.net/contest/362265#problem/D The next lecture in a high school requires two topi ...
- [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 ...
- cf 1324D. Pair of Topics
D. Pair of Topics 题意:给定ab序列,问i<j且ai+aj>bi+bj的对数. 转化:ai-bi<-(aj-bj) 一开始拿到题目想着sort,但是发现i<j ...
- CodeForces - 1324D Pair of Topics (分治+排序)
CodeForces - 1324D Pair of Topics 题目大意: 这题大意ai+aj>bi+bj全在这个式子上,就问你满足的组合有几种, 题目分析: 整理一下,得到(ai-bi)+ ...
最新文章
- JAVA环境变量的配置
- Hadoop 同步集群时间ntp
- 延迟或计划邮件的发送(zz.is2120)
- 浅谈MD5加密算法中的加盐值(SALT)
- Python实现心跳保活TCP长连接
- 为什么SimpleDateFormat不是线程安全的?
- mysql复制状态是什么意思_mysql8 参考手册--复制从SQL线程状态
- java基础—System类的方法演示
- vue搭建后可以改下全局配置
- 老毛子固件相关内容!
- 云计算Linux运维——Linux系统——软件管理
- Opencv 笔记8 霍夫变换
- 什么是庖丁解牛的思维?
- 【前端面试宝典】超基础的vue知识
- FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'D:/tjn/tra
- 对于环形组网次优和来回路径不一致的解决方法
- KT6368A蓝牙芯片的MTU的说明以及如何加快BLE传输速率
- [week9]东东学打牌
- 微软发布补丁封杀允许Surface RT安装Linux的“漏洞”
- 专栏介绍《AI在艺术和创意产业中的应用:赋予机器灵感》
热门文章
- 腾讯点播云,上传视频实例, 使用点播云播放器实例
- 免费HTTP代理怎么样
- 如何通过海外住宅代理预防Facebook账号出现关联的情况
- linux poweroff无法关机,linux关机重启六种方法(sysrq shutdown halt init ipmi poweroff)
- 轻量级网络 Slimmable Neural Networks
- Y7000P电池0%解决办法
- 极客日报:腾讯宣布捐赠1亿元驰援河南;苹果回应iPhone 安全隐患;贝索斯完成10分钟太空之旅
- 软件外包项目如何承接
- 缺少vcruntime140d.dll的解决办法
- 前端处理 token 时效性问题