結果

問題 No.1470 Mex Sum
ユーザー Mayimg
提出日時 2021-04-09 22:22:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 806 bytes
コンパイル時間 3,306 ms
コンパイル使用メモリ 194,028 KB
最終ジャッジ日時 2025-01-20 14:42:24
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto& x : a) cin >> x;
  vector<int> cnt1(n + 1), cnt2(n + 1);
  for (int i = 0; i < n; i++) {
    cnt1[i + 1] = cnt1[i] + (a[i] == 1);
    cnt2[i + 1] = cnt2[i] + (a[i] == 2);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == 1) {
      ans += 2 * cnt1[i];
      ans += 3 * cnt2[i];
      ans += 2 * (i - cnt1[i] - cnt2[i]);
    } else if (a[i] == 2) {
      ans += 3 * cnt1[i];
      ans += 1 * cnt2[i];
      ans += 1 * (i - cnt1[i] - cnt2[i]);
    } else {
      ans += 2 * cnt1[i];
      ans += 1 * cnt2[i];
      ans += 1 * (i - cnt1[i] - cnt2[i]);
    }
  }
  cout << ans << endl;  
  return 0;
}
0