結果
| 問題 | 
                            No.198 キャンディー・ボックス2
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2015-06-16 11:52:32 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                CE
                                 
                             
                            
                            (最新)
                                AC
                                 
                             
                            (最初)
                            
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,040 bytes | 
| コンパイル時間 | 337 ms | 
| コンパイル使用メモリ | 51,660 KB | 
| 最終ジャッジ日時 | 2024-11-14 19:04:10 | 
| 合計ジャッジ時間 | 870 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
            
            
            
            
            ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
main.cpp:9:1: error: ‘vector’ does not name a type
    9 | vector<i64> c;
      | ^~~~~~
main.cpp: In function ‘i64 calc(i64)’:
main.cpp:15:42: error: ‘c’ was not declared in this scope
   15 |   for(int i : range(n)) { res += abs(p - c[i]); }
      |                                          ^
main.cpp: In function ‘int main()’:
main.cpp:21:3: error: ‘c’ was not declared in this scope
   21 |   c.assign(n, 0);
      |   ^
main.cpp:20:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |   i64 b; scanf("%lld%d", &b, &n);
      |          ~~~~~^~~~~~~~~~~~~~~~~~
            
            ソースコード
#include <iostream>
#include <algorithm>
using namespace std;
using i64 = long long;
class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n):i({0}),n({n}){}range(int i,int n):i({i}),n({n}){}I& begin(){return i;}I& end(){return n;}};
vector<i64> c;
int n;
const i64 inf = 987654321987654321;
i64 calc(i64 p) {
  i64 res = 0;
  for(int i : range(n)) { res += abs(p - c[i]); }
  return res;
}
int main(void) {
  i64 b; scanf("%lld%d", &b, &n);
  c.assign(n, 0);
  i64 summa = b;
  for(int i : range(n)) {
    scanf("%lld", &c[i]);
    summa += c[i];
  }
  sort(c.begin(), c.end());
  i64 lo = 0, hi = summa / n;
  while(hi - lo > 10) {
    i64 m1 = (lo + lo + hi) / 3,
        m2 = (lo + hi + hi) / 3;
    i64 r1 = calc(m1),
        r2 = calc(m2);
    if(r1 > r2) {
      lo = m1;
    } else {
      hi = m2;
    }
  }
  i64 res = inf;
  for(i64 x : range(lo, hi+1)) {
    res = min(res, calc(x));
  }
  printf("%lld\n", res);
  return 0;
}