Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <iomanip>
- using namespace std;
- size_t getBalons(const size_t& length, const size_t& amount)
- {
- size_t result = 0;
- result = length / amount;
- if (result == 0)
- {
- return 1;
- }
- if (amount * result < length)
- {
- result += 1;
- }
- return result;
- }
- using elements_t = vector<size_t>;
- elements_t combo;
- vector<elements_t> combos;
- void calculateCombinations(const elements_t& numbers, size_t size)
- {
- for (size_t i = 0; i < numbers.size(); i++)
- {
- combo.push_back(numbers[i]);
- if (size <= 1)
- {
- combos.push_back(combo);
- combo.erase(combo.end() - 1);
- }
- else
- {
- calculateCombinations(numbers, size - 1);
- combo.erase(combo.end() - 1);
- }
- }
- }
- int main()
- {
- elements_t a;
- size_t N, M, K;
- cin >> N >> M >> K;
- a.resize(N);
- for (size_t i = 0; i < N; i++)
- {
- cin >> a[i];
- }
- const elements_t variants = {0, 1};
- const size_t size = N - 1;
- elements_t line;
- // берём отрезок
- size_t startBalons = 0;
- // берём элемент отрезка
- for (auto& jt : a)
- {
- startBalons += getBalons(jt, K);
- }
- cout << "start balons = " << startBalons << endl;
- size_t* minBalons = nullptr;
- vector<elements_t> minSplit;
- calculateCombinations(variants, size);
- for (size_t i = 0; i < combos.size(); i++)
- {
- vector<elements_t> current_a_split(N);
- /// 00000001
- size_t id = 0;
- current_a_split[id].push_back(a[0]);
- for (size_t j = 0; j < combos[i].size(); j++)
- {
- if (combos[i][j] == 0)
- {
- id++;
- }
- current_a_split[id].push_back(a[j + 1]);
- }
- size_t balons = 0;
- // берём отрезок
- bool isFit = true;
- for (auto& it : current_a_split)
- {
- if (it.size() > M)
- {
- isFit = false;
- break;
- }
- if (!it.empty())
- {
- size_t split_length = 0;
- // берём элемент отрезка
- for (auto& jt : it)
- {
- split_length += jt;
- }
- balons += getBalons(split_length, K);
- }
- }
- if (isFit)
- {
- if (minBalons == nullptr)
- {
- minBalons = new size_t(balons);
- minSplit = current_a_split;
- }
- else
- {
- if (*minBalons > balons)
- {
- *minBalons = balons;
- minSplit = current_a_split;
- }
- }
- }
- }
- size_t P = 1;
- vector<pair<size_t, size_t>> pIds;
- for (auto it = minSplit.begin(); it != minSplit.end(); ++it)
- {
- if (it->size() > 1)
- {
- pIds.push_back(pair<size_t, size_t>(P, it->size()));
- P += it->size();
- }
- }
- cout << endl << "Min split has parameters:" << endl;
- cout << "elements = ";
- for (auto& it : minSplit)
- {
- // берём элемент отрезка
- for (auto& jt : it)
- {
- cout << setw(2) << jt;
- }
- cout << "-";
- }
- cout << endl << startBalons - *minBalons << endl << pIds.size() << endl;
- for (size_t i = 0; i < pIds.size(); i++)
- {
- cout << pIds[i].first << " " << pIds[i].second << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement