2015華為機試——合唱隊,2015華為合唱隊

來源:互聯網
上載者:User

2015華為機試——合唱隊,2015華為合唱隊
描述:
計算最少出列多少位同學,使得剩下的同學排成合唱隊形
說明:
N位同學站成一排,音樂老師要請其中的(N-K)位同學出列,使得剩下的K位同學排成合唱隊形。 
合唱隊形是指這樣的一種隊形:設K位同學從左至右依次編號為1,2…,K,他們的身高分別為T1,T2,…,TK,   則他們的身高滿足存在i(1<=i<=K)使得Ti<T2<......<Ti-1<Ti>Ti+1>......>TK。 
你的任務是,已知所有N位同學的身高,計算最少需要幾位同學出列,可以使得剩下的同學排成合唱隊形。 
 
題目類別: 迴圈 
難度: 初級 
已耗用時間限制: 無限制
記憶體限制: 無限制
階段: 入職前練習 
輸入:  
整數N
一行整數,空格隔開,N位同學身高
 
輸出:  
最少需要幾位同學出列
 
範例輸入:
8
186 186 150 200 160 130 197 200
                  
範例輸出:

4


代碼如下:

public class he_ChangDui{public static void main(String[] args){Scanner sc=new Scanner(System.in);//n為人數int n=sc.nextInt();int[] height=new int[n];for (int i = 0; i < n; i++){height[i]=sc.nextInt();}int[] temp=new int[n];for (int i = 0; i < n; i++){int []a=new int[i];for (int j = 0; j < i; j++){a[j]=height[j];}int[] b=new int[height.length-i];for (int k = i; k < height.length; k++){b[k-i]=height[height.length-k+i-1];}temp[i]=handle(a)+handle(b);}Arrays.sort(temp);System.out.println(n-temp[n-1]);sc.close();}public static int handle(int[] num){if (num.length==0){return 0;}int[] curMax=new int[num.length];for (int i = 0; i < num.length; i++){curMax[i]=1;for (int j = 0; j < i; j++){if (num[i]>num[j]&&curMax[i]<curMax[j]+1){curMax[i]=curMax[j]+1;}}}Arrays.sort(curMax);return curMax[num.length-1];}}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.