Flying to the Mars
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 16670 Accepted Submission (s): 5392
Problem Description
The year 8888, the Earth was 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 has to remind your that there isn ' t any road to the Mars from here! " PPF replies. "Fly!" another answers. PPF smiles: "Clever guy! Although we haven ' t got wings, I can buy some magic broomsticks from HARRY POTTER to help you. " Now, it's time to learn-fly on a broomstick! We assume that one soldier have one level number indicating his degree. The soldier who have a higher level could teach the lower, that's to say the former's level > the latter ' s. But the lower can ' t teach the higher. One soldier can has only one teacher at the most, certainly, has no teacher is also legal. Similarly one soldier can has only one sTudent at the most and have no student is also possible. Teacher can teach his student on the same broomstick. Certainly, all the soldier must has 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 is 5 soldiers (A B C D E) with level Numbers:2 4 5 6 4;
One method:
C could teach B; B could teach A; So, A B C is eligible to study on the same broomstick.
D could teach E; So D E is eligible to study on the same broomstick;
Using This method, we need 2 broomsticks.
Another method:
D could teach A; So A D is eligible to study on the same broomstick.
C could teach B; So B C is eligible to study on the same broomstick.
E with no teacher or student is eligible to study on one broomstick.
Using the method, we need 3 broomsticks.
......
After checking the-possible method, we found that 2 are the minimum number of broomsticks needed.
Input input 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 an integer on each line and indicating the level number for each soldier. (less than digits);
Output for each case, output the minimum number of broomsticks on a.
Sample Input
4 10 20 30 04 5 2 3 4 3 4
Sample Output
1 2 Direct greed, find one column at a time until you find it. Code:
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <cstdio>
using namespace std;
int cmp (int x,int y) {
return x>y;
}
int main () {
int n;
int a[3005],v[3005];
while (~SCANF ("%d", &n)) {for
(int i=0;i<n;i++) {
scanf ("%d", &a[i]);
v[i]=0;
}
Sort (a,a+n,cmp);
int count=0,k;
for (int i=0;i<n;i++) {
if (v[i]) continue;
V[i]=1;
count++;k=i;
for (int j=i+1;j<n;j++) {
if (!v[j]&&a[k]>a[j]) {
v[j]=1;k=j;
}
}
}
printf ("%d\n", count);
}
}
Then a after I found that the speed is not fast, I Baidu a bit to find ridiculous crazy code than I more simple, but not very efficient.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main ()
{
int n,i,max,t;
int a[3005];
while (scanf ("%d", &n)!=eof)
{
for (i=0;i<n;++i)
scanf ("%d", &a[i]);
Sort (a,a+n);
Max=1;
T=1;
for (I=1;i<n;++i)
{
if (a[i]!=a[i-1])
t=1;
else
++t;
if (T>max)
max=t;
}
printf ("%d\n", max);
}
return 0;
}