codeforces 895B XK Segments 思维 二分
2017-12-13 20:25
483 查看
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.
Example
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.
题意:输入n,x,k; 和一个 n 长度的 序列 , 一个二元组的定义如下(i,j)
满足 ai<= y <= aj, 使得y|x的y个数为k个(y为[ai,aj]闭区间的整数)
注意 (i,j) != (j,i) 如果 i!=j的情况下,求出满足这样的二元组的个数
考虑无论从左还是右,低的都会对高的有影响。所以我们不用考虑 ij的位置,把全部的排序,排序后,考虑找到 包含k个x的范围 x*k和 x*k+x-1,同时对于k==0的情况下,都比x小的情况是没办法得出左右边界,
就是数据
2 5 0
4 3 都在0和5的范围内,所以左边界要和a[i] 比较,一定要大于a[i]。为什么是取更大,因为这样不用考虑0
和负数的影响
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.
Example
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.
题意:输入n,x,k; 和一个 n 长度的 序列 , 一个二元组的定义如下(i,j)
满足 ai<= y <= aj, 使得y|x的y个数为k个(y为[ai,aj]闭区间的整数)
注意 (i,j) != (j,i) 如果 i!=j的情况下,求出满足这样的二元组的个数
考虑无论从左还是右,低的都会对高的有影响。所以我们不用考虑 ij的位置,把全部的排序,排序后,考虑找到 包含k个x的范围 x*k和 x*k+x-1,同时对于k==0的情况下,都比x小的情况是没办法得出左右边界,
就是数据
2 5 0
4 3 都在0和5的范围内,所以左边界要和a[i] 比较,一定要大于a[i]。为什么是取更大,因为这样不用考虑0
和负数的影响
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[100010]; int main() { int n,x,k; cin>>n>>x>>k; for(int i=1;i<=n;i++) scanf("%lld",&a[i]); sort(a+1,a+n+1); ll ans=0; for(int i=1;i<=n;i++) { int tt=(a[i]-1)/x; tt+=k; ll l=1ll*tt*x,r=1ll*tt*x+x-1; l=max(l,a[i]); ans+=upper_bound(a+1,a+n+1,r)-a-(lower_bound(a+1,a+n+1,l)-a); } cout<<ans<<endl; } // 3 5 0 // 4 4 4 // 2 5 0 // 3 2
相关文章推荐
- Codeforces 689C Mike and Chocolate Thieves【二分+思维】
- Codeforces 675C Money Transfers 【思维】 + 675D Tree Construction 【二分】
- CodeForces 672D (思维+二分)
- codeforces 485D. Maximum Value(二分+思维)
- CodeForces - 672D Robin Hood (思维二分)
- codeforces-729【思维】【二分】
- CodeForces 785C Anton and Fairy Tale【二分答案+思维】
- CodeForces 672D Robin Hood(二分 + 思维)
- Codeforces 557B Pasha and Tea【二分+思维】
- Codeforces 689D Friends and Subsequences【思维+二分+RMQ】套路题
- 【CodeForces 689C】Mike and Chocolate Thieves 【二分+ 思维 】
- Codeforces 672D Robin Hood【思维+二分】这题思路有点劲啊
- Codeforces 349C Mafia【二分+思维判定】
- codeForces 535C.Tavas and Karafs(二分+思维)
- Codeforces 651D Image Preview【思维+二分+前缀后缀和】
- Codeforces 551C GukiZ hates Boxes【二分+思维处理】好题!
- CodeForces - 567D One-Dimensional Battle Ships —— 二分思维
- Codeforces 460C Present【二分+思维优化】
- Codeforces 344E Read Time [二分答案]【思维】
- Codeforces-448【A数学函数ceil】【B思维】【D二分】