Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <stdexcept>
- using namespace std;
- template <typename T> //queue type to store group
- class queue
- {
- private:
- struct Node {
- T value;
- Node *next;
- Node(T _value) : value(_value), next(NULL) {}
- };
- Node *front;
- Node *back;
- public:
- queue() : front(NULL), back(NULL) {}
- ~queue() {
- while (front != NULL)
- dequeue();
- }
- void enqueue(T _value) {
- Node *newNode = new Node(_value);
- if (front == NULL)
- front = newNode;
- else
- back->next = newNode;
- back = newNode;
- }
- T dequeue() {
- if (front == NULL)
- throw std::underflow_error("Nothing to dequeue");
- Node *temp = front;
- T result = front->value;
- front = front->next;
- delete temp;
- return result;
- }
- T Front() {
- return front->value;
- }
- T Back() {
- return back->value;
- }
- };
- int main()
- {
- long long nbPlaces;
- long long nbTours;
- int nbGroupes;
- cin >> nbPlaces >> nbTours >> nbGroupes; cin.ignore();
- int groupes [nbGroupes];
- for (int i = 0; i < nbGroupes; i++)
- {
- cin >> groupes[i] ; cin.ignore();
- }
- int gains [nbGroupes];
- int groupeSuivant [nbGroupes];
- for (int i = 0; i < nbGroupes; i++)
- {
- int currentIndex = i;
- gains[i] = 0;
- while (true)
- {
- int nextGp = groupes[currentIndex];
- if (gains[i] + nextGp > nbPlaces)
- {
- break;
- }
- gains[i] += nextGp;
- currentIndex++;
- if (currentIndex == nbGroupes)
- {
- currentIndex = 0;
- }
- if (currentIndex == i)
- {
- break;
- }
- }
- groupeSuivant[i] = currentIndex;
- }
- long long total = 0;
- int currentIndex = 0;
- for (int i = 0; i < nbTours; i++)
- {
- total += gains[currentIndex];
- currentIndex = groupeSuivant[currentIndex];
- }
- std::wcout << total << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment