結果

問題 No.2500 Products in a Range
ユーザー nono00
提出日時 2023-10-13 22:43:17
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 2,704 bytes
コンパイル時間 3,247 ms
コンパイル使用メモリ 260,180 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-15 18:28:32
合計ジャッジ時間 5,273 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

namespace nono {

struct Init {};

void solve([[maybe_unused]] const Init& init) {
    long long n, l, r;
    std::cin >> n >> l >> r;
    std::vector<long long> a(n);
    for (int i = 0; i < n; i++) std::cin >> a[i];
    std::ranges::sort(a);
    int mid = std::distance(a.begin(), std::ranges::lower_bound(a, 0));
    int right = 0;
    int ans = 0;

    auto ok = [&](int left, int right) {
        if (right - left <= 1) return true;
        if (right <= mid) {
            if (r < a[left] * a[left + 1]) return false;
            if (a[right - 1] * a[right - 2] < l) return false;
        } else if (mid <= left) {
            if (a[right - 1] * a[right - 2] > r) return false;
            if (a[left] * a[left + 1] < l) return false;
        } else {
            if (a[right - 1] * a[left] < l) return false;
            if (mid - left >= 2) {
                if (a[left] * a[left + 1] > r) return false;
            }
            if (right - mid >= 2) {
                if (a[right - 1] * a[right - 2] > r) return false;
            }
            if (right - mid < 2 && mid - left < 2) {
                if (r < a[right - 1] * a[left]) return false;
            }
        }
        return true;
    };

    for (int left = 0; left < n; left++) {
        if (right < left) right = left;
        while (right < n && ok(left, right + 1)) {
            right++;
        }
        bool flag = false;
        for (int i = 0; i < n; i++) {
            if (left <= i && i < right) continue;

            if (a[i] == 0) {
                if (l <= 0 && 0 <= r) {
                    flag = true;
                    break;
                }
            } else if (a[i] > 0) {
                if (right <= mid) {
                    if (a[i] * a[left] < l) continue;
                    if (a[i] * a[right - 1] > r) continue;
                } else {
                    if (a[i] * a[left] < l) continue;
                    if (a[i] * a[right - 1] > r) continue;
                }
                flag = true;
                break;
            } else {
                if (right <= mid) {
                    if (a[i] * a[left] > r) continue;
                    if (a[i] * a[right - 1] < l) continue;
                } else {
                    if (a[i] * a[left] > r) continue;
                    if (a[i] * a[right - 1] < l) continue;
                }
                flag = true;
                break;
            }
        }
        ans = std::max(ans, right - left + flag);
    }
    std::cout << ans << std::endl;
}

}  //  namespace nono

int main() {
    std::cin.tie(0)->sync_with_stdio(0);

    int t = 1;
    nono::Init init;

    while (t--) nono::solve(init);
}
0