結果

問題 No.2501 Maximum Inversion Number
ユーザー baluteshihbaluteshih
提出日時 2023-10-13 21:50:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 1,645 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 198,816 KB
実行使用メモリ 5,008 KB
最終ジャッジ日時 2023-10-13 21:50:10
合計ジャッジ時間 3,770 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 33 ms
4,348 KB
testcase_02 AC 57 ms
4,348 KB
testcase_03 AC 39 ms
4,352 KB
testcase_04 AC 37 ms
5,008 KB
testcase_05 AC 38 ms
4,928 KB
testcase_06 AC 38 ms
4,988 KB
testcase_07 AC 34 ms
4,352 KB
testcase_08 AC 38 ms
4,348 KB
testcase_09 AC 25 ms
4,348 KB
testcase_10 AC 36 ms
4,352 KB
testcase_11 AC 32 ms
4,352 KB
testcase_12 AC 32 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 43 ms
4,992 KB
testcase_15 AC 33 ms
4,352 KB
testcase_16 AC 118 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

const ll MAXC = 2e9;
int L[200005], R[200005];

void solve() {
    int n, m;
    ll sumL = 0, sumR = 0;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> L[i], sumL += L[i];
    for (int i = 1; i <= n; ++i)
        cin >> R[i], sumR += R[i];
    if (sumL > m || sumR < m) {
        cout << "-1\n";
        return;
    }
    if (sumL < m) {
        auto cal = [&](ll mid) {
            ll tar = (mid + 1) / 2;
            ll res = 0;
            for (int i = 1; i <= n; ++i)
                res += min((ll)R[i], max((ll)L[i], tar));
            return res;
        };

        ll l = 0, r = MAXC;
        while (r - l > 1) {
            ll mid = (l + r) >> 1;
            if (cal(mid) >= m) r = mid;
            else l = mid;
        }
        ll tar = (l + 1) / 2;
        sumL = 0;
        for (int i = 1; i <= n; ++i) {
            L[i] = min((ll)R[i], max((ll)L[i], tar));
            sumL += L[i];
        }
        assert(sumL < m);
        for (int i = 1; i <= n && sumL < m; ++i) {
            if (L[i] == tar && L[i] + 1 <= R[i]) {
                ++L[i];
                ++sumL;
            }
        }
        assert(sumL == m);
    }
    ll ans = (ll)m * m;
    for (int i = 1; i <= n; ++i)
        ans -= (ll)L[i] * L[i];
    cout << ans / 2 << "\n";
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
0