結果

問題 No.198 キャンディー・ボックス2
ユーザー ToshokahnToshokahn
提出日時 2019-11-06 15:52:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,575 bytes
コンパイル時間 1,206 ms
コンパイル使用メモリ 146,940 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 02:37:13
合計ジャッジ時間 3,053 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 1 ms
4,352 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:89:19: warning: ‘ans’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if (num + B < x * N)
                 ~~^~~
main.cpp:65:5: note: ‘ans’ was declared here
   T ans;
     ^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = a; i <= (b); ++i)
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;

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

template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/*
  凸関数fに対し
  argmax/min f
  を返す。

  ※凸関数でないとダメ
 */

template <class T>
T find_max(T l, T r, T f(T)) {
  T cl, cr;
  while (r - l > 3) {
    cl = (l * 2 + r) / 3, cr = (l + r * 2) / 3;
    if (f(cl) < f(cr))
      l = cl;
    else
      r = cr;
  }

  T ans;
  T fmax = numeric_limits<T>::min();
  repp(i, l, r) {
    if (chmax(fmax, f(i)))
      ans = i;
  }
  return ans;
}

template <class T>
T find_min(T l, T r, T f(T)) {
  T cl, cr;
  while (r - l > 3) {
    cl = (l * 2 + r) / 3, cr = (l + r * 2) / 3;
    if (f(cl) > f(cr))
      l = cl;
    else
      r = cr;
  }

  T ans;
  T fmin = numeric_limits<T>::max();
  repp(i, l, r) {
    if (chmin(fmin, f(i)))
      ans = i;
  }
  return ans;
}

/*
https://yukicoder.me/problems/no/198
*/

vector<ll> C;
ll B, N;

ll f(ll x) {
  ll cnt = 0;
  ll num = 0;
  rep(i, N) {
    cnt += abs(C[i] - x);
    num += C[i];
  }

  if (num + B < x * N)
    cnt = LINF;
  return cnt;
}

int main() {
  cin >> B >> N;
  C = vector<ll>(N);
  rep(i, N) cin >> C[i];

  ll x = find_min(0LL, (ll)INF, f);
  cout << f(x) << endl;
}
0