Defensive missile algorithm

Source: Internet
Author: User
Tags radar

Algorithm efficiency is absolutely the worst (Big Bird without spray), time and space efficiency completely without consideration, but may be the most intuitive, the most idiotic idea. The idea of not using the XXX algorithm is to ensure that anyone who has not read the algorithm-related books can read. Title Source: http://www.programfan.com/acm/show.asp?qid=5
The title is as follows: Defensive missiles problem a missile interception system 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 is a maximum of 20 integers that indicate the height of the missile in turn (the radar gives a positive integer with a height of no more than 30000) and 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 Input300 275 252 138 245Sample OUTPUT5 2
First of all, the most intuitive thinking is as follows: Problem translation: This problem is to solve a random sequence of the longest descending sequence thinking: For the target sequence s, divided into two segments, where S1 is already traversed, 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 depth of the AOV network, and how many systems n is required is the number of end nodes of the AOV network.
Source:
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. #include <vector> #include <memory> #include <algorithm> #include <iostream> #define Maxaltitude 30000//Missile Maximum Height # define getarraysize (a) (sizeof (a)/sizeof (*a))//Get the number of elements in the array static int count = 0;// The core statement count Template<class T>class Descendaov{private:bool isvisit;//Whether the value of the current node has been accessed by the T value;//STD::VECTOR&LT;STD:: shared_ptr<descendaov> > minset;//stores a collection void Terminalcount (int &num) {///End node count if (Isvisit) return, which is smaller than the node value; Isvisit = True;if (Minset.empty ()) ++num;for (auto itr:minset) itr->terminalcount (num);} 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 added node value D->value, then a collection smaller than his is added, This!=d.get () is to prevent itself from being repeatedly inserted into itself. for (auto Itr:minset) Itr->insert (d); Minset.push_back (d);}} void Insert (const T &t) {Insert (std::shared_ptr<descendaov<t>> (New descendaov<t> (T)));} Public:descendaov (const T v): Value (v), IsVIsit (False) {};//is built by value AoV net Descendaov (t* sequence, int length):D Escendaov (maxaltitude) {//AoV net for (int i = 0; I < le Ngth; ++i) {//Create Target data structure insert (Sequence[i]);}}; int getthelengthofthelongestdescendsequence (int i = 0) {//Traverse AOV net to get the height of the AoV net, which is the maximum descending sequence length required int high = i;for (Auto itr:mins ET) high = Std::max (Itr->getthelengthofthelongestdescendsequence (i + 1), high); int Gettheterminalnumber () {//Statistics terminal node number int num = 0; Terminalcount (num); return num;}}; int _tmain (int argc, _tchar* argv[]) {int missile[] = {300,250,275,252,200,138,245};//element number not more than 7, otherwise there will be a horrible thing, and then give an explanation, Why is it scary descendaov<int> root (missile, getarraysize (missile)); Std::cout << "Maximum number of missiles intercepted:" << root. Getthelengthofthelongestdescendsequence () << std::endl;std::cout << "How many sets of systems are needed:" << root. Gettheterminalnumber () << std::endl;std::cout << "core code cycles:" << count << Std::endl;return 0;}


Problem Analysis:

This algorithm has a fatal problem:
Why can't array elements exceed 7? We can assume that the number of array elements is n, then the expansion speed of the tree will be N*N*N.....N, that is, the n^n side. 7 of the 7 is 823543, to 8^8 will be an astronomical.

Defensive missile algorithm

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.