結果

問題 No.2501 Maximum Inversion Number
ユーザー SSRSSSRS
提出日時 2023-10-13 21:18:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 607 ms / 2,000 ms
コード長 1,262 bytes
コンパイル時間 2,849 ms
コンパイル使用メモリ 202,872 KB
実行使用メモリ 5,520 KB
最終ジャッジ日時 2023-10-13 21:18:54
合計ジャッジ時間 5,863 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 80 ms
4,348 KB
testcase_02 AC 185 ms
4,352 KB
testcase_03 AC 108 ms
4,348 KB
testcase_04 AC 89 ms
5,460 KB
testcase_05 AC 91 ms
5,464 KB
testcase_06 AC 96 ms
5,512 KB
testcase_07 AC 99 ms
4,352 KB
testcase_08 AC 101 ms
4,348 KB
testcase_09 AC 82 ms
4,352 KB
testcase_10 AC 97 ms
4,352 KB
testcase_11 AC 73 ms
4,348 KB
testcase_12 AC 73 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 120 ms
5,520 KB
testcase_15 AC 78 ms
4,352 KB
testcase_16 AC 607 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int n, m;
    cin >> n >> m;
    vector<int> L(n);
    for (int j = 0; j < n; j++){
      cin >> L[j];
    }
    vector<int> R(n);
    for (int j = 0; j < n; j++){
      cin >> R[j];
    }
    long long SL = 0, SR = 0;
    for (int j = 0; j < n; j++){
      SL += L[j];
      SR += R[j];
    }
    if (!(SL <= m && m <= SR)){
      cout << -1 << endl;
    } else {
      int tv = -1, fv = 1000000001;
      int cnt = 0;
      while (fv - tv > 1){
        int mid = (tv + fv) / 2;
        long long sum = 0;
        for (int j = 0; j < n; j++){
          sum += min(R[j], max(L[j], mid));
        }
        if (sum <= m){
          tv = mid;
          cnt = m - sum;
        } else {
          fv = mid;
        }
      }
      vector<int> C(n);
      for (int j = 0; j < n; j++){
        C[j] = min(R[j], max(L[j], tv));
      }
      for (int j = 0; j < n; j++){
        if (C[j] == tv && R[j] > tv && cnt > 0){
          C[j]++;
          cnt--;
        }
      }
      long long ans = (long long) m * (m - 1) / 2;
      for (int j = 0; j < n; j++){
        ans -= (long long) C[j] * (C[j] - 1) / 2;
      }
      cout << ans << endl;
    }
  }
}
0