2020 CCPC Wannafly Winter Camp Day1-F-乘法

题目传送门

sol:二分答案$K$,算大于$K$的乘积有多少个。关键在于怎么算这个个数,官方题解上给出的复杂度是$O(nlogn)$,那么计算个数的复杂度是$O(n)$的。感觉写着有点困难,自己写了一个复杂度是$O(nlog^{2}n)$,也够AC了。有正有负,控制边界有点难度。

  • 二分答案
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    const int MAXN = 1e5 + 10;
    int a[MAXN], b[MAXN];
    int n, m; LL k;
    LL check(LL mid) {
        LL sum = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] == 0) {
                if (mid < 0) sum += m;
                continue;
            }
            if (a[i] > 0) {
                if (mid >= 0) {
                    sum += b + 1 + m - upper_bound(b + 1, b + 1 + m, mid / a[i]);
                } else {
                    sum += b + 1 + m - lower_bound(b + 1, b + 1 + m, (mid + 1) / a[i]);
                }
            } else {
                if (mid >= 0) {
                    sum += lower_bound(b + 1, b + 1 + m, mid / a[i]) - 1 - (b + 1 - 1);
                } else {
                    sum += upper_bound(b + 1, b + 1 + m, (mid + 1) / a[i]) - 1 - (b + 1 - 1);
                }
            }
        }
        return sum;
    }
    int main() {
        scanf("%d%d%lld", &n, &m, &k);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
        sort(b + 1, b + 1 + m);
        LL l = -1e12 - 10, r = 1e12 + 10;
        while (l + 1 < r) {
            LL mid = l + r >> 1;
            if (check(mid) < k) r = mid;
            else l = mid;
        }
        printf("%lld\n", r);
        return 0;
    }

     

posted @ 2020-01-21 12:19  Jathon-cnblogs  阅读(312)  评论(0编辑  收藏  举报