[CF1166C]A Tale of Two Lands题解
比赛的时候lowerbound用错了 现场WA on test4(好吧我承认我那份代码确实有除了lowerbound以外的问题)
于是只能手动二分 (我好菜啊QAQ
经过一波数学推算,我们发现,设序列为\(a\),对于\(a_i\)它不管是正的还是负的答案都是一样的,所以上来我们就珂以给这个数列ABS一下,然后我们拿到了一个正数数列,那么应该怎么求呢?
设有两个序列中的数\(a_x\),\(a_y\)。且我们定义\(a_y<a_x\)。我们发现Arrayland的领土边界为\([a_y,a_x]\),而Vectorland的领土范围为\([a_x-a_y,a_x+a_y]\),注意到因为经过ABS处理的a序列大于等于零,显然\(a_x+a_y \geq a_x\),所以是一定满足的,那么我们只需要考虑左端的情况,为了使\(a_x - a_y \leq a_y\),变形一下显然需要满足\(a_x \leq a_y \times 2\),显然原序列的顺序对答案无影响,所以我们将原序列排序,从头开始枚举每个\(a_y\),显而易见,我们的\(a_x\)只需要在a数组的\([i+1,n]\)范围内二分找出最大的\(k\),使得\(a_k \leq a_i \times 2\),因为原数组排序后的有序性,显而易见a数组中下标小于等于k,大于i的元素都珂以作为\(a_x\),那么当前\(a_y\)对于答案的贡献就是\(k - i\)。
完结撒花。
贴个代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
inline ll read(){
ll x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}
ll a[200005];
int main(){
int n = read();
for (int i = 1; i <= n; ++i)
a[i] = abs(read());
sort(a + 1, a + n + 1);
ll ans = 0;
for (int i = 1; i <= n; ++i){
int l = i + 1, r = n, k = -1, val = a[i] << 1;
while (l <= r){
int mid = (l + r) >> 1;
if (a[mid] <= val){
l = mid + 1;
k = mid;
}
else
r = mid - 1;
}
if (k != -1)
ans += k - i;
}
printf("%I64d", ans);
return 0;
}
[CF1166C]A Tale of Two Lands题解的更多相关文章
- CF1166C A Tale of Two Lands
思路: 搞了半天发现和绝对值无关. http://codeforces.com/blog/entry/67081 实现: #include <bits/stdc++.h> using na ...
- Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】
A Tale of Two Lands 题目链接(点击) The legend of the foundation of Vectorland talks of two integers xx and ...
- Codeforces Round #561 (Div. 2) C. A Tale of Two Lands
链接:https://codeforces.com/contest/1166/problem/C 题意: The legend of the foundation of Vectorland talk ...
- <每日一题> Day7:CodeForces-1166C.A Tale of Two Lands (二分 + 排序)
原题链接 参考代码: #include <cstdio> #define mid ((l + r) / 2) #include <algorithm> using namesp ...
- CodeForces 1166C A Tale of Two Lands
题目链接:http://codeforces.com/problemset/problem/1166/C 题目大意 给定 n 个数,任选其中两个数 x,y,使得区间 [min(|x - y|, |x ...
- Codeforces Round #561 (Div. 2)
C. A Tale of Two Lands 题意: 给出 n 个数,问有多少点对(x,y)满足 |x-y| ≤ |x|,|y| ≤ |x+y|: (x,y) 和 (y,x) 表示一种答案: 题解: ...
- 算法(第四版)C# 习题题解——3.1
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更方便的版本见:https ...
- 算法(第四版)C# 习题题解——2.5
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更方便的版本见:https ...
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分
C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...
随机推荐
- python实现excel转换成pdf
1.安装 需要安装pywin32包,以实现对Office文件的操作,可以批量转换为pdf文件.支持 doc, docx, ppt, pptx, xls, xlsx 等格式. pip install p ...
- spring boot 整合 RabbitMQ 错误
1.错误 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spr ...
- 【Linux】limits.conf 不重启就生效或者不生效的原因
前阵子,我要用到使LInux的文件打开数为65534个,而且需要永久生效,于是将配置写到了: vim /etc/security/limits.conf * soft nofile 65534* ha ...
- Python笔记(二十三)_魔法方法_字符串的魔法方法
__str__和__repr__ 如果要把一个类的实例变成字符串,就需要实现魔法方法__str__()和__repr__ >>>class A: def name(self): re ...
- 重拾SQL——从无到有
2016.10.22 因为工作需要,在这里提前重拾sql. 0.创建并选择数据库 mysql> SHOW DATABASES; +--------------------+ | Database ...
- Nginx配置之rewrite、proxy_pass、upstream、location
如图,这是Nginx的配置文件nginx.conf中的一段配置代码. 在http段中定义了一个名为webservers的upstream模块,主要用于负载均衡. 在server模块中,定义了一个loc ...
- SpringBoot使用RestTemplate基础认证
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
- 剑指Offer编程题(Java实现)——数组中的重复数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 一个阿里云apache服务器配置两个或多个域名forLinux
一个阿里云apache服务器配置两个或多个域名for Linux: 默认已经配置好了阿里云提供的一键web安装,可以参考:http://www.42iot.com/?id=8 修改/alidata/s ...
- MIT 6.824学习笔记4 Lab1
现在我们准备做第一个作业Lab1啦 wjk大神也在做6.824,可以参考大神的笔记https://github.com/zzzyyyxxxmmm/MIT6824_Distribute_System P ...