Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <valarray>
- #include <vector>
- #include <list>
- using namespace std;
- int main()
- {
- using namespace std;
- typedef list<vector<valarray<int> > > Matrix;
- enum : Matrix::size_type { Allocation = 0, Max = 1, Need = 1 };
- Matrix mat {
- // Allocation , Max / Need
- { { 0, 0, 1, 4 }, { 0, 6, 5, 6 } },
- { { 1, 4, 3, 2 }, { 1, 9, 4, 2 } },
- { { 1, 3, 5, 4 }, { 1, 3, 5, 6 } },
- { { 1, 0, 0, 0 }, { 1, 7, 5, 9 } }
- };
- valarray<int> Available { 1, 5, 2, 0 };
- for_each( mat.begin(), mat.end(), [] (Matrix::reference val) { val[Need] = val[Max] - val[Allocation]; } );
- Matrix::iterator it;
- while ( ( it = find_if( mat.begin(), mat.end(),
- [&Available] (Matrix::const_reference val) { return (val[Need] <= Available).min() == true; } ) ) != mat.end() )
- Available += (*it)[Allocation], mat.erase(it), it = mat.begin();
- cout << (mat.empty() ? "Safe!" : "Unsafe!") << endl;
- }
Add Comment
Please, Sign In to add comment