結果

問題 No.3801 Split Pairs
コンテスト
ユーザー 👑 Nachia
提出日時 2026-11-13 22:41:32
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 34 ms / 2,000 ms
+ 557µs
コード長 1,647 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 672 ms
コンパイル使用メモリ 108,260 KB
実行使用メモリ 16,408 KB
最終ジャッジ日時 2026-11-13 22:41:52
合計ジャッジ時間 3,734 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
// disable assert
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
template <class T> using V = vector<T>;
template <class A, class B> void chmax(A& l, const B& r){ if(l < r) l = r; }
template <class A, class B> void chmin(A& l, const B& r){ if(r < l) l = r; }

using ull = unsigned long long;

ull wt(V<ll> a){
  ull s = 0, ans = 0;
  for(ll x : a){
    ans += s * ull(x);
    s += x;
  }
  return ans;
}

template<typename Int>
Int FloorDiv(Int a, Int b){
    if(b < Int(0)){ a = -a; b = -b; }
    if(a >= Int(0) || a % b == Int(0)) return a / b;
    return a / b - Int(1);
}

void testcase(){
  ll N; cin >> N;
  V<ll> A(N); REP(i,N) cin >> A[i];
  sort(A.begin(), A.end());
  V<tuple<ll,ll,ll,ll>> st;
  for(ll a : A){
    ll l = a, r = a + 2, sz = 1, sum = a;
    while(st.size() && l < get<1>(st.back())){
      sz += get<2>(st.back());
      sum += get<3>(st.back());
      l = FloorDiv(sum - sz * (sz - 1) / 2, sz);
      r = l + sz + 1;
      st.pop_back();
    }
    st.push_back({ l, r, sz, sum });
  }
  V<ll> B;
  for(auto [l,r,sz,sum] : st){
    // cout << l << " " << r << " " << sz << " " << sum << endl;
    ll ab = (l + r - 1) * (r - l) / 2 - sum;
    for(ll i=l; i<r; i++) if(i != ab) B.push_back(i);
  }
  // for(auto b : A) cout << b << " "; cout << endl;
  // for(auto b : B) cout << b << " "; cout << endl;
  cout << (wt(A) - wt(B)) << "\n";
}

int main(){
  cin.tie(0)->sync_with_stdio(0);
  testcase();
  return 0;
}
0