B. XK Segments
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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

Input

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.

Output

Print one integer — the answer to the problem.

Examples
input
4 2 1
1 3 5 7
output
3
input
4 2 0
5 3 1 7
output
4
input
5 3 1
3 3 3 3 3
output
25
Note

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的更多相关文章

  1. CodeForces:#448 div2 B. XK Segments

    传送门:http://codeforces.com/contest/895/problem/B B. XK Segments time limit per test1 second memory li ...

  2. codeforces 895B XK Segments 二分 思维

    codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...

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

  4. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  5. Codeforces 193 D. Two Segments

    http://codeforces.com/contest/193/problem/D 题意: 给一个1~n的排列,在这个排列中选出两段区间,求使选出的元素排序后构成公差为1的等差数列的方案数. 换个 ...

  6. Codeforces 1108E2 Array and Segments (Hard version)(差分+思维)

    题目链接:Array and Segments (Hard version) 题意:给定一个长度为n的序列,m个区间,从m个区间内选择一些区间内的数都减一,使得整个序列的最大值减最小值最大. 题解:利 ...

  7. Codeforces 895.E Eyes Closed

    E. Eyes Closed time limit per test 2.5 seconds memory limit per test 256 megabytes input standard in ...

  8. 【CodeForces】899 E. Segments Removal

    [题目]E. Segments Removal [题意]给定n个数字,每次操作删除最长的连续相同数字(等长删最左),求全部删完的最少次数.n<=2*10^6,1<=ai<=10^9. ...

  9. 『ACM C++』 Codeforces | 1066A - Points in Segments

    大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...

随机推荐

  1. Wacom发布Cintiq Companion 2

    全新的Cintiq Companion 2是一款强大的平板电脑,让创意专业人士获得最佳的屏幕笔触,让创意随时随地进行.用户还可以在家中或工作时连接到Mac或PC电脑获得无与伦比的灵活性! 2015年1 ...

  2. arcgis--arcmap导出点的X,Y坐标

    arcmap操作的

  3. 第七次ScrumMeeting博客

    第七次ScrumMeeting博客 本次会议于11月1日(三)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 1. 每个人的工作(有Issue的 ...

  4. mysql优化建议21条

    转自: http://blog.csdn.net/waferleo/article/details/7179009 今 天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于 ...

  5. Python3 迭代器和生成器

    想要搞明白什么是迭代器,首先要了解几个名词:容器(container).迭代(iteration).可迭代对象(iterable).迭代器(iterator).生成器(generator). 看图是不 ...

  6. Thunder团队——bug修正

    团队:欢迎来怼 发现的问题: 1.首先用户通过爱阅APP内部的网址跳转到各大电子书网站时,需要额外启动手机自身浏览器:就以豆瓣网为例,阅读豆瓣网上的一些书籍,是跳转到手机自带浏览器的,APP内部提供的 ...

  7. 【Alpha】阶段第四次Scrum Meeting

    [Alpha]阶段第四次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 获取课程评论接口 增加课程接口 赵智源 整合前端进行部署 整合前端进行部署 肖萌威 编写 ...

  8. 第八,九周web制作代码

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  9. 把握曝光三要素(上):快门、光圈、ISO

    概要: 如果你还没有掌握快门.光圈和ISO,那这篇文章或许对你有所帮助! 把照片比作水池.把进光量比作水.把快门比作关闭水龙头的速度.把光圈比作水龙头的大小.把感光度ISO比作水龙头的滤网,这就变得好 ...

  10. saltstack进阶

    查看minion端的文件内容 [root@linux-node2 ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 1 ...