CodeForces:#448 div2 B. XK Segments
传送门:http://codeforces.com/contest/895/problem/B
B. XK Segments
time limit per test1 second
memory limit per test256 megabytes
Problem Description
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)满足ai到aj的所有数中是x的倍数的数目刚好是k个为一个方案,叫你输出给你的数列中所有的方案数。
- 先输入然后排个序,二分也很容易想到,刚开始只是想到了枚举每个起点,然后二分右边的终点,然后向后寻找符合条件的,结果TLE了。既然要二分那么久二分到底,还是枚举起点,二分符合条件的最左边的右边终点,然后再二分符合条件的最右边的右边终点,然后答案数目就是最左边到最右边的符合条件的终点。
- 判断边界问题是真的痛苦啊,智商不高还是老老实实画着图来写吧,不然各种WA。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
ll num[maxn];
int main()
{
ll n,x,k;
scanf("%lld%lld%lld",&n,&x,&k);
for(int i=0;i<n;i++)
scanf("%lld",&num[i]);
sort(num,num+n);
ll ans = 0;
for(int i=0;i<n;i++)
{
ll l = lower_bound(num,num+n,max(((num[i]-1)/x + k)*x,num[i]))-num;//右端点的最左边
ll r = upper_bound(num,num+n,((num[i]-1)/x+k+1)*x-1)-num-1;//右端点的最右边
ans += (r-l+1);
}
printf("%lld",ans);
return 0;
}
CodeForces:#448 div2 B. XK Segments的更多相关文章
- CodeForces:#448 div2 a Pizza Separation
传送门:http://codeforces.com/contest/895/problem/A A. Pizza Separation time limit per test1 second memo ...
- 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 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces #448 Div2 E
#448 Div2 E 题意 给出一个数组,有两种类型操作: 选定不相交的两个区间,分别随机挑选一个数,交换位置. 查询区间和的期望. 分析 线段树区间更新区间求和. 既然是涉及到两个区间,那么对于第 ...
- Codeforces 895.B XK Segments
B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- Codeforces Round #448(Div.2) Editorial ABC
被B的0的情况从头卡到尾.导致没看C,心情炸裂又掉分了. A. Pizza Separation time limit per test 1 second memory limit per test ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round #448 C. Square Subsets
题目链接 Codeforces Round #448 C. Square Subsets 题解 质因数 *质因数 = 平方数,问题转化成求异或方程组解的个数 求出答案就是\(2^{自由元-1}\) , ...
随机推荐
- AJPFX关于面向对象之封装,继承,多态 (上)
Java是一种面向对象的语言,这是大家都知道的,他与那些像c语言等面向过程语言不同的是它本身所具有的面向对象的特性--封装,继承,多态,这也就是传说中的面向对象三大特性 一:从类和对象开始说起: Oo ...
- java8 api简介(一)
from https://www.aliyun.com/jiaocheng/785076.html 摘要:函数式编程详解:前言:现在有很多公司都用了jdk8,但是函数式编程也许没有用上,jdk8也提供 ...
- Error和Exception的区别?
Error和Exception都继承自Throwable类 二者不同之处在于: Exception: 1.可以是可控制的(checked)或是不可控制的(unchecked) 2.表示一个有程序员编写 ...
- react的setState使用中遇到的问题
setState()更新的数据和自己预期的不一致 对 React 新手来说,使用 setState 是一件很复杂的事情.即使是熟练的 React 开发,也很有可能因为 React 的一些机制而产生一些 ...
- <Android 基础(三)> MVP模式
前言 MVP,这里指的并不是篮球比赛中的MVP(最有价值球员),而是一种代码框架和设计思想,它是由MVC演变而来的. MVP模式(Model-View-Presenter) 是MVC模式的一个衍生.主 ...
- 在vim中插入命令行的输出结果
vim是linux中常见的编辑器,这里讲讲如何在vim中插入命令行的输出结果. 基本用法: 在指令模式下运行 :!command ,如!date将日期显示在vim底部,!ls列出当前目录 将命令结果插 ...
- C#使用Aspose.Words操作word文档
最近接到个需求,由于客服这边要导出大量有一定规则的word文件,里面的内容希望系统自动填充,例如 这里我使用Aspose.Words.dll这个类库, 1.首先,我们需要创建模板文件,毕竟有规则的东西 ...
- 重置SQLSERVER表的自增列,让自增列重新计数【转】
很多时候我们需要重置某个表的自增列,让自增列重新从1开始记数.最蠢的方法当然是把该表删掉再重新建表了.其实,还有其它的方法可以重置自增列的值: 方法一:使用TRUNCATE TABLE语句: TRUN ...
- 在 Visual Studio 或 SQLServer Management Studio 的代码编辑器中使用正则表达式匹配日期格式
使用正则查找时间格式文本 VS正则: (:z表示数字) ':z-:z-:z :z[\:]:z[\:]:z'
- 汶川大地震中的SAP成都研究院
5·12汶川地震,发生于北京时间(UTC+8)2008年5月12日(星期一)14时28分04秒,此次地震的面波震级 里氏震级达8.0Ms.矩震级达8.3Mw,地震烈度达到11度.地震波及大半个中国及亚 ...