# Include <stdio. h>
// Sort Definition
# Include <algorithm>
// Vector Definition
# Include <vector>
// Deque Definition
# Include <deque>
// Similarly, introduce the STD namespace
Using namespace STD;
// Save with an Array
Int friends [1000];
// Save with Vector
Vector <int> vtfriends;
// Save with deque
Deque <int> dqfriends;
Int main ()
...{
Int T;
Scanf ("% d", & T );
Int icase;
For (icase = 1; icase <= T; icase ++)
...{
// Enter the vector and deque fields each time.
Vtfriends. Clear ();
Dqfriends. Clear ();
Printf ("Scenario # % d:", icase );
Int N, F, TN;
Scanf ("% d", & N, & F );
// Because we need to find three times later, we need to save the N value first.
Tn = N;
Int I;
For (I = 0; I <F; I ++)
...{
// Save it to an array
Scanf ("% d", & Friends [I]);
// Save to Vector
Vtfriends. push_back (Friends [I]);
// Save it to deque
Dqfriends. push_back (Friends [I]);
}
// Sort. If the third parameter is not specified, the default value is in ascending order.
// Sort implements fast sorting, Which is unstable sorting,
// Stable_sort is used for stable sorting. The usage is exactly the same as that of sort.
// Sort the Array
Sort (friends, friends + F );
// Sort the Vector
Sort (vtfriends. Begin (), vtfriends. End ());
// Sort deque
Sort (dqfriends. Begin (), dqfriends. End ());
// Search in the array
For (I = f-1; I> = 0; I --)
...{
N-= friends [I];
If (n <= 0)
Break;
}
// Search in Vector
// Restore n first
N = tn;
For (I = f-1; I> = 0; I --)
...{
N-= vtfriends. at (I );
If (n <= 0)
Break;
}
// Search in deque
// Restore n first
N = tn;
For (I = f-1; I> = 0; I --)
...{
// Deque's at method is less efficient than that of vector,
// Because deque elements are not completely continuous in the memory
N-= dqfriends. at (I );
If (n <= 0)
Break;
}
If (I <0)
Puts ("impossible ");
Else
Printf ("% d", f-I );
Puts ("");
}
Return 0;
}
I used a school written by a cool, here we introduce the usage of vector, deque and sort, these are STL content, give you a website, http://www.stlchina.org /;
However, it seems that we do not advocate the use of STL in the program from the very beginning. Those algorithms are the best to write on their own and will be further understood when they are learned!