Codeforces 895.B XK Segments
1 second
256 megabytes
standard input
standard output
While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j)such that ai ≤ aj and there are exactly k integers y such that ai ≤ y ≤ aj and y is divisible by x.
In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1).
The first line contains 3 integers n, x, k (1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), where n is the size of the array a and x and k are numbers from the statement.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
Print one integer — the answer to the problem.
4 2 1
1 3 5 7
3
4 2 0
5 3 1 7
4
5 3 1
3 3 3 3 3
25
In first sample there are only three suitable pairs of indexes — (1, 2), (2, 3), (3, 4).
In second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4).
In third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25.
大致题意:求有多少对(i,j),使得a[i]到a[j]之间x的倍数恰好有k个,a[i] ≤ a[j].
分析:我的做法是比较套路的二分加速枚举区间.
先将a数组排序,这样就消除了a[i] ≤ a[j]这个限制,然后枚举左端点,在左端点右边进行二分.二分出倍数正好为k个的下界l和上界r,接下来的统计有点麻烦.如果二分得到的区间正好包含了i,那么答案要-1,因为(i,i)不能被统计两次.接着,还要求出一个区间[i,p],使得这个区间中的所有数都等于a[i].如果这个区间中的每一个数和a[i]都满足要求,那么(i,j)和(j,i)实质上是一样的,答案要*2,剩下的只需要加上区间长度就好了.
这种写法比较繁琐,一个比较好的处理方法是二分的不仅仅局限于i右边的区间,而是被包含整个区间的区间.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; ll n, x, k, a[], ans; ll solve(ll p, ll q)
{
return a[q] / x - (a[p] - ) / x;
} int main()
{
cin >> n >> x >> k;
for (ll i = ; i <= n; i++)
cin >> a[i];
sort(a + , a + + n); for (ll i = ; i <= n; i++)
{
ll l = i, r = n, ans1 = i;
while (l <= r)
{
ll mid = (l + r) >> ;
if (solve(i, mid) >= k)
{
ans1 = mid;
r = mid - ;
}
else
l = mid + ;
}
ll ans2 = n;
l = i, r = n;
while (l <= r)
{
ll mid = (l + r) >> ;
if (solve(i, mid) <= k)
{
ans2 = mid;
l = mid + ;
}
else
r = mid - ;
}
l = i, r = n;
ll ans3 = i;
while (l <= r)
{
ll mid = (l + r) >> ;
if (a[mid] > a[i])
r = mid - ;
if (a[i] == a[mid])
{
l = mid + ;
ans3 = mid;
}
}
if (solve(i, ans1) == k && solve(i, ans2) == k)
{
if (ans3 < ans1)
ans += (ans2 - ans1 + );
else
{
ans += (ans3 - ans1 + ) * ;
ans += (ans2 - ans3);
if (ans1 == i)
ans--;
}
}
}
cout << ans << endl;
return ;
}
Codeforces 895.B XK Segments的更多相关文章
- CodeForces:#448 div2 B. XK Segments
传送门:http://codeforces.com/contest/895/problem/B B. XK Segments time limit per test1 second memory li ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces Round #448 (Div. 2) B. XK Segments【二分搜索/排序/查找合法的数在哪些不同区间的区间数目】
B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- Codeforces 193 D. Two Segments
http://codeforces.com/contest/193/problem/D 题意: 给一个1~n的排列,在这个排列中选出两段区间,求使选出的元素排序后构成公差为1的等差数列的方案数. 换个 ...
- Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)
题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...
- Codeforces 895.E Eyes Closed
E. Eyes Closed time limit per test 2.5 seconds memory limit per test 256 megabytes input standard in ...
- 【CodeForces】899 E. Segments Removal
[题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...
- 『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
随机推荐
- http-equiv=mobile-agent说明
Meta声明的格式:<meta http-equiv=”mobile-agent” content=”format=[wml|xhtml|html5]; url=url”> 比如: < ...
- HBASE理论篇
1.Hbase是什么 HBase是一种构建在HDFS之上的分布式.面向列的存储系统.在需要实时读写.随机访问超大规模数据集时,可以使用HBase. 尽管已经有许多数据存储和访问的策略和实现方法,但事实 ...
- 机器学习之决策树(ID3)算法
最近刚把<机器学习实战>中的决策树过了一遍,接下来通过书中的实例,来温习决策树构造算法中的ID3算法. 海洋生物数据: 不浮出水面是否可以生存 是否有脚蹼 属于鱼类 1 是 是 是 2 ...
- Wacom将在CES 2015上发布全新旗舰版Cintiq
Cintiq 27QHD和Cintiq 27QHD touch拥有宽大的工作表面,以及令人惊喜的屏幕笔触和颜色性能. 2015年1月6日,Wacom发布了Cintiq 27QHD和Cintiq 27Q ...
- Python 招聘信息爬取及可视化
自学python的大四狗发现校招招python的屈指可数,全是C++.Java.PHP,但看了下社招岗位还是有的.于是为了更加确定有多少可能找到工作,就用python写了个爬虫爬取招聘信息,数据处理, ...
- 梯度下降算法以及其Python实现
一.梯度下降算法理论知识 我们给出一组房子面积,卧室数目以及对应房价数据,如何从数据中找到房价y与面积x1和卧室数目x2的关系? 为了实现监督学习,我们选择采用自变量x1.x2的线性函数来评估因变 ...
- Yii2 配置request组件解析 json数据
在基础版本的config目录下 web.php 或者高级版config目录下的main.php中配置 'components' =>[ 'request' => [ 'parsers' = ...
- php的大小写敏感问题整理
php的大小写敏感问题整理 今天在开发php的过程中,因为命名大小写的问题导致代码错误,所以从网上整理了php的大小写敏感的一些资料,需要的朋友可以参考下. PHP对大小写敏感问题的处理比较乱,写 ...
- 效能检测 psp
1.本周psp: 2.本周进度条: 3.累计进度图(折线图) 4.psp饼状图:
- 对首师大研究生院的UI分析
我们分析的是首都师范大学研究生院的UI界面 网站链接http://grad.cnu.edu.cn/index.htm 学校设计和石家庄铁道大学的界面类似,都是有一个大的置顶的名字,将学校或者学院的名字 ...