比赛的时候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题解的更多相关文章

  1. CF1166C A Tale of Two Lands

    思路: 搞了半天发现和绝对值无关. http://codeforces.com/blog/entry/67081 实现: #include <bits/stdc++.h> using na ...

  2. 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 ...

  3. 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 ...

  4. <每日一题> Day7:CodeForces-1166C.A Tale of Two Lands (二分 + 排序)

    原题链接 参考代码: #include <cstdio> #define mid ((l + r) / 2) #include <algorithm> using namesp ...

  5. CodeForces 1166C A Tale of Two Lands

    题目链接:http://codeforces.com/problemset/problem/1166/C 题目大意 给定 n 个数,任选其中两个数 x,y,使得区间 [min(|x - y|, |x ...

  6. Codeforces Round #561 (Div. 2)

    C. A Tale of Two Lands 题意: 给出 n 个数,问有多少点对(x,y)满足 |x-y| ≤ |x|,|y| ≤ |x+y|: (x,y) 和 (y,x) 表示一种答案: 题解: ...

  7. 算法(第四版)C# 习题题解——3.1

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更方便的版本见:https ...

  8. 算法(第四版)C# 习题题解——2.5

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更方便的版本见:https ...

  9. 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 ...

随机推荐

  1. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_1_Map集合概述

    map集合是双列集合 map有两个泛型.左边K也叫作键 右边V是value

  2. js 实现两个小数的相乘、相除功能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 【python+selenium自动化】图像识别技术在UI自动化测试中的实际运用

    引言: 目前在图像识别方面的自动化测试框架有很多,其中比较有名的是airtest,主要做手机端的游戏自动化测试(http://airtest.netease.com/) 因为没有实际把airtest运 ...

  4. js导入外部文件

  5. SpringMVC起步(一)

    SpringMVC起步(一) 笔记来源于慕课网:https://www.imooc.com/video/7126/0 MVC:Model-View-Controller Model:模型层,业务数据的 ...

  6. 20171110面试笔记 服务器端程序员+C/C++开发

    socket 模型: https://www.cnblogs.com/nsnow/archive/2011/05/03/2036017.html http://blog.csdn.net/normal ...

  7. Forbidden (CSRF token missing or incorrect.):错误解决办法

    在JS中,使用post方法提交数据到后台,出现错误: Forbidden (CSRF token missing or incorrect.):.........; 解决办法: 在页面导入JS的位置, ...

  8. PayPal支付对接

    开发时间:2019-04-30 我的目标:在我们公司的海外网站上,接入PayPal支付,美国用户在线完成付款. 准备: (1)准备:公司注册信息(执照,注册号,法人等),法人信息(身份证,住址等) ( ...

  9. VS #include 【bits/bstdc++.h】出错

    目录 1. 本文地址 2. 按 3. 操作步骤 1. 本文地址 博客园:https://www.cnblogs.com/coco56/p/11163142.html 简书:https://www.ji ...

  10. Taro -- 原生微信小程序转taro

    微信小程序转Taro  (转发https://nervjs.github.io/taro/docs/taroize.html) Taro 可以将你的原生微信小程序应用转换为 Taro 代码,进而你可以 ...