[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 ...
随机推荐
- Delphi XE2 之 FireMonkey 入门(16) - 滤镜: 实例测试
窗体上需要 TImage.TOpenDialog 和六个按钮. unit Unit1; interface uses System.SysUtils, System.Types, System.U ...
- JQuery关于span标签的取值赋值
span取值赋值方法有别于一般的页面元素.JQ://赋值$("#spanid").html("hello world") //取值$("#spanid ...
- HTML: 引号不能忽视
在js中常常生成拼接html,然后放到dom中,但是有些拼接的html标签需要加一些指或者属性,这个时候不能忽略引号 如果data.link_tel有空格,不加单引号导致value的值不完全 str ...
- Maven构建SpringMVC+Mybatis项目
1.创建Maven项目时,起始是没有src/main/java.src/test/java.src/test/resources,需要修改一些配置之后,自动就会创建出来: 2.开始引入Spring+M ...
- <每日一题> Day3:CodeForces-1141B.MaximalContinuousRest(简单题)
题目链接 参考代码: #include <iostream> #include <algorithm> using namespace std; + ; int value[m ...
- 2019牛客暑期多校训练营(第五场) - C - generator 2 - BSGS
https://ac.nowcoder.com/acm/contest/885/C 这个跟平时那种求离散对数的bsgs不一样,虽然可以转化成离散对数来做不过会T掉.展开递推式然后合并具有a的项,发现其 ...
- [转载]企业级应用架构(NHibernater+Spring.Net+MVC3)
本人已经从事公司两套这类架构系统的开发工作啦!对于这套架构,我惊叹不已!BPS和CMS系统都是采用这套架构.但本人也同时渐渐发现了这套架构有诸多 不足之处,于是本人利用闲暇时光进一步改进了这套架构.新 ...
- Storm分布式集群搭建
一.storm版本 选用storm0.9.6 二.本地模式 用于对storm业务逻辑的调试和测试,可以直接在本地运行. 三.分布式模式 生产环境,需要对应的zookeeper.nimbus.super ...
- Azkaban 2.5.0的详细安装过程
准备下载Azkaban2.5.0:https://azkaban.github.io/downloads.htm 准备插件: 一.MySQL安装与配置 启动数据库并查看状态:sudo service ...
- 解决 myEclipse与tomcat 不同步的问题
在我们使用eclipse做web调试的过程中,一般只需要在eclipse修改程序,然后在浏览器刷新就能发现文件更改,今天突然发现保存后不能更改了.1.检查tomcat中的文件发现文件没有更新.2.检查 ...