Defensive missile algorithm, missile Algorithm

Source: Internet
Author: User

Defensive missile algorithm, missile Algorithm

The time space efficiency of algorithm efficiency is not considered at all (laruence does not spray), but it may be the most intuitive and idiotic idea. Without using most of the greedy algorithm ideas on the Internet, it is to ensure that anyone who has not read the algorithm-related books can understand it.

 

Source:

Http://www.programfan.com/acm/show.asp? Qid = 5

 

The questions are as follows:

Defensive missile

Problem

A country develops a missile interception system to defend against missile attacks by the enemy.

However, this missile interception system has a defect: although its first shell can reach any height, it cannot be higher than the previous one in the future.

One day, the radar captured the enemy's missiles. Because the system is still in use, there is only one system, so it may not be able to intercept all missiles.

Input

A maximum of 20 integers indicate the heights of missiles flying in sequence (the radar gives a positive integer whose height is not greater than 30000)

Output

Two integers M and N. The system can intercept up to M missiles. To intercept all missiles, it must have at least N such missile systems.

Sample Input

300 250 275 252 200 138 245

Sample Output

5 2

 

To see the problem, first draw a mathematical model from the actual problem:

1. M this problem is to solve the longest descending sequence of a random sequence 2. N is to select the longest descending sequence from the sequence each time until all elements in the original sequence are selected.

For example:

300 250 275 252 200 138 245

Select the Maximum descending sequence each time. Select 300,275,252,200,138 for the first time and 250,245 for the second time. Now all elements are selected, so M is 5 and N is 2. Suppose there are two more elements, 700,100 will be added to the first selected sequence, 100 is independent into a sequence, then M is 6, N is 3

 

Thoughts:

For the target sequence S, it is divided into two segments, where S1 is already traversed, and S2 is not traversed.

Use a certain data structure to store all possible descending sequence segments in S1, add the first element in S2 to S1 each time, and modify all the descending sequence segments in S1, ensure that the new descending sequence segment is the new descending sequence segment of S1.

With this idea, we can think of using the AOV Network (topological sorting) to store all the descending sequence segments. Every time a new element is obtained from S2 and added to S1, it is equivalent to traversing all nodes in the tree, if the value of the stored node is greater than or equal to the value of the new node, add the copy of the new node to the subsequence of the node.

The longest sequence M is the maximum depth of the AOV network, and the number of system N required is the total number of topology sequences. Each time all nodes in the longest path are removed, all nodes can be removed N times. In other words, after each Topology Sorting, you can get the most nodes and then remove them all. Then, the new graph is sorted by topology to get the longest sequence and then removed. This repeats N times in a round robin manner.

 

Language conversion code:

Build a Data Structure: After analysis, we need to build an AOV network. recursion can simplify the code, because the generated AOV network is a directed acyclic graph, so every time we traverse all nodes, if the current node to be traversed is greater than the new node, a path from the original node to the new node is generated. For the convenience of coding, we can introduce the header node. the maximum value of the header node is its maximum height. This ensures that there are headnodes to the path of all remaining nodes, which is much more convenient for subsequent encoding.

Find the longest sequence length M: Based on the code for finding the height of the tree, we can easily get the code for directed acyclic graph and use recursion.

N: this is not easy, but by M, we can get the height of each node (the height of the node is defined: if the minimum terminal node is the node farthest from the root node, the node height is the distance from the node to the lowest terminal node ). At the same time, an access bit is set to indicate that the node has been accessed, and because we have set a header node, there is access strength to all nodes, as a result, we will not omit any node.


// Code analysis: To ensure the versatility of the Code (not limited to the int type), generic programming is used. // Use a smart pointer to ensure convenient memory management. // Select set instead of other containers, this is because set ensures the uniqueness of insertion. # include <memory> # include <algorithm> # include <iostream> # include <set> # define MAXALTITUDE 30000 // missile height # define GETARRAYSIZE (a) (sizeof (A)/sizeof (* A) // obtain the number of elements in the array. static int count = 0; // core statement count template <class T> class DescendAov {private: bool isVisited; // whether the T value has been accessed; // store the value of the current node std :: set <std: shared_ptr <DescendAov> minSet; // stores a set of int maxAltitude values smaller than the value of this node; // stores the maximum pu height of each node. Blic: DescendAov (const T v): value (v), isVisited (false), maxAltitude (0) {}; // DescendAov (T * sequence, int length): DescendAov (MAXALTITUDE) {// create an AOV network for (int I = 0; I <length; ++ I) {// create the target data structure Insert (sequence [I]) ;}}; int GetTheLengthOfTheLongestDescendSequence (int I = 0) {// traverse the AOV network to obtain the height of the AOV network, is the required Maximum descending sequence length int high = I; for (auto itr: minSet) {high = std: max (itr-> GetTheLengthOfTheLongestDescendS Equence (I + 1), high);} maxAltitude = high; return high;} int GetLongestTopoSequenceNumber () {// get the maximum number of topology 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 value of the node value is greater than or equal to the value of the newly added node d-> value, add it to a smaller set than the value of the new node. this! = D. get () is used to prevent repeated insertion of 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 () {// locate the highest and unaccessed node connected to the node std :: shared_ptr <DescendAov <T> Highest = NULL; for (auto itr: minSet) {if (! Itr-> isVisited) {// if (! Highest | Highest-> maxAltitude <= itr-> maxAltitude) // if the current node is empty or the current node is higher, Highest = itr ;}} if (! Highest) // There are two scenarios where the appropriate node cannot be found: 1. all nodes are ignored. 2. the current node is the last node. return false; Highest-> isVisited = true; Highest-> FindTheHighestNode (); return true ;}; int _ tmain (int argc, _ TCHAR * argv []) {int missile [] ={ 300,250,275,252,200,138,245,700,100}; DescendAov <int> root (missile, GETARRAYSIZE (missile); std :: cout <"Maximum number of Missiles intercepted:" <root. getTheLengthOfTheLongestDescendSequence () <std: endl; std: cout <"How many systems are required:" <root. getLongestTopoSequenceNumber () <std: endl; std: cout <"number of core code loops:" <count <std: endl; return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.