Codeforces Round #448 (Div. 2)B. XK Segments (双指针 or 二分 )

来源:互联网 发布:linux修改分区大小 编辑:程序博客网 时间:2024/05/16 10:56

题目:

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.

题意:

给你n个数字 然后让你求 第i个数字到第j个数字中恰好有k个膜x等于零的数字这样的ij对有多少个


思路:

这里提供两种方法 我刚开始看到这道题只想到了二分的写法  还有一种是双指针的写法
关于二分的写法我在处理 a[i] - a[j] 中有多少个膜x等于零的数字的时候用了  a[i] 是否整除x的蠢方法 后来看出题人题解才知道
a[r] / x - (a[l] - 1 ) / x 就好了 复杂度O(n * log(n))

我是不会告诉你们我在longlong上卡了两个小时的~~  我第一次看到我算的答案是负数还以为是区间问题~~~~~~ 
关于双指针的做法:

这种写法是看cf上的大佬才知道的   原来还有这种操作 
我们发现这个数组中的数字能选则的区间是有规律的  就是说  如果你当前数字能选的l - r区间在这里 那么当前数字的下一个数字能选的范围和当前这个范围相差不多
因为是递增的序列 所以区间不会往后退 所以  O(n) 便利就得到答案了~~~

先贴一个我写的二分 菜~~
#include <iostream>#include <algorithm>using namespace std;const static int N = 1e5 + 10;typedef long long ll;int main(){ll n , k , x, array[N];ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);while (cin >> n >> x >> k) {for (ll i = 0 ; i < n ; i ++) cin >> array[i];sort(array , array + n);ll res = 0;for (ll i = 0 ; i < n ; i ++) {ll temp = (array[i])/ x;if(array[i] % x) {ll l = (temp + k) * x ;ll r = (temp + k + 1) * x - 1;l = max(l , array[i]);//printf("array == %d l == %d R == %d\n",array[i], l, r);l = lower_bound(array , array + n, l) - array;r = upper_bound(array , array + n, r) - array;if (l == n) continue;if (r < l) continue;//if (k == 0) {//int pos = lower_bound(array , array + n , array[i]) - array;//res -= pos - l;//}res += r - l;} else {//printf("This array == %d\n",array[i]);ll l  = (temp + k - 1) * x ;ll r = (temp + k) * x - 1;l = max(l , array[i]);//printf("prel == %d prer == %d\n",l , r);l = lower_bound(array , array + n , l ) - array;r = upper_bound(array , array + n , r) - array;if (l == n) continue;if (r < l) continue;//if (k == 0) {//int pos = lower_bound(array , array + n , array[i]) - array;//res -= pos - l;//}//printf("l == %d R == %d \n", l, r);res += r - l;}}cout << res << endl;}}/*2 5 03 4*/

dalao们的二分是这样的:

#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const static int N = 1e5 + 10;int main(){ll n , k , x, array[N];ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);while (cin >> n >> x >> k) {for (ll i = 0 ; i < n ; i ++) cin >> array[i];sort(array , array + n);ll res = 0;for (ll i = 0 ; i < n ; i ++) {ll temp = (array[i] - 1)/ x;ll l = max((temp + k) * x , array[i]);ll r = (temp + k + 1) * x;res += lower_bound(array , array + n , r) - lower_bound(array , array + n , l);}cout << res << endl;}}/*2 5 03 4*/


双指针的做法:
#include <iostream>#include <algorithm>using namespace std;const static int N = 1e5 + 10;int array[N];int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n , x , k;cin >> n >> x >> k;for (int i = 0;  i < n ; i++) cin >> array[i];sort(array , array + n);long long res = 0;int l , r ;l = r = 0;for (int i = 0 ; i < n; i ++) {while (l < n && array[l] < array[i]) l ++;while (r < n && array[r] < array[i]) r ++;while (l < n && array[l] / x - (array[i] - 1) / x < k) l ++;while (r < n && array[r] / x - (array[i] - 1) / x <= k) r ++;res += r - l;}cout << res << endl;}/*4 2 05 3 1 7*/






原创粉丝点击