The efficiency of the algorithm in the time space efficiency is not considered at all (Big Bird without spray), but may be the most intuitive, the most idiotic idea. The idea of not using most of the greedy algorithms on the Internet is to ensure that anyone who has not read the algorithm books can read them.
Title Source:
Http://www.programfan.com/acm/show.asp?qid=5
The topics are as follows:
Defensive missiles
Problem
A missile interception system has been developed by a country to defend against enemy missile attacks.
But the missile interception system has a flaw: although its first artillery shells can reach any height, each projectile cannot be higher than the previous one.
One day, the radar caught the enemy's missiles to attack. Since the system is still in use, there is only one set of systems that may not be able to intercept all missiles.
Input
Up to 20 integers representing the height of the missile in turn (the radar gives the height data is a positive integer not greater than 30000)
Output
Two integers m and N. said: The system can intercept a maximum of M missiles, if you want to intercept all missiles should be equipped with at least N sets of such missile system.
Sample Input
300 250 275 252 200 138 245
Sample Output
5 2
To see the problem, we should first draw a mathematical model from the actual problem--the problem translation:
1. M-This problem is to solve a random sequence of the longest descending sequence 2. N is the selection of the longest descending sequence from the sequence until all the elements in the original sequence are selected, and the number of descending sequences is the desired n
For example:
300 250 275 252 200 138 245
Select the maximum descending sequence each time, choose 300,275,252,200,138 for the first time, select 250,245, and now all the elements are selected, so M is 5,n 2. Suppose there are 700,100 two elements, 100 will be added to the first selected sequence, 700 become a sequence independently, then M is 6,n 3
Thinking:
For the target sequence s, it is divided into two segments, where S1 is already traversed and S2 is not traversed.
A data structure is used to store all possible descending sequence segments in the S1, then each time the first element in the S2 is added to the S1, and the S1 all descending sequence segments are modified, the newly generated descending sequence segment is guaranteed to be the new descending sequence segment of S1.
Through this idea, can think of using AOV network (topological sort) to store all the descending sequence segments, each time from S2 to get a new element added into the S1 is equivalent to traversing all nodes of the tree, if the value of the stored node is greater than the value of the new node, the new node is added to the sub-sequence of the node.
The longest sequence of M is the maximum depth of the AOV network, and the desired number of systems n is all topological sequences, each removing the longest path of all nodes, n times can be eliminated all nodes. In other words, each topology is sorted to get the most nodes after they are all removed, the resulting new map is sorted by topology, the longest sequence is obtained, and then removed, so the cyclic n times.
Language Conversion Code:
Build data structure: Analysis to We need to establish a AOV network, the use of recursion can be very good to simplify the code, because the generated AoV network is a direction-free graph, so each time traversing all nodes, if the current node traversed to the new node is greater than the original node to generate a path to the new node. For coding convenience, we can introduce the head node, the maximum value of the head node is its maximum height, to ensure that there is a head node to all the remaining nodes of the path, for the subsequent encoding can be much more convenient.
Find the longest sequence length m: According to the Code of tree height, we can easily get the code of the direction-free graph, and use recursion to complete.
N: This is not an easy thing to do, but by asking for M, we can get the height of each node (the height of the node is defined as the node with the lowest endpoint being the farthest from the root node, the node height is the distance from the lowest end node). At the same time, set an access bit, with the access to represent the removal of the node, but also because we have set up a head node, has access to all nodes of the road, which leads us not to miss any one node.
Code Analysis: Generic programming is used to ensure the versatility of the code (not just the int type). To ensure the convenience of memory management, use smart pointers. Set is chosen instead of other containers because the set guarantees the uniqueness of the insertion # include <memory> #include <algorithm> #include <iostream> #in Clude <set> #define Maxaltitude 30000//missiles highest height # define getarraysize (a) (sizeof (a)/sizeof (*a))//Get the number of elements in the array static int count = 0;//Core statement count Template<class T>class Descendaov{private:bool isvisited; Whether or not to access the value of the current node stored in T value;//std::set<std::shared_ptr<descendaov> > minset;//stores a collection that is smaller than the value of the node int maxaltitude; Stores the maximum height of each node Public:descendaov (const T v): Value (v), isvisited (false), Maxaltitude (0) {};//by value AoV net Descendaov (t* sequence, int length):D Escendaov (maxaltitude) {//by sequence AOV net for (int i = 0; i < length; ++i) {//Create Target data structure insert (sequence[i ]);} };int getthelengthofthelongestdescendsequence (int i = 0) {//traverse the AOV net, get the height of the AoV net, is the maximum descending sequence length required int high = i;for (Auto Itr:mi Nset) {high = Std::max (Itr->getthelengthofthelongestdescendsequence (i + 1), high);} Maxaltitude = High;return High;} int GetloNgesttoposequencenumber () {//Gets the number of longest topological sequences int num = 0; Getthelengthofthelongestdescendsequence ();//Ensure that the height of each node has been obtained for (unsigned int i = 0; i < minset.size (); ++i) {if ( Findthehighestnode ()) Num++;elsebreak;} return num;} Private:void Insert (const std::shared_ptr<descendaov> D) {//Insert successor ++count;if (d->value <= value && This! = D.get ()) {//If the node value is greater than or equal to the newly joined node value D->value, then the collection is added to a smaller set than his, This!=d.get () is to prevent itself from being repeatedly inserted itself. for (auto Itr:minset) {Itr->insert (d);} Minset.insert (d);}} void Insert (const T &t) {Auto ptr = std::shared_ptr<descendaov<t>> (new descendaov<t> (T)); Insert ( PTR);} BOOL Findthehighestnode () {//finds the highest and not accessed node connected to the node std::shared_ptr<descendaov<t>> highest = Null;for ( Auto Itr:minset) {if (!itr->isvisited) {///has not been accessed if (! Highest | | Highest->maxaltitude <= itr->maxaltitude)//If the current node is empty or the current node is higher highest = ITR;}} if (! Highest)//can not find the appropriate node in two cases: 1. All nodes are asked; 2. The current node is the last node and return false; Highest->isvisited = true; Highest->findthehighestnode (); return true;}}; int _tmain (int argc, _tchar* argv[]) {int missile[] = {, +, 275, 252, $, 138, 245,700,100};D escendaov<int> ro OT (missile, getarraysize (missile)); Std::cout << "Maximum number of missiles intercepted:" << root. Getthelengthofthelongestdescendsequence () << std::endl;std::cout << "How many sets of systems are needed:" << root. Getlongesttoposequencenumber () << std::endl;std::cout << "core code cycles:" << count << Std::endl; return 0;}
Defensive missile algorithm