[Exercise 04] The dictionary tree 1002 flying to the Mars

Source: Internet
Author: User

Question:

In 8888, the Earth was ruled by the PPF kingdom. Due to population growth, PPF needs to find more land for newborns. Finally, PPF decided to notify Mars of kscinow attacks. The question is, how can we send soldiers to Mars? PPF convenes soldiers for advice. Finally, I decided to buy some magic brooms from Harry Potter to let the soldiers fly ~ Now those soldiers are learning to use the magic broom. We assume that each warrior has a level to indicate his level. High-level fighters can guide low-level fighters, but they cannot. A warrior can have at most one mentor or no mentor. Similarly, a warrior can guide at most one learner or no one. Instructors and learners can share the same broom. All fighters must be ready before they fly to Mars. A broom is very expensive. How can we minimize the quantity required?

For example, there are five fighters A, B, C, D, and E. Their levels are 2, 4, 5, 6, and 4;

Method 1:

C teach B, bteach A, so a, B, c can use a broom;

D teach E, so D and D can use a broom;

This requires two;

Method 2:

D teach a, so A and D use one;

C teach B, so B and c use one;

E;

In this way, 3 are required;

...

Finally, we found that at least two of all possible methods are required;

Input: multiple groups of Test Cases

Row 1: positive integer N (0-3000) indicates the number of fighters;

Next n rows, each row has a non-negative integer, indicating the level of the Warrior (no more than 30 digits)

Output: for each group of test cases, the minimum number of broom is output;

=

1. A maximum of several identical numbers can be entered.

2. If the warrior level is no more than 18 bits, you can use _ int64 or long to solve the problem. However, the question requires 30 bits and can only be solved using strings;

3. less than 3000 soldiers, that is, a maximum of 3000 soldiers

AlgorithmIdea: map.

The key to this question is to make statistics on the maximum values of the same level. Others use trie and use the Val array to save the occurrence frequency of the same "word" (string, trie is nothing more than overcoming the limit of (30 digits). It seems that it does not play a major role. The reason for using map is that it can use strings as key values.

However, some people directly use int for sorting and statistics. It can be seen that the data is very watery, and 30digits is used to scare people.

Tip:

1. Remove the leading 0 when using map and trie;

2. Map times out if the find function is used.

 

// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # include <string. h> # include <queue> # define SZ (x) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t;} template <CL Ass T> string tostring (t x) {ostringstream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} // end of the template (General Part) # define ifs cinmap <string, int> m; Map <string, int> :: iterator it; // [exercise 04] dictionary tree 1001 hat's wordsint main () {// ifstream ifs ("shuju.txt", IOS: In); int N; while (IFS> N) {ifs. ignore (35, '\ n'); M. clear (); For (INT I = 0; I <n; I ++) {string a; Getline (IFS, A, '\ n'); int K = 0; while (A [k ++] = '0'); A =. substr (k-1,. length ()-(k-1); m [a] ++;} int Max; For (IT = m. begin (), max = it-> Second; it! = M. end (); It ++) {If (max <it-> second) {max = it-> second ;}} cout <max <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.