結果
| 問題 | No.198 キャンディー・ボックス2 |
| コンテスト | |
| ユーザー |
ramia777
|
| 提出日時 | 2022-06-07 11:47:54 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,000 ms |
| コード長 | 2,780 bytes |
| コンパイル時間 | 648 ms |
| コンパイル使用メモリ | 97,184 KB |
| 実行使用メモリ | 7,840 KB |
| 最終ジャッジ日時 | 2025-11-26 11:46:22 |
| 合計ジャッジ時間 | 1,629 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
ソースコード
//Yukicoder
//Q198 candy box
//
//二次元可変配列
//vector <vector <int>> mass;
//vector <vector <int>> memo;
//vector <int> memo;
//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <list>//list
#include <queue> //queue
#include <set> //連想コンテナ(要素が自動でソートされる)、要素1つ(Key)、keyの重複ない、O(logN)
#include <map> //連想コンテナ(要素が自動でソートされる)、要素2つ(Key,data)、keyの重複ない、dataは重複あり、O(logN)
#include <unordered_set> //hash、O(1)
#include <unordered_map> //hash、O(1)
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <climits>
//#include <bits/stdc++.h>
using namespace std;
#define FOR(x,to) for(x=0;x<to;x++)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define FMAX(a,b) ((a)>(b)?(a):(b))
#define FMIN(a,b) ((a)<(b)?(a):(b))
#define FCEIL(a,b) ((a)+(b)-1)/(b)
typedef unsigned long long ULL;
typedef signed long long SLL;
queue<int> _queue;
vector <vector <SLL>> memo2;
vector<SLL> memo1;
vector<SLL> c;
vector<SLL> K;
vector<SLL> dp;
SLL N, D, H ,A, B;
SLL C[15];
SLL solve(SLL target)
{
SLL ans = 0;
//cout << "_target=" << target << endl;
// キャンディーの数の操作回数を求める
// 1個づつしか操作できないので差分が操作回数になる
for (int i = 0; i < N; i++)
{
//cout << c[i] << ": diff=" << abs(c[i] - target) << endl;
ans += abs(C[i] - target);
}
//cout << "_ans=" << ans << endl;
return ans;
}
int main()
{
cin >> B;
cin >> N;
SLL minimum = LLONG_MAX;
SLL total = 0;
for (int i=0;i<N;i++)
{
cin >> C[i];
if (C[i] < minimum) minimum = C[i];
total += C[i];
}
// 答えは low と high の間にある
SLL low = minimum; //各箱の中に最初から入っているキャンデーの数のなかで最も少ない数
SLL high = (total + B) / N; //各箱に入れることが出来る最大数
//箱の中のキャンディーの数
SLL c1, c2;
while (abs(high-low) > 2)
{
//cout << "low=" << low << ",high=" << high << endl;
c1 = (high + 2 * low) / 3; //箱の中のキャンディーの数の候補1
c2 = (high * 2 + low) / 3; //箱の中のキャンディーの数の候補2
// それに必要な操作回数の比較
if (solve(c1) < solve(c2))
high = c2;
else
low = c1;
}
SLL ans = LLONG_MAX;
//cout << "***low=" << low << ",high=" << high << endl;
//最終的に絞り込んだ範囲の中での最小値が答えになる
for (SLL i=low;i<=high;i++)
{
SLL tmp = solve(i);
if (tmp < ans)
{
ans = tmp;
//cout << "*ans=" << ans << endl;
}
}
cout << ans << endl;
//getchar();
return 0; //end
}
ramia777