Hangzhou electric 1800 flying to the Mars (Greedy)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1800

Flying to the Mars Time Limit: 5000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 10798 accepted submission (s): 3461


Problem description
In the year 8888, the Earth is ruled by the PPF empire. as the population growing, PPF needs to find more land for the newborns. finally, PPF decides to attack kscinow who ruling the Mars. here the problem comes! How can the soldiers reach the Mars? PPF convokes his soldiers and asks for their suggestions. "rush... "One soldier answers." Shut up! Do I have to remind you that there isn't any road to the Mars from here !" PPF replies. "fly !" Another answers. PPF smiles: "clever guy! Although we haven'tgot wings, I can buy some magic broomsticks from Harry Potter to help you. "Now, it's time to learn to fly on a broomstick! We assume that one soldier has one level number indicating his degree. the soldier who has a higher level cocould teach the lower, that is to say the former's level> the latter's. but the lower can't teach the higher. one soldier can have only one teacher at most, certainly, having no teacher is also legal. similarly one soldier can have only one student at most while having no student is also Possible. Teacher can teach his student on the same broomstick. Certainly, all the soldier must have practiced on the broomstick before they fly to the Mars! Magic broomstick is expensive! So, can you help PPF to calculate the minimum number of the broomstick needed.
For example:
There are 5 soldiers (a B c d e) with level numbers: 2 4 5 6 4;
One method:
C cocould teach B; B cocould teach a; so, a B C are eligible to study on the same broomstick.
D cocould teach E; so d e are eligible to study on the same broomstick;
Using this method, we need 2 broomsticks.
Another method:
D coshould teach a; so a D are eligible to study on the same broomstick.
C cocould teach B; so B C are eligible to study on the same broomstick.
E with no teacher or student are eligible to Study on One Broomstick.
Using the method, we need 3 broomsticks.
......

After checking up all possible method, we found that 2 is the minimum number of broomsticks needed.
Inputinput file contains multiple test cases.
In a test case, the first line contains a single positive number N indicating the number of soldiers. (0 <= n <= 3000)
Next n lines: there is only one nonnegative integer on each line, indicating the level number for each soldier. (less than 30 digits );
 
Outputfor each case, output the minimum number of broomsticks on a single line.
Sample Input
410203004523434
 
Sample output
12
  According to the requirements of the question, you can convert it into the number of occurrences that appear the most is the number of occurrences. Originally, I used arrays to mark and count the number of occurrences. Two loops are required. The data in this question is quite big, this will time out, and the following code will not time out. AC code:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int a[3002];bool cmp(int a,int b){    return a>b;}int main(){    int n,i,j,tot,k;    while(~scanf("%d",&n))    {        k=1;        tot=0;        for(i=0;i<n;i++)            scanf("%d",&a[i]);        sort(a,a+n,cmp);        for(i=1;i<n;i++)        {            if(a[i]==a[i-1])                k++;            else            {                if(tot<k)                {                    tot=k;                }              k=1;            }        }        if(k>tot)            tot=k;        printf("%d\n",tot);    }    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.