题目链接:http://codeforces.com/problemset/problem/1166/C

题目大意

  给定 n 个数,任选其中两个数 x,y,使得区间 [min(|x - y|, |x + y|), max(|x - y|, |x + y|)] 能完全盖过区间 [min(|x|, |y|), max(|x|, |y|)],请问一共有多少种选法?

分析

  先随便举个例子,比如 |x| = 3, |y| = 5,可以发现,不管是 [3, 5],还是 [-3, 5], [3, -5], [-3, -5], [min(|x - y|, |x + y|), max(|x - y|, |x + y|)] 都是 [2, 8],因此,负数对答案没有任何影响。
  不妨设 x <= y,若要 [y - x, x + y] 覆盖 [x, y],则必有 2*x >= y。这就很明显了,排完序之后对于每个数二分查找即可。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; LL n, a[maxN], ans; int main(){
INIT();
cin >> n;
Rep(i, n) {
cin >> a[i];
if(a[i] < ) a[i] = -a[i];
}
sort(a, a + n, greater< int >()); rFor(i, n - , ) {
LL tmp = lower_bound(a, a + i, * a[i], greater< int >()) - a;
ans += i - tmp;
}
cout << ans << endl;
return ;
}

CodeForces 1166C A Tale of Two Lands的更多相关文章

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

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

  3. CF1166C A Tale of Two Lands

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

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

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

  5. [CF1166C]A Tale of Two Lands题解

    比赛的时候lowerbound用错了 现场WA on test4(好吧我承认我那份代码确实有除了lowerbound以外的问题) 于是只能手动二分 (我好菜啊QAQ 经过一波数学推算,我们发现,设序列 ...

  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. 2021.3.3--vj补题

    题目 C - C CodeForces - 1166C The legend of the foundation of Vectorland talks of two integers xx and  ...

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

  9. 【codeforces 785C】Anton and Fairy Tale

    [题目链接]:http://codeforces.com/contest/785/problem/C [题意] 容量为n的谷仓,每一天都会有m个谷子入仓(满了就视为m);第i天 会有i只鸟叼走i个谷子 ...

随机推荐

  1. 分块——cf1207F

    这么傻逼的题当时想了那么久 用a数组维护原序列,b[i][j]表示 pos%i=j 的 a[pos]之和 对于每个修改1 x y,先直接修改a[x],然后枚举i=1..700,修改b[i][x%i] ...

  2. Python 内置模块:os模块

    Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.(一语中的) 二.常用方法 1.os.name 输出字符串指示正在使用的平台.如果是windo ...

  3. JZOI1142 排队布局

    #include <bits/stdc++.h> using namespace std; inline int read() { int x = 0,tmp = 1;char ch = ...

  4. JZOI1062 【USACO2013JAN】invite

    #include <bits/stdc++.h> #define ll long long #define INF 2147483647 #define ll_INF 9223372036 ...

  5. 【UR #2】跳蚤公路

    [UR #2]跳蚤公路 参照yjc方法.也就是地铁环线那个题. 求每个点不在负环内的x的取值范围.然后所有1到j能到i的j的范围取交.得到答案. 每个边形如kx+b的直线,每个环也是 每个点不在负环内 ...

  6. 51nod-1204 并查集

    你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案 ...

  7. Openstack组建部署 — Glance Install

    目录 目录 前文列表 Image service overview Openstack Image service包含的组件 Install and configure Prerequisites 先 ...

  8. ollydbg调试PE文件

    ollydbg项目地址:http://www.ollydbg.de/ 将exe文件打开到ollydbg项目中,就会直接停到"入口点"地址处,通过View->Memory Ma ...

  9. tftp 服务器的配置

    如果用下面一条命令能够看到服务已经启动, 则不用安装, 否则需要按 1 或 2 点安装 tftp-server 服务器. [arm@localhost arm]#netstat -a | grep t ...

  10. 从零开始搭建系统2.1——Nexus安装及配置

    在安装配置Nexus时,请先确定您已经配置好jdk 1.创建目录 2.下载安装包 [root@localhost usr]# cd nexus 下载地址:https://www.sonatype.co ...