cf246C

1717 ワード

タイトルリンク:http://codeforces.com/problemset/problem/246/C
配列を大きい順から小さい順に並べ替えます
要素を次のように組み合わせます.
1,a[1],a[2],a[3],a[4],a[5];
2,a[1]+a[2],a[1]+a[3],a[1]+a[4],a[1]+a[5];
3,a[1]+a[2]+a[3],a[1]+a[2]+a[4],a[1]+a[2]+a[5];
4,a[1]+a[2]+a[3]+a[4],a[1]+a[2]+a[3]+a[5];
5,a[1]+a[2]+a[3]+a[4]+a[5]
サイズ関係の制約は必ず繰り返されません.
コードは次のとおりです.
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000];
bool cmp(const int &a,const int &b)
{
    return a>b;
}
int main()
{
    int n,count,k;
    while(cin>>n>>k)
    {
        for(int i=0;i<n;i++)
            cin>>a[i];
        sort(a,a+n,cmp);
        if(k<=n)
            for(int i=0;i<k;i++)
               cout<<1<<" "<<a[i]<<endl;
        else
        {
            count=0;
            for(int i=0;i<n;i++)
                cout<<1<<" "<<a[i]<<endl;
            count=n;
            int r;
            int l=0;
            for(int r=2;r<=n;r++)
            {
                for(int j=r-1;j<n;j++)
                {
                    cout<<r;
                    for(int i=0;i<r-1;i++)
                    {
                        cout<<" "<<a[i];
                    }
                    cout<<" "<<a[j]<<endl;
                    count++;
                    if(count>=k)
                    {
                       l=1;
                       break;
                    }
                }
                if(l)
                    break;
            }
        }
    }
    return 0;
}