1800 Flying to the Mars 大數 最多不上升序列 簡化題意

 Flying to the Mars

          Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                Total Submission(s): 4318    Accepted Submission(s): 1393


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’t got 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 could 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 could teach B; B could teach A; So , A B C are eligible to study on the same broomstick.
D could teach E;So D E are eligible to study on the same broomstick;
Using this method , we need 2 broomsticks.
Another method:
D could teach A; So A D are eligible to study on the same broomstick.
C could 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.
 

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 integer on each line , indicating the level number for each soldier.( less than 30 digits);
 

Output
For each case, output the minimum number of broomsticks on a single line.
 

Sample Input
4
10
20
30
04
5
2
3
4
3
4
 

Sample Output
1
2
#include "stdio.h"

/*
 這題就是一道簡單的最大子序列問題
 或者所是找出所有所有不上升子序列的問題
 但是數很大,會超時,
 以下方法是在數據不大的情況下可以使用的。
 


 */
//繼續往後看,第二份代碼是能夠處理大數的


typedef struct{
    int value;
    int flag//代表是否被選過,1沒選過,0選過
}NUM;


void sort(NUM num[],int n){//從大到小排序
    
    for(int i=0;i<n;i++)
        for(int j=i;j<n;j++){
            if(num[i].value<num[j].value){
                int temp=num[i].value;
                num[i].value=num[j].value;
                num[j].value=temp;
            }
        }
}


int main(){
    
    
    freopen("/Users/qigelaodadehongxiaodi/Desktop/data1.txt", "r", stdin);
    //這個不理,是用來方便輸入輸出的東西,利用文本輸入流來讀取數據
    //提交代碼的時候記得註銷這條語句
    
    int n,max=0;
    NUM num[1000];
    int cnt=0,cntnum=0,total=0;
    
    while(scanf("%d",&n)!=EOF){
        
        for(int i=0;i<n;i++){
            scanf("%d",&num[i].value);
            num[i].flag=1;
        }
        
        sort(num,n);
        
        while(cnt!=n){
            while(max==0){
                if(num[cntnum].flag==1){
                    max=num[cntnum].value;
                    cnt++;
                    break;
                }else{
                    cntnum++;
                }
            }// 如果重新開始,則讓首個沒選過的當max
            
            if(num[cntnum].flag!=0&&max>num[cntnum].value){
                max=num[cntnum].value;
                cnt++;
                if(cnt==n){//注意這個,假如cnt==n,但是不是最後一項,則total不會++,
                    //因此自己定了一個退出機制
                    total++;
                    break;
                }
                cntnum++;
            }else{
                cntnum++;
            }
            if(cntnum==n){
                total++;
                cntnum=0;
                max=0;
            }
            
        }
        
        printf("%d\n",total);
           
    }
    
    return 0;
    
}






________________托馬斯爆炸帥的分割線————————————————————————


#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(){
    
    
    freopen("/Users/qigelaodadehongxiaodi/Desktop/data1.txt", "r", stdin);
    //這個不理,是用來方便輸入輸出的東西,利用文本輸入流來讀取數據
    //提交代碼的時候記得註銷這條語句
    
    int n,j,tot,k;
    while(~scanf("%d",&n)){
        k=1;
        tot=0;
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n,cmp);
        
        for(int 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);
    }
    
    
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章