結果
| 問題 | No.198 キャンディー・ボックス2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-06-16 11:52:32 |
| 言語 | C++11(old_compat) (gcc 12.4.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,000 ms |
| コード長 | 1,040 bytes |
| 記録 | |
| コンパイル時間 | 1,149 ms |
| コンパイル使用メモリ | 173,244 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2026-03-08 16:02:45 |
| 合計ジャッジ時間 | 2,216 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
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);
| ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:24:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
24 | scanf("%lld", &c[i]);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#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;
}