Codeforces 895 B XK Segments 思维 二分

  题目链接: http://codeforces.com/contest/895/problem/B

  题目描述: 给你长度为n的数列a,然后让你找出所有的对(i,j)满足ai≤aj并且[ai,aj]中能整除X的数有且仅能有k个,问这样的对数总数。其中(i,j) 与(j,i),i≠j视为不同对,(i,i)与(i,i)为同一对。

  解题思路: 排序, 找出满足K的区间的长度(二分), 当a[i]是x的倍数的时候需要特判

  代码: 

#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>
#include <cmath>
#include <cstring>
#include <forward_list>
using namespace std;


const int maxn = 1e5 + 10;
typedef long long ll;
ll a[maxn];

int main() {

    std::ios::sync_with_stdio(false);
    cin.tie(0); 
    cout.tie(0);
    ll n, x, k;
    cin >> n >> x >> k;
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }        
    sort(a, a+n);
    ll res = 0;
    for(int i = 0; i < n; i++) {
        ll z = a[i] / x;
        ll l = (z+k)*x;
        ll r = l + x;
        if(a[i] % x == 0) l = (z+k-1)*x, r = l + x;
        l = max(a[i], l);
        l = lower_bound(a, a+n, l) - a;
        r = lower_bound(a, a+n, r) - a;
        res += r - l;
    }
    cout << res << endl;
    return 0;
}
View Code

  思考: 这题我很烦, 没有做出来, 自己就是记不得二分啊, 总是忘, 为什么啊, 真的伤, 这是打的最差的一场CF了, 新疆区域赛我得好好打!

posted on 2017-11-30 21:49  FriskyPuppy  阅读(197)  评论(0编辑  收藏  举报

导航