CodeForces-895B XK Segments[二分][思维]



XK Segments


题目传送门:CodeForces-895B

Time Limit 1000 ms
Memory limit 262144 kB



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 ≤  1 0 5 10^5 105, 1 ≤ x ≤  1 0 9 10^9 109, 0 ≤ k ≤  1 0 9 10^9 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 ≤  a i a_i ai ≤  1 0 9 10^9 109) — the elements of the array a.

Output:

Print one integer — the answer to the problem.


Sample:

Sample OneInput

4 2 1
1 3 5 7

Sample OneOutput

3

Sample OneInput

4 2 0
5 3 1 7

Sample OneOutput

4

Sample OneInput

5 3 1
3 3 3 3 3

Sample OneOutput

25

Node

In first sample there are only three suitable pairs of indexes — ( 1 ,   2 ) ,   ( 2 ,   3 ) ,   ( 3 ,   4 ) (1, 2), (2, 3), (3, 4) (1,2),(2,3),(3,4).

In second sample there are four suitable pairs of indexes ( 1 ,   1 ) ,   ( 2 ,   2 ) ,   ( 3 ,   3 ) ,   ( 4 ,   4 ) (1, 1), (2, 2), (3, 3), (4, 4) (1,1),(2,2),(3,3),(4,4).

In third sample every pair (i, j) is suitable, so the answer is 5   ∗   5   =   25 5 * 5 = 25 55=25.



题目大意:

给一个长度为 n n n的数组,问满足 y ∈ { a [ i ] ≤ y ≤ a [ j ] } & & ( y % x = 0 ) y \in \{a[i] \leq y \leq a[j]\} \&\& (y \% x = 0) y{ a[i]ya[j]}&&(y%x=0),这样的 y y y的个数为 k k k个的对数 ( i , j ) (i,j) (i,j)共有多少对,其中 ( i , j ) (i,j) (i,j) ( j , i ) (j,i) (j,i)一样,只算一队


思路:

由于位置并无影响,所以可以先从小到大排序
满足上次条件的 y y y必定在范围 ( m a x ( t m p ∗ x , a [ i ] ) , t m p ∗ x + x − 1 ) (max(tmp*x, a[i]), tmp*x+x-1) (max(tmpx,a[i]),tmpx+x1)内,其中 t m p tmp tmp就是 ( a [ i ] − 1 ) / x + k (a[i] - 1) / x + k (a[i]1)/x+k; // 在 a [ i ] a[i] a[i]前的满足 a [ i ] % x = = 0 a[i] \% x == 0 a[i]%x==0个数
确定上下界后,只需要找到满足这个条件的数的个数即可
≤ l \leq l l第一个数的下标,以及 < r + 1 < r+1 <r+1的最后一个数下标,进行相减就可以得到对于 a [ i ] a[i] a[i]来说可以符合条件的对数



Code:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define  endc       std::ios::sync_with_stdio(false); // 关掉c++流
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  qwq(i, j)  for(int i = 0; i < j; ++i)
#define  qeq(i, j)  for(int i = 1; i <= j; ++i)
#define  isdigit(a) ((a)>='0'&&(a)<='9')
#define  xiao(a)    ((a)>='a'&&(a)<='z')
#define  da(a)      ((a)>='A'&&(a)<='Z')
#define  pii        pair
#define  lowbit(x)  x & (-x)
#define  fi         first
#define  se         second
#define  lson       id<<1
#define  rson       id<<1|1

typedef unsigned long long int ull;
typedef long long int ll;
const double eps  = 1e-8;
const double pi   = acos(-1.0);
const int    inf  = 0x3f3f3f3f;
const ll     INF  = 1e18 + 100;
const int    maxm = 1e6 + 6;
const int    maxn = 1e5 + 10;
const int    mod  = 1e9+7;
using namespace std;


int n, m, cas, tol = 0;
int head[maxn];
struct Edge {
     int u, v, w;}edge[maxm];

template<typename T>void re(T &x){
     x = 0; int f = 0; char ch = getchar();while(!isdigit(ch)){
     if(ch == '-') f=1; ch=getchar();}while(isdigit(ch)){
     x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}x = f?-x:x;}
// templateT fpow(T a, ll b) {T ans = 1; while(b) {if(b & 1) ans = a * ans % mod; a = a * a % mod; b >>= 1;}return ans;}
// inline void adde(int u, int v, int w) {edge[tol] = Edge{v, head[u], w}; head[u] = tol++;}
//POJ 3463 - Sightseeing

int a[maxn];

int main() {
     
    int n, x, k;
    scanf("%d%d%d", &n, &x, &k); // y = z*x
    for(int i = 1; i <= n; ++i) {
     
        scanf("%d", &a[i]);
    }
    ll l, r, res = 0;
    sort(a + 1, a + n + 1);
    for(int i = 1; i <= n; ++i) {
     
        int tmp = (a[i] - 1) / x; // 在a[i]前的个数
        tmp += k;
        l = max(a[i] * 1ll, tmp * 1ll * x);
        r = tmp * 1ll * x + x - 1; // l, r为有k个数的左右边界
        res += (upper_bound(a + 1, a + n + 1, r)-a) - (lower_bound(a + 1, a + n + 1, l)-a);
    }
    printf("%lld\n", res);
    return 0;
}

你可能感兴趣的:(思维,二分,思维,二分)