UVA11100 The Trip, 2007(贪心)

来源:互联网 发布:死亡天使蘑菇 淘宝 编辑:程序博客网 时间:2024/06/06 10:02

https://acm.bnu.edu.cn/v3/problem_show.php?pid=19518

题解:
这道题就是要这个序列划分成几个字序列,这几个子序列要求严格上升,然后每个序列的元素个数尽量相等。
而且这道题可以排序之后直接搞,可以任意选择数。
那就很好办了,分成子序列的个数其实就是最大的相同元素的个数。
直接输出就好了,开始没理解题意。
代码写的丑,勿喷。

#include<bits/stdc++.h>using namespace std;const int maxn=10005;int n,Len,A[maxn];bool used[maxn];int main() {#ifdef tangge//    freopen("11100.out","w",stdout);#endif // tangge    int f=0;    while(scanf("%d",&n)==1,n) {        if(f)printf("\n");        f=1;        for(int i=0; i<n; ++i) {            scanf("%d",&A[i]);        }        sort(A,A+n);        Len=0;        for(int i=0; i<n; ++i) {            int now=1;            for(int j=i+1; j<n; ++j) {                if(A[j]==A[j-1]) {                    ++now;                    Len=max(Len,now);                } else {                    i=j-1;                    break;                }            }        }        Len=max(Len,1);        printf("%d\n",max(Len,1));        int maxN=n/Len;        if(maxN*Len<n)++maxN;        memset(used,false,sizeof(used));        for(int i=0; i<n; ++i) {            if(used[i])continue;            printf("%d",A[i]);            used[i]=true;            int now=1,last=A[i];            for(int j=i+1; j<n; ++j) {                if(used[j])continue;                if(now<maxN&&(last^A[j])) {                    printf(" %d",A[j]);                    last=A[j];                    used[j]=true;                    ++now;                }            }            printf("\n");        }    }    return 0;}
1 0