結果

問題 No.694 square1001 and Permutation 3
ユーザー tetsutetsu
提出日時 2018-06-08 23:56:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,613 ms / 3,000 ms
コード長 1,093 bytes
コンパイル時間 1,806 ms
コンパイル使用メモリ 179,160 KB
実行使用メモリ 116,516 KB
最終ジャッジ日時 2023-09-13 00:33:17
合計ジャッジ時間 13,986 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 406 ms
22,768 KB
testcase_08 AC 1,478 ms
41,452 KB
testcase_09 AC 2,536 ms
116,516 KB
testcase_10 AC 239 ms
22,720 KB
testcase_11 AC 2,613 ms
116,232 KB
testcase_12 AC 2,611 ms
116,436 KB
testcase_13 AC 1 ms
4,384 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