zoukankan      html  css  js  c++  java
  • 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.


    解题心得:

    1. 题意很简单,如果一对数(i,j)满足ai到aj的所有数中是x的倍数的数目刚好是k个为一个方案,叫你输出给你的数列中所有的方案数。
    2. 先输入然后排个序,二分也很容易想到,刚开始只是想到了枚举每个起点,然后二分右边的终点,然后向后寻找符合条件的,结果TLE了。既然要二分那么久二分到底,还是枚举起点,二分符合条件的最左边的右边终点,然后再二分符合条件的最右边的右边终点,然后答案数目就是最左边到最右边的符合条件的终点。
    3. 判断边界问题是真的痛苦啊,智商不高还是老老实实画着图来写吧,不然各种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;
    }
  • 相关阅读:
    java 自定义线程池
    java 锁
    volatile相关内容
    消息驱动式微服务:Spring Cloud Stream & RabbitMQ
    JVM中的本机内存跟踪
    性能监控: SPF4J介绍
    Spring Batch 入门级示例教程
    使用 Spring Boot Actuator 构建 RESTful Web 应用
    回调“地狱”和反应模式
    Java动态规划
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107199.html
Copyright © 2011-2022 走看看