結果

問題 No.2501 Maximum Inversion Number
ユーザー tofu_dra2tofu_dra2
提出日時 2023-10-13 21:57:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 3,010 bytes
コンパイル時間 5,617 ms
コンパイル使用メモリ 266,104 KB
実行使用メモリ 16,844 KB
最終ジャッジ日時 2023-10-13 21:58:05
合計ジャッジ時間 7,171 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 57 ms
4,352 KB
testcase_02 AC 93 ms
4,352 KB
testcase_03 AC 72 ms
5,212 KB
testcase_04 AC 81 ms
15,392 KB
testcase_05 AC 82 ms
15,236 KB
testcase_06 AC 86 ms
16,844 KB
testcase_07 AC 68 ms
7,648 KB
testcase_08 AC 73 ms
8,116 KB
testcase_09 AC 29 ms
5,160 KB
testcase_10 AC 62 ms
5,260 KB
testcase_11 AC 65 ms
7,924 KB
testcase_12 AC 67 ms
7,816 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 88 ms
15,720 KB
testcase_15 AC 62 ms
5,052 KB
testcase_16 AC 354 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

typedef long long int ll;
typedef long double ld;

using namespace std;
using namespace atcoder;

#define inf 1010000000
#define llinf 1001000000000000000ll
#define pi 3.141592653589793238
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define rep1(i, n) for(ll i = 1; i <= (n); i++)
#define rep2(i,l,r) for(ll i = (l); i < (r); i++)
#define per(i, n) for(ll i = (n)-1; i >= 0; i--)
#define each(x, v) for (auto&& x : v)
#define rng(a) a.begin(),a.end()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define st string
#define pcnt __builtin_popcountll
#define bit(n) (1LL<<(n))

template <class T = ll>
inline T in(){ T x; cin >> x; return (x);}
#define vcin(x,n) {for(ll loop=0; loop<(n); loop++) cin>>x[loop];}

#define dame { puts("-1"); return 0;}
#define yes { puts("Yes"); return 0;}
#define no { puts("No"); return 0;}
#define ret(x) { cout<<(x)<<endl;}
#define rets(x) { cout<<(x)<< " ";}
#define Endl cout<<endl;
#define dump(x) { cout << #x << " = " << (x) << endl;}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false;}

// 仮マクロ 便利だったら昇格
#define unique(v) v.erase( unique(v.begin(), v.end()), v.end())
// ここまで仮マクロ

// clock()/CLOCKS_PER_SEC 秒数を知りたいときに用いる

#define mod 998244353
using mint = modint998244353;

/*

#define mod 1000000007
using mint = modint1000000007;

*/

vector<ll> dx={1,0,-1,0};
vector<ll> dy={0,1,0,-1};

using pl = pair<ll,ll>;
using ppl = pair<pl,ll>;
// G.assign(n, vector<ll>()); グローバル変数にGを置く時に置く

// 関数を置くのはここ以下

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll T = in();
  rep(_,T){
    ll n,m; cin >> n >> m; ll ans = m*m;
    vector<pl> v;
    rep(i,n){
      ll l = in();
      v.eb(l,0);
      ans -= l*l;
    }
    rep(i,n){
      ll r = in();
      v[i].se = r;
    }
    pl sum = {0,0};
    rep(i,n){
      sum.fi += v[i].fi;
      sum.se += v[i].se;
    }
    if(sum.fi>m || sum.se<m){
      ret(-1)
      continue;
    }
    vector<pl> v2;
    rep(i,n){
      v2.eb(v[i].fi,1);
      v2.eb(v[i].se,-1);
    }
    m -= sum.fi;
    sort(rng(v2));
    ll now = 0;
    rep(i,2*n-1){
      now += v2[i].se;
      if(now==0) continue;
      if(now * (v2[i+1].fi-v2[i].fi) < m){
        ans -= (v2[i+1].fi*v2[i+1].fi - v2[i].fi*v2[i].fi) * now;
        m -= now * (v2[i+1].fi-v2[i].fi);
      }else{
        // dump(ans)
        // dump(m)
        // dump(now)
        ans -= ((v2[i].fi + m/now)*(v2[i].fi + m/now) - v2[i].fi*v2[i].fi) * now;
        // dump(ans)
        ans -= (m%now) * ((v2[i].fi + m/now + 1)*(v2[i].fi + m/now + 1) - (v2[i].fi + m/now)*(v2[i].fi + m/now));
        // dump(ans)
        break;
      }
    }
    ret(ans/2)
  }
}
0