题目:
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.
题意:
给你n个数字 然后让你求 第i个数字到第j个数字中恰好有k个膜x等于零的数字这样的ij对有多少个
思路:
#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 0
3 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 0
3 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 0
5 3 1 7
*/