Submission #10190235


Source Code Expand

Copy
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(a)  (a).begin(),(a).end()

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}


// nC2を求める関数
ll pair_conb(ll n) {
    return n*(n-1)/2;
}


double getTime(chrono::system_clock::time_point st,chrono::system_clock::time_point ed) {
    return static_cast<double>(chrono::duration_cast<chrono::microseconds>(ed - st).count() / 1000.0);
}

void solve() {
    ll N,K;
    cin >> N >> K;
    vector<ll> A(N);
    REP(i,N) cin >> A[i];

    sort(ALL(A));
    // binary search (K番目の値である様なxを探す)
    // お気持ち的に NC2ループの二個目のforを二分探索にしてNlogNに計算量を落としおいる。
    ll left = -2e18, right = 2e18;
    while (right - left > 1) {
        // cnt はx(mid)よりも小さい数のカウンタ
        ll mid = (left+right)/2, cnt = 0;

        REP(i,N) {
            // 普通に書いたらO(N^2)だが、xを決め打ちするのと、Aの単調性を利用することでO(NlogN)に落とす。
            // 片方をA[i]と決定して,iより先にある値mで、A[i]*A[m] < x 以下である様な最大のmを二分探索で求める
            ll l = i, r = N;
            while ( r - l > 1) {
                ll m = (l+r)/2;
                if (A[i] >= 0) (A[i] * A[m] < mid) ? l = m : r = m;
                else (A[i] * A[m] < mid) ? r = m : l = m;

            }
            // l を足す(m) -> (r-l) == 0なら r = lなので自明
            // (r-l) == 1 なら m = (l+r)/2;から切り捨てられるので結局 (l+l)/2と同じなので l を使用する
            cnt += (A[i] >= 0) ? l - i : N - r;

        }
        // cnt が小さい -> xが小さい
        ( cnt < K) ? left = mid : right = mid;
    }
    cout << left << endl;
}
int main() {
    auto st = chrono::system_clock::now();
    solve();
    auto ed = chrono::system_clock::now();
    cerr << "end with: " << getTime(st,ed) << "ms" << endl;

    return 0;
}

Submission Info

Submission Time
Task D - Pairs
User reud
Language C++14 (GCC 5.4.1)
Score 400
Code Size 2480 Byte
Status AC
Exec Time 1131 ms
Memory 1792 KB

Judge Result

Set Name Sample Subtask1
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 79
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
Subtask1 sample_01.txt, sample_02.txt, sample_03.txt, sub1_01.txt, sub1_02.txt, sub1_03.txt, sub1_04.txt, sub1_05.txt, sub1_06.txt, sub1_07.txt, sub1_08.txt, sub1_09.txt, sub1_10.txt, sub1_11.txt, sub1_12.txt, sub1_13.txt, sub1_14.txt, sub1_15.txt, sub1_16.txt, sub1_17.txt, sub1_18.txt, sub1_19.txt, sub1_20.txt, sub1_21.txt, sub1_22.txt, sub1_23.txt, sub1_24.txt, sub1_25.txt, sub1_26.txt, sub1_27.txt, sub1_28.txt, sub1_29.txt, sub1_30.txt, sub1_31.txt, sub1_32.txt, sub1_33.txt, sub1_small_01.txt, sub1_small_02.txt, sub1_small_03.txt, sub1_small_04.txt, sub1_small_05.txt, sub1_small_06.txt, sub1_small_07.txt, sub1_small_08.txt, sub1_small_09.txt, sub1_small_10.txt, sub1_small_11.txt, sub1_small_12.txt, sub1_small_13.txt, sub1_small_14.txt, sub1_small_15.txt, sub1_small_16.txt, sub1_small_17.txt, sub1_small_18.txt, sub1_small_19.txt, sub1_small_20.txt, sub1_small_21.txt, sub1_small_22.txt, sub1_small_23.txt, sub1_small_24.txt, sub1_small_25.txt, sub1_small_26.txt, sub1_small_27.txt, sub1_small_28.txt, sub1_small_29.txt, sub1_small_30.txt, sub1_small_31.txt, sub1_small_32.txt, sub1_small_33.txt, sub1_small_34.txt, sub1_small_35.txt, sub1_small_36.txt, sub1_small_37.txt, sub1_small_38.txt, sub1_small_39.txt, sub1_small_40.txt, sub1_small_41.txt, sub1_small_42.txt, sub1_small_43.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB
sub1_01.txt AC 401 ms 896 KB
sub1_02.txt AC 765 ms 1408 KB
sub1_03.txt AC 1052 ms 1792 KB
sub1_04.txt AC 567 ms 1152 KB
sub1_05.txt AC 371 ms 896 KB
sub1_06.txt AC 122 ms 512 KB
sub1_07.txt AC 796 ms 1408 KB
sub1_08.txt AC 899 ms 1664 KB
sub1_09.txt AC 707 ms 1280 KB
sub1_10.txt AC 158 ms 512 KB
sub1_11.txt AC 925 ms 1536 KB
sub1_12.txt AC 102 ms 384 KB
sub1_13.txt AC 771 ms 1408 KB
sub1_14.txt AC 324 ms 768 KB
sub1_15.txt AC 112 ms 512 KB
sub1_16.txt AC 940 ms 1536 KB
sub1_17.txt AC 488 ms 1024 KB
sub1_18.txt AC 42 ms 384 KB
sub1_19.txt AC 1042 ms 1792 KB
sub1_20.txt AC 617 ms 1152 KB
sub1_21.txt AC 1059 ms 1792 KB
sub1_22.txt AC 442 ms 896 KB
sub1_23.txt AC 527 ms 1024 KB
sub1_24.txt AC 342 ms 768 KB
sub1_25.txt AC 233 ms 640 KB
sub1_26.txt AC 682 ms 1280 KB
sub1_27.txt AC 993 ms 1792 KB
sub1_28.txt AC 1094 ms 1792 KB
sub1_29.txt AC 1077 ms 1792 KB
sub1_30.txt AC 1065 ms 1792 KB
sub1_31.txt AC 1080 ms 1792 KB
sub1_32.txt AC 1095 ms 1792 KB
sub1_33.txt AC 1131 ms 1792 KB
sub1_small_01.txt AC 4 ms 256 KB
sub1_small_02.txt AC 2 ms 256 KB
sub1_small_03.txt AC 6 ms 256 KB
sub1_small_04.txt AC 3 ms 256 KB
sub1_small_05.txt AC 2 ms 256 KB
sub1_small_06.txt AC 6 ms 256 KB
sub1_small_07.txt AC 2 ms 256 KB
sub1_small_08.txt AC 4 ms 256 KB
sub1_small_09.txt AC 3 ms 256 KB
sub1_small_10.txt AC 3 ms 256 KB
sub1_small_11.txt AC 4 ms 256 KB
sub1_small_12.txt AC 6 ms 256 KB
sub1_small_13.txt AC 2 ms 256 KB
sub1_small_14.txt AC 6 ms 256 KB
sub1_small_15.txt AC 1 ms 256 KB
sub1_small_16.txt AC 6 ms 256 KB
sub1_small_17.txt AC 2 ms 256 KB
sub1_small_18.txt AC 2 ms 256 KB
sub1_small_19.txt AC 1 ms 256 KB
sub1_small_20.txt AC 6 ms 256 KB
sub1_small_21.txt AC 5 ms 256 KB
sub1_small_22.txt AC 6 ms 256 KB
sub1_small_23.txt AC 1 ms 256 KB
sub1_small_24.txt AC 1 ms 256 KB
sub1_small_25.txt AC 2 ms 256 KB
sub1_small_26.txt AC 2 ms 256 KB
sub1_small_27.txt AC 4 ms 256 KB
sub1_small_28.txt AC 3 ms 256 KB
sub1_small_29.txt AC 5 ms 256 KB
sub1_small_30.txt AC 2 ms 256 KB
sub1_small_31.txt AC 6 ms 256 KB
sub1_small_32.txt AC 4 ms 256 KB
sub1_small_33.txt AC 6 ms 256 KB
sub1_small_34.txt AC 5 ms 256 KB
sub1_small_35.txt AC 7 ms 256 KB
sub1_small_36.txt AC 2 ms 256 KB
sub1_small_37.txt AC 5 ms 256 KB
sub1_small_38.txt AC 3 ms 256 KB
sub1_small_39.txt AC 2 ms 256 KB
sub1_small_40.txt AC 7 ms 256 KB
sub1_small_41.txt AC 2 ms 256 KB
sub1_small_42.txt AC 2 ms 256 KB
sub1_small_43.txt AC 5 ms 256 KB