南京理工大學校外賽sequence

Description
將一個給定的數列,拆分成K個不降序列,每個數出現且只出現一次,且在各序列中各個數相對於原數列的相對順序不變。
如7 6 9 8 10可以拆成 7 9 10和6 8。求最小的K值。
Input
第一行輸入一個整數T(1 <= T <= 100),表示接下來T組測試數據,
每組兩行,第一行爲n,代表數列長度(1<=n<=10000)
接下來一行有n個數,空格分隔(每個數<=50000)。
Output
對每組數據輸出一個最小的K值。
Sample Input

2
5
7 6 9 8 10
5
5 4 3 2 1

Sample Output

2
5


解題思路:本題就是一個純貪心問題,只要我們對該數組進行一個遍歷就可以來對每一個數進行分類,找到小於等於它的序列就行。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 10010;

int num[maxn];
int t,n;
int ans;

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        ans = 0;
        memset(num,0,sizeof(num));
        while(n--)
        {
            int x,i;
            scanf("%d",&x);
            for(i=0;i<=ans;i++)
            {
                if(x>=num[i]){
                    num[i] = x;
                    break;
                }
            }
            if(i==ans+1)
                num[++ans] = x;
        }
        printf("%d\n",ans+1);
    }
    return 0;
}


發佈了70 篇原創文章 · 獲贊 47 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章