首页 > 代码库 > UVA11100- The Trip, 2007

UVA11100- The Trip, 2007

题目链接


题意:给定n个正整数,把它们划分成尽量少的严格递增序列(前一个数必须小于后一个数),输出序列个数的最小值k和这k个序列。

思路:出现次数最多的个数就是序列的个数。输出比较麻烦,但我们只要每k个数输出一个数字,那么最后就可以将所有序列都输出来了。

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

using namespace std;

const int INF = 1000000;
const int MAXN = 10005;

int n;
int arr[MAXN], num[MAXN];

int main() {
    int t = 0;
    while (scanf("%d", &n) && n) {
        memset(num, 0, sizeof(num));
        for (int i = 0; i < n; i++) { 
            scanf("%d", &arr[i]); 
            num[arr[i]]++; 
        }
        arr[n] = INF;
        int Max = 0, cnt = 0, temp = 0;
        for (int i = 0; i < MAXN; i++) {
            if (num[i] > cnt) {
                cnt = num[i]; 
                temp = i;
            } 
        }
        Max = num[temp];
        if (t) 
            printf("\n");  
        t = 1; 
        printf("%d\n", Max);

        sort(arr, arr + n); 
        for (int i = 0; i < Max; i++) {
            printf("%d", arr[i]);
            for (int j = i + Max; j < n; j += Max) {
                    printf(" %d", arr[j]);  
            }
            printf("\n"); 
        }
    }
    return 0;
}