码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 246C

时间:2018-08-02 16:05:44      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:最大值   def   sum   数组   一个   its   nbsp   元素   max   

题意略。

思路:

我们将数组中的数字从大到小排列,分别考虑取前0 + 1,1 + 1,2 + 1.....个的情况。

所谓i + 1的意思是,取前i个的时候,同时取第[i + 1],[i + 2],......,[n]个元素。这样产生的是一个递减的和。

我们将取前 i 个的这种情况定义为第 i 类。我们知道第 i 类的最大值也小于第 i+1 类的最小值。因此可以产生不重叠的sum和。

详见代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100;

int ai[maxn];

bool cmp(const int& a,const int& b){
    return a > b;
}

int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i = 1;i <= n;++i) scanf("%d",&ai[i]);
    sort(ai + 1,ai + 1 + n,cmp);
    for(int i = 1,cnt = 0;i <= n && cnt < k;++i){
        for(int j = i;j <= n && cnt < k;++j){
            printf("%d",i);
            for(int k = 1;k <= i - 1;++k)
                printf(" %d",ai[k]);
            printf(" %d\n",ai[j]);
            ++cnt;
        }
    }
    return 0;
}

 

Codeforces 246C

标签:最大值   def   sum   数组   一个   its   nbsp   元素   max   

原文地址:https://www.cnblogs.com/tiberius/p/9407298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!