結果

問題 No.694 square1001 and Permutation 3
ユーザー tetsutetsu
提出日時 2018-06-09 00:02:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,441 ms / 3,000 ms
コード長 1,094 bytes
コンパイル時間 2,164 ms
コンパイル使用メモリ 178,432 KB
実行使用メモリ 108,772 KB
最終ジャッジ日時 2023-09-13 00:36:58
合計ジャッジ時間 13,185 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 327 ms
14,808 KB
testcase_08 AC 1,311 ms
33,692 KB
testcase_09 AC 2,420 ms
108,684 KB
testcase_10 AC 174 ms
14,804 KB
testcase_11 AC 2,441 ms
108,772 KB
testcase_12 AC 2,348 ms
108,696 KB
testcase_13 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

ll tentou(vector<ll> &v) {
  if(v.size()<=1) {
    return 0;
  }
  ll n = v.size();
  vector<ll> h1(v.begin(), v.begin()+n/2);
  vector<ll> h2(v.begin()+n/2, v.end());
  ll t1 = tentou(h1);
  ll t2 = tentou(h2);
  sort(h1.begin(), h1.end());
  sort(h2.begin(), h2.end());
  ll idx=0;
  ll ans = t1+t2;
  for(auto x : h1) {
    while(idx<h2.size()&&h2[idx]<x) idx++;
    ans+=idx;
  }
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> v(n);
  map<ll,ll> m;
  rep(i, n) {
    cin >> v[i];
    if(m.find(v[i])!=m.end()) {
      m[v[i]]++;
    } else {
      m[v[i]]=1;
    }
  }
  map<ll,ll> bigger;
  map<ll,ll> smaller;
  {
    ll acc = 0;
    for(auto itr : m) {
      smaller[itr.first] = acc;
      bigger[itr.first] = n-acc-itr.second;
      acc+=itr.second;
    }
  }
  ll cnt = tentou(v);
  cout << cnt << "\n";
  rep(i, n-1) {
    cnt-=smaller[v[i]];
    cnt+=bigger[v[i]];
    cout << cnt<<"\n";
  }
  return 0;
}
0