結果

問題 No.2501 Maximum Inversion Number
ユーザー publfl2publfl2
提出日時 2023-10-13 21:49:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 1,251 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 32,468 KB
実行使用メモリ 6,160 KB
最終ジャッジ日時 2023-10-13 21:49:44
合計ジャッジ時間 2,321 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 46 ms
4,348 KB
testcase_02 AC 80 ms
4,348 KB
testcase_03 AC 53 ms
4,352 KB
testcase_04 AC 52 ms
6,104 KB
testcase_05 AC 44 ms
6,092 KB
testcase_06 AC 59 ms
6,112 KB
testcase_07 AC 48 ms
4,348 KB
testcase_08 AC 44 ms
4,348 KB
testcase_09 AC 31 ms
4,604 KB
testcase_10 AC 45 ms
4,348 KB
testcase_11 AC 40 ms
4,352 KB
testcase_12 AC 43 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 52 ms
6,160 KB
testcase_15 AC 39 ms
4,348 KB
testcase_16 AC 147 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

long long int L[200010],R[200010];
int main()
{
    int Case;
    scanf("%d",&Case);
    while(Case--)
    {
        int a;
        long long int b;
        scanf("%d%lld",&a,&b);
        for(int i=1;i<=a;i++) scanf("%lld",&L[i]);
        for(int i=1;i<=a;i++) scanf("%lld",&R[i]);
        long long int min = 0, max = 1e9;
        long long int ans = -1;
        while(min<=max)
        {
            long long int h = (min+max)/2;
            long long int s = 0, t = 0;
            for(int i=1;i<=a;i++)
            {
                if(R[i]<=h) s += R[i];
                else if(L[i]>h) s += L[i];
                else s += h, t++;
            }
            if(s>b) max = h-1;
            else if(s+t<b) min = h+1;
            else
            {
                t = b-s;
                long long int val = 0;
                for(int i=1;i<=a;i++)
                {
                    if(R[i]<=h) val += R[i]*R[i];
                    else if(L[i]>h) val += L[i]*L[i];
                    else if(t>0) val += (h+1)*(h+1), t--;
                    else val += h*h;
                }
                ans = ans>(b*b-val)/2?ans:(b*b-val)/2;
                min = h+1;
            }
        }
        printf("%lld\n",ans);
    }
}
0